Skip to content

Commit

Permalink
Fix Rendering Issue in PBRTerrain
Browse files Browse the repository at this point in the history
This PR attempts to fix a rendering bug that a few jme have experienced on certain devices, as discussed in this thread:
https://hub.jmonkeyengine.org/t/requesting-help-troubleshooting-pbrterrain-bug/47895

I believe the issue was due to a combination of things that were wrong with the process of reading normal maps, calculating tangents, and then blending them.

The order of these operations is important, and I believe it is also important to normalize the normal value prior to blending each layer, which I previously was not doing.

There's also some code that puts a normal map in the proper range, and this was being done improperly for tri-planar normal mapping.

Once this PR is done I'll post back to that thread and request testing from the users who were experiencing the bug on their device.
  • Loading branch information
yaRnMcDonuts authored Feb 6, 2025
1 parent 1f31c14 commit 863646a
Showing 1 changed file with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef __TRIPLANAR_UTILS_MODULE__
#define __TRIPLANAR_UTILS_MODULE__

vec3 triBlending;
vec3 triBlending;

void TriPlanarUtils_calculateBlending(vec3 geometryNormal){
triBlending = abs( geometryNormal );
triBlending = (triBlending -0.2) * 0.7;
Expand All @@ -11,7 +11,8 @@
triBlending /= vec3(b, b, b);
}

vec4 getTriPlanarBlend(in vec4 coords, in sampler2D map, in float scale) {
// basic triplanar blend:
vec4 getTriPlanarBlend(in vec3 coords, in sampler2D map, in float scale) {
vec4 col1 = texture2D( map, coords.yz * scale);
vec4 col2 = texture2D( map, coords.xz * scale);
vec4 col3 = texture2D( map, coords.xy * scale);
Expand All @@ -21,14 +22,47 @@
return tex;
}

vec4 getTriPlanarBlendFromTexArray(in vec4 coords, in int idInTexArray, in float scale, in sampler2DArray texArray) {
// basic triplanar blend for TextureArrays:
vec4 getTriPlanarBlendFromTexArray(in vec3 coords, in int idInTexArray, in float scale, in sampler2DArray texArray) {
vec4 col1 = texture2DArray( texArray, vec3((coords.yz * scale), idInTexArray ) );
vec4 col2 = texture2DArray( texArray, vec3((coords.xz * scale), idInTexArray ) );
vec4 col3 = texture2DArray( texArray, vec3((coords.xy * scale), idInTexArray ) );
// blend the results of the 3 planar projections.
vec4 tex = col1 * triBlending.x + col2 * triBlending.y + col3 * triBlending.z;

return tex;
}

#endif
}

// triplanar blend for Normal maps:
vec4 getTriPlanarNormalBlend(in vec3 coords, in sampler2D map, in float scale) {
vec4 col1 = texture2D( map, coords.yz * scale);
vec4 col2 = texture2D( map, coords.xz * scale);
vec4 col3 = texture2D( map, coords.xy * scale);

col1.xyz = col1.xyz * vec3(2.0) - vec3(1.0);
col2.xyz = col2.xyz * vec3(2.0) - vec3(1.0);
col3.xyz = col3.xyz * vec3(2.0) - vec3(1.0);

// blend the results of the 3 planar projections.
vec4 tex = normalize(col1 * triBlending.x + col2 * triBlending.y + col3 * triBlending.z);

return tex;
}

// triplanar blend for Normal maps in a TextureArray:
vec4 getTriPlanarNormalBlendFromTexArray(in vec3 coords, in int idInTexArray, in float scale, in sampler2DArray texArray) {
vec4 col1 = texture2DArray( texArray, vec3((coords.yz * scale), idInTexArray ) );
vec4 col2 = texture2DArray( texArray, vec3((coords.xz * scale), idInTexArray ) );
vec4 col3 = texture2DArray( texArray, vec3((coords.xy * scale), idInTexArray ) );

col1.xyz = col1.xyz * vec3(2.0) - vec3(1.0);
col2.xyz = col2.xyz * vec3(2.0) - vec3(1.0);
col3.xyz = col3.xyz * vec3(2.0) - vec3(1.0);

// blend the results of the 3 planar projections.
vec4 tex = normalize(col1 * triBlending.x + col2 * triBlending.y + col3 * triBlending.z);

return tex;
}

#endif

0 comments on commit 863646a

Please sign in to comment.