可可西

逐顶点光照与逐像素光照

在三维图形渲染管线中,可以在两个地方进行光照计算:①在vs中进行光照计算(逐顶点光照) ②在ps中进行光照计算(逐像素光照)

下面以Phong着色法为例,给出它分别在逐顶点光照逐像素光照中的实现过程。

 

逐顶点光照(per-vertex lighting)

在vs中为每个顶点计算光照,然后在光栅化时读取图元顶点的颜色对其边界和内部进行线性插值,输出图元的所有像素颜色。

 

unity中的实现如下(Chapter6-SpecularVertexLevel.shader):

Shader "Unity Shaders Book/Chapter 6/Specular Vertex-Level" {
    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;
                fixed3 color : COLOR;
            };
            
            v2f vert(a2v v) {
                v2f o;
                // Transform the vertex from object space to projection space
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                
                // Get ambient term
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;  // 环境光
                
                // Transform the normal from object space to world space
                fixed3 worldNormal = normalize(mul(v.normal, (float3x3)_World2Object));
                // Get the light direction in world space
                fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                
                // Compute diffuse term
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir)); // 兰伯特漫反射光
                
                // Get the reflect direction in world space
                fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormal));
                // Get the view direction in world space
                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(_Object2World, v.vertex).xyz);
                
                // Compute specular term
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss); // Phong高光反射光
                
                o.color = ambient + diffuse + specular;
                                 
                return o;
            }
            
            fixed4 frag(v2f i) : SV_Target {
                return fixed4(i.color, 1.0);
            }
            
            ENDCG
        }
    } 
    FallBack "Specular"
}

 

逐像素光照(per-pixel lighting)

光栅化之后,在ps中为每个像素计算光照,获得像素的法线(可以是对顶点法线插值得到的,也可以从法线贴图中采样得到),然后进行光照计算。

 

unity中的实现如下(Chapter6-SpecularPixelLevel.shader):

Shader "Unity Shaders Book/Chapter 6/Specular Pixel-Level" {
    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;
                // Transform the vertex from object space to projection space
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                
                // Transform the normal from object space to world space
                o.worldNormal = mul(v.normal, (float3x3)_World2Object);
                // Transform the vertex from object spacet to world space
                o.worldPos = mul(_Object2World, v.vertex).xyz;
                
                return o;
            }
            
            fixed4 frag(v2f i) : SV_Target {
                // Get ambient term
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz; // 环境光
                
                fixed3 worldNormal = normalize(i.worldNormal);
                fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
                
                // Compute diffuse term
                fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLightDir)); // 兰伯特漫反射光
                
                // Get the reflect direction in world space
                fixed3 reflectDir = normalize(reflect(-worldLightDir, worldNormal));
                // Get the view direction in world space
                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
                // Compute specular term
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir, viewDir)), _Gloss); // Phong高光反射光
                
                return fixed4(ambient + diffuse + specular, 1.0);
            }
            
            ENDCG
        }
    } 
    FallBack "Specular"
}

 

两种光照的优缺点总结

逐像素光照能得到更高质量的光照效果,无论是漫反射还是高光反射都更加平滑。

 

由于顶点数目往往远小于像素数目,因此逐顶点光照的计算量要小,效率较高。

逐顶点光照依赖于线性插值来得到内部像素颜色,这会导致图元内部的颜色总是暗于顶点处的最高颜色值,在某些情况下会产生明显的棱角现象。

逐顶点得到的高光效果有比较大的问题,从高光中心点向外扩散显得很不平滑。这主要是因为,高光反射部分的计算是非线性的,而在vs中计算光照再进行插值的过程是线性的,破坏了原计算的非线性关系,就会出现较大的视觉问题。

另外,逐顶点光照需要模型有较多较密的顶点才行。如果模型的顶点很少(如一个大立方体),光照结果可能就完全不对了。

 

unity实现效果对比:

逐顶点光照的高光表现出明显的不平滑,高光区域也没有呈现圆形并向外扩展

 

当无specular部分的光照时,虽然在明暗过渡地方有一点点不平滑,但整体的逐顶点光照效果还算不错

 

参考

Unity_Shaders_Book(candycat1992)

《Unity Shader入门精要》随书彩色插图

 

posted on 2022-01-22 15:05  可可西  阅读(954)  评论(0编辑  收藏  举报

导航