vec3 T =normalize(vec3(model *vec4(tangent,0.0)));
vec3 N =normalize(vec3(model *vec4(normal,0.0)));// re-orthogonalize T with respect to N
T =normalize(T -dot(T, N)* N);// then retrieve perpendicular vector B with the cross product of T and N
vec3 B =cross(T, N);
mat3 TBN =mat3(T, B, N)
vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir){// number of depth layersconstfloat minLayers =8;constfloat maxLayers =32;float numLayers =mix(maxLayers, minLayers,abs(dot(vec3(0.0,0.0,1.0), viewDir)));// calculate the size of each layerfloat layerDepth =1.0/ numLayers;// depth of current layerfloat currentLayerDepth =0.0;// the amount to shift the texture coordinates per layer (from vector P)
vec2 P = viewDir.xy / viewDir.z * heightScale;
vec2 deltaTexCoords = P / numLayers;// get initial values
vec2 currentTexCoords = texCoords;float currentDepthMapValue =texture(depthMap, currentTexCoords).r;while(currentLayerDepth < currentDepthMapValue){// shift texture coordinates along direction of P
currentTexCoords -= deltaTexCoords;// get depthmap value at current texture coordinates
currentDepthMapValue =texture(depthMap, currentTexCoords).r;// get depth of next layer
currentLayerDepth += layerDepth;}return currentTexCoords;}
vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir){// number of depth layersconstfloat minLayers =8;constfloat maxLayers =32;float numLayers =mix(maxLayers, minLayers,abs(dot(vec3(0.0,0.0,1.0), viewDir)));// calculate the size of each layerfloat layerDepth =1.0/ numLayers;// depth of current layerfloat currentLayerDepth =0.0;// the amount to shift the texture coordinates per layer (from vector P)
vec2 P = viewDir.xy / viewDir.z * heightScale;
vec2 deltaTexCoords = P / numLayers;// get initial values
vec2 currentTexCoords = texCoords;float currentDepthMapValue =texture(depthMap, currentTexCoords).r;while(currentLayerDepth < currentDepthMapValue){// shift texture coordinates along direction of P
currentTexCoords -= deltaTexCoords;// get depthmap value at current texture coordinates
currentDepthMapValue =texture(depthMap, currentTexCoords).r;// get depth of next layer
currentLayerDepth += layerDepth;}// get texture coordinates before collision (reverse operations)
vec2 prevTexCoords = currentTexCoords + deltaTexCoords;// get depth after and before collision for linear interpolationfloat afterDepth = currentDepthMapValue - currentLayerDepth;float beforeDepth =texture(depthMap, prevTexCoords).r - currentLayerDepth + layerDepth;// interpolation of texture coordinatesfloat weight = afterDepth /(afterDepth - beforeDepth);
vec2 finalTexCoords = prevTexCoords * weight + currentTexCoords *(1.0- weight);return finalTexCoords;}