Working With Raymarching Toolkit for Unity

Along with the other four pieces of technology we have taped together, we also have one other thing helping bring our blobs to life: Raymarching.

I’m not really qualified to talk about exactly what raymarching is or how it works, and if you want to know more about the nitty-gritty of it there are several smarter people that can explain it far better than I ever could. Long story short its a form of rendering, and with it you can do things like this:

Pretty neat, but the fractals and infinitely repeating shapes aren’t what we’re after. What we want is this:

Using Raymarching you can blend shapes together, which also happens to be the perfect kind of consistency we’re trying to give our blobs. Here’s an example of what it looks like with the blob and its “tail”:

View post on imgur.com

We’re using Raymarching Toolkit for Unity to do this, but using it isn’t all sunshine and rainbows and there are a few limitations to it.

The most straightforward ones are outlined on the websites limitations page , but the tl;dr version of it goes something like this:

  • You can’t (shouldn’t) have more than 10 Raymarched objects.
  • Raymarching doesn’t work with an orthographic camera
  • You can’t change the hierarchy while the game is running (no moving children of objects around)

Beyond that however there are a few things that you should also probably know.

The first is that if you haven’t been paying attention to how raymarching works, you may be surprised to find out that Raymarched objects don’t use meshes.  Raymarched objects are created through shaders which means a couple of things.

The first is that if you want to animate anything made of raymarched objects you’ll have to do so using Unity’s in engine animation, which may be an issue if you’re used to working in an external 3D modeling program like Maya. There may be some way to work around this using placeholder objects in Maya or whatever and then replacing them with raymarched objects in Unity, but that’s a headache we didn’t want to deal with.

The second thing that this means is that raymarched objects can’t track collision. Yes, they can do their neat blending stuff but unless you put a collider on them you won’t be able to get collision and trigger messages like OnCollisionEnter(). You can still put a collider on raymarched objects, but keep in mind that your colliders won’t deform in the same way that your raymarched object. A sphere collider will stay a sphere, no crazy blending there. I have however seen some people get dynamic collision on raymarched objects, but you’ll have to figure that out yourself.

The last thing that because raymarching is all done via shaders you’ll need to know some shader code if you want to be able to change things past a certain point. Lets say that you want a sword to be raymarched, we can’t 3D model a sword and import it so what do we do? We’ll one option is to make it yourself via shader.

Now thankfully the Raymarching Toolkit for Unity comes with several basic shapes, so if you have no idea how to write shade code a better option may be to instead make the sword out of simpler primitive shapes. However we then run into the issue of performance. Remember, you can only have 10 raymarched objects per scene, and your probably going to use all of them making the sword. To solve this you’ll have to use modifiers to mirror and transform raymarched objects, however once again if you need anything beyond what the toolkit provides you’ll have to make write it yourself the same way you would make a custom shape.

But man, that whole “no more than 10 raymarched objects” thing is a real bummer huh? What if we just ignored that? Well, early on we had stress tested raymarching with around 30 raymarched objects and it seemed to be ok~ a slight drop but nothing too drastic. Thankfully the machines we’re running on have top of the line specs so we figured we would be able to handle whatever.

This was not the case. The initial design we had for the blobs was a bit too much, and with 12 blobs we were running less than 15 frames a second. Our blobs were made up of 6 raymarched objects each, so with 12 of them we were well over the limit of 10 blobs. To cut down on the numbers we swapped out a couple of raymarched objects for meshes, since some objects didn’t really need to be raymarched.

Hopefully you have a better idea now whether or not you want to use Raymarching Toolkit for Unity. From what I’ve seen there aren’t a whole lot of raymarching solutions like this, the only other one I know of being uRaymarching but the developer of that is Japanese so you might have a bit of trouble with any notes or documentation if you want to try using that instead.

Leave a Reply

Your email address will not be published. Required fields are marked *