庄懂TA lesson5

Phong 连连看

 Phong blinn-Phong shader代码实现

Shader "Shader/oldschool" {
    Properties{
        _MainCol("color",color) = (1.0,1.0,1.0,1.0)
        _SpecularPow("specularPow",range(1,90)) = 30
    }
    SubShader{
        Tags {
            "RenderType" = "Opaque"
        }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode" = "ForwardBase"
            }


            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0
            uniform float3 _MainCol;
            uniform float _SpecularPow;
            struct VertexInput {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };
            struct VertexOutput {
                float4 posCS : SV_POSITION;
                float4 posWS : TEXCOORD1;
                float3 nDirWS: TEXCOORD0;
            };
            VertexOutput vert(VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.posCS = UnityObjectToClipPos(v.vertex);
                o.posWS = mul(unity_ObjectToWorld,v.vertex);
                o.nDirWS=UnityObjectToWorldNormal(v.normal);
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                float3 nDir = i.nDirWS;
                float3 lDir = normalize(_WorldSpaceLightPos0.xyz);
                float3 vDir = normalize(_WorldSpaceCameraPos.xyz-i.posWS.xyz);
                float3 hDir = normalize(lDir+vDir);
                float3 relDir = reflect(-lDir,nDir);//lDir是光的反方向要乘-1,再求沿法线方向的反射光

                float ndoth = dot(nDir,hDir);//blinnPhong
                float ndotl = dot(nDir,lDir);//lambert
                float vdotrl = dot(vDir,relDir);//Phong 反射光和人的视线点乘

                float bPhong = pow(max(0,ndoth),_SpecularPow);
                float Phong = pow(max(0,vdotrl),_SpecularPow);
                float lambert = max(0,ndotl);
                float3 res = _MainCol*lambert+Phong;
                return float4(res,1);
            }
                ENDCG
            }
    }
        FallBack "Diffuse"
}

 BRDF的双向指的是光照方向,人观察方向,光的反射分布取决于以上两方向,也就是说提供光照,观察以及物体法线方向BRDF可以生成光反射结果

posted @ 2023-01-30 21:53  LOFU  阅读(21)  评论(0编辑  收藏  举报