Unity3d Shaderlab Simulated Texture movement
In this article, we're going to talk about the effect of a UV map on a shader's scrolling effect, such as a scene that can be used in rivers, waterfalls, lava, and so on. is one of the basic techniques for creating texture animations.
So prepare a new shader file and a new material.
This time we first add some code to the Properties:
Properties {
_maintint ("Diffuse Tint", Color) = (1,1,1,1)
_maintex ("Base (RGB)", 2D) = "White" {}
_scrollxoff ("X Offset", Range (0,10)) =2.3
_scrollyoff ("Y Offset", Range (0,10)) =2.5
}
One of the _maintex is the default, OH.
The next step is to add the variables that are cgprogram in subshader .
FLOAT4 _maintint;
Sampler2d _maintex;
float _scrollxoff;
float _scrollyoff;
Further down, the surf function is modified, and the UV coordinates are changed by the tex2d () function . dynamic Uvsare implemented with built-in _time.
void Surf (Input in, InOut surfaceoutput o) {
Fixed2 scrolluv = In.uv_maintex;
Fixed xOff = _scrollxoff*_time;
Fixed yoff = _scrollyoff*_time;
Scrolluv + = Fixed2 (Xoff,yoff);
Half4 C = tex2d (_maintex, Scrolluv);
O.albedo = C.rgb;
O.alpha = C.A;
}
Finish will then be able to run the observation in the editor with the Shader just written .
From the above procedure, we use the XY Direction parameter to control The scrolling speed of the texture in 2 directions.
at the beginning of the program, we will UV value is stored in the Scrolluv , this is a FLOAT2 or Fixed2 type of variable, because UV value is passed input struct passed in.
when accessing the grid's UV , we can use the scrolling speed variable and the built -in _time variable for texture offset.
speaking of _time it is a Unity the increment floating-point value of the game clock.
So, we can use it in UV the direction to calculate the new UV value, and then passes the new offset value to the original UV value, which is calculated and passed to tex2d () as the new UV texture.
Through these steps, the motion of the texture is simply simulated.
Code Start------------------------------------------------------------
Shader "91ygame/basicoffset" {
Properties {
_maintint ("Diffuse Tint", Color) = (1,1,1,1)
_maintex ("Base (RGB)", 2D) = "White" {}
_scrollxoff ("X Offset", Range (0,10)) =2.3
_scrollyoff ("Y Offset", Range (0,10)) =2.5
}
Subshader {
Tags {"Rendertype" = "Opaque"}
LOD 200
Cgprogram
#pragma surface surf Lambert
FLOAT4 _maintint;
Sampler2d _maintex;
float _scrollxoff;
float _scrollyoff;
struct Input {
FLOAT2 Uv_maintex;
};
void Surf (Input in, InOut surfaceoutput o) {
Fixed2 scrolluv = In.uv_maintex;
Fixed xOff = _scrollxoff*_time;
Fixed yoff = _scrollyoff*_time;
Scrolluv + = Fixed2 (Xoff,yoff);
Half4 C = tex2d (_maintex, Scrolluv);
O.albedo = C.rgb;
O.alpha = C.A;
}
Endcg
}
FallBack "Diffuse"
}
Code End--------------------------------------------------------------
Unity3d Shaderlab Simulated Texture movement