Skip to content

Commit 98c0f9d

Browse files
authored
Merge pull request #2365 from jMonkeyEngine/yaRnMcDonuts-patch-7
Fix Rendering Issue in PBRTerrain
2 parents 1f31c14 + b8277ef commit 98c0f9d

File tree

5 files changed

+53
-30
lines changed

5 files changed

+53
-30
lines changed

jme3-core/src/main/resources/Common/MatDefs/Light/PBRLighting.vert

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ attribute vec3 inNormal;
3030
varying vec3 wNormal;
3131
varying vec3 wPosition;
3232

33-
varying vec4 lPosition;
33+
varying vec3 lPosition;
3434

3535
attribute vec4 inTangent;
3636
varying vec4 wTangent;
@@ -46,7 +46,7 @@ void main(){
4646
vec3 modelSpaceNorm = inNormal;
4747
vec3 modelSpaceTan = inTangent.xyz;
4848

49-
lPosition = modelSpacePos;
49+
lPosition = modelSpacePos.xyz;
5050

5151

5252
#ifdef USE_VERTEX_COLORS_AS_SUN_INTENSITY
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
#ifndef __TANGENT_UTILS_MODULE__
22
#define __TANGENT_UTILS_MODULE__
33

4-
//used for calculating tangents in-shader
5-
6-
7-
//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)
8-
vec3 calculateTangentsAndApplyToNormals(in vec3 normalIn, in vec3 worldNorm){
9-
vec3 returnNorm = normalize((normalIn.xyz * vec3(2.0) - vec3(1.0)));
10-
4+
//used for calculating tangents in-shader for axis-aligned quads/planes/terrains
5+
//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)
6+
vec3 calculateTangentsAndApplyToNormals(in vec3 normalIn, in vec3 worldNorm){
117
vec3 baseNorm = worldNorm.rgb + vec3(0, 0, 1);
12-
returnNorm *= vec3(-1, -1, 1);
13-
returnNorm = baseNorm.rgb*dot(baseNorm.rgb, returnNorm.rgb)/baseNorm.z - returnNorm.rgb;
14-
15-
returnNorm = normalize(returnNorm);
8+
normalIn = baseNorm.rgb*dot(baseNorm.rgb, normalIn.rgb)/baseNorm.z - normalIn.rgb;
9+
normalIn = normalize(normalIn);
1610

17-
18-
return returnNorm;
11+
return normalIn;
1912
}
20-
21-
#endif
13+
#endif
Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef __TRIPLANAR_UTILS_MODULE__
22
#define __TRIPLANAR_UTILS_MODULE__
33

4-
vec3 triBlending;
5-
4+
vec3 triBlending;
5+
66
void TriPlanarUtils_calculateBlending(vec3 geometryNormal){
77
triBlending = abs( geometryNormal );
88
triBlending = (triBlending -0.2) * 0.7;
@@ -11,7 +11,8 @@
1111
triBlending /= vec3(b, b, b);
1212
}
1313

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

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

3133
return tex;
32-
}
33-
34-
#endif
34+
}
35+
36+
// triplanar blend for Normal maps:
37+
vec4 getTriPlanarNormalBlend(in vec3 coords, in sampler2D map, in float scale) {
38+
vec4 col1 = texture2D( map, coords.yz * scale);
39+
vec4 col2 = texture2D( map, coords.xz * scale);
40+
vec4 col3 = texture2D( map, coords.xy * scale);
41+
42+
col1.xyz = col1.xyz * vec3(2.0) - vec3(1.0);
43+
col2.xyz = col2.xyz * vec3(2.0) - vec3(1.0);
44+
col3.xyz = col3.xyz * vec3(2.0) - vec3(1.0);
45+
46+
// blend the results of the 3 planar projections.
47+
vec4 tex = normalize(col1 * triBlending.x + col2 * triBlending.y + col3 * triBlending.z);
48+
49+
return tex;
50+
}
51+
52+
// triplanar blend for Normal maps in a TextureArray:
53+
vec4 getTriPlanarNormalBlendFromTexArray(in vec3 coords, in int idInTexArray, in float scale, in sampler2DArray texArray) {
54+
vec4 col1 = texture2DArray( texArray, vec3((coords.yz * scale), idInTexArray ) );
55+
vec4 col2 = texture2DArray( texArray, vec3((coords.xz * scale), idInTexArray ) );
56+
vec4 col3 = texture2DArray( texArray, vec3((coords.xy * scale), idInTexArray ) );
57+
58+
col1.xyz = col1.xyz * vec3(2.0) - vec3(1.0);
59+
col2.xyz = col2.xyz * vec3(2.0) - vec3(1.0);
60+
col3.xyz = col3.xyz * vec3(2.0) - vec3(1.0);
61+
62+
// blend the results of the 3 planar projections.
63+
vec4 tex = normalize(col1 * triBlending.x + col2 * triBlending.y + col3 * triBlending.z);
64+
65+
return tex;
66+
}
67+
68+
#endif

jme3-core/src/main/resources/Common/ShaderLib/module/pbrlighting/PBRLightingUtils.glsllib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#endif
4242

4343
#if defined(ENABLE_PBRLightingUtils_getLocalPosition)
44-
varying vec4 lPosition;
44+
varying vec3 lPosition;
4545
#endif
4646

4747

jme3-terrain/src/main/resources/Common/MatDefs/Terrain/PBRTerrain.vert

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ attribute vec2 inTexCoord;
88
varying vec2 texCoord;
99
varying vec3 wPosition;
1010
varying vec3 wNormal;
11-
11+
varying vec3 lPosition;
1212

1313
uniform vec4 g_AmbientLightColor;
1414

@@ -17,9 +17,6 @@ uniform vec4 g_AmbientLightColor;
1717
uniform vec3 g_CameraPosition;
1818
#endif
1919

20-
21-
varying vec4 lPosition;
22-
2320
void main(){
2421
vec4 modelSpacePos = vec4(inPosition, 1.0);
2522

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

3229
wNormal = normalize(TransformWorldNormal(inNormal));
3330

34-
lPosition = vec4(inPosition, 0.0);
31+
lPosition = modelSpacePos.xyz;
3532

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

0 commit comments

Comments
 (0)