Unity Experiment : Basic 3D

13 Jul 2017

This experiment was just going to be getting a model loaded and moving about, but it was so easy that I kept adding stuff so this is a much longer article then I expected.

Blender

Using Blender with Unity is really easy, if you’ve got .blend files set-up to open in the correct version any files put in your Assets folder get imported automatically whenever they change.

I was expecting there to be some odd issues or tweaking the exporter but it seems to just work.

Vertex coloring

I'm not bothering with fancy high poly models and the same goes with the texturing, so I thought I’d just use vertex coloring.

Only problem is, the default Unity importer and shaders don’t seem to support it, so I used the baking tools in Blender to turn the colors into a texture and imported that.

A brief overview of the steps to get this to work, assuming you’re using Cycles as the renderer:

  1. Build your model and create a vertex color layer (called Col in this example).
  2. Create an image texture the resolution you want.
  3. Unwrap the mesh. As we don’t care about the layout you can be lazy and just use ‘Lightmap Pack’ here.
  4. Add a material to the model, using the default Diffuse shader.
  5. Create a Input Attribute, set the name to the same as the vertex colour layer (Col in this example) and connect the Color output to the Diffuse node’s color input.
  6. Under the render settings open the Bake section, set the type to Diffuse and select the source options Direct, and Color.
  7. Click Bake, if it asks you for a texture use the one you created earlier. This bakes to the internal texture, which you have to go save out to disk.

Physics

Yet another thing that I thought would be tricky but ended up being a lot less effort in Unity. My only problem was not using real wheel/track physics so I had to hack turning torque so it felt right.

If you set the torque high enough to rotate when stationary it became really unstable once you got up a decent speed. As a quick and hack I scaled the turning torque inversely to speed. I also decreased the height of the collision box, which moved the center of gravity down making it more stable as well.

If this was more then just a quick test it’d be worth setting a lower center of gravity in the model and using a better physics setup for turning, but this will do for this experiment.

Procedural content

It’s not necessary to have much of a level for testing, but I thought I’d come up with something to make things a little interesting. Each level starts with a random seed number, and each tile is generated based on it’s coordinates in the world and that value.

The problem is, if you just use random numbers things don’t look very good as you can end up with clumps rather than an even random spread. There’s a great article all about this called When Random Numbers Are Too Random: Low Discrepancy Sequences .

To get the center point for each tree clump I ended up using Irrational Numbers and Poisson Disc via ‘Mitchell’s Best-Candidate’. A single random number based on the golden ration doesn’t work very well for x,y coordinates as it has a regular pattern. While you don’t know exactly the number that’s going to be next, if you plot them there’s clear diagonal lines.

I ended up using the golden ratio method to get the next step, then added this to a stored value and floating point modular 1.0, which seemed to work fine.

To generate a small group of trees around this point I came up with the following algorithm, it’s probably not original but works.

It generates them in a series of rings, with the radius increasing by twice the diameter of the tree object each time, which ensures that they don’t intersect and have a bit of a gap between.

You start each ring by picking a number between 0 and 4 to be the number of trees. Take Pi*2 and divide this by the number of trees to get the segment size.

For each tree it’s position around the circle is it’s number i multiplied by the segment size, with an extra random value that’s between 0…90% of the segment size. This means it’s at a random position within it’s segment.

To make things less uniform at the start of each circle pick a random number between 0…(2*Pi) and add that to all the values, e.g. rotate the ring by a random amount.

As you can see, this seems to work pretty well at making natural looking groups of trees.

Wrapping up

I managed to get everything working so just kept adding stuff until I ran out of time and had to start the next project. Everything was simpler to do then I expected, which I’m not going to complain about.

The only thing that didn’t work as well as I wanted was the trees in the procedural system. Sometimes you end up with intersecting trees, which shouldn’t happen in the first stage of the placement as it picks areas that aren’t used. I'm guessing what’s going wrong is there’s two clumps in adjacent tiles near the edge which are going over the boundary and causing the problem.

I think the best way to get around this without making the edges too obvious would be to have exclusion zones on the edges, but tile them so one overlaps a bit of the next.

This post has been taged as Unity