Unity Shaders and Effects Cookbook (7-2) for vertex animation in Surface Shader

Source: Internet
Author: User
Tags sin

As mentioned in the previous section, in Surface Shader, adding vertex functions, we can get vertex data in vertex functions, such as vertex color, vertex coordinates, and so on.

This section learns to get vertex coordinates and modify vertex coordinates to animate vertices.


Simple Introduction Principle:

In the vertex function, get to the vertex coordinate vertex, then the float OffsetY = sin (vertex.x), and then add the OffsetY to the vertex.y so that the original plane becomes the sine wave.


Then, using the built-in variable _time you learned earlier, the calculation becomes a float offsety=sin (vertex.x * _time), which forms an animation.


Start the exercise below.

Create a new scene, import the accompanying book resource, create a new material, and create a new shader.

Assigns the newly created Material to Plane.

Just set up a good scene, as follows


In accordance with the above-mentioned principle, to modify the shader.


1. First declare the parameter vertex, tell unity we have vertex function in shader

pragma surface surf Lambert vertex:vert

2. Create vert vertex function

void Vert (inout appdata_full v,out Input o) {unity_initialize_output (input,o); float Offsety=sin (v.vertex.x); V.VERTEX.XYZ=FLOAT3 (V.VERTEX.X,V.VERTEX.Y+OFFSETY,V.VERTEX.Z);}

In the vertex function, the vertex.x of the vertex coordinates as the parameter, the sin value as the OffsetY, and then vertex.y and OffsetY Add.

Get an effect like this

Transfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn

3, use _time, move up

void Vert (inout appdata_full v,out Input o) {unity_initialize_output (input,o); float Offsety=sin (v.vertex.x + _time.y); V.VERTEX.XYZ=FLOAT3 (V.VERTEX.X,V.VERTEX.Y+OFFSETY,V.VERTEX.Z);}



It's moving, but it doesn't have a light or dark effect.

This is because although the position of the vertex has been modified, the normal data is not modified. So now the normal data is still the previous plane of that set. Because the plane of the normal data are the same, there is no light and dark points.


4, modify the normal

modifying normals in vertex functions

void Vert (inout appdata_full v,out Input o) {unity_initialize_output (input,o); float Offsety=sin (v.vertex.x + _time.y); V.VERTEX.XYZ=FLOAT3 (V.VERTEX.X,V.VERTEX.Y+OFFSETY,V.VERTEX.Z); V.normal=normalize (FLOAT3 (V.normal.x+offsetY, V.NORMAL.Y,V.NORMAL.Z));}



5, looks more smooth

The above effect, can be clearly seen in the polyline, it does not look smooth. This is because the number of vertices of the Plane, such as only 10 vertices on the x-axis, the 10 vertices are assigned to 2 sine cycles, will certainly look tortuous.

So we reduce the sine wave period and make it look more sleek.


Modify the sentence of sin in the vertex function

Float Offsety=sin (v.vertex.x * 0.5 + _time.y);


Full shader code

Shader "Cookbookshaders/chapt7-2/vertexanimation" {Properties{_maintex ("Base (RGB)", 2D) = "White" {}}subshader {Tags { "Rendertype" = "Opaque"}lod 200cgprogram#pragma surface surf Lambert vertex:vertsampler2d _maintex;struct Input {float2 Uv_maintex;}; void Vert (inout appdata_full v,out Input o) {unity_initialize_output (input,o); float offsety=sin (v.vertex.x * 0.5 + _) TIME.Y); V.vertex.xyz=float3 (v.vertex.x,v.vertex.y+offsety,v.vertex.z); V.normal=normalize (FLOAT3 (v.normal.x+ OFFSETY,V.NORMAL.Y,V.NORMAL.Z));} void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = C.rgb;o. Alpha = C.A;} ENDCG} FallBack "Diffuse"}

The book also adds a mix of colors to make the effect look more dazzling, not here.

Transfer from Http://blog.csdn.net/huutu http://www.thisisgame.com.cn

Learn about a function mentioned in the book Lerp


Lerp interpolation function

float f = lerp (from,to,t) = from* (1-t) + to*t


When t = 0 returns from, when t = 1 returns to. When t = 0.5 returns the mean of the From and to.


Sample Project Download

Http://pan.baidu.com/s/1qXUbsUK





Unity Shaders and Effects Cookbook (7-2) for vertex animation in Surface Shader

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.