Water effect (1)

Source: Internet
Author: User
Tags mul

In the first section of the GPU Gems1, you can see the Gerstner wave function to simulate the ocean's wave effect, while in Unity's water (Pro) effect, part of the water wave effect in fx/water4 is modeled by this formula.

First look at the code of Fx-water4.shader.

    Tags {"Rendertype" = "Transparent" "Queue" = "Transparent"}    Lod    colormask RGB        grabpass {"_refractiontex "}    Pass {                #pragma vertex vert            #pragma fragment Frag        }}subshader {         Tags {" Rendertype "=" Transparent "Queue" = "Transparent"}        Lod    colormask RGB        Pass {             #pragma vertex vert300            #pragma fragment frag300          }    }subshader {         Tags {"Rendertype" = "Transparent" "Queue" = "Transparent"}        Lod    Colormask RGB        Pass {            #pragma vertex vert200            #pragma fragment frag200    }    }fallback " Transparent/diffuse "
It contains three Subshader, which is the specific subshader that is set SharedMaterial.shader.maximumLOD by WaterBase.cs. We start with low quality, the code for the vertex shader is as follows.
v2f_simple vert200 (Appdata_full v) {v2f_simple o; //calculates the world coordinates of a vertexHalf3 Worldspacevertex =Mul (_object2world, V.vertex). xyz; Half2 TILEABLEUV=Worldspacevertex.xz; //by setting the _bumpdirection.xy value to simulate the first direction, the ZW value simulates another direction, _bumptiling.xy simulates the rate of the first direction, and ZW simulates the rate of change in the second direction. O.BUMPCOORDS.XYZW = (Tileableuv.xyxy + _time.xxxx * _bumpdirection.xyzw) *_BUMPTILING.XYZW; //The viewinterpolator is used to calculate the orientation of the camera to that vertex. O.VIEWINTERPOLATOR.XYZ = worldspacevertex-_worldspacecamerapos; //POS is temporarily useless. O.pos =Mul (UNITY_MATRIX_MVP, V.vertex); O.VIEWINTERPOLATOR.W=1;//getdistancefadeout (Computescreenpos (O.pos). W, Distance_scale);            returno; }                                                                                                                

In the element shader, we can see that the calculation of the normals is done by the Perpixelnormal function, which can be found in the waterinclude.cginc.

inline Half3 perpixelnormal (sampler2d bumpmap, Half4 coords, Half3 vertexnormal, half bumpstrength) {//calculates the values of two different orientations in the normal map. Half4 bump = tex2d (BumpMap, Coords.xy) +tex2d (BumpMap, COORDS.ZW);//calculate the normal directionBump.xy = BUMP.WY-HALF2 (1.0,1.0);//by Bumpstrength to increase the proportion of x,z relative to Y, that is, to increase the texture of the bump sense, in unity can be seen, the greater the bumpstrength, the more obvious the surface reflection effect. Half3 worldnormal = vertexnormal + Bump.xxy * bumpstrength * HALF3 (1,0,1);//normalized calculation.     returnnormalize (worldnormal);} 

After getting the normal direction, the specific element shader code is as follows.

Half4 frag200 (v2f_simple i): sv_target {//Normal calculationHalf3 worldnormal = Perpixelnormal (_bumpmap, I.bumpcoords, Half3 (0,1,0), per_pixel_displace);//normalized angle of view vectorHalf3 Viewvector =normalize (i.viewinterpolator.xyz);//temporarily uselessHalf3 Reflectvector =normalize (Reflect (Viewvector, worldnormal));//half-width vector calculation, reducing the computational amount between light and normal (Jim Blinn), consistent with Binnphong high-light model calculationsHalf3 h = normalize ((_WORLDLIGHTDIR.XYZ) +viewvector.xyz); floatNH = MAX (0, Dot (worldnormal,-h)); floatSpec = max (0.0, POW (NH, _shininess)); //controlling scaling with the Fresnel parameterWorldnormal.xz *=_fresnelscale; //The Fresnel reflectivity is calculated. Half REFL2REFR =Fresnel (Viewvector, Worldnormal, Fresnel_bias, fresnel_power); Half4 Basecolor=_basecolor;//blends refraction and reflection colors by Fresnel reflectivity. Basecolor = Lerp (Basecolor, _reflectioncolor, saturate (REFL2REFR *2.0));//The greater the reflectivity of the Fresnel, the greater the alpha, the more visible the underwater items. BASECOLOR.A = Saturate (2.0* REFL2REFR +0.5);//mixed with highlights, final color calculation. Basecolor.rgb + = Spec *_specularcolor.rgb; returnBasecolor; }

The core is the calculation of Fresnel reflectivity, the basic principle of Fresnel is the lower the angle of sight and the normal, that is, the more perpendicular to the plane, the smaller the reflection, the greater the refraction, the clearer the underwater objects, the more parallel the line of sight and the plane, the greater the reflection, the more visible the underwater items. In the Waterinclude.cginc, there is a simple calculation formula of Fresnel.

inline Half Fresnel (half3 viewvector, Half3 worldnormal, half bias, half power) {/// angle calculation.     Half facing =  Clamp (1.00.00.0,1.0); // the calculation of reflectivity.        Half REFL2REFR = Saturate (bias+ (1.0-bias) * pow (facing,power));         return refl2refr;    }

This section mainly simulates the simple water waves by changing the UV texture, but does not achieve large wave-like effects under the same normal map. The next section focuses on how the Gerstner wave function achieves advanced water-wave effects.

Water effect (1)

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.