Cg 程式設計/Unity/鏡面高光

本教程涵蓋逐頂點光照(也稱為古羅德著色),使用馮氏反射模型。
它擴充套件了“漫反射”章節中的著色器程式碼,增加了兩個額外項:環境光照和鏡面反射。這三個項共同構成了馮氏反射模型。如果您尚未閱讀“漫反射”章節,現在是一個很好的機會閱讀它。
仔細觀察卡拉瓦喬左側的畫作。雖然白色襯衫的大部分割槽域都處於陰影中,但沒有部分是完全黑色的。顯然,總會有一些光線從牆壁和其他物體反射出來,照亮場景中的所有東西——至少在一定程度上。在馮氏反射模型中,這種效果透過環境光照來考慮,它取決於一般環境光強度 和漫反射的材料顏色。在環境光照強度 的方程式中
類似於“漫反射”章節中漫反射方程式,此方程式也可以解釋為光的紅色、綠色和藍色分量的向量方程式。
在 Unity 中,透過從主選單中選擇視窗 > 渲染 > 光照設定,將場景 > 環境光照 > 源設定為顏色並指定環境顏色來指定統一的環境光。在 Unity 中的 Cg 著色器中,此顏色可作為UNITY_LIGHTMODEL_AMBIENT使用,它是“世界空間著色”章節中提到的預定義制服之一。(如果您選擇漸變而不是顏色,則UNITY_LIGHTMODEL_AMBIENT和unity_AmbientSky指定天空顏色,而赤道顏色和地面顏色則分別由unity_AmbientEquator和unity_AmbientGround指定。)

如果您仔細觀察卡拉瓦喬的畫作,您會看到幾個鏡面高光:在鼻子、頭髮、嘴唇、魯特琴、小提琴、弓、水果等。馮氏反射模型包含一個鏡面反射項,可以模擬這種光滑表面上的高光;它甚至包含一個引數 來指定材料的光澤度。光澤度指定高光的大小:光澤度越高,高光越小。
一個完美光滑的表面只會反射來自光源的光線,其方向為幾何反射方向R。對於光澤度低於完美的表面,光線會反射到R周圍的方向:光澤度越低,擴散越廣。在數學上,規範化的反射方向R 由
定義,其中N 是規範化的表面法向量,L 是規範化的光源方向。在 Cg 中,函式float3 reflect(float3 I, float3 N)(或float4 reflect(float4 I, float4 N))計算相同的反射向量,但針對從光源到表面上的點的方向I。因此,我們必須將L取反以使用此函式。
鏡面反射項計算觀察者方向V 的鏡面反射。如上所述,如果V 接近R,則強度應該很大,其中“接近”由光澤度 引數化。在馮氏反射模型中,R 和V 之間角度的餘弦的 次方用於生成不同光澤度的亮點。與漫反射的情況類似,我們應該將負餘弦值鉗制為 0。此外,鏡面項需要材料顏色 用於鏡面反射,它通常只是白色,這樣所有亮點都具有入射光 的顏色。例如,卡拉瓦喬畫作中的所有亮點都是白色的。馮氏反射模型的鏡面項為
類似於漫反射的情況,如果光源位於表面的“錯誤”一側,則應忽略鏡面反射項;即如果點積 N·L 為負。
著色器程式碼
[edit | edit source]環境光照的著色器程式碼很簡單,使用逐分量向量-向量乘積
float3 ambientLighting =
UNITY_LIGHTMODEL_AMBIENT.rgb * _Color.rgb;
為了實現鏡面反射,我們需要在世界空間中獲得觀察者的方向,我們可以將其計算為相機位置和頂點位置之間的差值(都在世界空間中)。世界空間中的相機位置由 Unity 在 uniform _WorldSpaceCameraPos 中提供;頂點位置可以按照“漫反射”部分所述轉換為世界空間。然後可以像這樣實現世界空間中鏡面反射項的方程式
float3 viewDirection = normalize(_WorldSpaceCameraPos
- mul(modelMatrix, input.vertex).xyz);
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);
}
此程式碼片段使用與“漫反射”部分著色器程式碼相同的變數,以及使用者指定的屬性 _SpecColor 和 _Shininess。(這些名稱是專門選擇的,以便回退著色器可以訪問它們;請參閱“漫反射”部分的討論。)pow(a, b) 計算 .
如果環境光照被新增到第一遍(我們只需要它一次),並且鏡面反射被新增到“漫反射”部分的完整著色器的兩遍,它看起來像這樣
Shader "Cg per-vertex lighting" {
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 col : COLOR;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
float4x4 modelMatrix = unity_ObjectToWorld;
float3x3 modelMatrixInverse = unity_WorldToObject;
float3 normalDirection = normalize(
mul(input.normal, modelMatrixInverse));
float3 viewDirection = normalize(_WorldSpaceCameraPos
- mul(modelMatrix, input.vertex).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
- mul(modelMatrix, input.vertex).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
{
specularReflection = attenuation * _LightColor0.rgb
* _SpecColor.rgb * pow(max(0.0, dot(
reflect(-lightDirection, normalDirection),
viewDirection)), _Shininess);
}
output.col = float4(ambientLighting + diffuseReflection
+ specularReflection, 1.0);
output.pos = UnityObjectToClipPos(input.vertex);
return output;
}
float4 frag(vertexOutput input) : COLOR
{
return input.col;
}
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 col : COLOR;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
float4x4 modelMatrix = unity_ObjectToWorld;
float3x3 modelMatrixInverse = unity_WorldToObject;
float3 normalDirection = normalize(
mul(input.normal, modelMatrixInverse));
float3 viewDirection = normalize(_WorldSpaceCameraPos
- mul(modelMatrix, input.vertex).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
- mul(modelMatrix, input.vertex).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
{
specularReflection = attenuation * _LightColor0.rgb
* _SpecColor.rgb * pow(max(0.0, dot(
reflect(-lightDirection, normalDirection),
viewDirection)), _Shininess);
}
output.col = float4(diffuseReflection
+ specularReflection, 1.0);
// no ambient contribution in this pass
output.pos = UnityObjectToClipPos(input.vertex);
return output;
}
float4 frag(vertexOutput input) : COLOR
{
return input.col;
}
ENDCG
}
}
Fallback "Specular"
}
總結
[edit | edit source]恭喜,您剛剛學習瞭如何實現 Phong 反射模型。具體來說,我們已經看到了
- Phong 反射模型中的環境光照是什麼。
- Phong 反射模型中的鏡面反射項是什麼。
- 如何在 Unity 中使用 Cg 實現這些項。
進一步閱讀
[edit | edit source]如果您還想了解更多
- 關於著色器程式碼,您應該閱讀“漫反射”部分。