diff --git a/jme3-core/src/main/resources/Common/ShaderLib/TriPlanarUtils.glsllib b/jme3-core/src/main/resources/Common/ShaderLib/TriPlanarUtils.glsllib index 7b79a4181b..8134200ae3 100644 --- a/jme3-core/src/main/resources/Common/ShaderLib/TriPlanarUtils.glsllib +++ b/jme3-core/src/main/resources/Common/ShaderLib/TriPlanarUtils.glsllib @@ -1,6 +1,10 @@ #ifndef __TRIPLANAR_UTILS_MODULE__ #define __TRIPLANAR_UTILS_MODULE__ + #ifndef NORMAL_TYPE + #define NORMAL_TYPE -1.0 + #endif + vec3 triBlending; void TriPlanarUtils_calculateBlending(vec3 geometryNormal){ @@ -39,9 +43,9 @@ 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); + col1.xyz = col1.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0); + col2.xyz = col2.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0); + col3.xyz = col3.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0); // blend the results of the 3 planar projections. vec4 tex = normalize(col1 * triBlending.x + col2 * triBlending.y + col3 * triBlending.z); @@ -51,16 +55,16 @@ // 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 ) ); + 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); + col1.xyz = col1.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0); + col2.xyz = col2.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0); + col3.xyz = col3.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0); // blend the results of the 3 planar projections. - vec4 tex = normalize(col1 * triBlending.x + col2 * triBlending.y + col3 * triBlending.z); + vec4 tex = normalize(col1 * triBlending.x + col2 * triBlending.y + col3 * triBlending.z); return tex; } diff --git a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/AdvancedPBRTerrain.frag b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/AdvancedPBRTerrain.frag index d889ec82b7..9cad93f886 100644 --- a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/AdvancedPBRTerrain.frag +++ b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/AdvancedPBRTerrain.frag @@ -49,7 +49,7 @@ void main(){ // read and blend up to 12 texture layers #for i=0..12 (#ifdef ALBEDOMAP_$i $0 #endif) - PBRTerrainTextureLayer terrainTextureLayer_$i = PBRTerrainUtils_createAdvancedPBRTerrainLayer($i); + PBRTerrainTextureLayer terrainTextureLayer_$i = PBRTerrainUtils_createAdvancedPBRTerrainLayer($i, surface.geometryNormal); #ifdef USE_FIRST_LAYER_AS_TRANSPARENCY if($i == 0){ @@ -59,29 +59,51 @@ void main(){ } #endif + terrainTextureLayer_$i.roughness = m_Roughness_$i; + terrainTextureLayer_$i.metallic = m_Metallic_$i; terrainTextureLayer_$i.emission = m_EmissiveColor_$i; - - #if defined(TRI_PLANAR_MAPPING) || defined(TRI_PLANAR_MAPPING_$i) - //triplanar: - - PBRTerrainUtils_readTriPlanarAlbedoTexArray(ALBEDOMAP_$i, m_AlbedoMap_$i_scale, m_AlbedoTextureArray, terrainTextureLayer_$i); - #ifdef NORMALMAP_$i - PBRTerrainUtils_readTriPlanarNormalTexArray(NORMALMAP_$i, m_AlbedoMap_$i_scale, m_NormalParallaxTextureArray, terrainTextureLayer_$i); - #endif - #ifdef METALLICROUGHNESSMAP_$i - PBRTerrainUtils_readTriPlanarMetallicRoughnessAoEiTexArray(METALLICROUGHNESSMAP_$i, m_AlbedoMap_$i_scale, m_MetallicRoughnessAoEiTextureArray, terrainTextureLayer_$i); - #endif - #else - //non tri-planar: - - PBRTerrainUtils_readAlbedoTexArray(ALBEDOMAP_$i, m_AlbedoMap_$i_scale, m_AlbedoTextureArray, terrainTextureLayer_$i); - #ifdef NORMALMAP_$i - PBRTerrainUtils_readNormalTexArray(NORMALMAP_$i, m_AlbedoMap_$i_scale, m_NormalParallaxTextureArray, terrainTextureLayer_$i); - #endif - #ifdef METALLICROUGHNESSMAP_$i - PBRTerrainUtils_readMetallicRoughnessAoEiTexArray(METALLICROUGHNESSMAP_$i, m_AlbedoMap_$i_scale, m_MetallicRoughnessAoEiTextureArray, terrainTextureLayer_$i); - #endif - #endif + + #ifdef USE_TEXTURE_ARRAYS + #if defined(TRI_PLANAR_MAPPING) || defined(TRI_PLANAR_MAPPING_$i) + //triplanar for texture arrays: + PBRTerrainUtils_readTriPlanarAlbedoTexArray(m_AlbedoMap_$i, m_AlbedoMap_$i_scale, m_AlbedoTextureArray, terrainTextureLayer_$i); + #ifdef NORMALMAP_$i + PBRTerrainUtils_readTriPlanarNormalTexArray(m_NormalMap_$i, m_AlbedoMap_$i_scale, m_NormalParallaxTextureArray, terrainTextureLayer_$i); + #endif + #ifdef METALLICROUGHNESSMAP_$i + PBRTerrainUtils_readTriPlanarMetallicRoughnessAoEiTexArray(m_MetallicRoughnessMap_$i, m_AlbedoMap_$i_scale, m_MetallicRoughnessAoEiTextureArray, terrainTextureLayer_$i); + #endif + #else + //non tri-planar for texture arrays: + PBRTerrainUtils_readAlbedoTexArray(m_AlbedoMap_$i, m_AlbedoMap_$i_scale, m_AlbedoTextureArray, terrainTextureLayer_$i); + #ifdef NORMALMAP_$i + PBRTerrainUtils_readNormalTexArray(m_NormalMap_$i, m_AlbedoMap_$i_scale, m_NormalParallaxTextureArray, terrainTextureLayer_$i); + #endif + #ifdef METALLICROUGHNESSMAP_$i + PBRTerrainUtils_readMetallicRoughnessAoEiTexArray(m_MetallicRoughnessMap_$i, m_AlbedoMap_$i_scale, m_MetallicRoughnessAoEiTextureArray, terrainTextureLayer_$i); + #endif + #endif + #else + #if defined(TRI_PLANAR_MAPPING) || defined(TRI_PLANAR_MAPPING_$i) + //triplanar texture reads: + PBRTerrainUtils_readTriPlanarAlbedoTexture(m_AlbedoMap_$i, m_AlbedoMap_$i_scale, terrainTextureLayer_$i); + #ifdef NORMALMAP_$i + PBRTerrainUtils_readTriPlanarNormalTexture(m_NormalMap_$i, m_AlbedoMap_$i_scale, terrainTextureLayer_$i); + #endif + #ifdef METALLICROUGHNESSMAP_$i + PBRTerrainUtils_readTriPlanarMetallicRoughnessAoEiTexture(m_MetallicRoughnessMap_$i, m_AlbedoMap_$i_scale, terrainTextureLayer_$i); + #endif + #else + //non tri-planar texture reads: + PBRTerrainUtils_readAlbedoTexture(m_AlbedoMap_$i, m_AlbedoMap_$i_scale, terrainTextureLayer_$i); + #ifdef NORMALMAP_$i + PBRTerrainUtils_readNormalTexture(m_NormalMap_$i, m_AlbedoMap_$i_scale, terrainTextureLayer_$i); + #endif + #ifdef METALLICROUGHNESSMAP_$i + PBRTerrainUtils_readMetallicRoughnessAoEiTexture(m_MetallicRoughnessMap_$i, m_AlbedoMap_$i_scale, terrainTextureLayer_$i); + #endif + #endif + #endif //CUSTOM LIB EXAMPLE: uses a custom alpha map to desaturate albedo color for a color-removal effect #ifdef AFFLICTIONTEXTURE @@ -129,8 +151,8 @@ void main(){ gl_FragColor.rgb += surface.directLightContribution; gl_FragColor.rgb += surface.envLightContribution; gl_FragColor.rgb += surface.emission; - gl_FragColor.a = surface.alpha; - + gl_FragColor.a = surface.alpha; + #ifdef USE_FOG gl_FragColor = MaterialFog_calculateFogColor(vec4(gl_FragColor)); #endif @@ -138,5 +160,5 @@ void main(){ //outputs the final value of the selected layer as a color for debug purposes. #ifdef DEBUG_VALUES_MODE gl_FragColor = PBRLightingUtils_getColorOutputForDebugMode(m_DebugValuesMode, vec4(gl_FragColor.rgba), surface); - #endif + #endif } diff --git a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/AdvancedPBRTerrain.j3md b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/AdvancedPBRTerrain.j3md index 105a38b8b0..2098bd77f3 100644 --- a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/AdvancedPBRTerrain.j3md +++ b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/AdvancedPBRTerrain.j3md @@ -4,8 +4,8 @@ MaterialDef AdvancedPBRTerrain { Int BoundDrawBuffer Texture2D SunLightExposureMap - Boolean UseVertexColorsAsSunIntensity //set true to make the vertex color's R channel how exposed a vertex is to the sun - Float StaticSunIntensity //used for setting the sun exposure value for a whole material + Boolean UseVertexColorsAsSunExposure //set true to make the vertex color's R channel how exposed a vertex is to the sun + Float StaticSunExposure //used for setting the sun exposure value for a whole material //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. 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 { TextureArray NormalParallaxTextureArray -LINEAR TextureArray MetallicRoughnessAoEiTextureArray -LINEAR + //The type of normal map: -1.0 (DirectX), 1.0 (OpenGl) + Float NormalType : -1.0 + // Specular-AA Boolean UseSpecularAA : true // 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 { Float AfflictionMetallicValue : 0.0 Float AfflictionEmissiveValue : 0.0 //note that this is simplified into one value, rather than 2 with power and intensity like the regular pbr values. - // affliction texture splatting & desaturation functionality Boolean UseTriplanarAfflictionMapping @@ -43,7 +45,6 @@ MaterialDef AdvancedPBRTerrain { Float SplatNoiseVar - Int AfflictionMode_0 : 1 Int AfflictionMode_1 : 1 Int AfflictionMode_2 : 1 @@ -109,7 +110,6 @@ MaterialDef AdvancedPBRTerrain { Int AlbedoMap_10 Int AlbedoMap_11 - Float AlbedoMap_0_scale : 1 Float AlbedoMap_1_scale : 1 Float AlbedoMap_2_scale : 1 @@ -136,7 +136,6 @@ MaterialDef AdvancedPBRTerrain { Boolean UseTriPlanarMapping_10 Boolean UseTriPlanarMapping_11 - Int NormalMap_0 Int NormalMap_1 Int NormalMap_2 @@ -150,7 +149,6 @@ MaterialDef AdvancedPBRTerrain { Int NormalMap_10 Int NormalMap_11 - Int MetallicRoughnessMap_0 Int MetallicRoughnessMap_1 Int MetallicRoughnessMap_2 @@ -164,7 +162,6 @@ MaterialDef AdvancedPBRTerrain { Int MetallicRoughnessMap_10 Int MetallicRoughnessMap_11 - Float ParallaxHeight_0 Float ParallaxHeight_1 Float ParallaxHeight_2 @@ -178,13 +175,11 @@ MaterialDef AdvancedPBRTerrain { Float ParallaxHeight_10 Float ParallaxHeight_11 - - //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 Float TileWidth : 0 Vector3 TileLocation - // debug the final value of the selected layer as a color output + // debug the final value of the selected layer as a color output Int DebugValuesMode // Layers: // 0 - albedo (unshaded) @@ -195,6 +190,7 @@ MaterialDef AdvancedPBRTerrain { // 5 - emissive // 6 - exposure // 7 - alpha + // 8 - geometryNormals // use tri-planar mapping Boolean useTriPlanarMapping @@ -204,13 +200,6 @@ MaterialDef AdvancedPBRTerrain { Texture2D AlphaMap_1 -LINEAR Texture2D AlphaMap_2 -LINEAR - Boolean UseSpecGloss - Texture2D SpecularMap - Texture2D GlossinessMap - Texture2D SpecularGlossinessMap - Color Specular : 1.0 1.0 1.0 1.0 - Float Glossiness : 1.0 - Vector4 ProbeData // Prefiltered Env Map for indirect specular lighting @@ -222,7 +211,6 @@ MaterialDef AdvancedPBRTerrain { //integrate BRDF map for indirect Lighting Texture2D IntegrateBRDF -LINEAR - //shadows Int FilterMode Boolean HardwareShadows @@ -289,7 +277,6 @@ MaterialDef AdvancedPBRTerrain { ViewProjectionMatrix ViewMatrix Time - } Defines { @@ -301,10 +288,12 @@ MaterialDef AdvancedPBRTerrain { FOG_EXPSQ : ExpSqFog EXPOSUREMAP : SunLightExposureMap - USE_VERTEX_COLORS_AS_SUN_EXPOSURE : UseVertexColorsAsSunIntensity - STATIC_SUN_EXPOSURE : StaticSunIntensity + USE_VERTEX_COLORS_AS_SUN_EXPOSURE : UseVertexColorsAsSunExposure + STATIC_SUN_EXPOSURE : StaticSunExposure BRIGHTEN_INDOOR_SHADOWS : BrightenIndoorShadows + NORMAL_TYPE: NormalType + USE_FIRST_LAYER_AS_TRANSPARENCY : UseFirstLayerAsTransparency SPECULAR_AA : UseSpecularAA @@ -323,6 +312,8 @@ MaterialDef AdvancedPBRTerrain { AFFLICTIONEMISSIVEMAP : SplatEmissiveMap USE_SPLAT_NOISE : SplatNoiseVar + USE_TRIPLANAR_AFFLICTION_MAPPING : UseTriplanarAfflictionMapping + TRI_PLANAR_MAPPING : useTriPlanarMapping ALPHAMAP : AlphaMap @@ -383,10 +374,10 @@ MaterialDef AdvancedPBRTerrain { DEBUG_VALUES_MODE : DebugValuesMode + USE_TEXTURE_ARRAYS : AlbedoTextureArray } } - Technique PreShadow { VertexShader GLSL300 GLSL150 GLSL100 : Common/MatDefs/Shadow/PreShadow.vert diff --git a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/Modular/PBRTerrainUtils.glsllib b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/Modular/PBRTerrainUtils.glsllib index 9dc9390c45..55effd87b5 100644 --- a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/Modular/PBRTerrainUtils.glsllib +++ b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/Modular/PBRTerrainUtils.glsllib @@ -7,11 +7,17 @@ #import "Common/ShaderLib/TriPlanarUtils.glsllib" #ifdef ENABLE_PBRTerrainUtils_readPBRTerrainLayers + + #ifndef NORMAL_TYPE + #define NORMAL_TYPE -1.0 + #endif - //texture arrays: - uniform sampler2DArray m_AlbedoTextureArray; - uniform sampler2DArray m_NormalParallaxTextureArray; - uniform sampler2DArray m_MetallicRoughnessAoEiTextureArray; + #ifdef USE_TEXTURE_ARRAYS + //texture arrays: + uniform sampler2DArray m_AlbedoTextureArray; + uniform sampler2DArray m_NormalParallaxTextureArray; + uniform sampler2DArray m_MetallicRoughnessAoEiTextureArray; + #endif //texture-slot params for 12 unique texture slots (0-11) where the integer value points to the desired texture's index in the corresponding texture array: #for i=0..12 (#ifdef ALBEDOMAP_$i $0 #endif) @@ -21,15 +27,25 @@ uniform float m_AlbedoMap_$i_scale; uniform vec4 m_EmissiveColor_$i; - uniform int m_AlbedoMap_$i; + #ifdef USE_TEXTURE_ARRAYS + uniform int m_AlbedoMap_$i; + #ifdef NORMALMAP_$i + uniform int m_NormalMap_$i; + #endif + #ifdef METALLICROUGHNESSMAP_$i + uniform int m_MetallicRoughnessMap_$i; + #endif + #else + uniform sampler2D m_AlbedoMap_$i; + #ifdef NORMALMAP_$i + uniform sampler2D m_NormalMap_$i; + #endif + #ifdef METALLICROUGHNESSMAP_$i + uniform sampler2D m_MetallicRoughnessMap_$i; + #endif + #endif #endfor - #for n=0..12 (#ifdef METALLICROUGHNESSMAP_$n $0 #endif) - uniform int m_MetallicRoughnessMap_$n; - #endfor - #for x=0..12 (#ifdef NORMALMAP_$x $0 #endif) - uniform int m_NormalMap_$x; - #endfor //3 alpha maps : #ifdef ALPHAMAP @@ -41,9 +57,9 @@ #ifdef ALPHAMAP_2 uniform sampler2D m_AlphaMap_2; #endif + vec4 alphaBlend_0, alphaBlend_1, alphaBlend_2; - void PBRTerrainUtils_readAlphaMaps(){ #ifdef ALPHAMAP @@ -54,9 +70,7 @@ #endif #ifdef ALPHAMAP_2 alphaBlend_2 = texture2D( m_AlphaMap_2, texCoord.xy ); - #endif - - + #endif } float PBRTerrainUtils_getAlphaBlendFromChannel(int layer){ @@ -87,78 +101,118 @@ return finalAlphaBlendForLayer; } - - PBRTerrainTextureLayer PBRTerrainUtils_createAdvancedPBRTerrainLayer(int layerNum){ + PBRTerrainTextureLayer PBRTerrainUtils_createAdvancedPBRTerrainLayer(int layerNum, vec3 geometryNormal){ PBRTerrainTextureLayer terrainTextureLayer; terrainTextureLayer.blendValue = PBRTerrainUtils_getAlphaBlendFromChannel(layerNum); + terrainTextureLayer.albedo = vec4(1.0); + terrainTextureLayer.emission = vec4(0.0); + terrainTextureLayer.normal = geometryNormal; + terrainTextureLayer.alpha = 1.0; + terrainTextureLayer.ao = 1.0; + terrainTextureLayer.roughness = 1.0; + terrainTextureLayer.metallic = 0.0; + terrainTextureLayer.height = 0.0; return terrainTextureLayer; - } + } - //________ - + //3 functions to update layers from respective packed data vecs: void updateLayerFromPackedAlbedoMap(inout vec4 packedAlbedoVec, inout PBRTerrainTextureLayer layer){ layer.albedo = packedAlbedoVec; layer.alpha = packedAlbedoVec.a; } void updateLayerFromPackedNormalParallaxVec(inout vec4 packedNormalParallaxVec, inout PBRTerrainTextureLayer layer){ - layer.normal = calculateTangentsAndApplyToNormals(packedNormalParallaxVec.rgb, PBRLightingUtils_getWorldNormal()); + layer.normal = normalize(calculateTangentsAndApplyToNormals(packedNormalParallaxVec.rgb, PBRLightingUtils_getWorldNormal())); layer.height = packedNormalParallaxVec.a; } void updateLayerFromPackedMRAoEiVec(inout vec4 packedMRAoEiVec, inout PBRTerrainTextureLayer layer){ - layer.ao = packedMRAoEiVec.r; - layer.roughness = packedMRAoEiVec.g; - layer.metallic = packedMRAoEiVec.b; + layer.ao = packedMRAoEiVec.r; //ao only comes from texture (no float scalars) so no *= is done here + layer.roughness *= packedMRAoEiVec.g; + layer.metallic *= packedMRAoEiVec.b; layer.emission *= packedMRAoEiVec.a * layer.emission.a; + } + //________________________________ + // Basic Texture Reads: + + // Albedo: + void PBRTerrainUtils_readAlbedoTexture(in sampler2D tex, in float scale, inout PBRTerrainTextureLayer layer){ + vec4 packedAlbedoVec = texture2D(tex, texCoord * scale); + updateLayerFromPackedAlbedoMap(packedAlbedoVec, layer); } - //________ - - // read Triplanar Albedo from TextureArray: - void PBRTerrainUtils_readTriPlanarAlbedoTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){ - vec4 packedAlbedoVec = getTriPlanarBlendFromTexArray(lPosition, indexInTexArray, scale, texArray); + // normal: + void PBRTerrainUtils_readNormalTexture(in sampler2D tex, in float scale, inout PBRTerrainTextureLayer layer){ + vec4 packedNormalParallaxVec = texture2D(tex, texCoord * scale); + packedNormalParallaxVec.xyz = normalize(packedNormalParallaxVec.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0)); + updateLayerFromPackedNormalParallaxVec(packedNormalParallaxVec, layer); + } + // metallicRoughnessAoEi: + void PBRTerrainUtils_readMetallicRoughnessAoEiTexture(in sampler2D tex, in float scale, inout PBRTerrainTextureLayer layer){ + vec4 packedMRAoEi = texture2D(tex, texCoord * scale); + updateLayerFromPackedMRAoEiVec(packedMRAoEi, layer); + } + //________________________________ + // Basic Triplanar Reads: + + // Triplanar Albedo: + void PBRTerrainUtils_readTriPlanarAlbedoTexture(in sampler2D tex, in float scale, inout PBRTerrainTextureLayer layer){ + vec4 packedAlbedoVec = getTriPlanarBlend(lPosition, tex, scale); updateLayerFromPackedAlbedoMap(packedAlbedoVec, layer); } - // read Triplanar normal from TextureArray: - void PBRTerrainUtils_readTriPlanarNormalTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){ - vec4 packedNormalParallaxVec = getTriPlanarBlendFromTexArray(lPosition, indexInTexArray, scale, texArray); + // Triplanar normal: + void PBRTerrainUtils_readTriPlanarNormalTexture(in sampler2D tex, in float scale, inout PBRTerrainTextureLayer layer){ + vec4 packedNormalParallaxVec = getTriPlanarNormalBlend(lPosition, tex, scale); updateLayerFromPackedNormalParallaxVec(packedNormalParallaxVec, layer); } - // read TriPlanar metallicRoughnessAoEi from TextureArray: - void PBRTerrainUtils_readTriPlanarMetallicRoughnessAoEiTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){ - vec4 packedMRAoEi = getTriPlanarBlendFromTexArray(lPosition, indexInTexArray, scale, texArray); + // TriPlanar metallicRoughnessAoEi: + void PBRTerrainUtils_readTriPlanarMetallicRoughnessAoEiTexture(in sampler2D tex, in float scale, inout PBRTerrainTextureLayer layer){ + vec4 packedMRAoEi = getTriPlanarBlend(lPosition, tex, scale); updateLayerFromPackedMRAoEiVec(packedMRAoEi, layer); - } - //________ - - // read Albedo from TextureArray: + } + //________________________________ + // Basic TexArray reads: + + // Albedo TextureArray: void PBRTerrainUtils_readAlbedoTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){ vec4 packedAlbedoVec = texture2DArray(texArray, vec3(texCoord * scale, indexInTexArray)); - updateLayerFromPackedAlbedoMap(packedAlbedoVec, layer); - + updateLayerFromPackedAlbedoMap(packedAlbedoVec, layer); } - // read Normal from TextureArray: + // Normal TextureArray: void PBRTerrainUtils_readNormalTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){ vec4 packedNormalParallaxVec = texture2DArray(texArray, vec3(texCoord * scale, indexInTexArray)); + packedNormalParallaxVec.xyz = normalize(packedNormalParallaxVec.xyz * vec3(2.0, NORMAL_TYPE * 2.0, 2.0) - vec3(1.0, NORMAL_TYPE * 1.0, 1.0)); updateLayerFromPackedNormalParallaxVec(packedNormalParallaxVec, layer); } - // read metallicRoughnessAoEi from TextureArray: + // metallicRoughnessAoEi TextureArray: void PBRTerrainUtils_readMetallicRoughnessAoEiTexArray(in int indexInTexArray, float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){ vec4 packedMRAoEi = texture2DArray(texArray, vec3(texCoord * scale, indexInTexArray)); - updateLayerFromPackedMRAoEiVec(packedMRAoEi, layer); - + updateLayerFromPackedMRAoEiVec(packedMRAoEi, layer); } - //________ - - - - void PBRTerrainUtils_blendPBRTerrainLayer(inout PBRSurface surface, inout PBRTerrainTextureLayer layer){ - - - //mix values from this index layer to final output values based on finalAlphaBlendForLayer + //________________________________ + // Triplanar TexArray reads: + + // Triplana Albedo TextureArray: + void PBRTerrainUtils_readTriPlanarAlbedoTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){ + vec4 packedAlbedoVec = getTriPlanarBlendFromTexArray(lPosition, indexInTexArray, scale, texArray); + updateLayerFromPackedAlbedoMap(packedAlbedoVec, layer); + } + // Triplanar normal TextureArray: + void PBRTerrainUtils_readTriPlanarNormalTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){ + vec4 packedNormalParallaxVec = getTriPlanarNormalBlendFromTexArray(lPosition, indexInTexArray, scale, texArray); + updateLayerFromPackedNormalParallaxVec(packedNormalParallaxVec, layer); + } + // TriPlanar metallicRoughnessAoEi TextureArray: + void PBRTerrainUtils_readTriPlanarMetallicRoughnessAoEiTexArray(in int indexInTexArray, in float scale, in sampler2DArray texArray, inout PBRTerrainTextureLayer layer){ + vec4 packedMRAoEi = getTriPlanarBlendFromTexArray(lPosition, indexInTexArray, scale, texArray); + updateLayerFromPackedMRAoEiVec(packedMRAoEi, layer); + } + //_______________________________ + + //blend layer function. This mixes each layer's pbr vars over top of the current surface values based on the layer's blendValue + void PBRTerrainUtils_blendPBRTerrainLayer(inout PBRSurface surface, inout PBRTerrainTextureLayer layer){ surface.albedo = mix(surface.albedo, layer.albedo.rgb, layer.blendValue); - surface.normal = mix(surface.normal.rgb, layer.normal, layer.blendValue); + surface.normal = normalize(mix(surface.normal.rgb, layer.normal, layer.blendValue)); surface.metallic = mix(surface.metallic, layer.metallic, layer.blendValue); surface.roughness = mix(surface.roughness, layer.roughness, layer.blendValue); surface.ao = mix(surface.ao, vec3(layer.ao), layer.blendValue); @@ -167,4 +221,3 @@ #endif #endif - diff --git a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/PBRTerrain.frag b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/PBRTerrain.frag deleted file mode 100644 index b3705383c3..0000000000 --- a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/PBRTerrain.frag +++ /dev/null @@ -1,601 +0,0 @@ -#import "Common/ShaderLib/GLSLCompat.glsllib" -#import "Common/ShaderLib/PBR.glsllib" -#import "Common/ShaderLib/Parallax.glsllib" -#import "Common/ShaderLib/Lighting.glsllib" -#import "Common/MatDefs/Terrain/AfflictionLib.glsllib" - -varying vec3 wPosition; -varying vec3 vNormal; -varying vec2 texCoord; -uniform vec3 g_CameraPosition; -varying vec3 vPosition; -varying vec3 vnPosition; -varying vec3 vViewDir; -varying vec4 vLightDir; -varying vec4 vnLightDir; -varying vec3 lightVec; -varying vec3 inNormal; -varying vec3 wNormal; - -// Specular-AA -#ifdef SPECULAR_AA_SCREEN_SPACE_VARIANCE - uniform float m_SpecularAASigma; -#endif -#ifdef SPECULAR_AA_THRESHOLD - uniform float m_SpecularAAKappa; -#endif - -#ifdef DEBUG_VALUES_MODE - uniform int m_DebugValuesMode; -#endif - -uniform vec4 g_LightData[NB_LIGHTS]; -uniform vec4 g_AmbientLightColor; - -#if NB_PROBES >= 1 - uniform samplerCube g_PrefEnvMap; - uniform vec3 g_ShCoeffs[9]; - uniform mat4 g_LightProbeData; -#endif -#if NB_PROBES >= 2 - uniform samplerCube g_PrefEnvMap2; - uniform vec3 g_ShCoeffs2[9]; - uniform mat4 g_LightProbeData2; -#endif -#if NB_PROBES == 3 - uniform samplerCube g_PrefEnvMap3; - uniform vec3 g_ShCoeffs3[9]; - uniform mat4 g_LightProbeData3; -#endif - -#ifdef TRI_PLANAR_MAPPING - varying vec4 wVertex; -#endif - -//texture-slot params for 12 unique texture slots (0-11) : -#for i=0..12 ( $0 ) - uniform int m_AfflictionMode_$i; - uniform float m_Roughness_$i; - uniform float m_Metallic_$i; - - #ifdef ALBEDOMAP_$i - uniform sampler2D m_AlbedoMap_$i; - #endif - #ifdef ALBEDOMAP_$i_SCALE - uniform float m_AlbedoMap_$i_scale; - #endif - #ifdef NORMALMAP_$i - uniform sampler2D m_NormalMap_$i; - #endif -#endfor - -//3 alpha maps : -#ifdef ALPHAMAP - uniform sampler2D m_AlphaMap; -#endif -#ifdef ALPHAMAP_1 - uniform sampler2D m_AlphaMap_1; -#endif -#ifdef ALPHAMAP_2 - uniform sampler2D m_AlphaMap_2; -#endif - -#ifdef DISCARD_ALPHA - uniform float m_AlphaDiscardThreshold; -#endif - -//fog vars for basic fog : -#ifdef USE_FOG -#import "Common/ShaderLib/MaterialFog.glsllib" - uniform vec4 m_FogColor; - float fogDistance; - - uniform vec2 m_LinearFog; -#endif -#ifdef FOG_EXP - uniform float m_ExpFog; -#endif -#ifdef FOG_EXPSQ - uniform float m_ExpSqFog; -#endif - -//sun intensity is a secondary AO value that can be painted per-vertex in the red channel of the -// vertex colors, or it can be set as a static value for an entire material with the StaticSunIntensity float param -#if defined(USE_VERTEX_COLORS_AS_SUN_INTENSITY) - varying vec4 vertColors; -#endif - -#ifdef STATIC_SUN_INTENSITY - uniform float m_StaticSunIntensity; -#endif -//sun intensity AO value is only applied to the directional light, not to point lights, so it is important to track if the -//sun is more/less bright than the brightest point light for each fragment to determine how the light probe's ambient light should be scaled later on in light calculation code -float brightestPointLight = 0.0; - -//optional affliction paramaters that use the AfflictionAlphaMap's green channel for splatting m_SplatAlbedoMap and the red channel for splatting desaturation : -#ifdef AFFLICTIONTEXTURE - uniform sampler2D m_AfflictionAlphaMap; -#endif -#ifdef USE_SPLAT_NOISE - uniform float m_SplatNoiseVar; -#endif -//only defined for non-terrain geoemtries and terrains that are not positioned nor sized in correlation to the 2d array of AfflictionAlphaMaps used for splatting accross large tile based scenes in a grid -#ifdef TILELOCATION - uniform float m_TileWidth; - uniform vec3 m_TileLocation; -#endif -#ifdef AFFLICTIONALBEDOMAP - uniform sampler2D m_SplatAlbedoMap; -#endif -#ifdef AFFLICTIONNORMALMAP - uniform sampler2D m_SplatNormalMap; -#endif -#ifdef AFFLICTIONROUGHNESSMETALLICMAP - uniform sampler2D m_SplatRoughnessMetallicMap; -#endif -#ifdef AFFLICTIONEMISSIVEMAP - uniform sampler2D m_SplatEmissiveMap; -#endif - -uniform int m_AfflictionSplatScale; -uniform float m_AfflictionRoughnessValue; -uniform float m_AfflictionMetallicValue; -uniform float m_AfflictionEmissiveValue; -uniform vec4 m_AfflictionEmissiveColor; - -vec4 afflictionVector; -float noiseHash; -float livelinessValue; -float afflictionValue; -int afflictionMode = 1; - -//general temp vars : -vec4 tempAlbedo, tempNormal, tempEmissiveColor; -float tempParallax, tempMetallic, tempRoughness, tempAo, tempEmissiveIntensity; - -vec3 viewDir; -vec2 coord; -vec4 albedo = vec4(1.0); -vec3 normal = vec3(0.5,0.5,1); -vec3 norm; -float Metallic; -float Roughness; -float packedAoValue = 1.0; -vec4 emissive; -float emissiveIntensity = 1.0; -float indoorSunLightExposure = 1.0; - -vec4 packedMetallicRoughnessAoEiVec; -vec4 packedNormalParallaxVec; - -void main(){ - - #ifdef USE_FOG - fogDistance = distance(g_CameraPosition, wPosition.xyz); - #endif - - indoorSunLightExposure = 1.0; - - viewDir = normalize(g_CameraPosition - wPosition); - - norm = normalize(wNormal); - normal = norm; - - afflictionVector = vec4(1.0, 0.0, 1.0, 0.0); //r channel is sturation, g channel is affliction splat texture intensity, b and a unused (might use b channel for wetness eventually) - - #ifdef AFFLICTIONTEXTURE - - #ifdef TILELOCATION - //subterrains that are not centred in tile or equal to tile width in total size need to have m_TileWidth pre-set. (tileWidth is the x,z dimesnions that the AfflictionAlphaMap represents) - vec2 tileCoords; - float xPos, zPos; - - vec3 locInTile = (wPosition - m_TileLocation); - - locInTile += vec3(m_TileWidth/2, 0, m_TileWidth/2); - - xPos = (locInTile.x / m_TileWidth); - zPos = 1 - (locInTile.z / m_TileWidth); - - tileCoords = vec2(xPos, zPos); - - afflictionVector = texture2D(m_AfflictionAlphaMap, tileCoords).rgba; - #else - // ..othrewise when terrain size matches tileWidth and location matches tileLocation, the terrain's texCoords can be used for simple texel fetching of the AfflictionAlphaMap - afflictionVector = texture2D(m_AfflictionAlphaMap, texCoord.xy).rgba; - #endif - #endif - - livelinessValue = afflictionVector.r; - afflictionValue = afflictionVector.g; - - #ifdef ALBEDOMAP_0 - #ifdef ALPHAMAP - - vec4 alphaBlend; - vec4 alphaBlend_0, alphaBlend_1, alphaBlend_2; - int texChannelForAlphaBlending; - - alphaBlend_0 = texture2D( m_AlphaMap, texCoord.xy ); - - #ifdef ALPHAMAP_1 - alphaBlend_1 = texture2D( m_AlphaMap_1, texCoord.xy ); - #endif - #ifdef ALPHAMAP_2 - alphaBlend_2 = texture2D( m_AlphaMap_2, texCoord.xy ); - #endif - - vec2 texSlotCoords; - - float finalAlphaBlendForLayer = 1.0; - - vec3 blending = abs( norm ); - blending = (blending -0.2) * 0.7; - blending = normalize(max(blending, 0.00001)); // Force weights to sum to 1.0 (very important!) - float b = (blending.x + blending.y + blending.z); - blending /= vec3(b, b, b); - - #for i=0..12 (#ifdef ALBEDOMAP_$i $0 #endif) - - //assign texture slot's blending from index's correct alpha map - if($i <= 3){ - alphaBlend = alphaBlend_0; - }else if($i <= 7){ - alphaBlend = alphaBlend_1; - }else if($i <= 11){ - alphaBlend = alphaBlend_2; - } - - texChannelForAlphaBlending = int(mod(float($i), 4.0)); //pick the correct channel (r g b or a) based on the layer's index - switch(texChannelForAlphaBlending) { - case 0: - finalAlphaBlendForLayer = alphaBlend.r; - break; - case 1: - finalAlphaBlendForLayer = alphaBlend.g; - break; - case 2: - finalAlphaBlendForLayer = alphaBlend.b; - break; - case 3: - finalAlphaBlendForLayer = alphaBlend.a; - break; - } - - afflictionMode = m_AfflictionMode_$i; - - #ifdef TRI_PLANAR_MAPPING - //tri planar - tempAlbedo = getTriPlanarBlend(wVertex, blending, m_AlbedoMap_$i, m_AlbedoMap_$i_scale); - - #ifdef NORMALMAP_$i - tempNormal.rgb = getTriPlanarBlend(wVertex, blending, m_NormalMap_$i, m_AlbedoMap_$i_scale).rgb; - tempNormal.rgb = calculateTangentsAndApplyToNormals(tempNormal.rgb, wNormal);// this gets rid of the need for pre-generating tangents for TerrainPatches, since doing so doesn't seem to work (tbnMat is always blank for terrains even with tangents pre-generated, not sure why...) - #else - tempNormal.rgb = wNormal.rgb; - #endif - #else - - // non triplanar - texSlotCoords = texCoord * m_AlbedoMap_$i_scale; - - tempAlbedo.rgb = texture2D(m_AlbedoMap_$i, texSlotCoords).rgb; - #ifdef NORMALMAP_$i - tempNormal.xyz = texture2D(m_NormalMap_$i, texSlotCoords).xyz; - tempNormal.rgb = calculateTangentsAndApplyToNormals(tempNormal.rgb, wNormal); - #else - tempNormal.rgb = wNormal.rgb; - #endif - #endif - - //note: most of these functions can be found in AfflictionLib.glslib - tempAlbedo.rgb = alterLiveliness(tempAlbedo.rgb, livelinessValue, afflictionMode); //changes saturation of albedo for this layer; does nothing if not using AfflictionAlphaMap for affliction splatting - - //mix values from this index layer to final output values based on finalAlphaBlendForLayer - albedo.rgb = mix(albedo.rgb, tempAlbedo.rgb , finalAlphaBlendForLayer); - normal.rgb = mix(normal.rgb, tempNormal.rgb, finalAlphaBlendForLayer); - Metallic = mix(Metallic, m_Metallic_$i, finalAlphaBlendForLayer); - Roughness = mix(Roughness, m_Roughness_$i, finalAlphaBlendForLayer); - - #endfor - #endif - #endif - - - float alpha = albedo.a; - #ifdef DISCARD_ALPHA - if(alpha < m_AlphaDiscardThreshold){ - discard; - } - #endif - - - //APPLY AFFLICTIONN TO THE PIXEL - #ifdef AFFLICTIONTEXTURE - vec4 afflictionAlbedo; - - float newAfflictionScale = m_AfflictionSplatScale; - vec2 newScaledCoords; - - #ifdef AFFLICTIONALBEDOMAP - #ifdef TRI_PLANAR_MAPPING - newAfflictionScale = newAfflictionScale / 256; - afflictionAlbedo = getTriPlanarBlend(wVertex, blending, m_SplatAlbedoMap , newAfflictionScale); - #else - newScaledCoords = mod(wPosition.xz / m_AfflictionSplatScale, 0.985); - afflictionAlbedo = texture2D(m_SplatAlbedoMap , newScaledCoords); - #endif - - #else - afflictionAlbedo = vec4(1.0, 1.0, 1.0, 1.0); - #endif - - vec3 afflictionNormal; - #ifdef AFFLICTIONNORMALMAP - #ifdef TRI_PLANAR_MAPPING - - afflictionNormal = getTriPlanarBlend(wVertex, blending, m_SplatNormalMap , newAfflictionScale).rgb; - - #else - afflictionNormal = texture2D(m_SplatNormalMap , newScaledCoords).rgb; - #endif - - #else - afflictionNormal = norm; - - #endif - float afflictionMetallic = m_AfflictionMetallicValue; - float afflictionRoughness = m_AfflictionRoughnessValue; - float afflictionAo = 1.0; - - - vec4 afflictionEmissive = m_AfflictionEmissiveColor; - float afflictionEmissiveIntensity = m_AfflictionEmissiveValue; - - - #ifdef AFFLICTIONROUGHNESSMETALLICMAP - vec4 metallicRoughnessAoEiVec = texture2D(m_SplatRoughnessMetallicMap, newScaledCoords); - afflictionRoughness *= metallicRoughnessAoEiVec.g; - afflictionMetallic *= metallicRoughnessAoEiVec.b; - afflictionAo = metallicRoughnessAoEiVec.r; - afflictionEmissiveIntensity *= metallicRoughnessAoEiVec.a; //important not to leave this channel all black by accident when creating the mraoei map if using affliction emissiveness - - #endif - - #ifdef AFFLICTIONEMISSIVEMAP - vec4 emissiveMapColor = texture2D(m_SplatEmissiveMap, newScaledCoords); - afflictionEmissive *= emissiveMapColor; - #endif - - float adjustedAfflictionValue = afflictionValue; - #ifdef USE_SPLAT_NOISE - noiseHash = getStaticNoiseVar0(wPosition, afflictionValue * m_SplatNoiseVar); - - adjustedAfflictionValue = getAdjustedAfflictionVar(afflictionValue); - if(afflictionValue >= 0.99){ - adjustedAfflictionValue = afflictionValue; - } - #else - noiseHash = 1.0; - #endif - - Roughness = alterAfflictionRoughness(adjustedAfflictionValue, Roughness, afflictionRoughness, noiseHash); - Metallic = alterAfflictionMetallic(adjustedAfflictionValue, Metallic, afflictionMetallic, noiseHash); - albedo = alterAfflictionColor(adjustedAfflictionValue, albedo, afflictionAlbedo, noiseHash ); - normal = alterAfflictionNormalsForTerrain(adjustedAfflictionValue, normal, afflictionNormal, noiseHash , wNormal); - emissive = alterAfflictionGlow(adjustedAfflictionValue, emissive, afflictionEmissive, noiseHash); - emissiveIntensity = alterAfflictionEmissiveIntensity(adjustedAfflictionValue, emissiveIntensity, afflictionEmissiveIntensity, noiseHash); - emissiveIntensity *= afflictionEmissive.a; - //affliction ao value blended below after specular calculation - #endif - -// spec gloss pipeline code would go here if supported, but likely will not be for terrain shaders as defines are limited and heavily used - -float specular = 0.5; -float nonMetalSpec = 0.08 * specular; -vec4 specularColor = (nonMetalSpec - nonMetalSpec * Metallic) + albedo * Metallic; -vec4 diffuseColor = albedo - albedo * Metallic; -vec3 fZero = vec3(specular); - -gl_FragColor.rgb = vec3(0.0); - -//simple ao calculation, no support for lightmaps like stock pbr shader.. (probably could add lightmap support with another texture array, but -// that would add another texture read per slot and require removing 12 other defines to make room...) - vec3 ao = vec3(packedAoValue); - - #ifdef AFFLICTIONTEXTURE - ao = alterAfflictionAo(afflictionValue, ao, vec3(afflictionAo), noiseHash); // alter the AO map for affliction values - #endif - ao.rgb = ao.rrr; - specularColor.rgb *= ao; - - #ifdef STATIC_SUN_INTENSITY - indoorSunLightExposure = m_StaticSunIntensity; //single float value to indicate percentage of - //sunlight hitting the model (only works for small models or models with 100% consistent sunlighting accross every pixel) - #endif - #ifdef USE_VERTEX_COLORS_AS_SUN_INTENSITY - indoorSunLightExposure = vertColors.r * indoorSunLightExposure; //use R channel of vertexColors for.. - #endif - // similar purpose as above... - //but uses r channel vert colors like an AO map specifically - //for sunlight (solution for scaling lighting for indoor - // and shadey/dimly lit models, especially big ones that - // span accross varying directionalLight exposure) - brightestPointLight = 0.0; - - - float ndotv = max( dot( normal, viewDir ),0.0); - #ifdef SPECULAR_AA - float sigma = 1.0; - float kappa = 0.18; - #ifdef SPECULAR_AA_SCREEN_SPACE_VARIANCE - sigma = m_SpecularAASigma; - #endif - #ifdef SPECULAR_AA_THRESHOLD - kappa = m_SpecularAAKappa; - #endif - #endif - for( int i = 0;i < NB_LIGHTS; i+=3){ - vec4 lightColor = g_LightData[i]; - vec4 lightData1 = g_LightData[i+1]; - vec4 lightDir; - vec3 lightVec; - lightComputeDir(wPosition, lightColor.w, lightData1, lightDir, lightVec); - - float fallOff = 1.0; - #if __VERSION__ >= 110 - // allow use of control flow - if(lightColor.w > 1.0){ - #endif - fallOff = computeSpotFalloff(g_LightData[i+2], lightVec); - #if __VERSION__ >= 110 - } - #endif - //point light attenuation - fallOff *= lightDir.w; - - lightDir.xyz = normalize(lightDir.xyz); - vec3 directDiffuse; - vec3 directSpecular; - - #ifdef SPECULAR_AA - float hdotv = PBR_ComputeDirectLightWithSpecularAA( - normal, lightDir.xyz, viewDir, - lightColor.rgb, fZero, Roughness, sigma, kappa, ndotv, - directDiffuse, directSpecular); - #else - float hdotv = PBR_ComputeDirectLight( - normal, lightDir.xyz, viewDir, - lightColor.rgb, fZero, Roughness, ndotv, - directDiffuse, directSpecular); - #endif - - vec3 directLighting = diffuseColor.rgb *directDiffuse + directSpecular; - - #if defined(USE_VERTEX_COLORS_AS_SUN_INTENSITY) || defined(STATIC_SUN_INTENSITY) - if(fallOff == 1.0){ - directLighting.rgb *= indoorSunLightExposure;// ... *^. to scale down how intense just the sun is (ambient and direct light are 1.0 fallOff) - - } - else{ - brightestPointLight = max(fallOff, brightestPointLight); - - } - #endif - - gl_FragColor.rgb += directLighting * fallOff; - - } - - float minVertLighting; - #ifdef BRIGHTEN_INDOOR_SHADOWS - minVertLighting = 0.0833; //brighten shadows so that caves/indoors which are naturally covered from the DL shadows are not way too dark compared to when shadows are off (mostly only necessary for naturally dark scenes, or dark areas when using the sun intensity code above) - #else - minVertLighting = 0.0533; - - #endif - - indoorSunLightExposure = max(indoorSunLightExposure, brightestPointLight); - indoorSunLightExposure = max(indoorSunLightExposure, minVertLighting); //scale the indoorSunLightExposure back up to account for the brightest point light nearby before scaling light probes by this value below - - #if NB_PROBES >= 1 - vec3 color1 = vec3(0.0); - vec3 color2 = vec3(0.0); - vec3 color3 = vec3(0.0); - float weight1 = 1.0; - float weight2 = 0.0; - float weight3 = 0.0; - - float ndf = renderProbe(viewDir, wPosition, normal, norm, Roughness, diffuseColor, specularColor, ndotv, ao, g_LightProbeData, g_ShCoeffs, g_PrefEnvMap, color1); - #if NB_PROBES >= 2 - float ndf2 = renderProbe(viewDir, wPosition, normal, norm, Roughness, diffuseColor, specularColor, ndotv, ao, g_LightProbeData2, g_ShCoeffs2, g_PrefEnvMap2, color2); - #endif - #if NB_PROBES == 3 - float ndf3 = renderProbe(viewDir, wPosition, normal, norm, Roughness, diffuseColor, specularColor, ndotv, ao, g_LightProbeData3, g_ShCoeffs3, g_PrefEnvMap3, color3); - #endif - - #if NB_PROBES >= 2 - float invNdf = max(1.0 - ndf,0.0); - float invNdf2 = max(1.0 - ndf2,0.0); - float sumNdf = ndf + ndf2; - float sumInvNdf = invNdf + invNdf2; - #if NB_PROBES == 3 - float invNdf3 = max(1.0 - ndf3,0.0); - sumNdf += ndf3; - sumInvNdf += invNdf3; - weight3 = ((1.0 - (ndf3 / sumNdf)) / (NB_PROBES - 1)) * (invNdf3 / sumInvNdf); - #endif - - weight1 = ((1.0 - (ndf / sumNdf)) / (NB_PROBES - 1)) * (invNdf / sumInvNdf); - weight2 = ((1.0 - (ndf2 / sumNdf)) / (NB_PROBES - 1)) * (invNdf2 / sumInvNdf); - - float weightSum = weight1 + weight2 + weight3; - - weight1 /= weightSum; - weight2 /= weightSum; - weight3 /= weightSum; - #endif - - #ifdef USE_AMBIENT_LIGHT - color1.rgb *= g_AmbientLightColor.rgb; - color2.rgb *= g_AmbientLightColor.rgb; - color3.rgb *= g_AmbientLightColor.rgb; - #endif - - -// multiply probes by the indoorSunLightExposure, as determined by pixel's sunlightExposure and adjusted for -// nearby point/spot lights ( will be multiplied by 1.0 and left unchanged if you are not defining any of the sunlight exposure variables for dimming indoors areas) - color1.rgb *= indoorSunLightExposure; - color2.rgb *= indoorSunLightExposure; - color3.rgb *= indoorSunLightExposure; - - gl_FragColor.rgb += color1 * clamp(weight1,0.0,1.0) + color2 * clamp(weight2,0.0,1.0) + color3 * clamp(weight3,0.0,1.0); - - #endif - - if(emissive.a > 0){ - emissive = emissive * pow(emissive.a * 5, emissiveIntensity) * emissiveIntensity * 20 * emissive.a; - } - - // emissive = emissive * pow(emissiveIntensity * 2.3, emissive.a); - - gl_FragColor += emissive; - - // add fog after the lighting because shadows will cause the fog to darken - // which just results in the geometry looking like it's changed color - #ifdef USE_FOG - #ifdef FOG_LINEAR - gl_FragColor = getFogLinear(gl_FragColor, m_FogColor, m_LinearFog.x, m_LinearFog.y, fogDistance); - #endif - #ifdef FOG_EXP - gl_FragColor = getFogExp(gl_FragColor, m_FogColor, m_ExpFog, fogDistance); - #endif - #ifdef FOG_EXPSQ - gl_FragColor = getFogExpSquare(gl_FragColor, m_FogColor, m_ExpSqFog, fogDistance); - #endif - #endif - - //outputs the final value of the selected layer as a color for debug purposes. - #ifdef DEBUG_VALUES_MODE - if(m_DebugValuesMode == 0){ - gl_FragColor.rgb = vec3(albedo); - } - else if(m_DebugValuesMode == 1){ - gl_FragColor.rgb = vec3(normal); - } - else if(m_DebugValuesMode == 2){ - gl_FragColor.rgb = vec3(Roughness); - } - else if(m_DebugValuesMode == 3){ - gl_FragColor.rgb = vec3(Metallic); - } - else if(m_DebugValuesMode == 4){ - gl_FragColor.rgb = ao.rgb; - } - else if(m_DebugValuesMode == 5){ - gl_FragColor.rgb = vec3(emissive.rgb); - } - #endif - gl_FragColor.a = albedo.a; - -} diff --git a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/PBRTerrain.j3md b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/PBRTerrain.j3md index 707442fafd..92e601c1c1 100644 --- a/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/PBRTerrain.j3md +++ b/jme3-terrain/src/main/resources/Common/MatDefs/Terrain/PBRTerrain.j3md @@ -4,14 +4,16 @@ MaterialDef PBR Terrain { MaterialParameters { Int BoundDrawBuffer - - Boolean UseVertexColorsAsSunIntensity //set true to make the vertex color's R channel how exposed a vertex is to the sun - Float StaticSunIntensity //used for setting the sun exposure value for a whole material + Texture2D SunLightExposureMap + Boolean UseVertexColorsAsSunExposure //set true to make the vertex color's R channel how exposed a vertex is to the sun + Float StaticSunExposure //used for setting the sun exposure value for a whole material //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. 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 + Boolean UseFirstLayerAsTransparency + Boolean UseTriplanarAfflictionMapping Int AfflictionSplatScale : 8 Float AfflictionRoughnessValue : 1.0 @@ -24,6 +26,9 @@ MaterialDef PBR Terrain { Texture2D SplatRoughnessMetallicMap -LINEAR Texture2D SplatEmissiveMap -LINEAR + //The type of normal map: -1.0 (DirectX), 1.0 (OpenGl) + Float NormalType : -1.0 + // Specular-AA Boolean UseSpecularAA : true // 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. @@ -78,18 +83,44 @@ MaterialDef PBR Terrain { Float Metallic_10 : 0.0 Float Metallic_11 : 0.0 - - // debug the final value of the selected layer as a color output + Color EmissiveColor_0 : 0.0 0.0 0.0 0.0 + Color EmissiveColor_1 : 0.0 0.0 0.0 0.0 + Color EmissiveColor_2 : 0.0 0.0 0.0 0.0 + Color EmissiveColor_3 : 0.0 0.0 0.0 0.0 + Color EmissiveColor_4 : 0.0 0.0 0.0 0.0 + Color EmissiveColor_5 : 0.0 0.0 0.0 0.0 + Color EmissiveColor_6 : 0.0 0.0 0.0 0.0 + Color EmissiveColor_7 : 0.0 0.0 0.0 0.0 + Color EmissiveColor_8 : 0.0 0.0 0.0 0.0 + Color EmissiveColor_9 : 0.0 0.0 0.0 0.0 + Color EmissiveColor_10 : 0.0 0.0 0.0 0.0 + Color EmissiveColor_11 : 0.0 0.0 0.0 0.0 + + Boolean UseTriPlanarMapping_0 + Boolean UseTriPlanarMapping_1 + Boolean UseTriPlanarMapping_2 + Boolean UseTriPlanarMapping_3 + Boolean UseTriPlanarMapping_4 + Boolean UseTriPlanarMapping_5 + Boolean UseTriPlanarMapping_6 + Boolean UseTriPlanarMapping_7 + Boolean UseTriPlanarMapping_8 + Boolean UseTriPlanarMapping_9 + Boolean UseTriPlanarMapping_10 + Boolean UseTriPlanarMapping_11 + + // debug the final value of the selected layer as a color output Int DebugValuesMode - // Layers: - // 0 - albedo (un-shaded) + // 0 - albedo (unshaded) // 1 - normals // 2 - roughness // 3 - metallic // 4 - ao - // 5 - emissive - + // 5 - emissive + // 6 - exposure + // 7 - alpha + // 8 - geometryNormals // use tri-planar mapping Boolean useTriPlanarMapping @@ -154,21 +185,11 @@ MaterialDef PBR Terrain { Float AlbedoMap_11_scale Texture2D NormalMap_11 -LINEAR - - // Texture that specifies alpha values Texture2D AlphaMap -LINEAR Texture2D AlphaMap_1 -LINEAR Texture2D AlphaMap_2 -LINEAR - // For Spec gloss pipeline - Boolean UseSpecGloss - Texture2D SpecularMap - Texture2D GlossinessMap - Texture2D SpecularGlossinessMap - Color Specular : 1.0 1.0 1.0 1.0 - Float Glossiness : 1.0 - Vector4 ProbeData // Prefiltered Env Map for indirect specular lighting @@ -221,7 +242,6 @@ MaterialDef PBR Terrain { Boolean BackfaceShadows : false - Boolean UseFog Color FogColor Vector2 LinearFog @@ -237,7 +257,7 @@ MaterialDef PBR Terrain { LightMode SinglePassAndImageBased VertexShader GLSL300 GLSL150 GLSL130 GLSL100: Common/MatDefs/Terrain/PBRTerrain.vert - FragmentShader GLSL300 GLSL150 GLSL130 GLSL100: Common/MatDefs/Terrain/PBRTerrain.frag + FragmentShader GLSL300 GLSL150 GLSL130 GLSL100: Common/MatDefs/Terrain/AdvancedPBRTerrain.frag WorldParameters { WorldViewProjectionMatrix @@ -247,7 +267,6 @@ MaterialDef PBR Terrain { ViewProjectionMatrix ViewMatrix Time - } Defines { @@ -256,6 +275,7 @@ MaterialDef PBR Terrain { TILELOCATION : TileLocation AFFLICTIONTEXTURE : AfflictionAlphaMap + USE_TRIPLANAR_AFFLICTION_MAPPING : UseTriplanarAfflictionMapping AFFLICTIONALBEDOMAP: SplatAlbedoMap AFFLICTIONNORMALMAP : SplatNormalMap AFFLICTIONROUGHNESSMETALLICMAP : SplatRoughnessMetallicMap @@ -267,11 +287,15 @@ MaterialDef PBR Terrain { SPECULAR_AA_SCREEN_SPACE_VARIANCE : SpecularAASigma SPECULAR_AA_THRESHOLD : SpecularAAKappa - - USE_VERTEX_COLORS_AS_SUN_INTENSITY : UseVertexColorsAsSunIntensity - STATIC_SUN_INTENSITY : StaticSunIntensity + EXPOSUREMAP : SunLightExposureMap + USE_VERTEX_COLORS_AS_SUN_EXPOSURE : UseVertexColorsAsSunExposure + STATIC_SUN_EXPOSURE : StaticSunExposure BRIGHTEN_INDOOR_SHADOWS : BrightenIndoorShadows + NORMAL_TYPE: NormalType + + USE_FIRST_LAYER_AS_TRANSPARENCY : UseFirstLayerAsTransparency + DISCARD_ALPHA : AlphaDiscardThreshold USE_FOG : UseFog @@ -323,6 +347,19 @@ MaterialDef PBR Terrain { ALBEDOMAP_10_SCALE : AlbedoMap_10_scale ALBEDOMAP_11_SCALE : AlbedoMap_11_scale + TRI_PLANAR_MAPPING_0 : UseTriPlanarMapping_0 + TRI_PLANAR_MAPPING_1 : UseTriPlanarMapping_1 + TRI_PLANAR_MAPPING_2 : UseTriPlanarMapping_2 + TRI_PLANAR_MAPPING_3 : UseTriPlanarMapping_3 + TRI_PLANAR_MAPPING_4 : UseTriPlanarMapping_4 + TRI_PLANAR_MAPPING_5 : UseTriPlanarMapping_5 + TRI_PLANAR_MAPPING_6 : UseTriPlanarMapping_6 + TRI_PLANAR_MAPPING_7 : UseTriPlanarMapping_7 + TRI_PLANAR_MAPPING_8 : UseTriPlanarMapping_8 + TRI_PLANAR_MAPPING_9 : UseTriPlanarMapping_9 + TRI_PLANAR_MAPPING_10 : UseTriPlanarMapping_10 + TRI_PLANAR_MAPPING_11 : UseTriPlanarMapping_11 + DEBUG_VALUES_MODE : DebugValuesMode }