Unity Shaders and Effects Cookbook (7-2) Surface Shader Implemented Porthot Animation

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

This section learns to obtain vertex coordinates, and modify the vertex coordinates to achieve vertex animation.

A brief introduction to the principle:

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

Then use the built-in variable _Time that I have learned before, and the formula becomes float offsetY=sin(vertex.x * _Time), thus forming an animation.

Start practicing below.

Create a new Scene, import book resources, create a new Material, and create a new Shader.

Assign the newly created Material to Plane.

Just set up the scene, as follows

< /p>

According to the principle mentioned above, modify the Shader.

1. First declare the parameter vertex to tell Unity that there are vertex functions in our Shader

pragma surface surf Lambert vertex:vert

2, create a 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 coordinate vertex.x is used as a parameter, Find the value of sin as offsetY, and then add vertex.y to offsetY.

Get the effect as shown below

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

3. Use _Time to 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 there is no shading effect.

This is because although the position of the vertex is modified, the normal data is not modified. So the current normal data is still the set of the previous plane. Because the normal data of the plane is the same, there is no difference between light and dark.

4. Modify the normal line

Modify the normal line in the vertex function

 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. It looks more sleek

In the above effect , The broken line can be clearly seen, and it does not look smooth. This is because Plane has a small number of vertices. For example, there are only 10 vertices on the x-axis. These 10 vertices are assigned to 2 sine wave cycles, which will definitely look zigzags.

Then we reduce the period of the sine wave to make it look smoother.

Modify the sentence of sin in the vertex function

float offsetY=sin(v.vertex.x * 0.5 + _Time.y);

Complete Shader code

Shader "CookBookShaders/Chapt7-2/VertexAnimation" 
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}

SubShader
{
Tags {"RenderType "="Opaque" }
LOD 200

CGPROGRAM
#pragma surface surf Lambert vertex:vert

sampler2D _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;< br /> o.Alpha = ca;
}
ENDCG
}
FallBack "Diffuse"
}

Color mixing makes the effect look more dazzling, so I won’t do it here.

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

Learn about a function lerp mentioned in the book

Lerp (from: float, to: float, t: float)

Lerp interpolation function

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

When t = 0, return from, and when t = 1 return to. When t = 0.5 returns the average of from and to.

Sample project download

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

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

This section learns to obtain vertex coordinates, and modify the vertex coordinates to achieve vertex animation.


A brief introduction to the principle:

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


Then use the built-in variable _Time that I have learned before, and the formula becomes float offsetY=sin(vertex.x * _Time), thus forming an animation.


Start practicing below.

Create a new Scene, import book resources, create a new Material, and create a new Shader.

Assign the newly created Material to Plane.

Just set up the scene, as follows

< /p>


According to the principle mentioned above, modify the Shader.


1. First declare the parameter vertex to tell Unity that there are vertex functions in our Shader

pragma surface surf Lambert vertex:vert

2, create a 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 coordinate vertex.x is used as a parameter, Find the value of sin as offsetY, and then add vertex.y to offsetY.

Get the effect as shown below

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

3. Use _Time to 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 there is no shading effect.

This is because although the position of the vertex is modified, the normal data is not modified. So the current normal data is still the set of the previous plane. Because the normal data of the plane is the same, there is no difference between light and dark.

4. Modify the normal line

Modify the normal line in the vertex function

 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. It looks more sleek

In the above effect , The broken line can be clearly seen, and it does not look smooth. This is because Plane has a small number of vertices. For example, there are only 10 vertices on the x-axis. These 10 vertices are assigned to 2 sine wave cycles, which will definitely look zigzags.

Then we reduce the period of the sine wave to make it look smoother.

Modify the sentence of sin in the vertex function

float offsetY=sin(v.vertex.x * 0.5 + _Time.y);

Complete Shader code

Shader "CookBookShaders/Chapt7-2/VertexAnimation" 
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}

SubShader
{
Tags {"RenderType "="Opaque" }
LOD 200

CGPROGRAM
#pragma surface surf Lambert vertex:vert

sampler2D _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 = ca;
}
ENDCG
}
FallBack "Diffuse"
}

The book also adds color mixing to make the effect look It’s more dazzling, so I won’t do it here.

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

Learn about a function lerp mentioned in the book

Lerp (from: float, to: float, t: float)

Lerp interpolation function

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

When t = 0, return from, and when t = 1 return to. When t = 0.5 returns the average of from and to.

Sample project download

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

Leave a Comment

Your email address will not be published.