Unity3d make his own water water effect (i.)

Source: Internet
Author: User
Tags cos sin

First,i Wish you a Happy New year, and study in Spring Festival's Eve means you is Hardworking,haha. I write in the languages. One passage write in Chineseone Passage translate to 中文版. My 中文版 is poor., If I Write some thing wrong, welcome to correct my article.
First of all, I wish you crossing Happy New Year, in the New Year Eve read Bowen study are diligent people.
I use two languages, a paragraph of English a Chinese language. My English is not very good, if have the English grammatical error, or the writing wrong of everybody can tell me to correct in time
I study some about water lately. Looks like this in the GIF; It looks isn't bad, but it's incomplete and regret. This shader are surface shader, used the tessellation technology by DX11. Because of used the tessellation, this shader ' s vertex function can ' t pass parameter like position and normal, then, Caus Ed the water ' s reflect light inaccuracy, can ' t specular and other things, etc. The reason that can ' t specular and other things are can ' t analysis reflection map. But just now I try to write a fragment shader so can use the tessellation, and succeed, then the shader can pass Paramet Er. I'll write it in the second blog article, this blog article just talk about water waves.
Recently studied the water body, the effect as shown in GIF, at first glance feel can, but there are many residual, this example is written with surface shader, using the surface subdivision technology. Because of the surface subdivision, so surface shader vert can not be outgoing values, so the vertex coordinates and normal values can not be transmitted, resulting in inaccurate reflection, and can not reflect (large residual, water can not reflect =), The reason why it cannot be reflected is that it cannot parse the incoming reflection map of a script reflection map. But while writing this article and suddenly try a bit fragment shader, incredibly good, so it can also transfer value, in this article (ii) research, today (a) only study the fluctuation of water body.

Most of maves is caused by the action of wind, it's better to use circular waves in the pond scence.we just talk about th e directional waves.
Most of the fluctuations are caused by wind blows, and for the water in the pond is better with a circular wave, we only discuss directional waves

Mentioned waves, we all think of sin, cos, etc. This shader also used them.
If we just use sin, the that effects looks awful.
When it comes to volatility, it must be sin,cos, and so on, and our water body uses it to do some transformations.
If sin alone is somewhat monotonous, it does not seem to be true.

So, in this shader, I used the displacement mapping that I write a blog article about it before.
That can add some other waves in water.
So this example adds a map permutation (displacement mapping) previously written in the surface subdivision, adding some clutter to the water surface.
The tessellation?
Our water in unity is a model. The plan model ' s has little points, May is 100++. Used tessellation can add much more points and make the waves more smooth. The figure below was the waves not used tessellation. Looks like Jag.
Why join the surface subdivision?
Our water surface is a plane, that is plane,unity in the plane model vertex number is obvious, looks like 100++, added the surface subdivision, the top point is the original number of times, the waves look fine and smooth,
For surfaces that are not subdivided by surface, (= =; This is not jagged);

If you don ' t want the real wave effect, you don't need tessellation, like Unity's water4, water4 not wave truly, just some Effects like normal, reflect, etc. Brain always being cheated by eyes.

About tessellation your can see this I write before
If you do not want the actual visible wave effect, you do not use the curved surface subdivision, such as unity comes with the kind of water water4,water4 such as no real fluctuations, just normal, reflective and so on your eyes a kind of deception.
More about Unity Surface Subdivision This article before the blogger



Above I talked my shader is use sin, there was also many good waves like this. I refer to the water in "GPU Gems", it used Gerstner Waves, its reality. The real waves has sharper peaks and wider troughs.
Just said my example with the sin wave, there are a lot of good waveforms, refer to the GPU gems in the water, the book is used Gerstner wave, the reason is-real, real waves are more sharp, the trough is wider

Gerstner ' s function is:
The functions of the Gerstner wave are:

Then I try this

And then I tried it.

That's pretty good, the waves have many transformation, sharper peaks and wider troughs, with some small waves. Cause of used tessellation, the plane ' s mesh and vertex are disappeared in unity, could be that's a bug in unity.
The effect is great, there are many changes, it is obvious that the wave is very sharp, there are a lot of small waves, because it is used by the surface subdivision resulting in the water level does not display grid vertices, seemingly unity bug, residual read.
Here is the code, cause of that can ' t pass parameter, the water not having graet highlight, shade inaccurate, looks bad.
Given the code, interested crossing can study the next Gerstner wave.
Because surface shader can not pass normal information, there is no high-gloss, light and dark display, and then the good waves can not be seen =;

Float wave (float x, float z, float timer) {Float y = 0;float oct = _oct;float FAC = _fac;float D = sqrt (x * x + z * z);//L Ength (FLOAT2 (x, z)), for (Oct; oct>0; oct--) {y-= FAC * COS (TIMER * _sp + (1/FAC) * x * z * _ws); FAC/= 2;} Return 2 * _vs * d * y;}




Then again to talk about sin, although less detail than Gerstner. But I think that ' s also great. It ' s not troublesome.
Again, the sin wave, the previous example is the sin wave, no gerstner waves change meticulous, but I am also very satisfied, the implementation of this example does not need to be like gerstner waves need to stack, and no gerstner waves so complex,
first let us study the next wave:
1.     wavelength (L): The crest-to-crest distance between waves;
2.    amplitude (A): The height from the water plane to the wave crest.
3.    speed (S): The distance the crest moves forward per second.
4.    direction (D): The horizontal vector perpendicular to the wave front along which the crext travels.
1.     Wavelength (L): The distance between the crest and the crest.
2.     Amplitude (A): the height from the horizontal to the crest.
3.     Speed (S): The distance the crest moves per second.
4.     Direction (D): The direction of motion, perpendicular to the horizontal direction of the wave plane;
We can use this to defined the wave:
  We'll define the wave with these parameters
& nbsp
The code:
The main codes are as follows:

Float time = _time * _speed/2;float Wavevaluea = sin ((v.vertex.x + v.vertex.z) * _frequency + time * _frequency) * _amplit Ude


That's very easy, right?
It's simple, right?

Simply put how to find the normal, although can ' t pass it,
The normal is given by the cross product of the Binormal and Tangent,as:
N = B * T;
Although it doesn't make sense to pass the normal value to normal, it simply asks the law,
The partial normals B and the tangent T vectors are obtained, respectively, the derivative of the x,z direction,
Vector n = B * T;
And then it's OK.
We got the usual waves as follows:

Then, we used the displacement mapping.
We used the texture in Unity's water----water4 to being our displacement map.
After that, the clutter is on the map (displacement mapping).
We use the map in Unity's own water4 as a resource for map substitution

You can extract the texture in the water4.
Then was the blog article I write before about tessellation. We define a _displacement value to control the other waves ' s amplitude.
Define a _speedstrength value to control the other waves's speed and direction.
Crossing can pick themselves up from the water4.
Then the content of the previous surface subdivision, we set the normal offset to the external variable _displacement to control the amplitude of the clutter,
A parameter _speedstrength is defined externally to control the velocity and direction of movement of the clutter.
Code
The code here is as follows:

Float d = Tex2dlod (_maintex, FLOAT4 (v.texcoord.xy+_speedstrength.xy*_time.x,0,0)). R * _displacement;v.vertex.xyz + = V.normal * D;


Here is the effect:
And then we can see the effect.

That's the end effect about waves.
Is the waveform that is finally implemented in this example.

End of the subject.
Cause of the real waves has sharper peaks and wider troughs. I try to change the sin waves. That is 1-abs (sin);
Because the real wave is more sharp and the trough is wide, the blogger right makes a small change to the sin wave. is the absolute value of sin,

Then we had the waves like the figure above, that's more better than "Just sin".
Get this waveform, a lot better than the normal sin wave ....
I'll gonna to try the fragment shader water wish to has a better effect.
Bloggers will continue to study fragment shader strive for better results, (= =; School is coming, in the mold of the suffering ... Too many escapes)
The waves are over, the surf function would write in the second blog article, maybe I'll write things about the Refra Ction.
In the end, I wish you a Happy Spring festival, again.
Wave research is over, the specific coloring surf function will be discussed in detail in (ii), about refraction reflection and so on;
Finally, I wish you a Happy Spring festival;

------------------------------by wolf96

Unity3d make his own water water effect (i.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.