美文网首页
BlinnPhong补充

BlinnPhong补充

作者: Rayson | 来源:发表于2020-06-16 18:44 被阅读0次
            Shader "Davia/09_Specular Fragment-BlinnPhong"{

    Properties{
        _DiffuseColor("Diffuse Color",color) = (1,1,1,1)
        _SpecularColor("Specular Color",color) = (1,1,1,1)
        _SpecularPow("Specular Pow",Range(1,20)) = 1.0
    }

    SubShader
    {
        Pass
        {
            Tags{"Lightmode" = "ForwardBase"}
            CGPROGRAM
            #include"Lighting.cginc"
            #pragma vertex vert
            #pragma fragment frag

            fixed4 _DiffuseColor;
            fixed4 _SpecularColor;
            fixed  _SpecularPow;


            //application to vertex
            struct a2v{
                float4 vertex:POSITION;
                fixed3 normal:NORMAL;

            };
            //vertex to fragment
            struct v2f{
                float4 position:SV_POSITION;
                float3 worldnormal:TEXCOORD0;
                float3 worldvertex:TEXCOORD1;
            };

            //将计算过程放在顶点中:逐顶点光照
            v2f vert(a2v v) { 
                v2f f;
                //顶点位置从模型空间转换到裁减空间 也叫投影空间
                f.position = mul(UNITY_MATRIX_MVP,v.vertex);
                //法线、顶点在世界空间中的位置 放在顶点函数中计算
                f.worldnormal = mul(v.normal, (float3x3)_World2Object);
                f.worldvertex = mul(v.vertex, _World2Object).xyz;
                return f;
            } 

             //将计算过程放在片元中:逐像素光照
            fixed4 frag(v2f f):SV_Target
            {
                //光方向
                fixed3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
                //法线方向
                fixed3 normalDir = normalize(f.worldnormal);
                //漫反射光
                fixed3 diffuse = _LightColor0.rgb * max(dot(lightDir,normalDir),0) * _DiffuseColor.rgb;
                //环境光
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb;
                //反射光方向 - 在BlinnPhong光照中不需要计算反射光方向 故屏蔽
                //fixed3 reflectDir = normalize(reflect(-lightDir, normalDir));
                //观察方向
                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - f.worldvertex);
                //中间方向(入射光方向与观察方向中间方向)
                fixed3 helfDir = normalize(lightDir + viewDir);
                //反射高光计算
                fixed3 specular = _SpecularColor * _LightColor0.rgb * pow(max(dot(normalDir, helfDir ), 0), _SpecularPow);
                //颜色的叠加-通过相加
                fixed3 tempcolor = diffuse + ambient + specular;
                    return fixed4(tempcolor,0.5);
            }
            ENDCG
        }
    }

    Fallback"Unlit/Diffuse"
}

相关文章

  • BlinnPhong补充

  • 高光反射光照模型_Blinn Phong光照模型_03

    1.BlinnPhong光照模型混合和了Lambert的漫反射和标准的高光,渲染有时比Phong高光更柔和、更平滑...

  • swift笔记:函数补充,枚举补充,属性补充,继承补充

    关键字inout 这个例子是在playground下写的 inout这个形参接收的相当于接收的是结构体变量的地址,...

  • 补充

    一脸倦容的老先生倚靠在凳子上,一手举着茶壶,一手扇着扇子,背后支着面脏得看不清本色的旗子,上面歪歪扭扭地写着“卜”...

  • 补充

    1.生命周期 每个 Vue 实例在被创建时都要经过一系列的初始化过程——例如,需要设置数据监听、编译模板、将实例挂...

  • 补充

  • 补充

    让心很自然的平静下来,不再受外界的打扰,抛开生活中所有的烦恼,放下社会中的地位,将注意力让着我们的身体上,有内而外...

  • 补充

    v-html 可以识别html标签v-text 不可以识别html标签 v-once 只绑定一次v-pre ...

  • 补充

  • 补充

网友评论

      本文标题:BlinnPhong补充

      本文链接:https://www.haomeiwen.com/subject/vnirxktx.html