Parallax Occlusion Mapping for jme

here is the Terrain PBR/parallax occlusion based on mix of @yaRnMcDonuts work and @RiccardoBlb work.

yaRnMcDonuts source:

it dont tave tri plannar, because im not sure how to manage texCoords there, but someone might add it.

!!! Please replace “MatDefs” with “Common” for imports, or use same dirs in project as me. I use MatDefs folder with cloned libs to be able to see shader in 3.2.2 IDE properly.

Also there is:
**coord_2.x += g_Time * 0.03; **
to demonstrate tex moving like water, remove it or use if need.

And also: you need ShaderLib/OcclusionParallax.glsllib from Riccardo PR

NORMALMAP_PARALLAX - this is not used, would just need to add to j3md and use

calculateParallax(coord##index, m_ParallaxHeight##index, m_NormalMap##index, ab);

instead of

calculateParallax(coord##index, m_ParallaxHeight##index, m_ParallaxMap##index, ab);

when ifdef NORMALMAP_PARALLAX is true.

frag:

#import "MatDefs/ShaderLib/GLSLCompat.glsllib"
#import "MatDefs/ShaderLib/PBR.glsllib"
#import "MatDefs/ShaderLib/Parallax.glsllib"
#import "MatDefs/ShaderLib/Lighting.glsllib"

uniform vec4 g_LightData[NB_LIGHTS];

uniform vec4 g_AmbientLightColor;

varying vec3 wPosition;

uniform float g_Time;

// Configure height channel
#ifdef NORMALMAP_PARALLAX
  #define HEIGHT_MAP A_COMPONENT
#else
  #define HEIGHT_MAP R_COMPONENT
#endif
#import "MatDefs/ShaderLib/OcclusionParallax.glsllib"

#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 EMISSIVE
    uniform vec4 m_Emissive;
#endif
#ifdef EMISSIVEMAP
    uniform sampler2D m_EmissiveMap;
#endif
#if defined(EMISSIVE) || defined(EMISSIVEMAP)
    uniform float m_EmissivePower;
    uniform float m_EmissiveIntensity;
#endif 

#ifdef SPECGLOSSPIPELINE
  uniform sampler2D m_SpecularMap;
  uniform sampler2D m_GlossMap;
#endif

#ifdef LIGHTMAP
  uniform sampler2D m_LightMap;
#endif

varying vec3 vNormal;

#if defined(NORMALMAP_0) || defined(NORMALMAP_1) || defined(NORMALMAP_2) || defined(NORMALMAP_3) || defined(NORMALMAP_4) || defined(NORMALMAP_5) || defined(NORMALMAP_6) || defined(NORMALMAP_7) || defined(PLAGUEDNORMALMAP)
    varying vec4 wTangent;
#endif

varying vec2 texCoord;
uniform vec3 inPosition;
uniform mat4 g_WorldViewMatrix;
uniform vec3 g_CameraPosition;

varying vec3 vPosition;
varying vec3 vnPosition;
varying vec3 vViewDir;
varying vec4 vLightDir;
varying vec4 vnLightDir;
varying vec3 lightVec;

uniform float m_Roughness_0;
uniform float m_Roughness_1;
uniform float m_Roughness_2;
uniform float m_Roughness_3;
uniform float m_Roughness_4;
uniform float m_Roughness_5;
uniform float m_Roughness_6;
uniform float m_Roughness_7;
uniform float m_Roughness_8;
uniform float m_Roughness_9;
uniform float m_Roughness_10;
uniform float m_Roughness_11;

uniform float m_Metallic_0;
uniform float m_Metallic_1;
uniform float m_Metallic_2;
uniform float m_Metallic_3;
uniform float m_Metallic_4;
uniform float m_Metallic_5;
uniform float m_Metallic_6;
uniform float m_Metallic_7;
uniform float m_Metallic_8;
uniform float m_Metallic_9;
uniform float m_Metallic_10;
uniform float m_Metallic_11;

uniform float m_Parallax_0;
uniform float m_Parallax_1;
uniform float m_Parallax_2;
uniform float m_Parallax_3;
uniform float m_Parallax_4;
uniform float m_Parallax_5;
uniform float m_Parallax_6;
uniform float m_Parallax_7;
uniform float m_Parallax_8;
uniform float m_Parallax_9;
uniform float m_Parallax_10;
uniform float m_Parallax_11;

#ifdef PARALLAX_LOD_DISTANCE
    uniform float m_ParallaxLODDistance;
#endif

#ifdef ALBEDOMAP_0
  uniform sampler2D m_AlbedoMap_0;
#endif
#ifdef ALBEDOMAP_1
  uniform sampler2D m_AlbedoMap_1;
#endif
#ifdef ALBEDOMAP_2
  uniform sampler2D m_AlbedoMap_2;
#endif
#ifdef ALBEDOMAP_3
  uniform sampler2D m_AlbedoMap_3;
#endif
#ifdef ALBEDOMAP_4
  uniform sampler2D m_AlbedoMap_4;
#endif
#ifdef ALBEDOMAP_5
  uniform sampler2D m_AlbedoMap_5;
#endif
#ifdef ALBEDOMAP_6
  uniform sampler2D m_AlbedoMap_6;
#endif
#ifdef ALBEDOMAP_7
  uniform sampler2D m_AlbedoMap_7;
#endif
#ifdef ALBEDOMAP_8
  uniform sampler2D m_AlbedoMap_8;
#endif
#ifdef ALBEDOMAP_9
  uniform sampler2D m_AlbedoMap_9;
#endif
#ifdef ALBEDOMAP_10
  uniform sampler2D m_AlbedoMap_10;
#endif
#ifdef ALBEDOMAP_11
  uniform sampler2D m_AlbedoMap_11;
#endif


#ifdef ALBEDOMAP_0
  uniform float m_AlbedoMap_0_scale;
#endif
#ifdef ALBEDOMAP_1
  uniform float m_AlbedoMap_1_scale;
#endif
#ifdef ALBEDOMAP_2
  uniform float m_AlbedoMap_2_scale;
#endif
#ifdef ALBEDOMAP_3
  uniform float m_AlbedoMap_3_scale;
#endif
#ifdef ALBEDOMAP_4
  uniform float m_AlbedoMap_4_scale;
#endif
#ifdef ALBEDOMAP_5
  uniform float m_AlbedoMap_5_scale;
#endif
#ifdef ALBEDOMAP_6
  uniform float m_AlbedoMap_6_scale;
#endif
#ifdef ALBEDOMAP_7
  uniform float m_AlbedoMap_7_scale;
#endif
#ifdef ALBEDOMAP_8
  uniform float m_AlbedoMap_8_scale;
#endif
#ifdef ALBEDOMAP_9
  uniform float m_AlbedoMap_9_scale;
#endif
#ifdef ALBEDOMAP_10
  uniform float m_AlbedoMap_10_scale;
#endif
#ifdef ALBEDOMAP_11
  uniform float m_AlbedoMap_11_scale;
#endif


#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 NORMALMAP_0
  uniform sampler2D m_NormalMap_0;
#endif
#ifdef NORMALMAP_1
  uniform sampler2D m_NormalMap_1;
#endif
#ifdef NORMALMAP_2
  uniform sampler2D m_NormalMap_2;
#endif
#ifdef NORMALMAP_3
  uniform sampler2D m_NormalMap_3;
#endif
#ifdef NORMALMAP_4
  uniform sampler2D m_NormalMap_4;
#endif
#ifdef NORMALMAP_5
  uniform sampler2D m_NormalMap_5;
#endif
#ifdef NORMALMAP_6
  uniform sampler2D m_NormalMap_6;
#endif
#ifdef NORMALMAP_7
  uniform sampler2D m_NormalMap_7;
#endif
#ifdef NORMALMAP_8
  uniform sampler2D m_NormalMap_8;
#endif
#ifdef NORMALMAP_9
  uniform sampler2D m_NormalMap_9;
#endif
#ifdef NORMALMAP_10
  uniform sampler2D m_NormalMap_10;
#endif
#ifdef NORMALMAP_11
  uniform sampler2D m_NormalMap_11;
#endif


#ifdef PARALLAXMAP_0
  uniform sampler2D m_ParallaxMap_0;
  uniform float m_ParallaxHeight_0;
#endif
#ifdef PARALLAXMAP_1
  uniform sampler2D m_ParallaxMap_1;
  uniform float m_ParallaxHeight_1;
#endif
#ifdef PARALLAXMAP_2
  uniform sampler2D m_ParallaxMap_2;
  uniform float m_ParallaxHeight_2;
#endif
#ifdef PARALLAXMAP_3
  uniform sampler2D m_ParallaxMap_3;
  uniform float m_ParallaxHeight_3;
#endif
#ifdef PARALLAXMAP_4
  uniform sampler2D m_ParallaxMap_4;
  uniform float m_ParallaxHeight_4;
#endif
#ifdef PARALLAXMAP_5
  uniform sampler2D m_ParallaxMap_5;
  uniform float m_ParallaxHeight_5;
#endif
#ifdef PARALLAXMAP_6
  uniform sampler2D m_ParallaxMap_6;
  uniform float m_ParallaxHeight_6;
#endif
#ifdef PARALLAXMAP_7
  uniform sampler2D m_ParallaxMap_7;
  uniform float m_ParallaxHeight_7;
#endif
#ifdef PARALLAXMAP_8
  uniform sampler2D m_ParallaxMap_8;
  uniform float m_ParallaxHeight_8;
#endif
#ifdef PARALLAXMAP_9
  uniform sampler2D m_ParallaxMap_9;
  uniform float m_ParallaxHeight_9;
#endif
#ifdef PARALLAXMAP_10
  uniform sampler2D m_ParallaxMap_10;
  uniform float m_ParallaxHeight_10;
#endif
#ifdef PARALLAXMAP_11
  uniform sampler2D m_ParallaxMap_11;
  uniform float m_ParallaxHeight_11;
#endif


vec4 emissive;
  varying vec3 wNormal;
//ifdef TRI_PLANAR_MAPPING
  varying vec4 wVertex;
//#endif

varying float camDist;

vec2 coord;
vec4 albedo;
vec4 tempAlbedo, tempNormal;
vec3 normal = vec3(0.5,0.5,1);
vec3 newNormal;
vec3 parallax;
vec2 newTexCoord;
vec3 viewDir;
mat3 tbnMat;
vec3 norm;
float Metallic;
float Roughness;

#define DEFINE_COORD(index) vec2 coord##index = newTexCoord * m_AlbedoMap##index##_scale;

#define BLEND(index, ab)\
    tempAlbedo.rgb = texture2D(m_AlbedoMap##index, coord##index).rgb;\
    albedo.rgb = mix( albedo.rgb, tempAlbedo.rgb ,ab );\
    normal.rgb = mix(normal.xyz, wNormal.xyz, ab);\
    Metallic = mix(Metallic, m_Metallic##index, ab);\
    Roughness = mix(Roughness, m_Roughness##index, ab);

#define BLEND_NORMAL(index, ab)\
    tempAlbedo.rgb = texture2D(m_AlbedoMap##index, coord##index).rgb;\
    albedo.rgb = mix( albedo.rgb, tempAlbedo.rgb ,ab );\
    Metallic = mix(Metallic, m_Metallic##index, ab);\
    Roughness = mix(Roughness, m_Roughness##index, ab);\
    newNormal = texture2D(m_NormalMap##index, coord##index).xyz;\
    normal = mix(normal, newNormal, ab);

#define PARALLAX_BLEND(index, ab)\
    calculateParallax(coord##index, m_ParallaxHeight##index, m_ParallaxMap##index, ab);

void calculateParallax(inout vec2 newTexCoord, in float parallaxHeight, in sampler2D map, in float intensity) {
    #ifdef PARALLAX_LOD_DISTANCE  
        if(camDist < m_ParallaxLODDistance && intensity > 0.1){
            vec3 vViewDir =  viewDir * tbnMat;
            Parallax_initFor(vViewDir,parallaxHeight);
            Parallax_displaceCoords(newTexCoord,map);
        }
    #else
        if(intensity > 0.1){
            vec3 vViewDir =  viewDir * tbnMat;
            Parallax_initFor(vViewDir,parallaxHeight);
            Parallax_displaceCoords(newTexCoord,map);
        }
    #endif
}

vec4 calculateAlbedoBlend(in vec2 newTexCoord) {
    vec4 alphaBlend = texture2D( m_AlphaMap, newTexCoord.xy );
    vec4 albedo = vec4(1.0);
    Roughness = m_Roughness_0;
    Metallic = m_Metallic_0 ;
    
    vec3 blending = abs( wNormal );
    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);

    vec3 wvPosition=(vec4(inPosition,1)*g_WorldViewMatrix).xyz;
    #ifdef ALPHAMAP_1
      vec4 alphaBlend1   = texture2D( m_AlphaMap_1, newTexCoord.xy );
    #endif
    #ifdef ALPHAMAP_2
      vec4 alphaBlend2   = texture2D( m_AlphaMap_2, newTexCoord.xy );
    #endif
    #ifdef ALBEDOMAP_0   
        DEFINE_COORD(_0)
        //NOTE! the old (phong) terrain shaders do not have an "_0" for the first diffuse map, it is just "DiffuseMap"
        #if defined(PARALLAXMAPPING) && defined (PARALLAXMAP_0)
            PARALLAX_BLEND(_0, alphaBlend.r)
        #endif
        #ifdef NORMALMAP_0
            BLEND_NORMAL(_0,  alphaBlend.r)
        #else
            BLEND(_0,  alphaBlend.r)
        #endif
    #endif
    #ifdef ALBEDOMAP_1
        DEFINE_COORD(_1)
        #if defined(PARALLAXMAPPING) && defined (PARALLAXMAP_1)
            PARALLAX_BLEND(_1, alphaBlend.g)
        #endif
        #ifdef NORMALMAP_1
            BLEND_NORMAL(_1,  alphaBlend.g)
        #else
            BLEND(_1,  alphaBlend.g)
        #endif
    #endif
    #ifdef ALBEDOMAP_2
        DEFINE_COORD(_2)
        #if defined(PARALLAXMAPPING) && defined (PARALLAXMAP_2)
            coord_2.x += g_Time * 0.03;
            PARALLAX_BLEND(_2, alphaBlend.b)
        #endif
        #ifdef NORMALMAP_2
            BLEND_NORMAL(_2,  alphaBlend.b)
        #else
            BLEND(_2,  alphaBlend.b)
        #endif
    #endif
    #ifdef ALBEDOMAP_3 
        DEFINE_COORD(_3)
        #if defined(PARALLAXMAPPING) && defined (PARALLAXMAP_3)
            PARALLAX_BLEND(_3, alphaBlend.a)
        #endif
        #ifdef NORMALMAP_3
            BLEND_NORMAL(_3,  alphaBlend.a)
        #else
            BLEND(_3,  alphaBlend.a)
        #endif
    #endif

    #ifdef ALPHAMAP_1
        #ifdef ALBEDOMAP_4
            DEFINE_COORD(_4)
            #if defined(PARALLAXMAPPING) && defined (PARALLAXMAP_4)
                PARALLAX_BLEND(_4,  alphaBlend1.r)
            #endif
            #ifdef NORMALMAP_4
                BLEND_NORMAL(_4,  alphaBlend1.r)
            #else
                BLEND(_4,  alphaBlend1.r)
            #endif
        #endif
        #ifdef ALBEDOMAP_5
            DEFINE_COORD(_5)
            #if defined(PARALLAXMAPPING) && defined (PARALLAXMAP_5)
                PARALLAX_BLEND(_5,  alphaBlend1.g)
            #endif
            #ifdef NORMALMAP_5
                BLEND_NORMAL(_5,  alphaBlend1.g)
            #else
                BLEND(_5,  alphaBlend1.g)
            #endif
        #endif
        #ifdef ALBEDOMAP_6
            DEFINE_COORD(_6)
            #if defined(PARALLAXMAPPING) && defined (PARALLAXMAP_6)
                PARALLAX_BLEND(_6,  alphaBlend1.b)
            #endif
            #ifdef NORMALMAP_6
                BLEND_NORMAL(_6,  alphaBlend1.b)
            #else
                BLEND(_6,  alphaBlend1.b)
            #endif
        #endif
        #ifdef ALBEDOMAP_7
            DEFINE_COORD(_7)
            #if defined(PARALLAXMAPPING) && defined (PARALLAXMAP_7)
                PARALLAX_BLEND(_7,  alphaBlend1.a)
            #endif
            #ifdef NORMALMAP_7
                BLEND_NORMAL(_7,  alphaBlend1.a)
            #else
                BLEND(_7,  alphaBlend1.a)
            #endif
        #endif
    #endif

    #ifdef ALPHAMAP_2
        #ifdef ALBEDOMAP_8
            DEFINE_COORD(_8)
            #if defined(PARALLAXMAPPING) && defined (PARALLAXMAP_8)
                PARALLAX_BLEND(_8,  alphaBlend2.r)
            #endif
            #ifdef NORMALMAP_8
                BLEND_NORMAL(_8,  alphaBlend2.r)
            #else
                BLEND(_8,  alphaBlend2.r)
            #endif
        #endif
        #ifdef ALBEDOMAP_9
            DEFINE_COORD(_9)
            #if defined(PARALLAXMAPPING) && defined (PARALLAXMAP_9)
                PARALLAX_BLEND(_9,  alphaBlend2.g)
            #endif
            #ifdef NORMALMAP_9
                BLEND_NORMAL(_9,  alphaBlend2.g)
            #else
                BLEND(_9,  alphaBlend2.g)
            #endif
        #endif
        #ifdef ALBEDOMAP_10
            DEFINE_COORD(_10)
            #if defined(PARALLAXMAPPING) && defined (PARALLAXMAP_10)
                PARALLAX_BLEND(_10,  alphaBlend2.b)
            #endif
            #ifdef NORMALMAP_10
                BLEND_NORMAL(_10,  alphaBlend2.b)
            #else
                BLEND(_10,  alphaBlend2.b)
            #endif
        #endif
        #ifdef ALBEDOMAP_11
            DEFINE_COORD(_11)
            #if defined(PARALLAXMAPPING) && defined (PARALLAXMAP_11)
                PARALLAX_BLEND(_11,  alphaBlend2.a)
            #endif
            #ifdef NORMALMAP_11
                BLEND_NORMAL(_11,  alphaBlend2.a)
            #else
                BLEND(_11,  alphaBlend2.a)
            #endif
        #endif                   
    #endif
    return albedo;
}

#ifdef PROBE_COLOR
    uniform vec4 m_ProbeColor;
#endif

void main(){
    viewDir = normalize(g_CameraPosition - wPosition);
    
    vec3 norm = normalize(wNormal);
    normal = norm;
    //norm = vec3(0.5, 0.5, 1.0);
    #if defined(NORMALMAP_0) || defined(PARALLAXMAP) || defined(PLAGUEDNORMALMAP)
        vec3 tan = normalize(wTangent.xyz);
        //tbnMat = mat3(tan, wTangent.w * cross( (wNormal), (tan)), norm);
        tbnMat = mat3(tan, wTangent.w * cross( (norm), (tan)), norm);
    #endif
    
    newTexCoord=texCoord;

    //always calculated since the
    vec3 blending;
    #ifdef ALBEDOMAP_0
      #ifdef ALPHAMAP
        albedo = calculateAlbedoBlend(newTexCoord);
      #else
        albedo = texture2D(m_AlbedoMap_0, newTexCoord);
      #endif
    #endif

    if(albedo.a <= 0.1){
        albedo.r = 1.0;
        discard;
    }

    #ifdef ROUGHNESSMAP
        Roughness = texture2D(m_RoughnessMap, newTexCoord).r * Roughness;
    #endif
    Roughness = max(Roughness, 1e-4);
    #ifdef METALLICMAP   
        Metallic = texture2D(m_MetallicMap, newTexCoord).r;
    #endif

    #ifdef METALLICMAP
        Metallic = texture2D(m_MetallicMap, newTexCoord).r * max(Metallic, 0.0);
    #else
        Metallic =  max(Metallic, 0.0);
    #endif

    //---------------------
    // normal calculations
    //---------------------
    #if defined(NORMALMAP_0) || defined(NORMALMAP_1) || defined(NORMALMAP_2) || defined(NORMALMAP_3) || defined(NORMALMAP_4) || defined(NORMALMAP_5) || defined(NORMALMAP_6) || defined(NORMALMAP_7) || defined(NORMALMAP_8) || defined(NORMALMAP_9) || defined(NORMALMAP_10) || defined(NORMALMAP_11)
       #ifdef TRI_PLANAR_MAPPING
     //    normal = calculateNormalTriPlanar(wNormal, wVertex, texCoord);
       #else
     //    normal = calculateNormal(texCoord);
       #endif
       normal += norm * 0.9;
       normal = normalize(normal * vec3(2.0) - vec3(1.0));
       normal.z /= 100;
       normal = normalize(normal);
    #else
       normal = normalize(norm * vec3(2.0) - vec3(1.0));
       normal = wNormal;
    #endif

 //   normal = normalize(normal * vec3(2.0) - vec3(1.0));

     #ifdef SPECGLOSSPIPELINE
        #ifdef USE_PACKED_SG
            vec4 specularColor = texture2D(m_SpecularGlossinessMap, newTexCoord);
            float glossiness = specularColor.a * m_Glossiness;
            specularColor *= m_Specular;
        #else
            #ifdef SPECULARMAP
                vec4 specularColor = texture2D(m_SpecularMap, newTexCoord);
            #else
                vec4 specularColor = vec4(1.0);
            #endif
            #ifdef GLOSSINESSMAP
                float glossiness = texture2D(m_GlossinessMap, newTexCoord).r * m_Glossiness;
            #else
                float glossiness = m_Glossiness;
            #endif
            specularColor *= m_Specular;
        #endif
        vec4 diffuseColor = albedo;// * (1.0 - max(max(specularColor.r, specularColor.g), specularColor.b));
        Roughness = 1.0 - glossiness;
        vec3 fZero = specularColor.xyz;
    #else
        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);
    #endif

    gl_FragColor.rgb = vec3(0.0);
    vec3 ao = vec3(1.0);

    float ndotv = max( dot( normal, viewDir ),0.0);
    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;
        
        float hdotv = PBR_ComputeDirectLight(normal, lightDir.xyz, viewDir,
                            lightColor.rgb, fZero, Roughness, ndotv,
                            directDiffuse,  directSpecular);

        vec3 directLighting = diffuseColor.rgb *directDiffuse + directSpecular;
        gl_FragColor.rgb += directLighting * fallOff;
    }

    #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
        
        #if USE_AMBIENT_LIGHT
            color1.rgb *= g_AmbientLightColor.rgb;
            color2.rgb *= g_AmbientLightColor.rgb;
            color3.rgb *= g_AmbientLightColor.rgb;
        #endif
        
        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 defined(EMISSIVE) || defined (EMISSIVEMAP)
        #ifdef EMISSIVEMAP
            emissive = texture2D(m_EmissiveMap, newTexCoord);
        #else
            emissive = m_Emissive;
        #endif
        gl_FragColor += emissive * pow(emissive.a, m_EmissivePower) * m_EmissiveIntensity;
    #endif

    gl_FragColor.a = 1.0;
}

vert:

#import "MatDefs/ShaderLib/GLSLCompat.glsllib"
#import "MatDefs/ShaderLib/Instancing.glsllib"

attribute vec3 inPosition;
attribute vec3 inNormal;
attribute vec2 inTexCoord;
uniform vec3 g_CameraPosition;

uniform vec4 g_AmbientLightColor; //passed to .frag for scaling light probe brightness in 3.3 compatible version of this shader.

varying vec2 texCoord;
varying vec3 wPosition;
varying vec3 wNormal;

#ifdef TRI_PLANAR_MAPPING
  varying vec4 wVertex;
#endif


#if defined(NORMALMAP_0) || defined(NORMALMAP_1) || defined(NORMALMAP_2) || defined(NORMALMAP_3) || defined(NORMALMAP_4) || defined(PLAGUEDNORMALMAP)
    attribute vec4 inTangent;
    varying vec4 wTangent;
#endif

varying float camDist;   

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

    gl_Position = TransformWorldViewProjection(modelSpacePos);

    texCoord = inTexCoord;

    wPosition = TransformWorld(modelSpacePos).xyz;
    wNormal  = normalize(TransformWorldNormal(inNormal));
   #if  defined(NORMALMAP_0) || defined(NORMALMAP_1) || defined(NORMALMAP_2) || defined(NORMALMAP_3) || defined(NORMALMAP_4) || defined(NORMALMAP_5) || defined(NORMALMAP_6) || defined(NORMALMAP_7)  || defined(PLAGUEDNORMALMAP)
        wTangent = vec4(TransformWorldNormal(inTangent.xyz),inTangent.w);
    #endif

    #ifdef TRI_PLANAR_MAPPING
       wVertex = vec4(inPosition,0.0);
       wNormal = inNormal;
    #endif

    camDist = distance(g_CameraPosition.xyz, wPosition.xyz);
}
3 Likes