| 導購 | 订阅 | 在线投稿
分享
 
 
 

遊戲的生命---遊戲中的特效與光照處理

來源:互聯網  2008-05-19 00:45:19  評論

1、 引言(Introduction)

本文作者

Tomas Arce

2、什麽是逐象素光照(What Is Per-Pixel Lighting)

煩惱的命名。

Bad name

紋素光照處理會更加真實。

Texel-lighting is more accurate.

紋理分辨率的問題 ― 有助于處理貼圖。

Texture resolution matters -- tiling helps.

逐象素基本要做的複雜操作。

Complex operation done per-pixel basis.

紋理查找,三維點光照值生成及象素著色。

DP3Texture lookups, dp3, and pixel shaders.

利用顔色作爲數據。

Use colors as data.

對紋理的象素法向量進行編碼,填充RGB值:

Encode pixel-normals in texture, fill RGB as:

R=Normal.X, G=Normal.Y, B=Normal.Z

也要對光照引索進行編碼,填充頂點顔色:

Light dir is also encoded, fill vertex color as:

R=LightDir.X, G = LightDir.Y, B = LightDir.Z

現在我們可以做:

Now we can do:

( Texture.Texel ) dot ( Vertex.Color )

3、逐象素爲什麽更好(Why Is Per-Pixel Better)

每頂點光照更快且其更加靈活,但是缺乏分辨率。

Per-vertex light is faster and more flexible but lacks resolution.

光照貼圖有缺陷:

Light-maps have pit-falls:

不真實的鏡面處理。

No real specular.

低分辨率。

Low resolution.

不適于工作在動態對象上。

Doesn't work for dynamic objects.

投射紋理不能獲取更多的表面細節。

Projected textures don't give much detail for the surface.

細節?細節?細節。

Detail. Detail. Detail.

逐象素光照處理需要很少的網格多邊形。

ppLighting needs few polygons per-mesh.

簡化碰撞、模板陰影、存儲。

Simplifies collision, stencil shadows, and memory.

凹凸貼圖是逐象素光照的一個子集。

Bump mapping is a subset of the ppLighting

「法向量貼圖」

「Normal maps」

法向量貼圖可以很好的處理不同類型的光照及表面類型。

Normal maps handle different types of lights and surfaces well.

王朝网络

圖1

4、如何存儲紋素法向量(How to Store Normals in Texels)

空間是什麽?

What space?

本地空間對紋理空間。

Local-Space vs Texture Space.

本地空間法向量。

Local Space Normals (LS)

使用對象的坐標的原點提取法向量。

Use the origin of the object to extract normals

紋理空間法向量。

Texture Space Normals (TS).

存儲非普通空間的法向量,例如,正切空間。

Store normals in generic space, e.g., Tangent Space.

每個頂點儲存一個矩陣,表示光線從本地空間到紋理空間的轉換。

Store a matrix per vertex that takes the light from local-space to texture space.

王朝网络

Local Space

王朝网络

Texture Space

LS原理簡單且處理速度極快。

LS is simple to work with and can be very fast.

但不允許壓縮。

But cannot be compressed.

益用于處理象汽車般的人物與對象。

Good for characters and objects such as cars.

TS原理複雜但其有兩個優點。

TS is more complex to work with but has 2 advantages.

允許圖素貼圖。

Tileable maps.

堆砌紋理。

Palletized textures.

適用于帶細節紋理的地形。

Good for big things like terrain with detail textures.

5、光照的不同類型(Different Types of Lights)

三個典型的光照類型:

3 typical light types:

方向光

Directional

點光源

Point

聚光燈

Spot

點光源、聚光燈通常具有衰減系數。

Point and spot lighting usually have attenuation coefficients.

將聚光燈的衰減函數存在到紋理內。

Store spot lighting attenuation function in a texture.

X是距離的函數。

X is a function of the distance

Y是角度的函數。

Y is a function of the angle

點乘每個點光的方向向量。

The dot-product direction of the light with the vertex

直接在頂點著色內實現衰減函數:(1/(k1+d*k2 +d*d*k3))。

Implementing attenuation function (1/(k1+d*k2 +d*d*k3) ) directly in the vertex shader.

使用少量指令,就可使問題接近零。

Takes few instructions, but has issues going towards zero.

光照矢量通常不需要歸一化,但是…

Light vector normalization is usually not needed but…

使用立方體貼圖查找。

Use cube-map lookup (32x32 or so)

或在象素著色中使用Newton-Raphson方法。

Or use Newton-Raphson approximation in pixel-shader:

mul r0, LightDir, 0.5

dp3 r1, LightDir, LightDir

mad r0, 1-r1, r0, LightDir

王朝网络

圖3

6、LS對TS的標准壓縮(LS vs TS Standard Comparison)

; LS Directional Light

; Transform position to clip space and output it

dp4 oPos.x, V_POSITION, c[CV_WORLDVIEWPROJ_0]

dp4 oPos.y, V_POSITION, c[CV_WORLDVIEWPROJ_1]

dp4 oPos.z, V_POSITION, c[CV_WORLDVIEWPROJ_2]

dp4 oPos.w, V_POSITION, c[CV_WORLDVIEWPROJ_3]

; Output tex coords

mov oT0, V_TEXTURE

mov oT1, V_TEXTURE

; TS Directional Light

; Transform position to clip space and output it

dp4 oPos.x, V_POSITION, c[CV_WORLDVIEWPROJ_0]

dp4 oPos.y, V_POSITION, c[CV_WORLDVIEWPROJ_1]

dp4 oPos.z, V_POSITION, c[CV_WORLDVIEWPROJ_2]

dp4 oPos.w, V_POSITION, c[CV_WORLDVIEWPROJ_3]

; Transform local space light by basis vectors to put it

; into texture space

dp3 LIGHT_T.x, V_S, c[L_DIR_LOCAL]

dp3 LIGHT_T.y, V_T, c[L_DIR_LOCAL]

dp3 LIGHT_T.z, V_Q, c[L_DIR_LOCAL]

; Scale to 0-1

add LIGHT_T, LIGHT_T, c[CV_ONE]

mul oD0, LIGHT_T, c[CV_HALF]

; Output tex coords

mov oT0, V_TEXTURE

mov oT1, V_TEXTURE

這個例子主要對僵硬的幾何學而言。

This example is mostly for rigid geometry.

最優化TS:11條指令。

Optimized TS: 11 instructions

最優化LS:6條指令。

Optimizes LS: 6 instructions

注意LS能做到5個燈源的數目。

Note that the LS could do up to 5 lights

在本地空間裏所有光源的方向都應作爲象素著色的常量。

All light directions in local space are loaded into the pixel-shader constants

TS使用本地空間裏光源的方向。

The TS uses light direction in local space.

其比基礎矢量多邊形轉換要得多。

Faster than transforming the basis vectors

7、LS對TS的細膩蒙皮(LS vs TS Soft-Skin)

王朝网络

圖4

// Transform pos with Weight 1

mov a0.x, V_INDICES.x

dp4 r1.x, V_POSITION, c[a0.x + CV_BONESTART + 0]

dp4 r1.y, V_POSITION, c[a0.x + CV_BONESTART + 1]

dp4 r1.z, V_POSITION, c[a0.x + CV_BONESTART + 2]

// Weight the light part 1

mul r7, c[a0.x + CV_LDIR_LOCALSPACE], V_WEIGHT0.x

// Transform pos with Weight 2

mov a0.x, V_INDICES.y

dp4 r2.x, V_POSITION, c[a0.x + CV_BONESTART + 0]

dp4 r2.y, V_POSITION, c[a0.x + CV_BONESTART + 1]

dp4 r2.z, V_POSITION, c[a0.x + CV_BONESTART + 2]

// Weight the 2 part of the light

mad r8, c[a0.x + CV_LDIR_LOCALSPACE], V_WEIGHT1.x, r7

// Blend

  1、 引言(Introduction)   本文作者   Tomas Arce   2、什麽是逐象素光照(What Is Per-Pixel Lighting)   煩惱的命名。   Bad name   紋素光照處理會更加真實。   Texel-lighting is more accurate.   紋理分辨率的問題 ― 有助于處理貼圖。   Texture resolution matters -- tiling helps.   逐象素基本要做的複雜操作。   Complex operation done per-pixel basis.   紋理查找,三維點光照值生成及象素著色。   DP3Texture lookups, dp3, and pixel shaders.   利用顔色作爲數據。   Use colors as data.   對紋理的象素法向量進行編碼,填充RGB值:   Encode pixel-normals in texture, fill RGB as:   R=Normal.X, G=Normal.Y, B=Normal.Z   也要對光照引索進行編碼,填充頂點顔色:   Light dir is also encoded, fill vertex color as:   R=LightDir.X, G = LightDir.Y, B = LightDir.Z   現在我們可以做:   Now we can do:   ( Texture.Texel ) dot ( Vertex.Color )   3、逐象素爲什麽更好(Why Is Per-Pixel Better)   每頂點光照更快且其更加靈活,但是缺乏分辨率。   Per-vertex light is faster and more flexible but lacks resolution.   光照貼圖有缺陷:   Light-maps have pit-falls:   不真實的鏡面處理。   No real specular.   低分辨率。   Low resolution.   不適于工作在動態對象上。   Doesn't work for dynamic objects.   投射紋理不能獲取更多的表面細節。   Projected textures don't give much detail for the surface.   細節?細節?細節。   Detail. Detail. Detail.   逐象素光照處理需要很少的網格多邊形。   ppLighting needs few polygons per-mesh.   簡化碰撞、模板陰影、存儲。   Simplifies collision, stencil shadows, and memory.   凹凸貼圖是逐象素光照的一個子集。   Bump mapping is a subset of the ppLighting   「法向量貼圖」   「Normal maps」   法向量貼圖可以很好的處理不同類型的光照及表面類型。   Normal maps handle different types of lights and surfaces well.      [url=/bbs/detail_1440380.html][img]http://images.wangchao.net.cn/images/upload/images/lsdn/1211129118961.jpg[/img][/url]   圖1   4、如何存儲紋素法向量(How to Store Normals in Texels)   空間是什麽?   What space?   本地空間對紋理空間。   Local-Space vs Texture Space.   本地空間法向量。   Local Space Normals (LS)   使用對象的坐標的原點提取法向量。   Use the origin of the object to extract normals   紋理空間法向量。   Texture Space Normals (TS).   存儲非普通空間的法向量,例如,正切空間。   Store normals in generic space, e.g., Tangent Space.   每個頂點儲存一個矩陣,表示光線從本地空間到紋理空間的轉換。   Store a matrix per vertex that takes the light from local-space to texture space.      [url=/bbs/detail_1440380.html][img]http://images.wangchao.net.cn/images/upload/images/lsdn/1211129119180.jpg[/img][/url]   Local Space      [url=/bbs/detail_1440380.html][img]http://images.wangchao.net.cn/images/upload/images/lsdn/1211129119289.jpg[/img][/url]   Texture Space   LS原理簡單且處理速度極快。   LS is simple to work with and can be very fast.   但不允許壓縮。   But cannot be compressed.   益用于處理象汽車般的人物與對象。   Good for characters and objects such as cars.   TS原理複雜但其有兩個優點。   TS is more complex to work with but has 2 advantages.   允許圖素貼圖。   Tileable maps.   堆砌紋理。   Palletized textures.   適用于帶細節紋理的地形。   Good for big things like terrain with detail textures.   5、光照的不同類型(Different Types of Lights)   三個典型的光照類型:   3 typical light types:   方向光   Directional   點光源   Point   聚光燈   Spot   點光源、聚光燈通常具有衰減系數。   Point and spot lighting usually have attenuation coefficients.   將聚光燈的衰減函數存在到紋理內。   Store spot lighting attenuation function in a texture.   X是距離的函數。   X is a function of the distance   Y是角度的函數。   Y is a function of the angle   點乘每個點光的方向向量。   The dot-product direction of the light with the vertex   直接在頂點著色內實現衰減函數:(1/(k1+d*k2 +d*d*k3))。   Implementing attenuation function (1/(k1+d*k2 +d*d*k3) ) directly in the vertex shader.   使用少量指令,就可使問題接近零。   Takes few instructions, but has issues going towards zero.   光照矢量通常不需要歸一化,但是…   Light vector normalization is usually not needed but…   使用立方體貼圖查找。   Use cube-map lookup (32x32 or so)   或在象素著色中使用Newton-Raphson方法。   Or use Newton-Raphson approximation in pixel-shader:   mul r0, LightDir, 0.5   dp3 r1, LightDir, LightDir   mad r0, 1-r1, r0, LightDir      [url=/bbs/detail_1440380.html][img]http://images.wangchao.net.cn/images/upload/images/lsdn/1211129119383.gif[/img][/url]   圖3   6、LS對TS的標准壓縮(LS vs TS Standard Comparison)   ; LS Directional Light   ; Transform position to clip space and output it   dp4 oPos.x, V_POSITION, c[CV_WORLDVIEWPROJ_0]   dp4 oPos.y, V_POSITION, c[CV_WORLDVIEWPROJ_1]   dp4 oPos.z, V_POSITION, c[CV_WORLDVIEWPROJ_2]   dp4 oPos.w, V_POSITION, c[CV_WORLDVIEWPROJ_3]   ; Output tex coords   mov oT0, V_TEXTURE   mov oT1, V_TEXTURE   ; TS Directional Light   ; Transform position to clip space and output it   dp4 oPos.x, V_POSITION, c[CV_WORLDVIEWPROJ_0]   dp4 oPos.y, V_POSITION, c[CV_WORLDVIEWPROJ_1]   dp4 oPos.z, V_POSITION, c[CV_WORLDVIEWPROJ_2]   dp4 oPos.w, V_POSITION, c[CV_WORLDVIEWPROJ_3]   ; Transform local space light by basis vectors to put it   ; into texture space   dp3 LIGHT_T.x, V_S, c[L_DIR_LOCAL]   dp3 LIGHT_T.y, V_T, c[L_DIR_LOCAL]   dp3 LIGHT_T.z, V_Q, c[L_DIR_LOCAL]   ; Scale to 0-1   add LIGHT_T, LIGHT_T, c[CV_ONE]   mul oD0, LIGHT_T, c[CV_HALF]   ; Output tex coords   mov oT0, V_TEXTURE   mov oT1, V_TEXTURE   這個例子主要對僵硬的幾何學而言。   This example is mostly for rigid geometry.   最優化TS:11條指令。   Optimized TS: 11 instructions   最優化LS:6條指令。   Optimizes LS: 6 instructions   注意LS能做到5個燈源的數目。   Note that the LS could do up to 5 lights   在本地空間裏所有光源的方向都應作爲象素著色的常量。   All light directions in local space are loaded into the pixel-shader constants   TS使用本地空間裏光源的方向。   The TS uses light direction in local space.   其比基礎矢量多邊形轉換要得多。   Faster than transforming the basis vectors   7、LS對TS的細膩蒙皮(LS vs TS Soft-Skin)      [url=/bbs/detail_1440380.html][img]http://images.wangchao.net.cn/images/upload/images/lsdn/1211129119445.jpg[/img][/url]   圖4   // Transform pos with Weight 1   mov a0.x, V_INDICES.x   dp4 r1.x, V_POSITION, c[a0.x + CV_BONESTART + 0]   dp4 r1.y, V_POSITION, c[a0.x + CV_BONESTART + 1]   dp4 r1.z, V_POSITION, c[a0.x + CV_BONESTART + 2]   // Weight the light part 1   mul r7, c[a0.x + CV_LDIR_LOCALSPACE], V_WEIGHT0.x   // Transform pos with Weight 2   mov a0.x, V_INDICES.y   dp4 r2.x, V_POSITION, c[a0.x + CV_BONESTART + 0]   dp4 r2.y, V_POSITION, c[a0.x + CV_BONESTART + 1]   dp4 r2.z, V_POSITION, c[a0.x + CV_BONESTART + 2]   // Weight the 2 part of the light   mad r8, c[a0.x + CV_LDIR_LOCALSPACE], V_WEIGHT1.x, r7   // Blend
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有