纳金网

标题: Fun with Unity,一种简单的水波效果 [打印本页]

作者: 晃晃    时间: 2011-10-14 08:31
标题: 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:
         





           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.
         

作者: 菜刀吻电线    时间: 2012-2-18 23:20
都闪开,介个帖子,偶来顶

作者: C.R.CAN    时间: 2012-3-11 23:32
谢谢楼主,真是太实用了

作者: C.R.CAN    时间: 2012-3-19 23:24
呵呵,很好,方便罗。

作者: tc    时间: 2012-4-3 13:07
凡系斑竹滴话要听;凡系朋友滴帖要顶!

作者: 晃晃    时间: 2012-5-30 23:24
不错哦,谢谢楼主

作者: 奇    时间: 2012-7-3 23:24
很经典,很实用,学习了!

作者: 晃晃    时间: 2012-7-23 23:21
好铁多多发,感激分享

作者: 驰骋的风    时间: 2012-7-24 09:30

高手速来赐教!kismet里面怎么加一个延迟DELAY??



unity特效大家PP_武功盖世呀








【严重分享】Unity3D_3.0破解版[安装包][下载]






高手请赐教!用模型做的特效动画材质丢失








antares_universe(vizio)可视化编辑

作者: 晃晃    时间: 2012-8-4 23:56
既来之,则看之!

作者: C.R.CAN    时间: 2012-8-13 23:58
楼主收集的可真全哦

作者: 晃晃    时间: 2012-8-24 00:07
路过、路过、快到鸟,列位请继续...ing

作者: 菜刀吻电线    时间: 2012-9-10 23:26
加精、加亮滴铁子,尤其要多丁页丁页

作者: cgjch8    时间: 2014-3-11 23:35
可视化编辑
作者: Kadina    时间: 2014-5-14 09:24
thank you thanks sharing
作者: pz789as    时间: 2014-5-14 09:42
thanks for sharing
作者: Kadina    时间: 2014-5-14 15:43
这里的资源很不错!可以学习到很多
作者: 1547373072    时间: 2014-6-12 16:18
又是英文啊,看样子,自己得好好的学习一下英语了。
作者: jacob    时间: 2014-6-13 11:57
菜刀吻电线 发表于 2012-2-18 23:20
都闪开,介个帖子,偶来顶


谢谢楼主,真是太实用了
作者: 宁唯是宁唯    时间: 2014-6-13 14:12
下載學習先~~~




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5