Skip to content

Commit 75f0b73

Browse files
authored
Merge pull request #2377 from jMonkeyEngine/yaRnMcDonuts-patch-9
Finish Modularizing PBRTerrainUtils.glsllb
2 parents dd76451 + 3c5eb5c commit 75f0b73

File tree

6 files changed

+246
-740
lines changed

6 files changed

+246
-740
lines changed

jme3-core/src/main/resources/Common/ShaderLib/TriPlanarUtils.glsllib

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef __TRIPLANAR_UTILS_MODULE__
22
#define __TRIPLANAR_UTILS_MODULE__
33

4+
#ifndef NORMAL_TYPE
5+
#define NORMAL_TYPE -1.0
6+
#endif
7+
48
vec3 triBlending;
59

610
void TriPlanarUtils_calculateBlending(vec3 geometryNormal){
@@ -39,9 +43,9 @@
3943
vec4 col2 = texture2D( map, coords.xz * scale);
4044
vec4 col3 = texture2D( map, coords.xy * scale);
4145

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);
46+
col1.xyz = col1.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0);
47+
col2.xyz = col2.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0);
48+
col3.xyz = col3.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0);
4549

4650
// blend the results of the 3 planar projections.
4751
vec4 tex = normalize(col1 * triBlending.x + col2 * triBlending.y + col3 * triBlending.z);
@@ -51,16 +55,16 @@
5155

5256
// triplanar blend for Normal maps in a TextureArray:
5357
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 ) );
58+
vec4 col1 = texture2DArray( texArray, vec3((coords.yz * scale), idInTexArray ));
59+
vec4 col2 = texture2DArray( texArray, vec3((coords.xz * scale), idInTexArray ));
60+
vec4 col3 = texture2DArray( texArray, vec3((coords.xy * scale), idInTexArray ));
5761

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);
62+
col1.xyz = col1.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0);
63+
col2.xyz = col2.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0);
64+
col3.xyz = col3.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0);
6165

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

6569
return tex;
6670
}

jme3-terrain/src/main/resources/Common/MatDefs/Terrain/AdvancedPBRTerrain.frag

+48-26
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void main(){
4949
// read and blend up to 12 texture layers
5050
#for i=0..12 (#ifdef ALBEDOMAP_$i $0 #endif)
5151

52-
PBRTerrainTextureLayer terrainTextureLayer_$i = PBRTerrainUtils_createAdvancedPBRTerrainLayer($i);
52+
PBRTerrainTextureLayer terrainTextureLayer_$i = PBRTerrainUtils_createAdvancedPBRTerrainLayer($i, surface.geometryNormal);
5353

5454
#ifdef USE_FIRST_LAYER_AS_TRANSPARENCY
5555
if($i == 0){
@@ -59,29 +59,51 @@ void main(){
5959
}
6060
#endif
6161

62+
terrainTextureLayer_$i.roughness = m_Roughness_$i;
63+
terrainTextureLayer_$i.metallic = m_Metallic_$i;
6264
terrainTextureLayer_$i.emission = m_EmissiveColor_$i;
63-
64-
#if defined(TRI_PLANAR_MAPPING) || defined(TRI_PLANAR_MAPPING_$i)
65-
//triplanar:
66-
67-
PBRTerrainUtils_readTriPlanarAlbedoTexArray(ALBEDOMAP_$i, m_AlbedoMap_$i_scale, m_AlbedoTextureArray, terrainTextureLayer_$i);
68-
#ifdef NORMALMAP_$i
69-
PBRTerrainUtils_readTriPlanarNormalTexArray(NORMALMAP_$i, m_AlbedoMap_$i_scale, m_NormalParallaxTextureArray, terrainTextureLayer_$i);
70-
#endif
71-
#ifdef METALLICROUGHNESSMAP_$i
72-
PBRTerrainUtils_readTriPlanarMetallicRoughnessAoEiTexArray(METALLICROUGHNESSMAP_$i, m_AlbedoMap_$i_scale, m_MetallicRoughnessAoEiTextureArray, terrainTextureLayer_$i);
73-
#endif
74-
#else
75-
//non tri-planar:
76-
77-
PBRTerrainUtils_readAlbedoTexArray(ALBEDOMAP_$i, m_AlbedoMap_$i_scale, m_AlbedoTextureArray, terrainTextureLayer_$i);
78-
#ifdef NORMALMAP_$i
79-
PBRTerrainUtils_readNormalTexArray(NORMALMAP_$i, m_AlbedoMap_$i_scale, m_NormalParallaxTextureArray, terrainTextureLayer_$i);
80-
#endif
81-
#ifdef METALLICROUGHNESSMAP_$i
82-
PBRTerrainUtils_readMetallicRoughnessAoEiTexArray(METALLICROUGHNESSMAP_$i, m_AlbedoMap_$i_scale, m_MetallicRoughnessAoEiTextureArray, terrainTextureLayer_$i);
83-
#endif
84-
#endif
65+
66+
#ifdef USE_TEXTURE_ARRAYS
67+
#if defined(TRI_PLANAR_MAPPING) || defined(TRI_PLANAR_MAPPING_$i)
68+
//triplanar for texture arrays:
69+
PBRTerrainUtils_readTriPlanarAlbedoTexArray(m_AlbedoMap_$i, m_AlbedoMap_$i_scale, m_AlbedoTextureArray, terrainTextureLayer_$i);
70+
#ifdef NORMALMAP_$i
71+
PBRTerrainUtils_readTriPlanarNormalTexArray(m_NormalMap_$i, m_AlbedoMap_$i_scale, m_NormalParallaxTextureArray, terrainTextureLayer_$i);
72+
#endif
73+
#ifdef METALLICROUGHNESSMAP_$i
74+
PBRTerrainUtils_readTriPlanarMetallicRoughnessAoEiTexArray(m_MetallicRoughnessMap_$i, m_AlbedoMap_$i_scale, m_MetallicRoughnessAoEiTextureArray, terrainTextureLayer_$i);
75+
#endif
76+
#else
77+
//non tri-planar for texture arrays:
78+
PBRTerrainUtils_readAlbedoTexArray(m_AlbedoMap_$i, m_AlbedoMap_$i_scale, m_AlbedoTextureArray, terrainTextureLayer_$i);
79+
#ifdef NORMALMAP_$i
80+
PBRTerrainUtils_readNormalTexArray(m_NormalMap_$i, m_AlbedoMap_$i_scale, m_NormalParallaxTextureArray, terrainTextureLayer_$i);
81+
#endif
82+
#ifdef METALLICROUGHNESSMAP_$i
83+
PBRTerrainUtils_readMetallicRoughnessAoEiTexArray(m_MetallicRoughnessMap_$i, m_AlbedoMap_$i_scale, m_MetallicRoughnessAoEiTextureArray, terrainTextureLayer_$i);
84+
#endif
85+
#endif
86+
#else
87+
#if defined(TRI_PLANAR_MAPPING) || defined(TRI_PLANAR_MAPPING_$i)
88+
//triplanar texture reads:
89+
PBRTerrainUtils_readTriPlanarAlbedoTexture(m_AlbedoMap_$i, m_AlbedoMap_$i_scale, terrainTextureLayer_$i);
90+
#ifdef NORMALMAP_$i
91+
PBRTerrainUtils_readTriPlanarNormalTexture(m_NormalMap_$i, m_AlbedoMap_$i_scale, terrainTextureLayer_$i);
92+
#endif
93+
#ifdef METALLICROUGHNESSMAP_$i
94+
PBRTerrainUtils_readTriPlanarMetallicRoughnessAoEiTexture(m_MetallicRoughnessMap_$i, m_AlbedoMap_$i_scale, terrainTextureLayer_$i);
95+
#endif
96+
#else
97+
//non tri-planar texture reads:
98+
PBRTerrainUtils_readAlbedoTexture(m_AlbedoMap_$i, m_AlbedoMap_$i_scale, terrainTextureLayer_$i);
99+
#ifdef NORMALMAP_$i
100+
PBRTerrainUtils_readNormalTexture(m_NormalMap_$i, m_AlbedoMap_$i_scale, terrainTextureLayer_$i);
101+
#endif
102+
#ifdef METALLICROUGHNESSMAP_$i
103+
PBRTerrainUtils_readMetallicRoughnessAoEiTexture(m_MetallicRoughnessMap_$i, m_AlbedoMap_$i_scale, terrainTextureLayer_$i);
104+
#endif
105+
#endif
106+
#endif
85107

86108
//CUSTOM LIB EXAMPLE: uses a custom alpha map to desaturate albedo color for a color-removal effect
87109
#ifdef AFFLICTIONTEXTURE
@@ -129,14 +151,14 @@ void main(){
129151
gl_FragColor.rgb += surface.directLightContribution;
130152
gl_FragColor.rgb += surface.envLightContribution;
131153
gl_FragColor.rgb += surface.emission;
132-
gl_FragColor.a = surface.alpha;
133-
154+
gl_FragColor.a = surface.alpha;
155+
134156
#ifdef USE_FOG
135157
gl_FragColor = MaterialFog_calculateFogColor(vec4(gl_FragColor));
136158
#endif
137159

138160
//outputs the final value of the selected layer as a color for debug purposes.
139161
#ifdef DEBUG_VALUES_MODE
140162
gl_FragColor = PBRLightingUtils_getColorOutputForDebugMode(m_DebugValuesMode, vec4(gl_FragColor.rgba), surface);
141-
#endif
163+
#endif
142164
}

jme3-terrain/src/main/resources/Common/MatDefs/Terrain/AdvancedPBRTerrain.j3md

+14-23
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ MaterialDef AdvancedPBRTerrain {
44
Int BoundDrawBuffer
55

66
Texture2D SunLightExposureMap
7-
Boolean UseVertexColorsAsSunIntensity //set true to make the vertex color's R channel how exposed a vertex is to the sun
8-
Float StaticSunIntensity //used for setting the sun exposure value for a whole material
7+
Boolean UseVertexColorsAsSunExposure //set true to make the vertex color's R channel how exposed a vertex is to the sun
8+
Float StaticSunExposure //used for setting the sun exposure value for a whole material
99
//these are usually generated at run time or setup in a level editor per-geometry, so that models indoors can have the DirectionalLight dimmed accordingly.
1010

1111
Boolean BrightenIndoorShadows //set true if shadows are enabled and indoor areas without full sun exposure are too dark compared to when shadows are turned off in settings
@@ -16,6 +16,9 @@ MaterialDef AdvancedPBRTerrain {
1616
TextureArray NormalParallaxTextureArray -LINEAR
1717
TextureArray MetallicRoughnessAoEiTextureArray -LINEAR
1818

19+
//The type of normal map: -1.0 (DirectX), 1.0 (OpenGl)
20+
Float NormalType : -1.0
21+
1922
// Specular-AA
2023
Boolean UseSpecularAA : true
2124
// screen space variance,Use the slider to set the strength of the geometric specular anti-aliasing effect between 0 and 1. Higher values produce a blurrier result with less aliasing.
@@ -28,7 +31,6 @@ MaterialDef AdvancedPBRTerrain {
2831
Float AfflictionMetallicValue : 0.0
2932
Float AfflictionEmissiveValue : 0.0 //note that this is simplified into one value, rather than 2 with power and intensity like the regular pbr values.
3033

31-
3234
// affliction texture splatting & desaturation functionality
3335
Boolean UseTriplanarAfflictionMapping
3436

@@ -43,7 +45,6 @@ MaterialDef AdvancedPBRTerrain {
4345

4446
Float SplatNoiseVar
4547

46-
4748
Int AfflictionMode_0 : 1
4849
Int AfflictionMode_1 : 1
4950
Int AfflictionMode_2 : 1
@@ -109,7 +110,6 @@ MaterialDef AdvancedPBRTerrain {
109110
Int AlbedoMap_10
110111
Int AlbedoMap_11
111112

112-
113113
Float AlbedoMap_0_scale : 1
114114
Float AlbedoMap_1_scale : 1
115115
Float AlbedoMap_2_scale : 1
@@ -136,7 +136,6 @@ MaterialDef AdvancedPBRTerrain {
136136
Boolean UseTriPlanarMapping_10
137137
Boolean UseTriPlanarMapping_11
138138

139-
140139
Int NormalMap_0
141140
Int NormalMap_1
142141
Int NormalMap_2
@@ -150,7 +149,6 @@ MaterialDef AdvancedPBRTerrain {
150149
Int NormalMap_10
151150
Int NormalMap_11
152151

153-
154152
Int MetallicRoughnessMap_0
155153
Int MetallicRoughnessMap_1
156154
Int MetallicRoughnessMap_2
@@ -164,7 +162,6 @@ MaterialDef AdvancedPBRTerrain {
164162
Int MetallicRoughnessMap_10
165163
Int MetallicRoughnessMap_11
166164

167-
168165
Float ParallaxHeight_0
169166
Float ParallaxHeight_1
170167
Float ParallaxHeight_2
@@ -178,13 +175,11 @@ MaterialDef AdvancedPBRTerrain {
178175
Float ParallaxHeight_10
179176
Float ParallaxHeight_11
180177

181-
182-
183178
//used in order to convert world coords to tex coords so afflictionTexture accurately represents the world in cases where terrain is not scaled at a 1,1,1 value
184179
Float TileWidth : 0
185180
Vector3 TileLocation
186181

187-
// debug the final value of the selected layer as a color output
182+
// debug the final value of the selected layer as a color output
188183
Int DebugValuesMode
189184
// Layers:
190185
// 0 - albedo (unshaded)
@@ -195,6 +190,7 @@ MaterialDef AdvancedPBRTerrain {
195190
// 5 - emissive
196191
// 6 - exposure
197192
// 7 - alpha
193+
// 8 - geometryNormals
198194

199195
// use tri-planar mapping
200196
Boolean useTriPlanarMapping
@@ -204,13 +200,6 @@ MaterialDef AdvancedPBRTerrain {
204200
Texture2D AlphaMap_1 -LINEAR
205201
Texture2D AlphaMap_2 -LINEAR
206202

207-
Boolean UseSpecGloss
208-
Texture2D SpecularMap
209-
Texture2D GlossinessMap
210-
Texture2D SpecularGlossinessMap
211-
Color Specular : 1.0 1.0 1.0 1.0
212-
Float Glossiness : 1.0
213-
214203
Vector4 ProbeData
215204

216205
// Prefiltered Env Map for indirect specular lighting
@@ -222,7 +211,6 @@ MaterialDef AdvancedPBRTerrain {
222211
//integrate BRDF map for indirect Lighting
223212
Texture2D IntegrateBRDF -LINEAR
224213

225-
226214
//shadows
227215
Int FilterMode
228216
Boolean HardwareShadows
@@ -289,7 +277,6 @@ MaterialDef AdvancedPBRTerrain {
289277
ViewProjectionMatrix
290278
ViewMatrix
291279
Time
292-
293280
}
294281

295282
Defines {
@@ -301,10 +288,12 @@ MaterialDef AdvancedPBRTerrain {
301288
FOG_EXPSQ : ExpSqFog
302289

303290
EXPOSUREMAP : SunLightExposureMap
304-
USE_VERTEX_COLORS_AS_SUN_EXPOSURE : UseVertexColorsAsSunIntensity
305-
STATIC_SUN_EXPOSURE : StaticSunIntensity
291+
USE_VERTEX_COLORS_AS_SUN_EXPOSURE : UseVertexColorsAsSunExposure
292+
STATIC_SUN_EXPOSURE : StaticSunExposure
306293
BRIGHTEN_INDOOR_SHADOWS : BrightenIndoorShadows
307294

295+
NORMAL_TYPE: NormalType
296+
308297
USE_FIRST_LAYER_AS_TRANSPARENCY : UseFirstLayerAsTransparency
309298

310299
SPECULAR_AA : UseSpecularAA
@@ -323,6 +312,8 @@ MaterialDef AdvancedPBRTerrain {
323312
AFFLICTIONEMISSIVEMAP : SplatEmissiveMap
324313
USE_SPLAT_NOISE : SplatNoiseVar
325314

315+
USE_TRIPLANAR_AFFLICTION_MAPPING : UseTriplanarAfflictionMapping
316+
326317
TRI_PLANAR_MAPPING : useTriPlanarMapping
327318

328319
ALPHAMAP : AlphaMap
@@ -383,10 +374,10 @@ MaterialDef AdvancedPBRTerrain {
383374

384375
DEBUG_VALUES_MODE : DebugValuesMode
385376

377+
USE_TEXTURE_ARRAYS : AlbedoTextureArray
386378
}
387379
}
388380

389-
390381
Technique PreShadow {
391382

392383
VertexShader GLSL300 GLSL150 GLSL100 : Common/MatDefs/Shadow/PreShadow.vert

0 commit comments

Comments
 (0)