跳轉到內容

GLSL 程式設計/Unity/光澤紋理

來自華夏公益教科書,開放書籍,構建開放世界
從國際空間站 (ISS) 看到的太平洋上,夕陽帶著鏡面高光。

本教程涵蓋了 **部分光澤紋理表面的逐畫素光照**。

它結合了 “紋理球體”部分“平滑鏡面高光”部分 的著色器程式碼,計算使用紋理的 RGB 分量確定漫反射材質顏色的逐畫素光照,以及使用同一紋理的 A 分量確定鏡面反射的強度。如果你沒有閱讀過這些部分,現在是一個很好的機會來閱讀它們。

光澤對映

[編輯 | 編輯原始碼]

“光照紋理表面”部分 中,漫反射的材質常量由紋理影像的 RGB 分量決定。在這裡,我們擴充套件了這種技術,並透過同一紋理影像的 A(alpha)分量來確定鏡面反射的強度。僅使用一個紋理提供了顯著的效能優勢,特別是因為在某些情況下,RGBA 紋理查詢與 RGB 紋理查詢一樣昂貴。

如果紋理影像的“光澤”(即鏡面反射的強度)編碼在 RGBA 紋理影像的 A(alpha)分量中,我們可以簡單地將鏡面反射的材質常量 與紋理影像的 alpha 分量相乘。 “鏡面高光”部分 中介紹,並出現在 Phong 反射模型的鏡面反射項中

如果乘以紋理影像的 alpha 分量,則該項達到最大值(即表面是光澤的),其中 alpha 為 1,並且為 0(即表面根本沒有光澤),其中 alpha 為 0。

帶有透明水域的地球地圖,即水域的 alpha 分量為 0,陸地的 alpha 分量為 1。

每個畫素光照的著色器程式碼

[編輯 | 編輯原始碼]

著色器程式碼是 “平滑鏡面高光”部分 中的逐畫素光照和 “紋理球體”部分 中的紋理的組合。類似於 “光照紋理表面”部分,紋理顏色 textureColor 的 RGB 分量乘以漫反射材質顏色 _Color

在左邊的特定紋理影像中,alpha 分量對於水為 0,對於陸地為 1。但是,應該是水是光澤的,而陸地不是。因此,對於這個特定的影像,我們應該將鏡面材質顏色乘以 (1.0 - textureColor.a)。另一方面,通常的光澤貼圖需要乘以 textureColor.a。(注意對著色器程式進行這種改變是多麼容易。)

Shader "GLSL per-pixel lighting with texture" {
   Properties {
      _MainTex ("RGBA Texture For Material Color", 2D) = "white" {} 
      _Color ("Diffuse Material Color", Color) = (1,1,1,1) 
      _SpecColor ("Specular Material Color", Color) = (1,1,1,1) 
      _Shininess ("Shininess", Float) = 10
   }
   SubShader {
      Pass {	
         Tags { "LightMode" = "ForwardBase" } 
            // pass for ambient light and first light source

         GLSLPROGRAM

         // User-specified properties
         uniform sampler2D _MainTex;
         uniform vec4 _Color; 
         uniform vec4 _SpecColor; 
         uniform float _Shininess;

         // The following built-in uniforms (except _LightColor0) 
         // are also defined in "UnityCG.glslinc", 
         // i.e. one could #include "UnityCG.glslinc" 
         uniform vec3 _WorldSpaceCameraPos; 
            // camera position in world space
         uniform mat4 _Object2World; // model matrix
         uniform mat4 _World2Object; // inverse model matrix
         uniform vec4 _WorldSpaceLightPos0; 
            // direction to or position of light source
         uniform vec4 _LightColor0; 
            // color of light source (from "Lighting.cginc")
         
         varying vec4 position; 
            // position of the vertex (and fragment) in world space 
         varying vec3 varyingNormalDirection; 
            // surface normal vector in world space
         varying vec4 textureCoordinates; 

         #ifdef VERTEX
         
         void main()
         {				
            mat4 modelMatrix = _Object2World;
            mat4 modelMatrixInverse = _World2Object; // unity_Scale.w 
               // is unnecessary because we normalize vectors
            
            position = modelMatrix * gl_Vertex;
            varyingNormalDirection = normalize(vec3(
               vec4(gl_Normal, 0.0) * modelMatrixInverse));

            gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
            textureCoordinates = gl_MultiTexCoord0;
         }
         
         #endif

         #ifdef FRAGMENT
         
         void main()
         {
            vec3 normalDirection = normalize(varyingNormalDirection);

            vec3 viewDirection = 
               normalize(_WorldSpaceCameraPos - vec3(position));
            vec3 lightDirection;
            float attenuation;

            vec4 textureColor = 
               texture2D(_MainTex, vec2(textureCoordinates));

            if (0.0 == _WorldSpaceLightPos0.w) // directional light?
            {
               attenuation = 1.0; // no attenuation
               lightDirection = normalize(vec3(_WorldSpaceLightPos0));
            } 
            else // point or spot light
            {
               vec3 vertexToLightSource = 
                  vec3(_WorldSpaceLightPos0 - position);
               float distance = length(vertexToLightSource);
               attenuation = 1.0 / distance; // linear attenuation 
               lightDirection = normalize(vertexToLightSource);
            }
            
            vec3 ambientLighting = vec3(gl_LightModel.ambient) 
               * vec3(_Color) * vec3(textureColor);

            vec3 diffuseReflection = attenuation * vec3(_LightColor0) 
               * vec3(_Color) * vec3(textureColor) 
               * max(0.0, dot(normalDirection, lightDirection));
            
            vec3 specularReflection;
            if (dot(normalDirection, lightDirection) < 0.0) 
               // light source on the wrong side?
            {
               specularReflection = vec3(0.0, 0.0, 0.0); 
                  // no specular reflection
            }
            else // light source on the right side
            {
               specularReflection = attenuation * vec3(_LightColor0) 
                  * vec3(_SpecColor) * (1.0 - textureColor.a) 
                     // for usual gloss maps: "... * textureColor.a"
                  * pow(max(0.0, dot(
                  reflect(-lightDirection, normalDirection), 
                  viewDirection)), _Shininess);
            }

            gl_FragColor = vec4(ambientLighting 
               + diffuseReflection + specularReflection, 1.0);
         }
         
         #endif

         ENDGLSL
      }

      Pass {	
         Tags { "LightMode" = "ForwardAdd" } 
            // pass for additional light sources
         Blend One One // additive blending 


         GLSLPROGRAM

         // User-specified properties
         uniform sampler2D _MainTex;
         uniform vec4 _Color; 
         uniform vec4 _SpecColor; 
         uniform float _Shininess;

         // The following built-in uniforms (except _LightColor0) 
         // are also defined in "UnityCG.glslinc", 
         // i.e. one could #include "UnityCG.glslinc" 
         uniform vec3 _WorldSpaceCameraPos; 
            // camera position in world space
         uniform mat4 _Object2World; // model matrix
         uniform mat4 _World2Object; // inverse model matrix
         uniform vec4 _WorldSpaceLightPos0; 
            // direction to or position of light source
         uniform vec4 _LightColor0; 
            // color of light source (from "Lighting.cginc")
         
         varying vec4 position; 
            // position of the vertex (and fragment) in world space 
         varying vec3 varyingNormalDirection; 
            // surface normal vector in world space
         varying vec4 textureCoordinates; 

         #ifdef VERTEX
         
         void main()
         {				
            mat4 modelMatrix = _Object2World;
            mat4 modelMatrixInverse = _World2Object; // unity_Scale.w 
               // is unnecessary because we normalize vectors
            
            position = modelMatrix * gl_Vertex;
            varyingNormalDirection = normalize(vec3(
               vec4(gl_Normal, 0.0) * modelMatrixInverse));

            gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
            textureCoordinates = gl_MultiTexCoord0;
         }
         
         #endif

         #ifdef FRAGMENT
         
         void main()
         {
            vec3 normalDirection = normalize(varyingNormalDirection);

            vec3 viewDirection = 
               normalize(_WorldSpaceCameraPos - vec3(position));
            vec3 lightDirection;
            float attenuation;

            vec4 textureColor = 
               texture2D(_MainTex, vec2(textureCoordinates));

            if (0.0 == _WorldSpaceLightPos0.w) // directional light?
            {
               attenuation = 1.0; // no attenuation
               lightDirection = normalize(vec3(_WorldSpaceLightPos0));
            } 
            else // point or spot light
            {
               vec3 vertexToLightSource = 
                  vec3(_WorldSpaceLightPos0 - position);
               float distance = length(vertexToLightSource);
               attenuation = 1.0 / distance; // linear attenuation 
               lightDirection = normalize(vertexToLightSource);
            }
            
            vec3 diffuseReflection = attenuation * vec3(_LightColor0) 
               * vec3(_Color) * vec3(textureColor) 
               * max(0.0, dot(normalDirection, lightDirection));
            
            vec3 specularReflection;
            if (dot(normalDirection, lightDirection) < 0.0) 
               // light source on the wrong side?
            {
               specularReflection = vec3(0.0, 0.0, 0.0); 
                  // no specular reflection
            }
            else // light source on the right side
            {
               specularReflection = attenuation * vec3(_LightColor0) 
                  * vec3(_SpecColor) * (1.0 - textureColor.a) 
                     // for usual gloss maps: "... * textureColor.a"
                  * pow(max(0.0, dot(
                  reflect(-lightDirection, normalDirection), 
                  viewDirection)), _Shininess);
            }

            gl_FragColor = 
               vec4(diffuseReflection + specularReflection, 1.0);
         }
         
         #endif

         ENDGLSL
      }
   } 
   // The definition of a fallback shader should be commented out 
   // during development:
   // Fallback "Specular"
}

對上面特定紋理影像的這個著色器進行一個有用的修改是,將漫反射材質顏色設定為 alpha 分量為 0 的深藍色。

每個頂點光照的著色器程式碼

[編輯 | 編輯原始碼]

正如在 “平滑鏡面高光”部分 中所討論的那樣,鏡面高光通常使用每個頂點光照渲染得不好。但是,有時由於效能限制,別無選擇。為了在 “光照紋理表面”部分 的著色器程式碼中包含光澤對映,兩個通道的片段著色器都應該用這段程式碼替換

         #ifdef FRAGMENT
 
         void main()
         {
            vec4 textureColor = 
               texture2D(_MainTex, vec2(textureCoordinates));
            gl_FragColor = vec4(diffuseColor * vec3(textureColor)
               + specularColor * (1.0 - textureColor.a), 1.0);
         }
 
         #endif

請注意,通常的光澤貼圖需要乘以 textureColor.a,而不是 (1.0 - textureColor.a)

恭喜!你完成了關於光澤對映的重要教程。我們已經瞭解了

  • 什麼是光澤對映。
  • 如何為逐畫素光照實現它。
  • 如何為每個頂點光照實現它。

進一步閱讀

[編輯 | 編輯原始碼]

如果你還想了解更多


< GLSL 程式設計/Unity

除非另有說明,否則本頁上的所有示例原始碼都授予公有領域。
華夏公益教科書