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

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山竹炒大蒜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值