实际案例:bullet holes
基本原理就是偏移当前像素UV,形成高度差。
基本公式:UV + ViewDir.xy *( depth * scale + bias) / viewDir.z;
(ViewDir是贴图空间, depth为深度图中储存值,scale看实际的最大深度,比如(贴图覆盖2*2M的范围,并且最大高度差为0.02M,则scale为0.02/2 = 0.01), bias不固定,得看实际情况,一般情况下,-0.01会看起来下陷一些,而+0.01就抬高一些。)
reliefMap是指NormalMap中w值为depth。
steepMap技术是指更细微的搜索实际深度。(其中currPoint即为上公式所求值,currHeight为1.0, tStep为currPoint / 25, hStep = 1 / 25, iStep = 25)
float TraceLinear(sampler2D m_HeightMap, inout vec2 CurrPoint, inout float CurrHeight, vec2 tStep, float hStep, float mipLevel, float iSteps, inout float Intersected) { float tHeight = 0.0; for(float i = 0.0; i < iSteps; i+= 1.0) { // The trace will stop at the point before the actuall intersection with m_HeightMap field happens tHeight = 1.0f - texture2DLod( m_HeightMap, CurrPoint - tStep, mipLevel ).x; if( (CurrHeight - hStep > tHeight) && (CurrHeight > 0) ) { CurrHeight -= hStep; CurrPoint -= tStep; } else { Intersected = 1.0f; i = iSteps; } } return tHeight; }
JME3 lighting.frag例子
float h; h = texture2D(m_ParallaxMap, texCoord).r; h = texture2D(m_NormalMap, texCoord).a; float heightScale = 0.05; float heightBias = heightScale * -0.5; vec3 normView = normalize(vViewDir); // vViewDir为贴图空间 h = (h * heightScale + heightBias) * normView.z; newTexCoord = texCoord + (h * -normView.xy);