其他分享
首页 > 其他分享> > 图形 5 Unity Shader的简单了解(七)·基本光照模型中的高光反射

图形 5 Unity Shader的简单了解(七)·基本光照模型中的高光反射

作者:互联网

1. 基本光照模型中的高光反射计算公式:

  

   

   

   

   

   

 其中反射方向可以由表面法线和光源方向计算:

  

并且Cg给提供了计算反射方向的函数reflect,函数:reflect(i, n);

2.逐顶点

Shader "Custom/c6_SpecularVertexLevel_1"
{
    Properties
    {
        _Diffuse ("Diffuse", Color) = (1, 1, 1, 1)
        _Specular ("Specular", Color) = (1, 1, 1, 1)
        _Gloss ("Gloss", Range(8.0, 256)) = 20
    }
    SubShader
    {
        Pass {
            Tags { "LightMode"="ForwardBase" }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Lighting.cginc"
                fixed4 _Diffuse;
                fixed4 _Specular;
                float _Gloss;
                struct a2v {
                    float4 vertex : POSITION;
                    float3 normal : NORMAL;
                };
                struct v2f {
                    float4 pos : SV_POSITION;
                    float3 color : COLOR;
                };
                v2f vert(a2v v) {
                    v2f o;
                    o.pos = UnityObjectToClipPos(v.vertex); // UnityObjectToColorPos(v.vertex);
                    fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

                    fixed3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
                    fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                    fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));

                    fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormal));
                    fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, v.vertex).xyz);
                    fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)),_Gloss);
                    o.color = ambient + diffuse + specular;
                    return o;
                }
                fixed4 frag(v2f i) : SV_Target {
                    return fixed4(i.color, 1.0);
                }
            ENDCG
        }
    }
    FallBack "Diffuse"
}

3.逐像素(按照逐像素的方式处理光照可以得到更平滑的高光效果)

Shader "Unlit/c6_SpecularPixelLevel_1"
{
    Properties
    {
        _Diffuse ("Diffuse", Color) = (1, 1, 1, 1)
        _Specular ("Specular", Color) = (1, 1, 1, 1)
        _Gloss ("Gloss", Range(8.0, 256)) = 20
    }
    SubShader
    {
        Pass
        {
            Tags { "LightMode"="ForwardBase" }
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "Lighting.cginc"
                fixed4 _Diffuse;
                fixed4 _Specular;
                float _Gloss;
                struct a2v {
                    float4 vertex : POSITION;
                    float3 normal : NORMAL;
                };
                struct v2f {
                    float4 pos : SV_POSITION;
                    float3 worldNormal : TEXCOORD0;
                    float3 worldPos : TEXCOORD1;
                };
                v2f vert(a2v v) {
                    v2f o;
                    o.pos = UnityObjectToClipPos(v.vertex); // UnityObjectToColorPos(v.vertex);
                    o.worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject));
                    o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                    return o;
                }
                fixed4 frag(v2f i) : SV_Target {
                    fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

                    fixed3 worldNormal = normalize(i.worldNormal);
                    fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                    fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir));

                    fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormal));
                    fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
                    fixed3 spacular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)),_Gloss);

                    return fixed4(ambient + diffuse + spacular, 1.0);
                }
            ENDCG
        }
    }
    FallBack "Diffuse"
}

 

4.默认材质(左二)、逐顶点(中)与逐像素(右二)的对比

标签:normalize,xyz,vertex,Shader,fixed3,183,&#,worldNormal,Diffuse
来源: https://www.cnblogs.com/chenjianqi/p/14187499.html