Fun with Unity

Astute readers will already know that I am using Unity3d to build a new iPhone game: Snowferno. You might also already know that I do lots of work with multi-touch stuff, and recently have begun to marry the two technologies and use Unity to build multi-touch apps.

This is actually pretty fantastic as it allows you to build MT apps very quickly and they still look cool, and mostly the bugs and stability are handled by someone else (ie the unity guys) so it streamlines the process quite a bit.

Anyway, the reason for this particular post was just to put up some fun stuff I have been doing with Unity this past weekend. Sandor, the man behind all the good ideas, wanted to do some cool stuff with MT and needed some groovy ripples on water effect for a project he is working on. SO I went out to try and find a nice simple solution to do some very basic water simulation.

After finding lots of super duper complicated ways to do realistic fluid simulations that were doing my head in, or lots of crazy custom shaders to do ripple effects (also doing my head in) I found this guys site, which details an old-school method to simulate water ripples.

Old school is more my speed, and this algorithm basically creates a height map that behaves kinda sorta like water might behave. (which is really close enough for me).

So, first up i do what I always do, which is build a test app in the format which I am most accustomed at the moment, which, this week is as a cocoa app. Here are a few screenshots:

picture-23picture-22

Basically it is just the algorithm above applied to a black and white bitmap. pretty standard stuff. If you want, here is the xcode project:

BBRippleTest.zip

Anyhow, once I worked out all the kinks in cocoa, I moved to the slightly less familiar world of unity scripting. I started with javascript, but the various variable casting that needed to go on (casting floats to ints and back again) were not working the way that I thought they should (I really dont know what javascript is doing) and it was pissing me off, so I switched to using C# to script the object in Unity. Having never used C# before a few of the syntax bits were a bit foreign to me, but at it’s heart it is really just C, so that was much nicer in terms of casting variable types and other esoteric stuff like that.

Still shots of the actual effect dont look like anything because you need to motion of the ripples to be able to see it, but you can see the mesh deformation here in unity:
picture-27

picture-28

picture-29

In unity, what I did was build a large mesh (in the case of the pics it was 128 x 128 vertices. (well, technically it was 128×128 sections in cheetah3d, which ends up being 127×127 vertices and something like a kasquillion triangles) but the script works with any planar set of vertices as long as they are evenly spaced.

For anyone interested here is the unity project:

UnityRippleTest

I removed some of the auto-generated stuff that Unity adds into the projects to keep the file size down a bit. The downside to this pruning is that if you want to see it in action, you need to reattach the script and the texture to the mesh.
picture-30

Then you can adjust the various settings:
picture-31

The rows and cols need to match the number of sections the mesh has, (it will actually be one less than the total number of vertices across the plane. It seems a bit odd to do it this way, but that made making the meshes in cheetah simpler (ie mesh in cheetah has X x Y sections, script has X x Y rows)) if the cols dont match it will error out.

The splash force can be anything from 10 – 65000 ish.. it is effectively how hard you hit the mesh when you want to make a splash. ultimately if you have the wave height capped, it will boil down to how long you want the waves to propigate for. I find values in the 1000 – 10000 range seem to make pleasing results.

the max wave height is a cap on the wave height. Again, between 1 and 2 seem to make nice waves, anything over 4 or 5 gets a bit wacky.

The dampner is how quickly the waves get killed. numbers close to 1 make the waves propagate for longer. if you set it below 0.9 the waves barely go anywhere. there is actually a pretty big difference to be found in the very upper limits of the dampner. For instance, set the waves height to 2, the force to 10000 and the dampner to 0.99. Do a splash.. this will keep the mesh moving for about 3 – 4 seconds before it is still again. Now change the dampner to 0.9999 and it will go for about 4 times as long.

Anyhow, here is a short video of the whole process, which mostly survived the youtube compression: (it looks slightly better in high quality, but not much… I could have done a better job of making a high def video, but: too lazy)

Oh, and the tunes in that video are from the Snowferno soundtrack. Brent at Fatlab Music wrote that one, and he gave me special permission to use it for my silly little demo video here, so thanks for that Brent!!

Cheers!
-B

Oh and BTW, there is absolutely no implied warranty or whatever for any of the code above, so if you load it up and your computer gives your cat the pig flu because of it, I cannot be held responsible.

This entry was posted in multitouch. Bookmark the permalink.

6 Responses to Fun with Unity

  1. jpiphonedeveloper says:

    Hi,

    Is there any other samples Ripple mesh deformation using unity3d or iPhone variant.Sorry for asking like this.At this time i am looking same Water ripple effects using Unity 3d or iPhone[But above mentioned project is Mac version].If anybody having any samples or ideas please reply….

    Thanks in Advance,

  2. jooki says:

    hi that water effect is pretty nice! in between i thougt if it might be possible to kinda blur that heightmap, so the waves could shape more rounded.
    i wanted to integrate it in my unity-project but i couldn’t. first i thought i’d only need rippleSharp.cs, the collision plane and and the watersufaceplane. Is something special missing?…

  3. Ben says:

    Hey Jooki,

    You could definitely filter the heightmap image in various ways to give different deformations. Good idea! :-)

    As for the project, some people have had trouble with it because I used Cheetah3D to make the plane, if you dont have cheetah then it will throw an error. However, I only used cheetah because I was in a hurry when I made it. The best thing to do is just to modify ripple.cs to generate all of the vertexes and not even use a plane (that way you can more easily customize the shape etc)

    CHeers!
    -B

  4. runonthespot says:

    If anyone wants to know how to get this going without Cheetah 3d, simply go to Unity Communify website and grab CreatePlane editor script (http://www.unifycommunity.com/wiki/index.php?title=CreatePlane), place in a folder in your project called “Editor”. In your Unity component menu, you should see “Custom Plane” as a new option. Create one that is 128×128 and then go to the meshfilters in the sample scene and point them to the new filter you’ve just created. Finally delete the Cheetah file from the project.

  5. aamir_kind says:

    Hi ben,

    The tutorial really help me for iphone, but the problem is it does not give proper simulation when we tap around the corners in iphone. It ruins the smooth flow of ripple when we drag finger around iphone screen.

    So please help me how to overcome this problem. Or any suggestion / version update?

    Thanks in advance
    Aamir

  6. Zencoder says:

    Hey, Ben. I noticed you have not updated BenBritten.com since 2012. I like this project and I’d like to know if you are still using unity for iPhone development.

Leave a Reply