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

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


雖然方向光源和點光源會產生硬陰影,但任何面積光源都會產生軟陰影。對於所有真實光源也是如此,特別是太陽以及任何燈泡或燈具。從陰影投射器後面的某些點,光源的任何部分都不可見,陰影是均勻的黑暗:這就是本影。從其他點,光源的或多或少部分是可見的,因此陰影或多或少是完整的:這就是半影。最後,有一些點可以從那裡看到光源的整個區域:這些點位於陰影之外。
在許多情況下,陰影的柔和度主要取決於陰影投射器和陰影接收器之間的距離:距離越大,陰影越柔和。這在藝術中是一個眾所周知的現象;例如,請參考右邊的卡拉瓦喬的繪畫。

我們將近似計算表面上一個點的陰影,當半徑為的球體在S處(相對於表面點)遮擋了半徑為的球形光源在L處(同樣相對於表面點);參見左邊的圖。
為此,我們考慮一個方向為T的切線,該切線與球體相切並經過表面點。此外,選擇該切線位於L和S所跨越的平面內,即平行於左邊圖的視角平面。關鍵的觀察結果是,光源中心與該切線的最小距離與表面點的陰影量直接相關,因為它決定了從表面點可見的光源區域有多大。更準確地說,我們需要一個帶符號的距離(如果切線與球體在L的同側,則為正,否則為負),以確定表面點是在本影內 (),在半影內 (),還是在陰影之外 ()。
為了計算,我們考慮L和S之間的角度以及T和S之間的角度。這兩個角度之間的差值是L和T之間的角度,它與的關係為
.
因此,到目前為止,我們有
我們可以使用以下公式計算T和S之間的角度
.
因此
.
對於L和S之間的角度,我們使用叉積的一個特性
.
所以
.
總的來說,我們有
我們目前所做的近似處理影響不大;更重要的是它不會產生渲染偽影。如果效能是一個問題,我們可以更進一步地使用 arcsin(x) ≈ x;也就是說,我們可以使用
這避免了所有三角函式;然而,它確實引入了渲染偽像(特別是如果鏡面高光在面向光源的半影中)。這些渲染偽像是否值得效能提升需要針對每種情況做出決定。
接下來我們看看如何根據 計算陰影程度 。當 從 減少到 , 應該從 0 增加到 1。換句話說,我們想要在 的 -1 到 1 值之間進行平滑過渡。實現這一點可能最有效的方法是使用內建 GLSL 函式 smoothstep(a,b,x) = t*t*(3-2*t) 提供的 Hermite 插值,其中 t=clamp((x-a)/(b-a),0,1)
雖然這不是 和 之間基於物理的關係的特別好的近似,但它仍然能正確地體現基本特徵。
此外, 應該為 0,如果光線方向 L 與 S 方向相反;也就是說,如果它們的點積為負。這個條件有點棘手,因為它會導致 L 和 S 正交的平面上出現明顯的間斷。為了使這種間斷平滑,我們再次可以使用 smoothstep 來計算改進的值
此外,如果點光源比遮擋球更靠近表面點,我們必須將 設定為 0。這也有點棘手,因為球形光源可能會與投射陰影的球體相交。一個避免過於明顯的偽影(但無法處理完全相交問題)的解決方案是
在定向光源的情況下,我們只需設定 。然後,指定無陰影照明的級別的項 應該乘以光源的任何照明。(因此,環境光不應乘以該因子。)如果計算多個陰影投射器的陰影,則對於每個光源,必須組合所有陰影投射器的項 。常見的方法是將它們相乘,儘管這可能不準確(特別是當陰影相交時)。
實現
[edit | edit source]該實現計算lightDirection 和sphereDirection 向量的長度,然後繼續處理歸一化向量。這樣,這些向量的長度只需要計算一次,我們甚至可以避免一些除法,因為我們可以使用歸一化向量。以下是片段著色器的關鍵部分
// computation of level of shadowing w
vec3 sphereDirection = vec3(_SpherePosition - position);
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 的引數在允許的範圍內。
完整著色器程式碼
[edit | edit source]完整的原始碼定義了用於投射陰影的球體和光源半徑的屬性。所有值都應該在世界座標中。對於定向光源,光源半徑應以弧度表示(1 弧度 = 180° / π)。設定投射陰影的球體的位置和半徑的最佳方法是一個簡短的指令碼,該指令碼應該附加到使用著色器的所有接收陰影的物件,例如
@script ExecuteInEditMode()
var occluder : GameObject;
function Update () {
if (null != occluder) {
renderer.sharedMaterial.SetVector("_SpherePosition",
occluder.transform.position);
renderer.sharedMaterial.SetFloat("_SphereRadius",
occluder.transform.localScale.x / 2.0);
}
}
此指令碼有一個公共變數occluder,應該設定為投射陰影的球體。然後它設定了以下著色器的屬性_SpherePostion 和_SphereRadius(該著色器應該附加到與指令碼相同的接收陰影的物件)。
Shader "GLSL 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
GLSLPROGRAM
// User-specified properties
uniform vec4 _Color;
uniform vec4 _SpecColor;
uniform float _Shininess;
uniform vec4 _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
// 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
#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;
}
#endif
#ifdef FRAGMENT
void main()
{
vec3 normalDirection = normalize(varyingNormalDirection);
vec3 viewDirection =
normalize(_WorldSpaceCameraPos - vec3(position));
vec3 lightDirection;
float lightDistance;
float attenuation;
if (0.0 == _WorldSpaceLightPos0.w) // directional light?
{
attenuation = 1.0; // no attenuation
lightDirection = normalize(vec3(_WorldSpaceLightPos0));
lightDistance = 1.0;
}
else // point or spot light
{
lightDirection = vec3(_WorldSpaceLightPos0 - position);
lightDistance = length(lightDirection);
attenuation = 1.0 / lightDistance; // linear attenuation
lightDirection = lightDirection / lightDistance;
}
// computation of level of shadowing w
vec3 sphereDirection = vec3(_SpherePosition - position);
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);
}
vec3 ambientLighting =
vec3(gl_LightModel.ambient) * vec3(_Color);
vec3 diffuseReflection =
attenuation * vec3(_LightColor0) * vec3(_Color)
* 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) * pow(max(0.0, dot(
reflect(-lightDirection, normalDirection),
viewDirection)), _Shininess);
}
gl_FragColor = vec4(ambientLighting
+ (1.0 - w) * (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 vec4 _Color;
uniform vec4 _SpecColor;
uniform float _Shininess;
uniform vec4 _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
// 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
#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;
}
#endif
#ifdef FRAGMENT
void main()
{
vec3 normalDirection = normalize(varyingNormalDirection);
vec3 viewDirection =
normalize(_WorldSpaceCameraPos - vec3(position));
vec3 lightDirection;
float lightDistance;
float attenuation;
if (0.0 == _WorldSpaceLightPos0.w) // directional light?
{
attenuation = 1.0; // no attenuation
lightDirection = normalize(vec3(_WorldSpaceLightPos0));
lightDistance = 1.0;
}
else // point or spot light
{
lightDirection = vec3(_WorldSpaceLightPos0 - position);
lightDistance = length(lightDirection);
attenuation = 1.0 / lightDistance; // linear attenuation
lightDirection = lightDirection / lightDistance;
}
// computation of level of shadowing w
vec3 sphereDirection = vec3(_SpherePosition - position);
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);
}
vec3 diffuseReflection =
attenuation * vec3(_LightColor0) * vec3(_Color)
* 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) * pow(max(0.0, dot(
reflect(-lightDirection, normalDirection),
viewDirection)), _Shininess);
}
gl_FragColor = vec4((1.0 - w) * (diffuseReflection
+ specularReflection), 1.0);
}
#endif
ENDGLSL
}
}
// The definition of a fallback shader should be commented out
// during development:
// Fallback "Specular"
}
總結
[edit | edit source]恭喜!我希望您成功渲染了一些漂亮的軟陰影。我們已經瞭解了
- 什麼是軟陰影以及什麼是半影和本影。
- 如何計算球體的軟陰影。
- 如何實現計算,包括使用 JavaScript 編寫的指令碼,該指令碼根據另一個
GameObject設定一些屬性。
進一步閱讀
[edit | edit source]如果您還想了解更多資訊
- 關於著色器程式碼的其餘部分,您應該閱讀 “平滑鏡面高光”部分。
- 關於軟陰影的計算,您應該閱讀 Orion Sky Lawlor 的出版物:“插值友好型軟陰影貼圖”,發表在 2006 年計算機圖形和虛擬現實大會論文集,第 111-117 頁。預印本可在 網上 獲得。