mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-21 17:54:58 +01:00
fix(shaders): parallax mapping of the opengl33 materials is shared by the four parallax shaders in parallax.glsl and samples its depth with the gradients of the surface instead of letting a loop of varying length pick the mip level, picks the number of layers by the angle of view alone and refines the hit with a binary search instead of one linear step, so stones near the camera no longer show combs of layer slices; it fades out between 60 and 100 m instead of stopping at a hard line and fades out at grazing angles, and the simple offset without extra effects no longer divides by an unbounded cosine of the view
This commit is contained in:
45
shaders/parallax.glsl
Normal file
45
shaders/parallax.glsl
Normal file
@@ -0,0 +1,45 @@
|
||||
vec2 ParallaxMapping(vec2 f_coord, vec3 viewDir, float scale, float offset)
|
||||
{
|
||||
vec2 dx = dFdx(f_coord);
|
||||
vec2 dy = dFdy(f_coord);
|
||||
float fade = (1.0 - smoothstep(60.0, 100.0, length(f_pos.xyz))) * smoothstep(0.0, 0.1, viewDir.z);
|
||||
if (fade <= 0.0) {
|
||||
return f_coord;
|
||||
}
|
||||
|
||||
#if EXTRAEFFECTS_ENABLED
|
||||
const float minLayers = 8.0;
|
||||
const float maxLayers = 32.0;
|
||||
float numLayers = mix(maxLayers, minLayers, clamp(viewDir.z, 0.0, 1.0));
|
||||
float layerDepth = 1.0 / numLayers;
|
||||
vec2 deltaTexCoords = viewDir.xy * scale * fade / numLayers;
|
||||
vec2 currentTexCoords = f_coord;
|
||||
float currentLayerDepth = 0.0;
|
||||
float currentDepthMapValue = textureGrad(normalmap, currentTexCoords, dx, dy).b;
|
||||
for (int i = 0; i < 40 && currentLayerDepth < currentDepthMapValue; ++i)
|
||||
{
|
||||
currentTexCoords -= deltaTexCoords;
|
||||
currentLayerDepth += layerDepth;
|
||||
currentDepthMapValue = textureGrad(normalmap, currentTexCoords, dx, dy).b;
|
||||
}
|
||||
for (int i = 0; i < 5; ++i)
|
||||
{
|
||||
deltaTexCoords *= 0.5;
|
||||
layerDepth *= 0.5;
|
||||
if (currentLayerDepth < currentDepthMapValue) {
|
||||
currentTexCoords -= deltaTexCoords;
|
||||
currentLayerDepth += layerDepth;
|
||||
}
|
||||
else {
|
||||
currentTexCoords += deltaTexCoords;
|
||||
currentLayerDepth -= layerDepth;
|
||||
}
|
||||
currentDepthMapValue = textureGrad(normalmap, currentTexCoords, dx, dy).b;
|
||||
}
|
||||
return currentTexCoords;
|
||||
#else
|
||||
float height = textureGrad(normalmap, f_coord, dx, dy).b;
|
||||
vec2 p = viewDir.xy / max(viewDir.z, 0.2) * (height * (scale - offset) * 0.2 * fade);
|
||||
return f_coord - p;
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user