跳轉到內容

Cg 程式設計/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,陸地的 alpha 分量為 1。但是,應該是水具有光澤,而陸地沒有光澤。因此,對於這個特定的影像,我們應該將鏡面材料顏色乘以 (1.0 - textureColor.a)。另一方面,普通的 gloss map 需要乘以 textureColor.a。(請注意,對著色器程式進行這種修改是多麼容易。)

Shader "Cg 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
 
         CGPROGRAM
 
         #pragma vertex vert  
         #pragma fragment frag 
 
         #include "UnityCG.cginc"
         uniform float4 _LightColor0; 
            // color of light source (from "Lighting.cginc")
 
         // User-specified properties
         uniform sampler2D _MainTex;    
         uniform float4 _Color; 
         uniform float4 _SpecColor; 
         uniform float _Shininess;
 
         struct vertexInput {
            float4 vertex : POSITION;
            float3 normal : NORMAL;
            float4 texcoord : TEXCOORD0;
        };
         struct vertexOutput {
            float4 pos : SV_POSITION;
            float4 posWorld : TEXCOORD0;
            float3 normalDir : TEXCOORD1;
            float4 tex : TEXCOORD2;
        };
 
         vertexOutput vert(vertexInput input) 
         {
            vertexOutput output;
 
            float4x4 modelMatrix = unity_ObjectToWorld;
            float4x4 modelMatrixInverse = unity_WorldToObject;
 
            output.posWorld = mul(modelMatrix, input.vertex);
            output.normalDir = normalize(
               mul(float4(input.normal, 0.0), modelMatrixInverse).xyz);
            output.tex = input.texcoord;
            output.pos = UnityObjectToClipPos(input.vertex);
            return output;
         }
 
         float4 frag(vertexOutput input) : COLOR
         {
            float3 normalDirection = normalize(input.normalDir);
 
            float3 viewDirection = normalize(
               _WorldSpaceCameraPos - input.posWorld.xyz);
            float3 lightDirection;
            float attenuation;
 
            float4 textureColor = tex2D(_MainTex, input.tex.xy);
 
            if (0.0 == _WorldSpaceLightPos0.w) // directional light?
            {
               attenuation = 1.0; // no attenuation
               lightDirection = 
                  normalize(_WorldSpaceLightPos0.xyz);
            } 
            else // point or spot light
            {
               float3 vertexToLightSource = 
                  _WorldSpaceLightPos0.xyz - input.posWorld.xyz;
               float distance = length(vertexToLightSource);
               attenuation = 1.0 / distance; // linear attenuation 
               lightDirection = normalize(vertexToLightSource);
            }
 
            float3 ambientLighting = textureColor.rgb  
               * UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb;
 
            float3 diffuseReflection = textureColor.rgb  
               * attenuation * _LightColor0.rgb * _Color.rgb
               * max(0.0, dot(normalDirection, lightDirection));
 
            float3 specularReflection;
            if (dot(normalDirection, lightDirection) < 0.0) 
               // light source on the wrong side?
            {
               specularReflection = float3(0.0, 0.0, 0.0); 
                  // no specular reflection
            }
            else // light source on the right side
            {
               specularReflection = attenuation * _LightColor0.rgb 
                  * _SpecColor.rgb * (1.0 - textureColor.a) 
                     // for usual gloss maps: "... * textureColor.a" 
                  * pow(max(0.0, dot(
                  reflect(-lightDirection, normalDirection), 
                  viewDirection)), _Shininess);
            }
 
            return float4(ambientLighting + diffuseReflection 
               + specularReflection, 1.0);
         }
 
         ENDCG
      }
 
      Pass {    
         Tags { "LightMode" = "ForwardAdd" } 
            // pass for additional light sources
         Blend One One // additive blending 
 
          CGPROGRAM
 
         #pragma vertex vert  
         #pragma fragment frag 
 
         #include "UnityCG.cginc"
         uniform float4 _LightColor0; 
            // color of light source (from "Lighting.cginc")
 
         // User-specified properties
         uniform sampler2D _MainTex;    
         uniform float4 _Color; 
         uniform float4 _SpecColor; 
         uniform float _Shininess;
 
        struct vertexInput {
            float4 vertex : POSITION;
            float3 normal : NORMAL;
            float4 texcoord : TEXCOORD0;
        };
         struct vertexOutput {
            float4 pos : SV_POSITION;
            float4 posWorld : TEXCOORD0;
            float3 normalDir : TEXCOORD1;
            float4 tex : TEXCOORD2;
        };
 
         vertexOutput vert(vertexInput input) 
         {
            vertexOutput output;
 
            float4x4 modelMatrix = unity_ObjectToWorld;
            float4x4 modelMatrixInverse = unity_WorldToObject;
 
            output.posWorld = mul(modelMatrix, input.vertex);
            output.normalDir = normalize(
               mul(float4(input.normal, 0.0), modelMatrixInverse).xyz);
            output.tex = input.texcoord;
            output.pos = UnityObjectToClipPos(input.vertex);
            return output;
         }
 
         float4 frag(vertexOutput input) : COLOR
         {
            float3 normalDirection = normalize(input.normalDir);
 
            float3 viewDirection = normalize(
               _WorldSpaceCameraPos - input.posWorld.xyz);
            float3 lightDirection;
            float attenuation;
 
            float4 textureColor = tex2D(_MainTex, input.tex.xy);
 
            if (0.0 == _WorldSpaceLightPos0.w) // directional light?
            {
               attenuation = 1.0; // no attenuation
               lightDirection = 
                  normalize(_WorldSpaceLightPos0.xyz);
            } 
            else // point or spot light
            {
               float3 vertexToLightSource = 
                  _WorldSpaceLightPos0.xyz - input.posWorld.xyz;
               float distance = length(vertexToLightSource);
               attenuation = 1.0 / distance; // linear attenuation 
               lightDirection = normalize(vertexToLightSource);
            }
 
            float3 diffuseReflection = textureColor.rgb  
               * attenuation * _LightColor0.rgb * _Color.rgb
               * max(0.0, dot(normalDirection, lightDirection));
 
            float3 specularReflection;
            if (dot(normalDirection, lightDirection) < 0.0) 
               // light source on the wrong side?
            {
               specularReflection = float3(0.0, 0.0, 0.0); 
                  // no specular reflection
            }
            else // light source on the right side
            {
               specularReflection = attenuation * _LightColor0.rgb 
                  * _SpecColor.rgb * (1.0 - textureColor.a) 
                     // for usual gloss maps: "... * textureColor.a" 
                  * pow(max(0.0, dot(
                  reflect(-lightDirection, normalDirection), 
                  viewDirection)), _Shininess);
            }
 
            return float4(diffuseReflection 
               + specularReflection, 1.0);
               // no ambient lighting in this pass
         }
 
         ENDCG
      }
   }
   Fallback "Specular"
}

對上述特定紋理影像進行的有用修改是,在 alpha 分量為 0 的地方將漫反射材料顏色設定為深藍色。

逐頂點光照著色器程式碼

[編輯 | 編輯原始碼]

“平滑鏡面反射”部分 中所述,鏡面反射通常不能很好地使用逐頂點光照渲染。但是,有時由於效能限制別無選擇。為了在 “光照紋理表面”部分 的著色器程式碼中包含 gloss map,兩個通道的片段著色器都應該用以下程式碼替換

         float4 frag(vertexOutput input) : COLOR
         {
            float4 textureColor = tex2D(_MainTex, input.tex.xy);
            return float4(input.specularColor * (1.0 - textureColor.a) +
               input.diffuseColor * textureColor.rgb, 1.0);
         }

請注意,普通的 gloss map 需要乘以 textureColor.a 而不是 (1.0 - textureColor.a)

恭喜!您完成了關於 gloss map 的重要教程。我們已經瞭解了

  • 什麼是 gloss map。
  • 如何為逐畫素光照實現它。
  • 如何為逐頂點光照實現它。

進一步閱讀

[編輯 | 編輯原始碼]

如果您還想了解更多

< Cg 程式設計/Unity

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