官术网_书友最值得收藏!

How it works...

The scrolling system starts with the declaration of a couple of properties, which will allow the user of this shader to increase or decrease the speed of the scrolling effect itself. At their core, they are float values being passed from the material's Inspector tab to the surface function of the shader. For more information on shader properties, see Chapter 2, Creating Your First Shader.

Once we have these float values from the material's Inspector tab, we can use them to offset our UV values in the shader.

To begin this process, we first store the UVs in a separate variable called scrolledUV. This variable has to be float2/fixed2 because the UV values are being passed to us from the Input structure:

struct Input 
{ 
  float2 uv_MainTex; 
} 

Once we have access to the mesh's UVs, we can offset them using our scroll speed variables and built-in _Time variable. This built-in variable returns a variable of the float4 type, meaning that each component of this variable contains different values of time as it pertains to game time.

A complete description of these inpidual time values is described at the following link: http://docs.unity3d.com/Manual/SL-UnityShaderVariables.html

This _Time variable will give us an incremented float value based on Unity's game time clock. So, we can use this value to move our UVs in a UV direction and scale that time with our scroll speed variables:

// Create variables that store the inpidual x and y  
// components for the uv's scaled by time 
fixed xScrollValue = _ScrollXSpeed * _Time; 
fixed yScrollValue = _ScrollYSpeed * _Time; 

With the correct offset being calculated by time, we can add the new offset value back to the original UV position. This is why we are using the += operator in the next line. We want to take the original UV position, add the new offset value, and then pass this to the tex2D() function as the texture's new UVs. This creates the effect of the texture moving on the surface. What we are really doing is manipulating the UVs, so we are faking the effect of the texture moving:

scrolledUV += fixed2(xScrollValue, yScrollValue); 
half4 c = tex2D (_MainTex, scrolledUV); 
主站蜘蛛池模板: 瑞昌市| 阿拉善右旗| 玉门市| 谢通门县| 章丘市| 句容市| 澎湖县| 香河县| 黄平县| 剑阁县| 新安县| 荣成市| 汶上县| 阳高县| 东安县| 依兰县| 临漳县| 冕宁县| 沂南县| 康乐县| 凌源市| 泸水县| 杭州市| 沙洋县| 探索| 安图县| 清新县| 玉田县| 乡宁县| 西充县| 大理市| 正蓝旗| 肇东市| 嘉定区| 高安市| 夹江县| 湖北省| 铜鼓县| 广平县| 芦山县| 邵阳县|