Cg 程式設計/Unity/輪廓上的鏡面高光

本教程涵蓋了鏡面高光的菲涅爾係數。
它是關於光照的幾個教程之一,它超出了Phong 反射模型。然而,它基於“鏡面高光”部分(針對每個頂點的光照)和“平滑鏡面高光”部分(針對每個畫素的光照)中描述的Phong 反射模型的光照。如果您還沒有閱讀這些教程,請先閱讀它們。
許多材料(例如啞光紙)在光線掠過表面時會顯示強烈的鏡面反射;即,當背光從與觀察者相反的方向反射時,如左側照片所示。菲涅爾係數解釋了某些材料的這種強反射。當然,還有其他導致明亮輪廓的原因,例如半透明頭髮或織物(參見“半透明表面”部分)。
有趣的是,這種效果通常很難看到,因為當輪廓的背景非常明亮時最有可能出現。然而,在這種情況下,明亮的輪廓只會融入背景,因此幾乎不可察覺。

菲涅爾係數 描述了非導電材料在波長為 的非偏振光時的鏡面反射率。Schlick 近似為
其中V是指向觀察者的歸一化方向,H是歸一化的半向量:H = (V + L) / |V + L|,其中L是指向光源的歸一化方向。 是H·V = 1時的反射率,即當指向光源的方向、指向觀察者的方向和半向量都相同時。另一方面, 對於H·V = 0變為1,即當半向量與指向觀察者的方向正交時,這意味著指向光源的方向與指向觀察者的方向相反(即掠射光反射的情況)。事實上, 在這種情況下與波長無關,材料的行為就像一個完美的鏡子。
使用內建的Cg函式lerp(x,y,w) = x*(1-w) + y*w,我們可以將Schlick近似改寫為
這可能會稍微高效一些,至少在某些 GPU 上。我們將透過允許不同的 值來考慮對波長的依賴性;也就是說,我們將它視為 RGB 向量。事實上,我們將其與來自“鏡面高光”部分的常量材質顏色 相識別。換句話說,菲涅耳係數增加了材質顏色 對視角方向和半向量之間角度的依賴性。因此,我們在任何鏡面反射計算中用施裡克近似(使用)替換常量材質顏色。
例如,我們在 Phong 反射模型中關於鏡面項的方程是(參見“鏡面高光”部分)
用施裡克近似(使用)替換菲涅耳係數的,得到
該實現基於來自“平滑鏡面高光”部分的著色器程式碼。它只是計算半向量,幷包括菲涅耳係數的近似值
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
{
float3 halfwayDirection =
normalize(lightDirection + viewDirection);
float w = pow(1.0 - max(0.0,
dot(halfwayDirection, viewDirection)), 5.0);
specularReflection = attenuation * _LightColor0.rgb
* lerp(_SpecColor.rgb, float3(1.0, 1.0, 1.0), w)
* pow(max(0.0, dot(
reflect(-lightDirection, normalDirection),
viewDirection)), _Shininess);
}
將上面程式碼片段放入來自“Smooth Specular Highlights”部分的完整著色器中,將得到以下著色器。
Shader "Cg Fresnel highlights" {
Properties {
_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 float4 _Color;
uniform float4 _SpecColor;
uniform float _Shininess;
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;
float3x3 modelMatrixInverse = unity_WorldToObject;
output.posWorld = mul(modelMatrix, input.vertex);
output.normalDir = normalize(mul(input.normal, modelMatrixInverse));
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 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);
}
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
{
float3 halfwayDirection =
normalize(lightDirection + viewDirection);
float w = pow(1.0 - max(0.0,
dot(halfwayDirection, viewDirection)), 5.0);
specularReflection = attenuation * _LightColor0.rgb
* lerp(_SpecColor.rgb, float3(1.0, 1.0, 1.0), w)
* 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 float4 _Color;
uniform float4 _SpecColor;
uniform float _Shininess;
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 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);
}
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
{
float3 halfwayDirection =
normalize(lightDirection + viewDirection);
float w = pow(1.0 - max(0.0,
dot(halfwayDirection, viewDirection)), 5.0);
specularReflection = attenuation * _LightColor0.rgb
* lerp(_SpecColor.rgb, float3(1.0, 1.0, 1.0), w)
* pow(max(0.0, dot(
reflect(-lightDirection, normalDirection),
viewDirection)), _Shininess);
}
return float4(diffuseReflection
+ specularReflection, 1.0);
}
ENDCG
}
}
Fallback "Specular"
}
藝術控制
[edit | edit source]對上面實現的一個有用修改是將冪5.0替換為使用者指定的著色器屬性。這將為CG藝術家提供一個選項,根據他們的藝術需求誇大或衰減菲涅爾因子的影響。
半透明表面的影響
[edit | edit source]除了影響鏡面高光外,菲涅爾因子還應影響半透明表面的不透明度。事實上,菲涅爾因子描述了表面如何對於掠射光線變得更加反射,這意味著更少的入射光被吸收、折射或透射,即透明度降低,因此不透明度增加。為此,可以使用表面法線向量N代替半程向量H來計算菲涅爾因子,並且半透明表面的不透明度可以從使用者指定的值(在表面法線方向觀看)增加到 1(與波長無關),公式為
.
在“Silhouette Enhancement”部分中,不透明度被認為是由於光線穿過一層半透明材料而導致的衰減。這種不透明度應該與由於反射率增加而產生的不透明度相結合:總不透明度是 1 減去總透明度,它是由於衰減而產生的透明度(它是 1 減去)和由於菲涅爾因子而產生的透明度(它是 1 減去)的乘積,即
是如上計算得到的透明度,而 是在 “輪廓增強” 部分計算得到的透明度。對於平行於表面法向量的視角方向, 和 可以由使用者指定。然後,該方程確定了 的法線方向,實際上,它確定了所有常數,因此可以計算所有視角方向的 。請注意,漫反射或鏡面反射都不應乘以透明度 ,因為鏡面反射已經乘以菲涅爾係數,漫反射應該只乘以由於衰減造成的透明度 。
總結
[edit | edit source]恭喜您完成了本教程!我們已經瞭解了:
- 什麼是菲涅爾係數。
- 什麼是菲涅爾係數的施裡克近似。
- 如何實現鏡面高光的施裡克近似。
- 如何為實現新增更多藝術控制。
- 如何使用菲涅爾係數來呈現半透明表面。
進一步閱讀
[edit | edit source]如果您想了解更多關於以下內容:
- 使用 Phong 反射模型進行光照,請閱讀 “鏡面高光” 部分。
- 關於逐畫素光照(即 Phong 著色),請閱讀 “平滑鏡面高光” 部分。
- 關於施裡克近似,請閱讀 Christophe Schlick 發表的文章“An inexpensive BRDF model for physically-based rendering”,計算機圖形論壇,第 13 卷,第 3 期:233—246,1994 年。或者您可以閱讀 Randi Rost 等人撰寫的《OpenGL 著色語言》(第 3 版,2009 年由 Addison-Wesley 出版)的第 14.1 章,或 Wolfgang Engel、Jack Hoxley、Ralf Kornmann、Niko Suni 和 Jason Zink 撰寫的《程式設計頂點、幾何體和畫素著色器》(第 2 版,2008 年)的光照章節中的第 5 章(可在 網上 獲得)。