视差贴图(Parallax Mapping)
原理:
PixelShader
//PixelShader
float4 RenderScenePS( VS_OUTPUT In ) :COLOR0
{
// 计算视差贴图的偏移
float3 ViewDir = normalize( In.vLookAt );
float2 OffsetUV = In.TextureUV ;
float3 h = tex2D( MeshHeightSampler ,In.TextureUV ).y ;
OffsetUV += float2( -ViewDir.x , ViewDir.y ) * ( h * 0.04 - 0.02 );
float4 texColor = tex2D( MeshTextureSampler ,OffsetUV );
//
return texColor ;
}
VS_OUTPUT RenderSceneVS( VS_INPUT In )
{
VS_OUTPUT Out = ( VS_OUTPUT )0;
//正常流程变化模型的顶点
float4x4 matWorldView = mul( g_matWorld ,g_matView );
float4x4 matWorldViewProject = mul( matWorldView ,g_matProject );
Out.Position = mul( In.Position , matWorldViewProject );
Out.TextureUV = In.TextureUV;
//----------------- 在此计算一个变换矩阵 ---------------------
// 将世界座标变换到切线空间系的变换矩阵
float3x3 matWorldToModel = float3x3 (
mul( In.Tanget , g_matWorld ).xyz ,
mul( cross( In.Tanget,In.Normal ), g_matWorld ).xyz ,
mul( In.Normal , g_matWorld ).xyz );
//
float4 Position = mul( In.Position ,g_matWorld );
// 每个顶点上的指向观察者的方向
Out.vLookAt = mul ( matWorldToModel ,normalize( Position - g_vEyePosition ));
return Out;
}
陡峭视差贴图Steep Parallax Mapped
原理:
陡峭视差映射的工作方式在下面的图片上举例。深度被分割成8个层,每层的高度值是0.125。每层的纹理座标偏移是V.xy/V.z * scale/numLayers。从顶层黄色方块的位置开始检查,下面是手动计算步骤:1.层的深度为0,高度图深度H(T0)大约为0.75。采样到的深度大于层的深度,所以开始下一次迭代。
2.沿着V方向偏移纹理座标,选定下一层。层深度为0.125,高度图深度H(T1)大约为0.625。采样到的深度大于层的深度,所以开始下一次迭代。
4.沿着V方向偏移纹理座标,选定下一层。层深度为0.375,高度图深度H(T3)大约为0.2。采样到的深度小于层的深度,所以向量V上的当前点在表面之下。我们找到了纹理座标Tp=T3是实际交点的近似点。
vec2 steepPallaxMapping(in vec3 v, in vec2 t)
{
// determine number of layers from angle between V and N
const float minLayers = 5;
const float maxLayers = 15;
float numLayers = mix(maxLayers, minLayers, abs(dot(vec3(0, 0, 1), v)));
// height of each layer
float layerHeight = 1.0 / numLayers;
// depth of current layer
float currentLayerHeight = 0;
// shift of texture coordinates for each iteration
vec2 dtex = gHeightScale * v.xy / v.z / numLayers;
// current texture coordinates
vec2 currentTextureCoords = t;
// get first depth from heightmap
float heightFromTexture = texture(NormTexSampler, currentTextureCoords).a;
// while point is above surface
while(heightFromTexture > currentLayerHeight)
{
// to the next layer
currentLayerHeight += layerHeight;
// shift texture coordinates along vector V
currentTextureCoords -= dtex;
// get new depth from heightmap
heightFromTexture = texture(NormTexSampler, currentTextureCoords).a;
}
return currentTextureCoords;
}
Demo 下载