Thursday, 30 May 2013

Unity world's coolest tutorial about water, girls and sky with FREE assets




Take some pizza and cola and let's start.

Water, water, water everyone is crazy about it. Lets remember Bioshock. Jesus Christ whole team just for one water...Crysis...same shit. What's about Unity ? Even now with super pro custom advanced shader eat performance of whole game. Okey we have created awesome water, but if player want to go just deeper, deeper, deeper baby. Here is nothing except culling. Culling make feel people so lonely. But as always I have an idea and of course a realization.

Let's split our task into two:
1. Water
2. Underwater
Sounds simple at now.


Split into smaller parts:
1. Reflections
2. Refraction
3. Waves
4. Smooth bitch beach blending.
5. Mix everything

1. Reflection. Everything is really simple. Render environment opposite to water plane and store it in a texture.  I have used some old script that can do it.

2. Refraction. Render everything without plane. Store in a texture. For deferred render its really easy. Just got render without water plane.

3. Waves. Here we can choose two opportunities: mathematically calculate waves like combinations of sin functions in different directions and phases. Lots of opportunities like no tiles, custom waves that depends on wind speed or anything else. But we don't wanna fuck our brain up. Option 2: use texture of waves. Maybe normal map for proper specular calculation and sweet lighting. Hmmmm....guess second. Guys from Crysis definitely have choosed first one. Nice texture of a wave you can find on a

4. Smooth shore blending. Algorithm is quite easy. Get zbuffer in shader. Use that value for alpha blending. Closer objects like shore will receive less value for alpha and we will see smooth transition from water to shore.

5. If you ever study physics or stuff like that you may know that water have coefficient of self reflection. Sounds boring. But it's easy to understand if you look at water standing in it turn your head down you will see the bottom. If you see under some angle you will not see the bottom but will see the sky. So we need to calculate that that angle and make smooth transition between reflection and refraction. We can do like multiplication of of normal of some point in water and one length vector of plane where it's water situated. Then find angle from scalar multiplication and plane where it's plane is situated. This value is used for blending. Use some color coefficients for designers and little girls.

That's all as for top of the water.


1. Underwater reflections
2. Blue fog
3. Bubbles
4. Fish
5. Caustic cool effects
6. Camera color tint.

1. Underwater reflection. Unity have built in shader LOD system for shaders. So in one shader we can store both water and underwater. Use LOD fallback tag to provide that functionality. Write just plane reflection with wave without hardcore transitions and stuff like that

2. Blue fog. Simple as hell. Decrease fog distance and turn it color into some nice deep blue.

3. Bubbles. Just a simple particle emitter  Shrunken if you want or legacy.  Just tweak nice and leave as is. You can use my texture I have made it from series of bubbles.

4. Fish. I have modeled it by the texture. And animate in 3ds max. Two bones for swimming. But one fish that moving in a tank it's fucking boring. 5 better. 42 even better. But real pro will use cohesion separation algorithm for populations movement. Fuck yeah....use steer library for that shit. Just for extremely cool fishes.
5. Caustics is probably the most butt fucking moment of water creation. I have created it via projectors with caustic animation texture putted on it. I have used special program that can produce caustic animation like series of sprites and after that special scrip that can animate or simply change projector texture in update cycle. Out of topic: guess much faster is to store in one texture and use UV animation like in animated particles.

6. And underwater everything is looking more blue then above it. So we need to post process resulting screen for achieving water effect. That script is already available in effects. So our custom script will put it on camera.

That's it. Put some  rocks and water vegetation as you want.


Here we need to write custom script that handle player entering water or underwater mode. Of course that script should know about material of water. Player position, and something else that we know about much later like bubbles particles, underwater fog color.

Okay if player position by Y-axe is less then water position by this axe turn on underwater mode: fog density and color, shader LOD, turn on bubbles, you can leave fish moving in tank,  caustic is also switch according. When bigger just go back turn everything off. Also don't forget to turn camera rendering reflection direction into opposite one.

Some HACKs that was used:
1. you can't just turn off bubble particles when you leave water, cuz they have lifetime. So next time you entering water bubbles start emitting like they have waited you. Of course you can pre-warm. But this method have some strange behavior  like you should call them in for statement several times( you can find discussion about it on Unity answers). And by the way turn lot of objects on/off causing lags that we don't need. So use particle material and set it alpha to zero. Not so fast, but if you have better one you are welcome to write in comments.
2. Caustic. I have created couple of projectors and use script that takes player position and move caustic according to player movements. So it's small hack, small Hollywood trick.


1. Water came down effect when you go from underwater.
2.  Waves when come in water and stuff like small rain drops.
3. More accurate refraction and reflection model for water
4. Godrays

Smells like you have done. See you.

Thanks!Have joy and fun!
konstantin dvornik