GPU Gems1 - 19 基于图像的光照(Image-Based Lighting)
這篇文章打破了當(dāng)時(shí)立方體貼圖環(huán)境(Cube-Map Environment)用法的桎梏,深入研究了更多可能的逼真光照效果。文章主要研究了基于圖像的光照(Image-Based Lighting,IBL),包括局部化的立方體映射,類似于使用基于圖像的局部光照(Localizing Image-Based Lighting),然后介紹了如何把哪些重要的技巧用于著色模型,包括逼真的反射、陰影和漫反射/環(huán)境項(xiàng)。
cube map反射總是使它好像在無限遠(yuǎn)處,只與觀察角度有關(guān)(想象一下skybox),這限制了它對(duì)小的封閉環(huán)境的應(yīng)用.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
傳統(tǒng)cube map反射貼圖?
? 在室內(nèi)環(huán)境移動(dòng)模型時(shí),環(huán)境最好是近距離的cube map,距離的大小與當(dāng)前的房間類似。當(dāng)模型移動(dòng)式,根據(jù)模型在房間中的位置進(jìn)行反射。只要加很少的shader代碼就能將反射局部化。這種方法得到的模擬效果使人感到更為可靠和逼真。尤其在包含窗戶,屏幕和其他可識(shí)別光源的環(huán)境中。而只要加入很少的Shader數(shù)學(xué)就能將反射局部化。具體可以看原文貼出的Shader源碼。
?
局部化的反射?
不同位置上的局部反射?
頂點(diǎn)shader:由cpu傳入世界空間和光照空間的矩陣等信息,在頂點(diǎn)shader中將點(diǎn)和矢量轉(zhuǎn)換兩次,先由模型空間轉(zhuǎn)換到世界空間,然后從世界空間轉(zhuǎn)換到光照空間。具體代碼如下:
Example 19-1. Vertex Shader to Generate World-Space and Lighting-Space Coordinates
vertexOutput reflectVS(appdata IN,uniform float4x4 WorldViewProjXf,uniform float4x4 WorldITXf,uniform float4x4 WorldXf,uniform float4x4 ViewITXf,uniform float4x4 LightingXf,uniform float4x4 LightingITXf) {vertexOutput OUT;OUT.TexCoord = IN.UV;float4 Po = float4(IN.Position.xyz,1.0); // pad to "float4"OUT.HPosition = mul(WorldViewProjXf, Po);float4 Pw = mul(WorldXf, Po); // world coordinatesfloat3 WorldEyePos = ViewITXf [3].xyz;float4 LightingEyePos = mul(LightingXf, float4(WorldEyePos, 1.0));float4 Pu = mul(LightingXf, Pw);float4 Nw = mul(WorldITXf, IN.Normal);float4 Tw = mul(WorldITXf, IN.Tangent);float4 Bw = mul(WorldITXf, IN.Binormal);OUT.LightingEyeVec = (LightingEyePos - Pu).xyz;OUT.LightingNormal = mul(LightingITXf, Nw).xyz;OUT.LightingTangent = mul(LightingITXf, Tw).xyz;OUT.LightingBinorm = mul(LightingITXf, Bw).xyz;OUT.LightingPos = mul(LightingXf, Pw).xyz;return OUT; }片元shader:給定待著色點(diǎn)的位置和矢量,在光照空間計(jì)算反射矢量,它的起點(diǎn)是光照空間的表面,通過解球的二次方程,讓矢量與那個(gè)球面相交,球面中心在照明空間的原點(diǎn),半徑=1.0。得到反射矢量后就可以采樣從cube map采樣。該shader還提供了幾個(gè)另外的選項(xiàng),以增加shader的逼真性,如表面顏色,Fresnel反射衰減等。
Example 19-2. Localized-Reflection Pixel Shader
float4 reflectPS(vertexOutput IN,uniform samplerCUBE EnvMap,uniform sampler2D NormalMap,uniform float4 SurfColor,uniform float Kr, // intensity of reflectionuniform float KrMin, // typical: 0.05 * Kruniform float FresExp, // typical: 5.0uniform float Bumpiness // amount of bump) : COLOR {float3 Nu = normalize(IN.LightingNormal);// for bump mapping, we will alter "Nu" to get "Nb"float3 Tu = normalize(IN.LightingTangent);float3 Bu = normalize(IN.LightingBinorm);float3 bumps = Bumpiness *(tex2D(NormalMap, IN.TexCoord.xy).xyz - (0.5).xxx);float3 Nb = Nu + (bumps.x * Tu + bumps.y * Bu);Nb = normalize(Nb); // expressed in user-coord spacefloat3 Vu = normalize(IN.LightingEyeVec);float vdn = dot(Vu, Nb); // or "Nu" if unbumped - see text// "fres" attenuates the strength of the reflection// according to Fresnel's lawfloat fres = KrMin + (Kr - KrMin) * pow((1.0 - abs(vdn)), FresExp);float3 reflVect = normalize(reflect(Vu, Nb)); // yes, normalize// now we intersect "reflVect" with a sphere of radius 1.0float b = -2.0 * dot(reflVect, IN.LightingPos);float c = dot(IN.LightingPos, IN.LightingPos) - 1.0;float discrim = b * b - 4.0 * c;bool hasIntersects = false;float4 reflColor = float4(1, 0, 0, 0);if (discrim > 0) {// pick a small error value very close to zero as "epsilon"hasIntersects = ((abs(sqrt(discrim) - b) / 2.0) > 0.00001);}if (hasIntersects) {// determine where on the unit sphere reflVect intersectsreflVect = nearT * reflVect - IN.LightingPos;// reflVect.y = -reflVect.y; // optional - see text// now use the new intersection location as the 3D directionreflColor = fres * texCUBE(EnvMap, reflVect);}float4 result = SurfColor * reflColor;return result; }另外,我們可以將3D幾何體做成立方體貼圖,并且在正常地渲染環(huán)境的時(shí)候,把貼圖應(yīng)用到該環(huán)境的物體上。也可以使用貼圖作為環(huán)境,把它投射到較簡(jiǎn)單的幾何體上。
立方體貼圖也能用來決定漫反射光照。Debevec的HDRShop程序能夠從映射立方體光照環(huán)境積分出全部的漫反射貢獻(xiàn)度,那么通過把表面法線帶入預(yù)先卷積的立方體貼圖,能夠簡(jiǎn)單地查詢漫反射貢獻(xiàn)。
基于圖像的光照為復(fù)雜的光照計(jì)算提供了綜合而廉價(jià)的替代品,將一點(diǎn)數(shù)學(xué)加入紋理方法,可以大大拓寬“簡(jiǎn)單”IBL效果,給3D圖像提供更強(qiáng)的的方位感。
【關(guān)鍵詞提煉】
基于圖像的光照(Image-Based Lighting,IBL)
立方體貼圖環(huán)境(Cube-Map Environment )
基于圖像的局部光照(Localizing Image-Based Lighting)
?
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的GPU Gems1 - 19 基于图像的光照(Image-Based Lighting)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用邓西百度网盘消息群发工具对百度网盘的
- 下一篇: taro-ui scrollview o