- 最后登录
- 2018-6-29
- 注册时间
- 2011-7-1
- 阅读权限
- 20
- 积分
- 359
- 纳金币
- 335582
- 精华
- 0
|
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:
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:
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 128x128 sections in cheetah3d, which ends up being 127x127 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 ***ning is that if you want to see it in action, you need to reattach the script and the texture to the mesh.
Then you can adjust the various settings:
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.
|
|