第二章 (2)添加紋理 VU 滾動

                                        添加紋理 VU 滾動


給着色器 添加紋理:

2D 圖片 是如何 映射到 3D 模型上的 ,這個映射過程 稱爲 紋理映射

現在我們都知道 每個模型都是由 三角面構成的;二在模型製作過程中 每個三角面其實都 記錄過 UV 信息了,

 

 

一些 Shader 的 固定寫法:

Properties
{
    _MainTex ("Albedo (RGB)", 2D) = "white" {}
}

SubShader
{
     sampler2D _MainTex;

     struct Input
     {
         float2 uv_MainTex;
     };
     
}
sampler2D _MainTex;

float2 uv_MainTex;

那麼  uv_MainTex  就是   _MainTex 的 uv 座標

 

下面是是一個 手寫的額 UV 移動的 河流

Shader "Custom/chapter02/ScrollShader"
{
    Properties
    {
        _MainColor ("DiffuseColor", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
	_ScrollSpeedX("ScrollSpeedX", float )= 2   //移動 X  軸的速度
	_ScrollSpeedY("ScrollSpeedY", float) = 2   //移動 y  軸的速度
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };
        fixed4 _MainColor;

	fixed _ScrollSpeedX;
	fixed _ScrollSpeedY;



        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
			fixed2 scrolledUV = IN.uv_MainTex;
			fixed  scrollX = _ScrollSpeedX * _Time;
			fixed  scrollY = _ScrollSpeedY * _Time;

			scrolledUV += fixed2(scrollX, scrollY);

			half4 c = tex2D(_MainTex, scrolledUV);
			o.Albedo = c.rgb * _MainColor;
			o.Alpha  = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

 

Shader 中內置 Time 時間 變量,一般常用於 帶動畫的 shader

Time 變量 經常用於 Shader 中一些 可以的動的東西; 它是一個 float4  類型的變量;每一個分量都包含一個 Unity的時間 值

名稱 類型 說明
_Time float4 t,  ( t/20,   t,  t*2, t*3 )     t 爲shader開始運行累計時間
_SinTime float4 t,  (  t/8,   t/4,  t/2,  t )
_CosTime float4 t,  (  t/8,   t/4,  t/2,  t )
unity_DeltaTime float4 dt, (dt,  1/dt, smoothDt, 1/smoothDt)

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章