【技美百人计划】图形3.1模板与深度测试

模板测试

盒子蒙版

stenceil 设置ref , Equal话的就着色
蒙版shader:

Shader "Stencil/stencilMask"
{
    Properties
    {
        _ID("Mask ID", Int) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue" = "Geometry+1" }
        ColorMask 0
        ZWrite off
        Stencil {
            Ref [_ID]
            Comp always
            Pass replace
        }

        Pass
        {

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            float4 vert (float4 v:POSITION) : SV_POSITION
            {
                return UnityObjectToClipPos(v);
            }

            fixed4 frag () : SV_Target
            {
                return fixed4(1,1,1,1);
            }
            ENDCG
        }
    }
}

obj 的 shader:

Shader "Stencil/stencilObj"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _ID("Mask ID", Int) = 1
		_BaseColor("Base Color", Color) = (0, 1, 0, 1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue" = "Geometry+2" }
        LOD 100
        Stencil {
            Ref [_ID]
            Comp equal
        }
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            float4 vert (float4 v:POSITION) : SV_POSITION
            {
                return UnityObjectToClipPos(v);
            }

            fixed4 _BaseColor;

            fixed4 frag () : SV_Target
            {
                return _BaseColor;
            }
            ENDCG

        }
    }
}

请添加图片描述

描边

双pass:
1pass : 写入stencil ref 1
2pass :用法线往外扩,设置stencil ref 0 ,通过NotEqual 过滤第一个pass的内容

Shader "Stencil/StencilStroke"
{
    Properties
    {
        _StrokeLen("StrokeLen", Float) = 0.05
		_OutlineColor("Base Color", Color) = (0, 1, 0, 1)
    }
    SubShader
    {
        Pass
        {
            Stencil {
                Ref 1
                Comp Always
                Pass Replace
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            float4 vert (float4 v:POSITION) : SV_POSITION
            {
                return UnityObjectToClipPos(v);
            }

            fixed4 frag () : SV_Target
            {
                return fixed4(1,1,1,1);
            }
            ENDCG
        }

        Pass
        {
            Stencil {
                Ref 1
                Comp NotEqual
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            float _StrokeLen;
            fixed4 _OutlineColor;


            struct appdata
            {
                float4 vertex : POSITION;
                float4 normal : NORMAL;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };


            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = v.vertex + normalize(v.normal) * _StrokeLen;
                o.vertex = UnityObjectToClipPos(o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                return _OutlineColor;
            }
            ENDCG
        }
    }
}

请添加图片描述

深度测试

X-Ray

1pass:先渲染不透明部分,写入深度,防止第二个pass覆盖掉

2pass:再渲染透视部分,只有挡板前的深度才足以渲染得到。用视角与法线的点乘描边,再用Blend混合透明度

Shader "Depth/xray"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _rayColor("Base Color", Color) = (0, 1, 1, 1)
    }
    SubShader
    {

        Pass
        {
            Tags { "RenderType"="Opaque" }
            ZTest LEqual
            ZWrite on

            CGPROGRAM
            #include "UnityCG.cginc"
            #pragma vertex vert
            #pragma fragment frag

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

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

            
            sampler2D _MainTex;
            float4 _MainTex_ST;

             v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.pos);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);

                return o;
            }

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

        Pass
        {
            Tags { "RenderType"="Transparent" "Queue"="Transparent" }
            Blend SrcAlpha One
            ZTest Greater
            ZWrite Off
            CULL Back

            CGPROGRAM
            #include "UnityCG.cginc"
            #pragma vertex vert
            #pragma fragment frag

            struct appdata
            {
                float4 pos : POSITION;
                float4 normal : NORMAL;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float4 color : COLOR;
            };

            fixed4 _rayColor;

            v2f vert (appdata vertex)
            {
                v2f o;

                o.pos = UnityObjectToClipPos(vertex.pos);

                float3 viewDir = ObjSpaceViewDir(o.pos);
                viewDir = normalize(viewDir);
                float3 normal = normalize(vertex.normal);
                float rim = 1 - dot(normal, viewDir);
                o.color = _rayColor * rim;

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                return i.color;
            }
            ENDCG
        }
    }
}

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值