23 September 2015

Concave triggers in Unity 5

We're currently in the progress of converting our game to Unity5. Not as easy as I had hoped it would be, but that's for another post :)

One of the things that changed in Unity5 is that concave triggers are not possible anymore. They used to be, but they triggered enter/exit events every time a collider passed through the surface of the trigger. Now you just get an error that you can't have concave triggers.

Too bad for us, because we used a lot of them via my Area Trigger system. The code in that post is obsolete in Unity 5 (as are many of my other Unity-related posts). So I've updated the code on bitbucket.

To recap: the idea of the area trigger concept is that it exists of a height and a 2d polygon, that can be concave. This defines a concave volume. We can then add a mesh trigger and flag it as convex. We now receive events from the convex volume, we check these against the concave volume and if the collider is in the concave volume we pass an area trigger event.

The only downside now is that we check whether the position of the collider is in the area trigger instead of the whole volume (which would be far more difficult and obviously the reason why even PhysX doesn't do it). For our purposes however this is enough; we use these area triggers to establish whether the player or an npc walks into a zone (or area, hence the name) in the scene.

A colleague of mine added a cool feature that was requested by our artists: you can now also use the system to create area colliders. With it, you can quickly generate simple mesh colliders as blockers in Unity, instead of having to create them in a modelling tool, where this doesn't make much sense. (I hate FBX files where visuals and physics are mixed).

The shading system also changed a bit in Unity 5, we can't create shaders from a string anymore for example, so I changed the visualization of the triggers as well.

Enjoy!

10 August 2015

Games in Belgium - continued

I could have titled this post "Even more games in Belgium", but the event that led me to writing this post is the sad news that one of the once bigger game companies in our country has filed for bankruptcy. GriN is no more.

This is sad news, for numerous reasons

  • Woolfe was a promising game. I've never got around to play it but I have seen a lot of footage and gameplay - there was a lot of love and dedication in it.
  • My kids love the game "Forest Pals", and I mean love! They know the song in the game by heart.
  • Wim Wouters, the CEO of GriN is a very enthusiastic and driven game developer, who has done a lot for the Belgian Game Development scene, which makes this news extra harsh.

    I wish him all the luck in the world, and lots of time to recharge his batteries. Because I know we'll see more of him in the Belgian game dev scene, he'll be back :)

    In previous posts about games in Belgium I wrote that the business was expanding, rapidly. I have the impression that we're not expanding that fast anymore, but we are by no means shrinking either. Last gamescom there was a strong representation of Belgian games joined in a Belgian Booth (I should have gone this year). DAE grows in number of students (could use two of them on Kweetet, hint, hint), and news of Belgian games is popping up more and more. There are also a few entries of Belgian games in the Dutch Game Awards, curious to see if we'll win one! (And by 'we' I mean both Belgians in general and ourselves at the Kweetet team ;) )

    Of course there is Larian, they have set records with Original Sin and now they're bringing an even better version to consoles. They're definitely growing strong and I'm curious to what Swen is tweeting about.

    The Flemish games association is also growing stronger, I find them quite active. I really should go to one of the café events they hold, but somehow there was always something that took priority. Their list of game companies in Belgium has grown longer and I have the impression that some of them are growing well in team size. Definitely follow the Flega news and the Belgian Games Industry group on LinkedIn. So there was sad news today - but the future still looks bright :)

  • 17 May 2015

    Bending the world with Unity

    I am a big fan of Deathspank. I love the art style, I like the humor, the gameplay, the combat, the inventory, the start menu, all of it. You can see that in the game I currently work on (Kweetet): some of the art style has a resemblance, the dialog system is the same and: we've got a rolling world too. I really like the rolling world effect in Deathspank, it creates a cartoony feel for the world in general. So I said the concept artist that I really wanted it in the game, he liked it too, so we implemented it. In this post I'd like to explain how we did it, what problems were encountered and how we solved them or didn't solve them.

    Here is a movie that illustrates the effect in the game:

    The shader

    The idea behind it is actually very simple, we just displace the vertices of any mesh in the vertex shader according to a parabola. The vertex shader of the main material in Kweetet looks like this:

    #pragma vertex vert
    #pragma fragment frag
    #pragma multi_compile __ BEND_ON
    #pragma multi_compile __ BEND_OFF
    
    ...
    
    v2f vert (appdata_t v)
    {
     v2f o;
     #if defined(BEND_OFF)
     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
     #else
      #if defined(BEND_ON)
      o.vertex = mul(UNITY_MATRIX_MVP, DSEffect(v.vertex));
      #else
      o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
      #endif
     #endif
     o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
     ...
     return o;
    }
    

    As you see at the top, we have two multi compile directives. The first one is used to enable bending on a global level via a shader keyword. That way all materials bend if it's enabled in the current scene. Sometimes however you do want to disable the bending, even if the world bends, for example objects that are shown in the UI instead of the scene. To do that enable the BEND_OFF keyword via the material. We use these two because we can't disable the BEND_ON keyword via the material if it was enabled via a global shader keyword.

    If BEND_OFF is disabled and BEND_ON is enabled, you see that the function DSEffect is called before we multiply the vertex with the modelviewprojection matrix (DSEffect as short for "DeathSpankEffect" - I called it like that when we were still experimenting and never took the time to refactor). That function looks like this:

    uniform float _HORIZON = 0.0f;
    uniform float _ATTENUATE = 0.0f;
    uniform float _SPREAD = 0.0f;
    
    float4 DSEffect (float4 v)
    {
     float4 t = mul (_Object2World, v);
     float dist = max(0, abs(_HORIZON - t.z) - _SPREAD);
     t.y -= dist * dist * _ATTENUATE;
     t.xyz = mul(_World2Object, t).xyz * unity_Scale.w;
     return t;
    }
    

    There are three global variables, that I'll explain further. First we put the given vertex in world space. Then we calculate it's z depth (more on that line later). Then we apply the parabola formula:

    y = -a * z^2

    where a is the attenuation of the parabola. It's a negative attenuation so the world goes down. (If it were positive the world would bend upwards. Can be cool too). We add this result to the y value of the vertex. Then we put the vertex back in object space and return the result. In Unity 4.x you need to multiply with unity_Scale.w, in Unity 5 this is no longer needed.

    For the characteristics of our game we only need bending in the z direction, and only with a parabolic function. But off course we can use other functions that alter the y value in function of both the z and the x coordinates.

    The bendcontroller

    In a scene that needs to bend we place a gameobject with a bend controller script attached, that looks something like this:

    using UnityEngine;
    
    public class HeroBendController : MonoBehaviour {
     
     public float attenuate = 1.0f;
     public float horizonOffset = 0.0f;
     public float spread = 1.0f;
     public Transform player;
     
     private void Update () 
     {
      Shader.EnableKeyword("BEND_ON");
      Shader.SetGlobalFloat("_HORIZON", player.position.z + horizonOffset);
      Shader.SetGlobalFloat("_SPREAD", spread);
      Shader.SetGlobalFloat("_ATTENUATE", attenuate);
     }
     
     private void OnDisable()
     {
      Shader.DisableKeyword("BEND_ON");
      Shader.SetGlobalFloat("_ATTENUATE", 0);
      Shader.SetGlobalFloat("_SPREAD", 0);
      Shader.SetGlobalFloat("_HORIZON", 0);
     }
    }
    

    We need the transform of the avatar of the player (or some other reference), the z position of the avatar needs to be the top of the parabola, so this is the offset we have to apply on the vertices of the other meshes. This is what I called the _HORIZON variable in the shader. It's the z coordinate in world space where the top of the parabola will be. It looks something like this:

    In this case the avatar is walking on top of the world, but most of the time we want the horizon to be a bit higher, so that's why we have the horizonOffset value in the controller. This is the same scene, but with an offset of 3:

    And that's it. There's nothing more to it than this to achieve the bending world effect. Unfortunately the effect introduces some consequences that need to be addressed, if the game is impacted by them.

    Raycasting

    The effect is done in the vertex shader, not on the actual vertices. This means that the meshes in the game do not bend, and thus the physics world doesn't bend. This is a good thing, because if the physics world would bend, everything would start to fall off. The same goes for pathfinding meshes; they are still valid. The raycasts that may be used in pathfinding are valid too, so everything remains ok.

    But when we perform a raycast from the camera into the scene, to select objects in the world or to select the next position that the avatar needs to walk to, things are more difficult. The user will click on the visual position of an object but if the object is bended the actual location in the physics world is different.

    The following image illustrates this: the user wants to click above the cube in the scene on the floor (the red ray) but will actually click on the cube. If the user would have clicked on the bottom of the cube (to select it for example) he would have clicked on the floor instead.

    My first idea was to determine the position that the ray hits in the visual world by calculating the intersection of the ray with the parabola. I projected that position back to its original height and then I did a raycast to that position instead. This worked quite perfectly for situations similar to the one in the above image, however when the ray is above the horizon we don't have an intersection point, so this solution doesn't cover all cases.

    The best solution of course is not to use these raycasts at all of course. This is what Deathspank does; navigation is not done by clicking on the next spot where Deathspank needs to go but with the keys or the gamepad. Interaction with objects is done when you're close to them with keys or the gamepad. In the pc version of DeathSpank you can use the mouse to navigate, so I don't know what solution they've come up with.

    Animal Crossing uses a rolling world as well. I've never played it but since it's a 3DS game I don't think there's ever any raycasts needed from the camera, so they won't have this issue either.

    With Kweetet we are targeting children in primary school with possibly limited gaming skills playing on a pc in the browser, so they need to be able to do everything with a single mouse click. So I had no choice but to find a solution, or we would have to abandon the idea entirely.

    In the game, objects can be interacted with once they are within a certain distance to the avatar. That distance is mostly between 3 and 5 units. This means that if we create an area of +/- 5 units around the avatar where the world doesn't bend, all relevant raycasts will be correct. This is what the _SPREAD variable does in the vertex shader. it flattens the top of the parabola, creating an area around the avatar where physics and visuals match.

    This turned out to be the perfect solution, raycasts work where they should and the flat area around the player is small enough to still enjoy the effect of the world bending.

    Frustum culling

    Frustum culling is done before any vertex shaders are executed (obviously, or it wouldn't be culling). Because of that, objects that are bended into the view of the camera but are actually outside of it get culled. The result is that these objects pop in and out when they shouldn't.

    In the above image the red line and circle is the non bended world, the black line and circle is the bended world. The blue lines depict the camera frustum. As you can see, the circle will be culled, while it should be visible.

    These objects are typical small and highly located in the world. Ideally, we want to disable frustum culling for these objects, but that is not possible in Unity.

    [Start edit]

    The first comment on this post pointed me in another direction to solve this issue, so I changed this part. In Unity 4 it wasn't possible to change the camera used for culling without affecting the camera used for rendering. It turns it that in Unity 5 this has been made possible!

    This totally removes the need to, as I used to do, enlarge bounding boxes of meshes or combine them into larger objects. Instead, you can add a script to the camera that looks somewhat like this:

    using UnityEngine;
    public class BenderCamera : MonoBehaviour
    {
     [Range(0, 0.5f)]
     public float extraCullHeight;
     public Camera _camera;
    
     private void Start() {
      if(_camera == null)
       _camera = GetComponent<Camera>();
     }
    
     private void OnPreCull() {
      float ar = _camera.aspect;
      float fov = _camera.fieldOfView;
      float viewPortHeight = Mathf.Tan(Mathf.Deg2Rad * fov * 0.5f);
      float viewPortwidth = viewPortHeight * ar;
    
      float newfov = fov * (1 + extraCullHeight);
      float newheight = Mathf.Tan(Mathf.Deg2Rad * newfov * 0.5f);
      float newar = viewPortwidth / (newheight);
    
      _camera.projectionMatrix = Matrix4x4.Perspective(newfov, newar, _camera.nearClipPlane, _camera.farClipPlane);
     }
    
     private void OnPreRender() {
      _camera.ResetProjectionMatrix();
     }
    }
    

    This adds a bit on the top and bottom of the culling camera, as you can see in this screenshot (the grey lines are the view frustum of the render camera, the colored lines are the view frustum of the cull camera):

    This solution solves the culling issue completely, without any manual intervention other than tweaking the extra cull height value.

    [End edit]

    Particles

    Another thing to consider are particles. With particles you can choose between meshes or billboards. With meshes nothing changes so you can use the same vertex shader, but billboards are passed in camera space instead of object space to the vertex shader. Thus we need a camera to world space matrix to achieve the bending effect on billboard particles. I have this in a script on my camera:

    private void OnPreCull()
    {
     Shader.SetGlobalMatrix ("_Camera2World", camera.cameraToWorldMatrix );
     Shader.SetGlobalMatrix ("_World2Camera", camera.worldToCameraMatrix );
    }
    

    And with these matrices we have this function for the vertex shaders:

    float4x4 _Camera2World;
    float4x4 _World2Camera;
    
    float4 ParticleDSEffect (float4 v)
    {
     float4 t = mul (_Camera2World, v);
     float dist = max(0, abs(_HORIZON - t.z) - _SPREAD);
     t.y -= dist * dist * _ATTENUATE;
     t.xyz = mul(_World2Camera, t).xyz;
     return t;
    }
    

    Which is basically the same one, but without the unity_Scale and using the other matrices. We override the built-in particle materials in Unity to use this bending function when appropriate. In other words, this situation is easily fixed, but it requires some careful handling of the shaders.

    Conclusion

    This is in short how we've implemented this technique in our game and how we solved certain problems. I'm by no means sure that these are good solutions, so I'm happy to receive feedback on all of this. I'm planning to set up a Unity project on github where I can place all the scripts I describe on this blog, with demo scenes of every technique. And if this post helps the reader to apply this technique in another game then I'm pleased to be able to contribute.

    [Edit]The unity project with a demo is now available here (it's a bitbucket repo instead of github). Look for the world bender scene in the project.[/Edit]

    [Edit]Since 2018.2 is out, I also have a node that can be used in a shader graph, read all about it here.

    14 March 2015

    Text localization in Unity 4.6 and Unity 5

    Unity's UI system used to be very limited, so most people turned to the asset store for UI plugins. One of the most popular ones is NGUI. I've been using NGUI for more than two years now. Needless to say that I was happy to hear that the developer of NGUI was going to work for Unity on a new UI system. Apparently he did that for only a year but the result I hoped for has been achieved.

    Unity 4.6 and 5 have a new UI system and it is very resemblant to NGUI. I could convert my NGUI version to the unity version with minimal changes in the code. But with all new components of course.

    But there was one capital feature missing: NGUI has an easy text localization system and Unity hasn't build an alternative for that. So I rewrote the localization code from NGUI so it would work with the new unity UI. I also changed a few things so it would fit my needs better, it is not a one to one conversion. I have asked permission to post it on this blog.

    NGUI expects a Localization.csv file somewhere in a Resources folder. In my version you'll see there's a Resources folder containing a single asset. In the files collection of that asset you can assign any csv anywhere in your project. That way you can have multiple csv files containing localization.

    The reason to do this was that the excel sheet containing the localization became very large and hard to maintain, so I wanted the ability to split it up into multiple sheets, resulting in multiple csv's.

    To be able to export these sheets easily in multiple csv's I found this page which I used to write the program below. It takes an xlsx filename and an output path as input and will generate a csv for each sheet in the excel file. In my unity project I have this excel file and my build process uses this program to generate these csv's that are linked in the localization asset. That way I always have the latest version of the localization in the excel file build into the game.

    [Edit]For the program below to work you need to have Excel installed and the correct oledb drivers. If you have a 64bit version of excel make sure to compile the program as 64bit or 32bit if Excel is 32bit.[/Edit]

    As always this software is free to use at will, enjoy!

    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.IO;
    
    namespace Xls2CsvConverter
    {
     class Program
     {
      static void Main(string[] args)
      {
       Console.Out.WriteLine("Source file: " + args[0]);
       Console.Out.WriteLine("Target path: " + args[1]);
    
       string excelFile = Path.GetFullPath(args[0]);
       string csvPath = Path.GetFullPath(args[1]);
    
    
       ConvertExcelToCsv(excelFile, csvPath);
    
       Console.Out.WriteLine("Done");
      }
    
      static void ConvertExcelToCsv(string excelFilePath, string csvOutputFile)
      {
       if (!File.Exists(excelFilePath)) throw new FileNotFoundException(excelFilePath);
       //if (File.Exists(csvOutputFile)) throw new ArgumentException("File exists: " + csvOutputFile);
    
       // connection string
       var connectionString =
        String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=No'",
         excelFilePath);
       var oleDbConnection = new OleDbConnection(connectionString);
    
       // get schema, then data
       try
       {
        oleDbConnection.Open();
        var schemaTable = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        for (int i = 0; i < schemaTable.Rows.Count; ++i)
        {
         string worksheet = schemaTable.Rows[i]["table_name"].ToString().Replace("'", "");
         string sql = String.Format("select * from [{0}]", worksheet);
         var dataTable = new DataTable();
         var dataAdapter = new OleDbDataAdapter(sql, oleDbConnection);
         dataAdapter.Fill(dataTable);
    
         string csvFile = Path.Combine(csvOutputFile, worksheet.Replace("$", "") + ".csv");
         MakeWritable(csvFile);
         using (var wtr = new StreamWriter(csvFile))
         {
          foreach (DataRow row in dataTable.Rows)
          {
           bool firstLine = true;
           foreach (DataColumn col in dataTable.Columns)
           {
            if (!firstLine)
            {
             wtr.Write(",");
            }
            else
            {
             firstLine = false;
            }
            var data = row[col.ColumnName].ToString().Replace("\"", "\"\"");
            wtr.Write("\"{0}\"", data);
           }
           wtr.WriteLine();
          }
         }
        }
       }
       finally
       {
        // free resources
        oleDbConnection.Close();
       }
       
      }
    
      private static void MakeWritable(string path)
      {
       if (!File.Exists(path)) return;
       FileAttributes attributes = File.GetAttributes(path);
    
       if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
       {
        // Make the file RW
        attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
        File.SetAttributes(path, attributes);
       } 
      }
    
      private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
      {
       return attributes & ~attributesToRemove;
      }
     }
    }
    

    Edit: As per request I added a demo project here. You'll see I have a GameController class who calls Init on the text localization class and selects the correct language. In the localization asset in the Resources folder I added a demonstration CSV file.

    20 September 2014

    Bugsnag integration for Unity

    Recently we started using bugsnag in kweetet. Bugsnag is a website that exposes an api that you can use to send your error reporting to, and then you have a nice interface to follow up on these errors, assign them to users, resolve them, etc. All well described here. What a lifesaver it has been to us!

    They also have a unity notifier. Too bad it only works on Android and iOS. Since we are developing for the unity webplayer I had no choice but to build my own notifier. This turns out to be really easy, starting with the notifier documentation. There's one subtlety that is not mentioned in the docs: you must provide a stack trace of at least one line, or the error report won't show. I received tremendous good help from the people on their support chat.

    The one problem with the unity webplayer is that it runs in a security sandbox. With the WWW class you can only access websites that are on the same domain as the webplayer. So sending reports from kweetet.be to bugsnag.com is a no-go.

    With a bit of research I managed to set up a proxy on that forwards the error reports to bugsnag, and with that solution I was able to receive the errors, hurray! Kweetet runs on nginx, I added this to our server configuration (something similar is possible with apache):

    location ~ /bugsnag {
     rewrite /bugsnag / break;
     proxy_set_header X-Forwarded-Host $host;
     proxy_set_header X-Forwarded-Server $host;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_pass https://notify.bugsnag.com;
    }

    This enabled me to send the reports to our own domain which then gets forwarded. Now we receive all errors that occur in the game from anyone! The setup is similar to the official unity notifier. For web players you can specify another reporting url, depending on the proxy that you've set up. For all other platforms it should work with the default url.

    I started a bitbucket project, I'll be improving this notifier since I keep on improving it for kweetet. This is the link: https://bitbucket.org/grrava/unitybugsnag.

    It should also work on android and ios. I confirmed that it worked on android but not on ios. On android however I wasn't able to get a stacktrace, so probably the official unity notifier from bugsnag will be more suited for those two platforms.

    As always, use the code at will - let me know if it helped you in any way or if you have questions, post them here or on the bitbucket project page.