跳轉到內容

Cg 程式設計/Unity/球體的軟陰影

來自 Wikibooks,開放世界開放書籍
陰影不僅對於理解場景的幾何形狀很重要(例如,物體之間的距離);它們也可能非常美麗。

本教程涵蓋球體的軟陰影

這是關於光照的幾個教程之一,這些教程超出了 Phong 反射模型,Phong 反射模型是一種區域性光照模型,因此沒有考慮陰影。所提出的技術渲染單個球體在任何網格上的軟陰影,並且與 Orion Sky Lawlor 提出的技術有一定的關係(參見“進一步閱讀”部分)。該著色器可以擴充套件以渲染少量球體的陰影,但這會以渲染效能為代價;然而,它不能輕鬆地應用於任何其他型別的陰影投射器。潛在的應用包括電腦球類遊戲(球通常是唯一需要軟陰影的物體,也是唯一應該在所有其他物體上投射動態陰影的物體),具有球形主角的電腦遊戲(例如,“彈球狂熱”),僅由球體組成的視覺化效果(例如,行星視覺化、小原子核、原子或分子的球體模型等),或者可以填充球體並受益於軟陰影的測試場景。

本影(黑色)和半影(灰色)是軟陰影的主要部分。
卡拉瓦喬的“荊棘加冕”(約 1602 年)。注意左上角的陰影線,隨著距離陰影投射牆的距離增加,陰影變得更柔和。

軟陰影

[編輯 | 編輯原始碼]

雖然定向光源和點光源會產生硬陰影,但任何面積光源都會產生軟陰影。對於所有真實的光源,特別是太陽和任何燈泡或燈,情況也是如此。在陰影投射器後面的某些點,光源的任何部分都不可見,陰影均勻地變暗:這是本影。從其他點,光源或多或少可見,因此陰影或多或少完整:這是半影。最後,有些點可以看見光源的整個面積:這些點位於陰影之外。

在許多情況下,陰影的柔和度主要取決於陰影投射器和陰影接收器之間的距離:距離越大,陰影越柔和。這是藝術中眾所周知的現象;例如,參見卡拉瓦喬的右側繪畫。

用於計算軟陰影的向量:指向光源的向量 L,指向球體中心的向量 S,切線向量 T,以及切線到光源中心的距離 d。

我們將近似計算表面上一個點的陰影,當半徑為 的球體在S(相對於表面點)處遮擋半徑為 的球形光源在L(相對於表面點)處時;參見左側的圖形。

為此,我們考慮一個方向為T、經過表面點的球體切線。此外,選擇此切線位於LS所跨越的平面上,即平行於左側圖形的檢視平面。關鍵的觀察結果是光源中心的最小距離 和這條切線直接與表面點的陰影量相關,因為它決定了從表面點可見的光源區域的大小。更確切地說,我們需要一個帶符號的距離(如果切線在與球體相同的一側的L上,則為正,否則為負)來確定表面點是在本影中 (),半影中 (),還是陰影之外 ()。

為了計算 ,我們考慮LS之間的角度以及TS之間的角度。這兩個角度之差是LT之間的角度,它與 的關係為

.

因此,到目前為止,我們有

  

我們可以使用以下公式計算 **T** 和 **S** 之間的角度

.

因此

.

對於 **L** 和 **S** 之間的角度,我們使用向量叉積的一個性質

.

因此

.

總而言之,我們有

我們到目前為止所做的近似並不重要;更重要的是,它不會產生渲染偽影。 如果效能是一個問題,我們可以更進一步地使用 arcsin(x) ≈ x;即,我們可以使用

這避免了所有三角函式;但是,它確實引入了渲染偽影(特別是在面向光源的半影中存在鏡面高光時)。 這些渲染偽影是否值得效能提升,需要在每種情況下進行判斷。

接下來,我們看看如何根據 計算陰影程度 。當 減小到 時, 應該從 0 增加到 1。換句話說,我們希望在 的 -1 到 1 之間的值之間實現一個平滑的過渡。可能是實現這一點最有效的方法是使用內建 Cg 函式 smoothstep(a,b,x) = t*t*(3-2*t) 提供的 Hermite 插值,其中 t=clamp((x-a)/(b-a),0,1)

雖然這不是 之間基於物理的關聯的特別好的近似,但它仍然能很好地體現基本特徵。

此外,如果光線方向 LS 的方向相反,即它們的點積為負,則 應該為 0。這個條件實際上有點棘手,因為它會導致 LS 正交的平面上出現明顯的間斷。為了平滑這種間斷,我們可以再次使用 smoothstep 來計算一個改進的值

此外,如果點光源距離表面點比遮擋球體更近,則我們必須將 設定為 0。這同樣有點棘手,因為球形光源可能會與投射陰影的球體相交。一個避免過於明顯的偽影(但無法處理完全相交問題)的解決方案是

對於方向光源,我們只需設定 。然後, 表示無陰影照明水平,應該乘以光源的任何照明。(因此,環境光不應該乘以這個因子。)如果計算多個陰影投射者的陰影,則需要將所有陰影投射者的 項組合起來,用於每個光源。通常的方法是將它們相乘,雖然這可能不準確(尤其是在陰影重疊時)。

實現計算 lightDirectionsphereDirection 向量的長度,然後使用歸一化向量進行處理。這樣,這些向量的長度只需要計算一次,我們甚至可以避免一些除法運算,因為我們可以使用歸一化向量。以下是片段著色器的關鍵部分

            // computation of level of shadowing w  
            float3 sphereDirection = 
               _SpherePosition.xyz - input.posWorld.xyz;
            float sphereDistance = length(sphereDirection);
            sphereDirection = sphereDirection / sphereDistance;
            float d = lightDistance 
               * (asin(min(1.0, 
               length(cross(lightDirection, sphereDirection)))) 
               - asin(min(1.0, _SphereRadius / sphereDistance)));
            float w = smoothstep(-1.0, 1.0, -d / _LightSourceRadius);
            w = w * smoothstep(0.0, 0.2, 
               dot(lightDirection, sphereDirection));
            if (0.0 != _WorldSpaceLightPos0.w) // point light source?
            {
               w = w * smoothstep(0.0, _SphereRadius, 
                  lightDistance - sphereDistance);
            }

使用 asin(min(1.0, ...)) 確保 asin 的引數在允許的範圍內。

完整的著色器程式碼

[編輯 | 編輯原始碼]

完整的原始碼定義了陰影投射球體和光源半徑的屬性。所有值都應以世界座標表示。對於方向光源,光源半徑應以弧度表示(1 rad = 180° / π)。設定陰影投射球體的位置和半徑的最佳方法是簡短的指令碼,該指令碼應附加到使用該著色器的所有接收陰影的物件,例如

@script ExecuteInEditMode()

var occluder : GameObject;

function Update () {
   if (null != occluder) {
      GetComponent(Renderer).sharedMaterial.SetVector("_SpherePosition", 
         occluder.transform.position);
      GetComponent(Renderer).sharedMaterial.SetFloat("_SphereRadius", 
         occluder.transform.localScale.x / 2.0);
   }
}

該指令碼具有一個公共變數 occluder,該變數應設定為陰影投射球體。然後,它設定以下著色器的屬性 _SpherePostion_SphereRadius(該著色器應附加到與指令碼相同的接收陰影物件)。

片段著色器相當長,實際上我們必須使用 #pragma target 3.0 行來忽略舊版 GPU 的一些限制,如 Unity 參考 中所述。

Shader "Cg shadow of sphere" {
   Properties {
      _Color ("Diffuse Material Color", Color) = (1,1,1,1) 
      _SpecColor ("Specular Material Color", Color) = (1,1,1,1) 
      _Shininess ("Shininess", Float) = 10
      _SpherePosition ("Sphere Position", Vector) = (0,0,0,1)
      _SphereRadius ("Sphere Radius", Float) = 1
      _LightSourceRadius ("Light Source Radius", Float) = 0.005
   }
   SubShader {
      Pass {      
         Tags { "LightMode" = "ForwardBase" } 
            // pass for ambient light and first light source
 
         CGPROGRAM
 
         #pragma vertex vert  
         #pragma fragment frag 
 
         #pragma target 3.0
 
         #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 _SpherePosition; 
            // center of shadow-casting sphere in world coordinates
         uniform float _SphereRadius; 
            // radius of shadow-casting sphere
         uniform float _LightSourceRadius; 
            // in radians for directional light sources
 
         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);
            float3 lightDirection;
            float lightDistance;
            float attenuation;
 
            if (0.0 == _WorldSpaceLightPos0.w) // directional light?
            {
               attenuation = 1.0; // no attenuation
               lightDirection = 
                  normalize(_WorldSpaceLightPos0.xyz);
               lightDistance = 1.0;
            } 
            else // point or spot light
            {
               lightDirection = 
                  _WorldSpaceLightPos0.xyz - input.posWorld.xyz;
               lightDistance = length(lightDirection);
               attenuation = 1.0 / lightDistance; // linear attenuation
               lightDirection = lightDirection / lightDistance;
            }
 
            // computation of level of shadowing w  
            float3 sphereDirection = 
               _SpherePosition.xyz - input.posWorld.xyz;
            float sphereDistance = length(sphereDirection);
            sphereDirection = sphereDirection / sphereDistance;
            float d = lightDistance 
               * (asin(min(1.0, 
               length(cross(lightDirection, sphereDirection)))) 
               - asin(min(1.0, _SphereRadius / sphereDistance)));
            float w = smoothstep(-1.0, 1.0, -d / _LightSourceRadius);
            w = w * smoothstep(0.0, 0.2, 
               dot(lightDirection, sphereDirection));
            if (0.0 != _WorldSpaceLightPos0.w) // point light source?
            {
               w = w * smoothstep(0.0, _SphereRadius, 
                  lightDistance - sphereDistance);
            }
 
            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);
            }
 
            return float4(ambientLighting 
               + (1.0 - w) * (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 
 
         #pragma target 3.0
 
         #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 _SpherePosition; 
            // center of shadow-casting sphere in world coordinates
         uniform float _SphereRadius; 
            // radius of shadow-casting sphere
         uniform float _LightSourceRadius; 
            // in radians for directional light sources
 
         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);
            float3 lightDirection;
            float lightDistance;
            float attenuation;
 
            if (0.0 == _WorldSpaceLightPos0.w) // directional light?
            {
               attenuation = 1.0; // no attenuation
               lightDirection = normalize(_WorldSpaceLightPos0.xyz);
               lightDistance = 1.0;
            } 
            else // point or spot light
            {
               lightDirection =
                  _WorldSpaceLightPos0.xyz - input.posWorld.xyz;
               lightDistance = length(lightDirection);
               attenuation = 1.0 / lightDistance; // linear attenuation
               lightDirection = lightDirection / lightDistance;
            }
 
            // computation of level of shadowing w  
            float3 sphereDirection = 
               _SpherePosition.xyz - input.posWorld.xyz;
            float sphereDistance = length(sphereDirection);
            sphereDirection = sphereDirection / sphereDistance;
            float d = lightDistance 
               * (asin(min(1.0, 
               length(cross(lightDirection, sphereDirection)))) 
               - asin(min(1.0, _SphereRadius / sphereDistance)));
            float w = smoothstep(-1.0, 1.0, -d / _LightSourceRadius);
            w = w * smoothstep(0.0, 0.2, 
               dot(lightDirection, sphereDirection));
            if (0.0 != _WorldSpaceLightPos0.w) // point light source?
            {
               w = w * smoothstep(0.0, _SphereRadius, 
                  lightDistance - sphereDistance);
            }
 
            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);
            }
 
            return float4((1.0 - w) * (diffuseReflection 
               + specularReflection), 1.0);
         }
 
         ENDCG
      }
   } 
   Fallback "Specular"
}

恭喜!我希望你成功渲染了一些漂亮的柔和陰影。我們已經瞭解了

  • 什麼是柔和陰影,以及什麼是半影和本影。
  • 如何計算球體的柔和陰影。
  • 如何實現計算,包括使用 JavaScript 編寫的指令碼,該指令碼根據另一個 GameObject 設定一些屬性。

進一步閱讀

[編輯 | 編輯原始碼]

如果你還想了解更多

  • 關於著色器程式碼的其餘部分,你應該閱讀 部分“平滑鏡面高光”.
  • 關於柔和陰影的計算,你應該閱讀 Orion Sky Lawlor 的出版物:“插值友好的柔和陰影貼圖”,發表在計算機圖形和虛擬現實 ’06 會議論文集,第 111–117 頁。可以在 網上 找到預印本。

< Cg 程式設計/Unity

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