UnityShader學習之旅(1)雙面材質

先上代碼

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Unlit/NewUnlitShader"
{
   Properties
    {
        _MainTexOne ("TEXONE", 2D) = "white" {}//正面貼圖
        _MainTexTwo ("TEXTWO", 2D) = "white" {}//反面貼圖
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }//渲染隊列
        LOD 100
        Pass//pass用來渲染前面,裁剪掉背面
        {
            Tags{"LightMode" = "ForwardBase"}
            Cull Back//裁剪背面
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTexOne;
            float4 _MainTexOne_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTexOne);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTexOne, i.uv);      
                return col;
            }
            ENDCG
        }
        Pass//pass用來渲染背面,裁剪前面
        {
            Tags{"LightMode" = "ForwardBase"}
            Cull Front//裁剪前面
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTexTwo;
            float4 _MainTexTwo_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTexTwo);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTexTwo, i.uv);      
                return col;
            }
            ENDCG
        }

    }

Unity3d製作雙面材質:

Shader中Cull指令的使用: 
通過Cull指令來控制需要剔除那個面的渲染圖元。Cull指令的語法如下: 
Cull Back | Front | Off 
設置爲Back,背對攝像機的渲染圖源就不會被渲染,設置爲Front,朝向攝像機的渲染圖元就不會被渲染。 
設置爲Off就會關閉剔除功能,即所有圖元都會被渲染。

步驟: 
1.在項目面板中創建plane作爲演示。

2.導入兩張圖片作爲貼圖演示。

3.在項目面板中創建DoubleSurface shader

4.打開創建的shader進行編輯:
 

Shader "Custom/DoubleSurface" {
    Properties{
        _Color("Main Color", Color) = (1,1,1,1)//Tint Color
        _MainTex("Base (RGB)", 2D) = "white" {} //背面紋理
        _MainTex_2("Base (RGB)", 2D) = "white" {} //正面紋理
    }

        SubShader{
        Tags{ "RenderType" = "Opaque" }    //設置渲染類型 Opaque不透明
        LOD 100

        Pass{
        Cull Front       //關閉正面渲染
        Lighting Off
        SetTexture[_MainTex]{ combine texture }
        SetTexture[_MainTex]
            {
            ConstantColor[_Color]
            Combine Previous * Constant
            }
        }

        Pass
        {
        Cull Back      //關閉背面渲染
        Lighting Off
        SetTexture[_MainTex_2]{ combine texture }
        SetTexture[_MainTex_2]
            {
            ConstantColor[_Color]
            Combine Previous * Constant
            }
        }
    }
}

參考: 
《Shader入門精要》,馮樂樂

如果是透明通道的圖片可以替換類型

Tags{"Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True"}
	   Blend SrcAlpha OneMinusSrcAlpha

 

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