Unity shader学习之反射

shader如下:

Shader "Custom/Reflection"
{
    Properties
    {
        _Cubemap("Cubemap", Cube) = "" {}
        _ReflectionAmount("Reflection Amount", Range(0, 1)) = 0.5
    }

    SubShader
    {
        Pass
        {
            Tags
            {
                "LightMode" = "ForwardBase"
            }
            Cull Off

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase

            #include "UnityCG.cginc"
            #include "Lighting.cginc"
            #include "AutoLight.cginc"

            samplerCUBE _Cubemap;
            float _ReflectionAmount;

            struct appdata
            {
                float4 vertex : POSITION;
                fixed4 color : COLOR;
                float3 normal : NORMAl;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                fixed4 color : COLOR;
                float3 worldNormal : TEXCOORD0;
                float3 worldPos : TEXCOORD1;
                SHADOW_COORDS(2)
            };

            v2f vert(appdata v)
            {
                v2f o;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.color = v.color;
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                TRANSFER_SHADOW(o);
                return o;
            }

            fixed4 frag(v2f i) : SV_TARGET
            {
                fixed3 albedo = i.color.rgb;

                fixed3 ambient = albedo * UNITY_LIGHTMODEL_AMBIENT.rgb;

                float3 worldLight = UnityWorldSpaceLightDir(i.worldPos);
                fixed3 diffuse = albedo * _LightColor0.rgb * max(0, dot(worldLight, i.worldNormal));

                float3 worldView = UnityWorldSpaceViewDir(i.worldPos);
                float3 refDir = reflect(-worldView, i.worldNormal);
                fixed3 refCol = texCUBE(_Cubemap, refDir);

                UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);

                fixed3 col = ambient + lerp(diffuse, refCol, _ReflectionAmount) * atten;

                return fixed4(col, 1);
            }

            ENDCG
        }
    }

    Fallback "VertexLit"
}

效果如下:

 

posted @ 2017-07-18 14:08  孤独の巡礼  阅读(1224)  评论(0编辑  收藏  举报