Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Rendering Issue in PBRTerrain #2365

Merged
merged 6 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ attribute vec3 inNormal;
varying vec3 wNormal;
varying vec3 wPosition;

varying vec4 lPosition;
varying vec3 lPosition;

attribute vec4 inTangent;
varying vec4 wTangent;
Expand All @@ -46,7 +46,7 @@ void main(){
vec3 modelSpaceNorm = inNormal;
vec3 modelSpaceTan = inTangent.xyz;

lPosition = modelSpacePos;
lPosition = modelSpacePos.xyz;


#ifdef USE_VERTEX_COLORS_AS_SUN_INTENSITY
Expand Down
22 changes: 7 additions & 15 deletions jme3-core/src/main/resources/Common/ShaderLib/TangentUtils.glsllib
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
#ifndef __TANGENT_UTILS_MODULE__
#define __TANGENT_UTILS_MODULE__

//used for calculating tangents in-shader


//primarily used for terrains, since jme terrains do not store pre-calculated tangents by default (thus the tbnMat cannot be used for PBR light calculation like it is in jme's stock PBR shader)
vec3 calculateTangentsAndApplyToNormals(in vec3 normalIn, in vec3 worldNorm){
vec3 returnNorm = normalize((normalIn.xyz * vec3(2.0) - vec3(1.0)));

//used for calculating tangents in-shader for axis-aligned quads/planes/terrains
//primarily used for PBR terrains, since jme terrains do not store pre-calculated tangents by default (thus the tbnMat cannot be used for PBR light calculation like it is in jme's stock PBR shader)
vec3 calculateTangentsAndApplyToNormals(in vec3 normalIn, in vec3 worldNorm){
vec3 baseNorm = worldNorm.rgb + vec3(0, 0, 1);
returnNorm *= vec3(-1, -1, 1);
returnNorm = baseNorm.rgb*dot(baseNorm.rgb, returnNorm.rgb)/baseNorm.z - returnNorm.rgb;

returnNorm = normalize(returnNorm);
normalIn = baseNorm.rgb*dot(baseNorm.rgb, normalIn.rgb)/baseNorm.z - normalIn.rgb;
normalIn = normalize(normalIn);


return returnNorm;
return normalIn;
}

#endif
#endif
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#endif

#if defined(ENABLE_PBRLightingUtils_getLocalPosition)
varying vec4 lPosition;
varying vec3 lPosition;
#endif


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ attribute vec2 inTexCoord;
varying vec2 texCoord;
varying vec3 wPosition;
varying vec3 wNormal;

varying vec3 lPosition;

uniform vec4 g_AmbientLightColor;

Expand All @@ -17,9 +17,6 @@ uniform vec4 g_AmbientLightColor;
uniform vec3 g_CameraPosition;
#endif


varying vec4 lPosition;

void main(){
vec4 modelSpacePos = vec4(inPosition, 1.0);

Expand All @@ -31,7 +28,7 @@ void main(){

wNormal = normalize(TransformWorldNormal(inNormal));

lPosition = vec4(inPosition, 0.0);
lPosition = modelSpacePos.xyz;

#ifdef USE_FOG
fogDistance = distance(g_CameraPosition, (g_WorldMatrix * modelSpacePos).xyz);
Expand Down