• 高光反射 - UnityShader


    高光反射分为两种实现

    • 逐顶点高光反射(主要依靠顶点着色器,计算顶点的光照)
    • 逐片元高光反射(主要依靠片元着色器,计算片元的光照)

    Phong 模型
    c s p e c u l a r = ( c l i g h t ⋅ m s p e c u l a r ) ( v ^ ⋅ r ^ ) m g l o s s (1) c_{specular} = (c_{light} \cdot m_{specular}) (\widehat{v} \cdot \widehat{r})^{m_{gloss}}\tag{1} cspecular=(clightmspecular)(v r )mgloss(1)
    r ^ = 2 ( n ^ ⋅ l ^ ) n ^ − l ^ (2) \widehat{r} = 2(\widehat{n} \cdot \widehat{l})\widehat{n} - \widehat{l}\tag{2} r =2(n l )n l (2)

    r ^ \widehat{r} r 为光线的反射对于法线的方向
    c l i g h t c_{light} clight是光线的颜色, m s p e c u l a r m_{specular} mspecular是材质的颜色, v v v是摄像机的观察方向

    Blinn - Phong 模型
    c s p e c u l a r = ( c l i g h t ⋅ m s p e c u l a r ) ( n ^ ⋅ h ^ ) m g l o s s (1) c_{specular} = (c_{light} \cdot m_{specular}) (\widehat{n} \cdot \widehat{h})^{m_{gloss}}\tag{1} cspecular=(clightmspecular)(n h )mgloss(1)
    h ^ = n o r m a l i z e ( v ^ + l ^ ) (2) \widehat{h} = normalize(\widehat{v} + \widehat{l})\tag{2} h =normalize(v +l )(2)

    参数与上面相同,h 为特殊的参数

    代码包含了 漫反射,高光反射,环境光。这个代码是继续上次博客的代码 -> 漫反射-UnityShader

    逐顶点高光反射

    Shader "Lit/DiffuseLitShader1"
    {
        Properties
        {
            _Diffuse ("Diffuse", Color) = (1, 1, 1, 1)
            _Specular ("Specular", Color) = (1, 1, 1, 1)
            _Gloss ("Gloss", Range(8.0, 256.0)) = 20
        }
        SubShader
        {
            Pass
            {
                Tags { "LightMode" = "ForwardBase" }
                
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
    
                #include 
                #include 
                #include 
    
                struct a2v
                {
                    float4 vertex : POSITION;
                    float3 normal : NORMAL;
                };
    
                struct v2f
                {
                    float4 pos : SV_POSITION;
                    fixed3 color : COLOR0;
                };
    
                fixed4 _Diffuse;
                fixed4 _Specular;
                float _Gloss;
    
                //逐顶点漫反射
                v2f vert(a2v v)
                {
                    v2f o;
                    //设置裁剪空间
                    o.pos = UnityObjectToClipPos(v.vertex.xyz);
                    
                    //得到环境光
                    fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
    
                    float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    
                    //从物体坐标的法向量变换到全局坐标的法向量
                    float3 worldNormal = normalize(mul(v.normal, (float3x3)unity_WorldToObject)).xyz;
                    
                    //光照方向
                    fixed3 litDir = normalize(UnityWorldSpaceLightDir(worldPos));
                    
                    //兰伯特定律 c = (c * m) max(0, dot(n.normalize, l.normalize)) = (c * m) * cos
                    //fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, litDir));
    
                    //半兰伯特定理 c = (c * m)(0.5 * cos + 0.5), 可实现暗部增强亮光
                    fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * (dot(worldNormal, litDir) * 0.5 + 0.5);
                    
                    //高光反射 Phong 模型
                    // fixed3 reflectDir = normalize(reflect(-litDir, worldNormal));
                    // fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, v.vertex).xyz);
                    //
                    // fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss);
    
                    //高光反射 使用 Blinn - Phong 模型
                    fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));
                    fixed3 halfDir = normalize(viewDir + litDir);
                    fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormal, halfDir)), _Gloss);
                    
                    o.color = ambient + diffuse + specular;
    
                    return o;
                }
    
                fixed4 frag(v2f i) : SV_Target
                {
                    return fixed4(i.color, 1.0);
                }
                ENDCG
            }
        }
        
        FallBack "Specular"
    }
    

    逐片元高光反射

    Shader "Lit/DiffuseLitShader2"
    {
        Properties
        {
            _Diffuse("Diffuse", Color) = (1, 1, 1, 1)
            _Specular ("Specular", Color) = (1, 1, 1, 1)
            _Gloss ("Gloss", Range(8.0, 256.0)) = 20
        }
        SubShader
        {
            Pass
            {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
    
                #include 
                #include 
                #include 
    
                struct a2v
                {
                    float4 pos : POSITION;
                    float3 normal : NORMAL;
                };
    
                struct v2f
                {
                    float4 pos : SV_POSITION;
                    float4 worldNormal : TEXCOORD0;
                    float4 worldPos : TEXCOORD1;
                };
    
                fixed4 _Diffuse;
                fixed4 _Specular;
                float _Gloss;
    
                v2f vert(a2v v)
                {
                    v2f o;
                    
                    //得到裁剪空间
                    o.pos = UnityObjectToClipPos(v.pos.xyz);
    
                    //获得世界空间的法向量,并保存在 uv 中
                    o.worldNormal = float4(UnityObjectToWorldNormal(v.normal.xyz), 1.0);
    
                    //获得模型的世界坐标
                    o.worldPos = mul(unity_ObjectToWorld, v.pos.xyz);
    
                    return o;
                }
    
                fixed4 frag(v2f i) : SV_Target
                {
                    //得到 ambient
                    fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
    
                    fixed3 worldNormal = normalize(i.worldNormal.xyz);
                    //gl 的 方向
                    float3 litDir = normalize(UnityWorldSpaceLightDir(i.worldPos.xyz));
    
                    //使用 兰伯特定律
                    //fixed3 diffuse = _LightColor0.rgb * _Diffuse * saturate(dot(i.worldNormal.xyz, litDir));
    
                    //使用 半兰伯特定律
                    fixed3 diffuse = _LightColor0.rgb * _Diffuse * (dot(worldNormal, litDir) * 0.5 + 0.5);
    
                    //高光反射 使用 Phong 模型
                    // fixed3 reflectDir = normalize(reflect(-litDir, worldNormal));
                    // fixed3 viewDir = normalize(_WorldSpaceCameraPos - i.worldPos.xyz);
                    //
                    // fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss);
    
                    //高光反射 使用 Blinn - Phong 模型
                    fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos.xyz));
                    fixed3 halfDir = normalize(litDir + viewDir);
                    fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormal, halfDir)), _Gloss);
                    return fixed4(diffuse + ambient + specular, 1.0);
                }
                ENDCG
            }
        }
        
        FallBack "Specular"
    }
    

    来源为 Unity Shader 入门精要这本书 - 高光反射的实现

  • 相关阅读:
    神经网络-卷积神经网络案例详解
    Steger算法实现结构光光条中心提取(python版本)
    MyBatis(一)--------十分灵活
    【GitHub】记一次给 docusaurus 官方提交 pull request 的过程
    毕业仅1年,干Python赚了50W!网友:不是吹的
    计算机毕业设计Java网上鲜花交易平台(源码+系统+mysql数据库+Lw文档)
    判断JS是否加载完成
    day10
    智能合约是什么?
    高手过招不用鼠标,一款超好用的跨平台命令行界面库
  • 原文地址:https://blog.csdn.net/m0_52361859/article/details/126951500