跳轉到內容

Cg 程式設計/Unity/半透明表面

來自華夏公益教科書,自由的教科書
從兩側照亮的葉子:注意,缺少鏡面反射會導致背光葉子的綠色更飽和。

本教程介紹半透明表面

它是關於照明的幾個教程之一,這些教程超出了 Phong 反射模型。但是,它基於每個畫素的照明,使用 Phong 反射模型,如“光滑鏡面高光”部分中所述。如果您還沒有閱讀該教程,您應該先閱讀它。

Phong 反射模型沒有考慮半透明性,即光線穿透材料的可能性。本教程介紹半透明表面,即允許光線從一個表面穿透到另一個表面的表面,例如紙張、衣服、塑膠薄膜或葉子。

對於半透明照明,觀察者的向量 V 和光源的向量 L 位於相反的一側。

漫射半透明

[編輯 | 編輯原始碼]

我們將區分兩種光線傳輸:漫射半透明和前向散射半透明,它們分別對應於 Phong 反射模型中的漫射項和鏡面項。漫射半透明是光的漫射傳輸,類似於 Phong 反射模型中的漫射反射項(參見“漫射反射”部分):它僅取決於表面法線向量和光源方向的點積——除了我們使用負表面法線向量,因為光源位於背面,因此漫射半透明照明的方程式為

這是許多半透明表面的最常見照明,例如紙張和葉子。

前向散射半透明

[編輯 | 編輯原始碼]

一些半透明表面(例如塑膠薄膜)幾乎是透明的,並允許光線幾乎直接地穿透表面,但具有一些前向散射;也就是說,人們可以透過表面看到光源,但影像有點模糊。這類似於 Phong 反射模型的鏡面項(參見“鏡面高光”部分以獲取方程式),除了我們將反射光線方向R替換為負光線方向-L,並且指數現在對應於前向散射光線的銳度

當然,這種前向散射半透明模型並不準確,但它允許我們模擬這種效果並調整引數。

以下實現基於“光滑鏡面高光”部分,它展示了使用 Phong 反射模型的每個畫素照明。該實現允許渲染背面,並在這種情況下使用內建的 Cg 函式faceforward(n, v, ng)翻轉表面法線向量,該函式在dot(v,ng)<0時返回n,否則返回-n。這種方法通常在輪廓線處失效,這會導致某些畫素的照明不正確。改進後的版本將使用不同的通道和顏色來渲染正面和背面,如“雙面光滑表面”部分中所示。

除了 Phong 反射模型的項之外,我們還使用此程式碼計算漫射半透明和前向散射半透明的照明

            float3 diffuseTranslucency = 
               attenuation * _LightColor0.rgb 
               * _DiffuseTranslucentColor.rgb 
               * max(0.0, dot(lightDirection, -normalDirection));
 
            float3 forwardTranslucency;
            if (dot(normalDirection, lightDirection) > 0.0) 
               // light source on the wrong side?
            {
               forwardTranslucency = float3(0.0, 0.0, 0.0); 
                  // no forward-scattered translucency
            }
            else // light source on the right side
            {
               forwardTranslucency = attenuation * _LightColor0.rgb
                  * _ForwardTranslucentColor.rgb * pow(max(0.0, 
                  dot(-lightDirection, viewDirection)), _Sharpness);
            }

完整著色器程式碼

[編輯 | 編輯原始碼]

完整的著色器程式碼定義了材料常量的著色器屬性,併為具有新增劑混合的附加光源添加了另一個通道,但不包括環境光。

Shader "Cg translucent surfaces" {
   Properties {
      _Color ("Diffuse Material Color", Color) = (1,1,1,1) 
      _SpecColor ("Specular Material Color", Color) = (1,1,1,1) 
      _Shininess ("Shininess", Float) = 10
      _DiffuseTranslucentColor ("Diffuse Translucent Color", Color) 
         = (1,1,1,1) 
      _ForwardTranslucentColor ("Forward Translucent Color", Color) 
         = (1,1,1,1) 
      _Sharpness ("Sharpness", Float) = 10
   }
   SubShader {
      Pass {      
         Tags { "LightMode" = "ForwardBase" } 
            // pass for ambient light and first light source
         Cull Off // show frontfaces and backfaces
 
         CGPROGRAM
 
         #pragma vertex vert  
         #pragma fragment frag 
 
         #include "UnityCG.cginc"
         uniform float4 _LightColor0; 
            // color of light source (from "Lighting.cginc")
 
         // User-specified properties
         uniform float4 _Color; 
         uniform float4 _SpecColor; 
         uniform float _Shininess;
         uniform float4 _DiffuseTranslucentColor; 
         uniform float4 _ForwardTranslucentColor; 
         uniform float _Sharpness;
 
         struct vertexInput {
            float4 vertex : POSITION;
            float3 normal : NORMAL;
         };
         struct vertexOutput {
            float4 pos : SV_POSITION;
            float4 posWorld : TEXCOORD0;
            float3 normalDir : TEXCOORD1;
         };
 
         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.pos = mul(UNITY_MATRIX_MVP, input.vertex);
            return output;
         }
 
         float4 frag(vertexOutput input) : COLOR
         {
            float3 normalDirection = normalize(input.normalDir);
            float3 viewDirection = normalize(
               _WorldSpaceCameraPos - input.posWorld.xyz);
 
            normalDirection = faceforward(normalDirection,
               -viewDirection, normalDirection);
               // flip normal if dot(-viewDirection, normalDirection)>0
 
            float3 lightDirection;
            float attenuation;
 
            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);
            }
 
            // Computation of the Phong reflection model:
 
            float3 ambientLighting = 
               UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb;
 
            float3 diffuseReflection = 
               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 * pow(max(0.0, dot(
                  reflect(-lightDirection, normalDirection), 
                  viewDirection)), _Shininess);
            }
 
            // Computation of the translucent illumination:
 
            float3 diffuseTranslucency = 
               attenuation * _LightColor0.rgb 
               * _DiffuseTranslucentColor.rgb 
               * max(0.0, dot(lightDirection, -normalDirection));
 
            float3 forwardTranslucency;
            if (dot(normalDirection, lightDirection) > 0.0) 
               // light source on the wrong side?
            {
               forwardTranslucency = float3(0.0, 0.0, 0.0); 
                  // no forward-scattered translucency
            }
            else // light source on the right side
            {
               forwardTranslucency = attenuation * _LightColor0.rgb
                  * _ForwardTranslucentColor.rgb * pow(max(0.0, 
                  dot(-lightDirection, viewDirection)), _Sharpness);
            }
 
            // Computation of the complete illumination:
 
            return float4(ambientLighting 
               + diffuseReflection + specularReflection 
               + diffuseTranslucency + forwardTranslucency, 1.0);
         }
         ENDCG
      }
 
      Pass {      
         Tags { "LightMode" = "ForwardAdd" } 
            // pass for additional light sources
         Cull Off
         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 float4 _Color; 
         uniform float4 _SpecColor; 
         uniform float _Shininess;
         uniform float4 _DiffuseTranslucentColor; 
         uniform float4 _ForwardTranslucentColor; 
         uniform float _Sharpness;
 
         struct vertexInput {
            float4 vertex : POSITION;
            float3 normal : NORMAL;
         };
         struct vertexOutput {
            float4 pos : SV_POSITION;
            float4 posWorld : TEXCOORD0;
            float3 normalDir : TEXCOORD1;
         };
 
         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.pos = mul(UNITY_MATRIX_MVP, input.vertex);
            return output;
         }
 
         float4 frag(vertexOutput input) : COLOR
         {
            float3 normalDirection = normalize(input.normalDir);
            float3 viewDirection = normalize(
               _WorldSpaceCameraPos - input.posWorld.xyz);
 
            normalDirection = faceforward(normalDirection,
               -viewDirection, normalDirection);
               // flip normal if dot(-viewDirection, normalDirection)>0
 
            float3 lightDirection;
            float attenuation;
 
            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);
            }
 
            // Computation of the Phong reflection model:
 
            float3 diffuseReflection = 
               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 * pow(max(0.0, dot(
                  reflect(-lightDirection, normalDirection), 
                  viewDirection)), _Shininess);
            }
 
            // Computation of the translucent illumination:
 
            float3 diffuseTranslucency = 
               attenuation * _LightColor0.rgb 
               * _DiffuseTranslucentColor.rgb 
               * max(0.0, dot(lightDirection, -normalDirection));
 
            float3 forwardTranslucency;
            if (dot(normalDirection, lightDirection) > 0.0) 
               // light source on the wrong side?
            {
               forwardTranslucency = float3(0.0, 0.0, 0.0); 
                  // no forward-scattered translucency
            }
            else // light source on the right side
            {
               forwardTranslucency = attenuation * _LightColor0.rgb
                  * _ForwardTranslucentColor.rgb * pow(max(0.0, 
                  dot(-lightDirection, viewDirection)), _Sharpness);
            }
 
            // Computation of the complete illumination:
 
            return float4(diffuseReflection + specularReflection 
               + diffuseTranslucency + forwardTranslucency, 1.0);
         }
         ENDCG
      }
   }
}

恭喜!您完成了本關於半透明表面的教程,它們非常常見,但無法透過 Phong 反射模型來建模。我們涵蓋了

  • 什麼是半透明表面。
  • 哪些形式的半透明性最常見(漫射半透明和前向散射半透明)。
  • 如何實現漫射和前向散射半透明。

進一步閱讀

[編輯 | 編輯原始碼]

如果您還想了解更多

< Cg 程式設計/Unity

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