【光能蜗牛的图形学之旅】Unity镜面反射

图片效果


【光能蜗牛的图形学之旅】Unity镜面反射_第1张图片
spec.png

Unity镜面反射基本代码模型

Blinn-Phone模型

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

Shader "Unlit/Specular"
{
    Properties
    {
        _Diffuse ("_DiffuseColor", Color) = (1,1,1,1)
        _ValveIndex("ValveIndex",Range(0,1)) = 0.5
        _Specular("_SpecularColor",Color) = (1,1,1,1)
        _Glossness("Glossness",Range(1.0,256))=8.0
    }
        SubShader
    {
        Tags { 
            "RenderType" = "Opaque" 
            "LightMode" = "ForwardBase"
        }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

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

            fixed4 _Diffuse;
            float _ValveIndex;
            fixed4 _Specular;
            float _Glossness; 

            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal:NORMAL;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 Pos : SV_POSITION;
                float3 color:COLOR;
                float3 worldNormal: TEXCOORD1;
                float3 WordPos: TEXCOORD2;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            
            v2f vert (a2v v)
            {
                v2f o;
                o.Pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.worldNormal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));
                o.WordPos = normalize(mul((float3x3)unity_ObjectToWorld, v.vertex));
                ///UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
                { 


                float3 nDir = i.worldNormal;
                float3 lDir = normalize(_WorldSpaceLightPos0.xyz);
                float3 reflectDir = normalize(reflect(-lDir,i.worldNormal));
                float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.WordPos.xyz);


                fixed3 specular = _LightColor0 * _Specular * pow(saturate(dot(viewDir, reflectDir)),_Glossness);
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                fixed3 diffuse = _LightColor0 * _Diffuse * saturate(dot(nDir, lDir)* _ValveIndex + (1 - _ValveIndex) );
                float3 color = ambient + diffuse + specular;
                // sample the texture
                //fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                //UNITY_APPLY_FOG(i.fogCoord, col);
                //return col;
                return fixed4(color,1);
            }
            ENDCG
        }
    }
}

Blinn-Phone模型 改进后的 半角向量模型

效果差不多,但是性能上会好一些,因为它不计算反射向量


【光能蜗牛的图形学之旅】Unity镜面反射_第2张图片
微信截图_20180407211708.png
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

Shader "Unlit/HalfSpecular"
{
    Properties
    {
        _Diffuse ("_DiffuseColor", Color) = (1,1,1,1)
        _ValveIndex("ValveIndex",Range(0,1)) = 0.5
        _Specular("_SpecularColor",Color) = (1,1,1,1)
        _Glossness("Glossness",Range(1.0,256))=8.0
    }
        SubShader
    {
        Tags { 
            "RenderType" = "Opaque" 
            "LightMode" = "ForwardBase"
        }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

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

            fixed4 _Diffuse;
            float _ValveIndex;
            fixed4 _Specular;
            float _Glossness; 

            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal:NORMAL;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 Pos : SV_POSITION;
                float3 color:COLOR;
                float3 worldNormal: TEXCOORD1;
                float3 WordPos: TEXCOORD2;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            
            v2f vert (a2v v)
            {
                v2f o;
                o.Pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.worldNormal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));
                o.WordPos = normalize(mul((float3x3)unity_ObjectToWorld, v.vertex));
                ///UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
                { 


                float3 nDir = i.worldNormal;
                float3 lDir = normalize(_WorldSpaceLightPos0.xyz);
                float3 reflectDir = normalize(reflect(-lDir,i.worldNormal));
                float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.WordPos.xyz);
                float3 halfDir = normalize(lDir + viewDir);


                fixed3 specular = _LightColor0 * _Specular * pow(saturate(dot(i.worldNormal, halfDir)),_Glossness);
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                fixed3 diffuse = _LightColor0 * _Diffuse * saturate(dot(nDir, lDir)* _ValveIndex + (1 - _ValveIndex) );
                float3 color = ambient + diffuse + specular;
                // sample the texture
                //fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                //UNITY_APPLY_FOG(i.fogCoord, col);
                //return col;
                return fixed4(color,1);
            }
            ENDCG
        }
    }
}

你可能感兴趣的:(【光能蜗牛的图形学之旅】Unity镜面反射)