(April 2018) Monthly WIP & Screenshot thread

I think this was because there was some change in the pbr area.

This changed version of PBRTerrain.frag does work with the current master (at least it does not throw any errors:

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

uniform vec4 g_LightData[NB_LIGHTS];

varying vec3 wPosition;

#ifdef INDIRECT_LIGHTING
//  uniform sampler2D m_IntegrateBRDF;
  uniform samplerCube g_PrefEnvMap;
  uniform vec3 g_ShCoeffs[9];
  uniform vec4 g_LightProbeData;
#endif

#ifdef METALLICMAP
    uniform sampler2D m_MetallicMap;
#else
    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;
#endif
#ifdef ROUGHNESSMAP
    uniform sampler2D m_RoughnessMap;
#endif
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;

#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 wNormal;
#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)
    varying vec4 wTangent;
#endif

varying vec2 texCoord;
#ifdef DIFFUSEMAP_0
  uniform sampler2D m_DiffuseMap_0;
#endif
#ifdef DIFFUSEMAP_1
  uniform sampler2D m_DiffuseMap_1;
#endif
#ifdef DIFFUSEMAP_2
  uniform sampler2D m_DiffuseMap_2;
#endif
#ifdef DIFFUSEMAP_3
  uniform sampler2D m_DiffuseMap_3;
#endif
#ifdef DIFFUSEMAP_4
  uniform sampler2D m_DiffuseMap_4;
#endif
#ifdef DIFFUSEMAP_5
  uniform sampler2D m_DiffuseMap_5;
#endif
#ifdef DIFFUSEMAP_6
  uniform sampler2D m_DiffuseMap_6;
#endif
#ifdef DIFFUSEMAP_7
  uniform sampler2D m_DiffuseMap_7;
#endif
#ifdef DIFFUSEMAP_8
  uniform sampler2D m_DiffuseMap_8;
#endif
#ifdef DIFFUSEMAP_9
  uniform sampler2D m_DiffuseMap_9;
#endif
#ifdef DIFFUSEMAP_10
  uniform sampler2D m_DiffuseMap_10;
#endif
#ifdef DIFFUSEMAP_11
  uniform sampler2D m_DiffuseMap_11;
#endif

#ifdef DIFFUSEMAP_0_SCALE
  uniform float m_DiffuseMap_0_scale;
#endif
#ifdef DIFFUSEMAP_1_SCALE
  uniform float m_DiffuseMap_1_scale;
#endif
#ifdef DIFFUSEMAP_2_SCALE
  uniform float m_DiffuseMap_2_scale;
#endif
#ifdef DIFFUSEMAP_3_SCALE
  uniform float m_DiffuseMap_3_scale;
#endif
#ifdef DIFFUSEMAP_4_SCALE
  uniform float m_DiffuseMap_4_scale;
#endif
#ifdef DIFFUSEMAP_5_SCALE
  uniform float m_DiffuseMap_5_scale;
#endif
#ifdef DIFFUSEMAP_6_SCALE
  uniform float m_DiffuseMap_6_scale;
#endif
#ifdef DIFFUSEMAP_7_SCALE
  uniform float m_DiffuseMap_7_scale;
#endif
#ifdef DIFFUSEMAP_8_SCALE
  uniform float m_DiffuseMap_8_scale;
#endif
#ifdef DIFFUSEMAP_9_SCALE
  uniform float m_DiffuseMap_9_scale;
#endif
#ifdef DIFFUSEMAP_10_SCALE
  uniform float m_DiffuseMap_10_scale;
#endif
#ifdef DIFFUSEMAP_11_SCALE
  uniform float m_DiffuseMap_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
  #define NORMALMAP
  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 PARALLAXMAPPING
    uniform float m_ParallaxHeight;
    vec2 classicOffset;

    void applyParallax(inout vec2 coord, in sampler2D normalMap){
        coord = coord + (texture2D(normalMap,coord).a - 0.6) *  classicOffset;
    }
#else
    void applyParallax(inout vec2 coord, in sampler2D normalMap){}
#endif

#define DEFINE_COORD(index) vec2 coord##index = texCoord * m_DiffuseMap##index##_scale;

#define BLEND(index, ab)\
    DEFINE_COORD(index)\
    diffuseColor.rgb = mix( diffuseColor.rgb,  texture2D(m_DiffuseMap##index, coord##index).rgb ,ab );\
    Metallic = mix(Metallic, m_Metallic##index; ab);\
    Roughness = mix(Roughness, m_Roughness##index, ab);

#define BLEND_NORMAL(index, ab)\
    DEFINE_COORD(index)\
    applyParallax(coord##index, m_NormalMap##index);\
    normal = mix(normal,texture2D(m_NormalMap##index, coord##index).xyz, ab);\
    diffuseColor.rgb = mix( diffuseColor.rgb,  texture2D(m_DiffuseMap##index, coord##index).rgb ,ab );\
    Metallic = mix(Metallic, m_Metallic##index, ab);\
    Roughness = mix(Roughness, m_Roughness##index, ab);

uniform vec3 g_CameraPosition;
void main(){
    vec3 viewDir = normalize(g_CameraPosition - wPosition);
    vec3 norm = normalize(wNormal);

    #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)
        mat3 tbnMat = mat3(wTangent.xyz, wTangent.w * cross( (wNormal), (wTangent.xyz)), wNormal.xyz);
    #endif

    #ifdef PARALLAXMAPPING
        vec3 vViewDir =  normalize(viewDir * tbnMat);  
        classicOffset = m_ParallaxHeight * vViewDir.xy * vViewDir.z;
    #endif

    #ifdef ALPHAMAP
        vec4 alphaBlend = texture2D( m_AlphaMap, texCoord);
    #elif defined(DIFFUSEMAP_0) || defined(DIFFUSEMAP_1) || defined(DIFFUSEMAP_2) || defined(DIFFUSEMAP_3) 
        vec4 alphaBlend = vec4(0.5);
    #endif
    #ifdef DIFFUSEMAP_0
        DEFINE_COORD(_0)
        #ifdef NORMALMAP_0       
            applyParallax(coord_0, m_NormalMap_0);
            vec3 normal = texture2D(m_NormalMap_0, coord_0).xyz * alphaBlend.r;
         #else   
            vec3 normal = vec3(0.5,0.5,1);
         #endif  
        vec4 diffuseColor =  vec4(texture2D(m_DiffuseMap_0, coord_0).rgb * alphaBlend.r, 1.0);
        float Metallic =  m_Metallic_0;
        float Roughness =  m_Roughness_0;
    #endif

    #ifdef DIFFUSEMAP_1
        #ifdef NORMALMAP_1
            BLEND_NORMAL(_1, alphaBlend.g)
        #else
            BLEND(_1,  alphaBlend.g)
        #endif
    #endif
    #ifdef DIFFUSEMAP_2
        #ifdef NORMALMAP_2
            BLEND_NORMAL(_2, alphaBlend.b)
        #else
            BLEND(_2,  alphaBlend.b)
        #endif
    #endif
    #ifdef DIFFUSEMAP_3
        #ifdef NORMALMAP_3
            BLEND_NORMAL(_3, alphaBlend.a)
        #else
            BLEND(_3,  alphaBlend.a)
        #endif
    #endif

    #ifdef ALPHAMAP_1
        vec4 alphaBlend1 = texture2D( m_AlphaMap_1, texCoord);  
    #elif defined(DIFFUSEMAP_4) || defined(DIFFUSEMAP_5) || defined(DIFFUSEMAP_6) || defined(DIFFUSEMAP_7) 
        vec4 alphaBlend1 = vec4(0.5);
    #endif
    #ifdef DIFFUSEMAP_4  
        #ifdef NORMALMAP_4
            BLEND_NORMAL(_4, alphaBlend1.r)
        #else
            BLEND(_4, alphaBlend1.r)
        #endif
    #endif
    #ifdef DIFFUSEMAP_5
        #ifdef NORMALMAP_5
            BLEND_NORMAL(_5, alphaBlend1.g)
        #else
            BLEND(_5,  alphaBlend1.g)
        #endif
    #endif  
    #ifdef DIFFUSEMAP_6
        #ifdef NORMALMAP_6
            BLEND_NORMAL(_6, alphaBlend1.b)
        #else
            BLEND(_6,  alphaBlend1.b)
        #endif
    #endif
    #ifdef DIFFUSEMAP_7
        #ifdef NORMALMAP_7
            BLEND_NORMAL(_7, alphaBlend1.a)
        #else
            BLEND(_7,  alphaBlend1.a)
        #endif
    #endif
    #ifdef ALPHAMAP_2
        vec4 alphaBlend2 = texture2D( m_AlphaMap_2, texCoord);  
    #elif defined(DIFFUSEMAP_8) || defined(DIFFUSEMAP_9) || defined(DIFFUSEMAP_10) || defined(DIFFUSEMAP_11) 
        vec4 alphaBlend2 = vec4(0.5);
    #endif
    #ifdef DIFFUSEMAP_8
        BLEND(_8,  alphaBlend2.r)
    #endif                   
    #ifdef DIFFUSEMAP_9
        BLEND(_9,  alphaBlend2.g)
    #endif
    #ifdef DIFFUSEMAP_10
        BLEND(_10,  alphaBlend2.b)
    #endif                   
    #ifdef DIFFUSEMAP_11
        BLEND(_11,  alphaBlend2.a)
    #endif          

    float specular = 0.5;
    Roughness =  max(Roughness, 1e-8);
    #ifdef ROUGHNESSMAP
        Roughness = texture2D(m_RoughnessMap, texCoord).r * Roughness;
    #endif
    #ifdef METALLICMAP   
        Metallic = texture2D(m_MetallicMap, texCoord).r;
    #endif
 
    #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)
      normal = normalize(tbnMat * normalize(normal * vec3(2.0) - vec3(1.0)));
    #else
      normal = normalize(wNormal);            
    #endif

    #ifdef SPECGLOSSPIPELINE
          vec4 specularColor = texture2D(m_SpecularMap, texCoord);
          Roughness = 1.0 - texture2D(m_GlossMap, texCoord).r;          
          vec3 fZero = specularColor.xyz;
    #else      
        float nonMetalSpec = 0.08 * specular;
        vec4 specularColor = (nonMetalSpec - nonMetalSpec * Metallic) + diffuseColor * Metallic;
        diffuseColor = diffuseColor - diffuseColor * Metallic;
        vec3 fZero = vec3(specular);
    #endif

    #ifdef LIGHTMAP
       vec3 lightMapColor;
       #ifdef SEPARATE_TEXCOORD
          lightMapColor = texture2D(m_LightMap, texCoord2).rgb;
       #else
          lightMapColor = texture2D(m_LightMap, texCoord).rgb;
       #endif
       specularColor.rgb *= lightMapColor;
       diffuseColor.rgb  *= lightMapColor;
    #endif


    gl_FragColor.rgb = vec3(0.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;        
    }

    #ifdef INDIRECT_LIGHTING
        vec3 rv = reflect(-viewDir.xyz, normal.xyz);
        //prallax fix for spherical bounds from https://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/
        // g_LightProbeData.w is 1/probe radius + nbMipMaps, g_LightProbeData.xyz is the position of the lightProbe.
        float invRadius = fract( g_LightProbeData.w);
        float nbMipMaps = g_LightProbeData.w - invRadius;
        rv = invRadius * (wPosition - g_LightProbeData.xyz) +rv;

         //horizon fade from http://marmosetco.tumblr.com/post/81245981087
        float horiz = dot(rv, norm);
        float horizFadePower = 1.0 - Roughness;
        horiz = clamp( 1.0 + horizFadePower * horiz, 0.0, 1.0 );
        horiz *= horiz;

        vec3 indirectDiffuse = vec3(0.0);
        vec3 indirectSpecular = vec3(0.0);
        indirectDiffuse = sphericalHarmonics(normal.xyz, g_ShCoeffs) * diffuseColor.rgb;
        vec3 dominantR = getSpecularDominantDir( normal, rv.xyz, Roughness*Roughness );
        indirectSpecular = ApproximateSpecularIBLPolynomial(g_PrefEnvMap, specularColor.rgb, Roughness, ndotv, dominantR, nbMipMaps);
        indirectSpecular *= vec3(horiz);

        vec3 indirectLighting = indirectDiffuse + indirectSpecular;

        gl_FragColor.rgb = gl_FragColor.rgb + indirectLighting * step( 0.0, g_LightProbeData.w);
    #endif

    #if defined(EMISSIVE) || defined (EMISSIVEMAP)
        #ifdef EMISSIVEMAP
            vec4 emissive = texture2D(m_EmissiveMap, texCoord);
        #else
            vec4 emissive = m_Emissive;
        #endif
        gl_FragColor += emissive * pow(emissive.a, m_EmissivePower) * m_EmissiveIntensity;
    #endif

    gl_FragColor.a = 1.0;
}

But apart from a little fix this is the shader from @b00n

2 Likes

cool, I will integrate it to jMB…

This might compile without any material parameters set, but as soon as you add more than one texture it crashes again. Found some typos which cause crashing, but I’m not sure if I’ll be able to get it to work. Will post here if I can make it work.

EDIT: Managed to resolve all errors, but now everything is black. I’m on 3.2.1 stable and I guess it’s not worth further complications.

Strange - i am currently using 3 textures without any apparent problems or crashes…

In which version of the engine?

Was this question for me? I am using the latest master.

Yeah it was. See, it doesn’t work with any of the release versions. For nearly all people that means it doesn’t work at all.