# 【透光】

可以实现类似太阳散射光,蜡烛透光,耳朵透光等效果。

Markdown 图片

Shader "Unlit/TestPenetrate"
{
    Properties
    {
        _Pow("Pow",Range(1,100)) = 1
        _Scale("Scale",Range(0,10)) = 1
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            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;
                float3 posWS : TEXCOORD1;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Pow;
            float _Scale;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.posWS = mul(unity_ObjectToWorld,v.vertex).xyz;
                return o;
            }

            float4 frag (v2f i) : SV_Target
            {
                float3 vDirWS = normalize(_WorldSpaceCameraPos.xyz - i.posWS.xyz);
                float3 lDirWS = normalize(_WorldSpaceLightPos0.xyz);
                float vDotl = dot(-vDirWS,lDirWS);
                float penetrate = max(pow(vDotl,_Pow) * _Scale,0);
                return penetrate;
            }
            ENDCG
        }
    }
}