• See 1- Video operations
  • Shader code
  • Diffusecolor /fragColor to look at the shader

Simple thinking and analysis

MAX_LAYERS

#define MAX_LAYERS 3
#ifdef USE_BRDF
    #if (MAX_LAYERS > 0) //MAX_LAYERS 
        uniform float BRDFEnabled[MAX_LAYERS];
        uniform float BRDFSubsurfaces[MAX_LAYERS]; / / surface
        uniform float BRDFMetallics[MAX_LAYERS];  / / metal
        uniform float BRDFSpeculars[MAX_LAYERS];  / / highlights?
        uniform float BRDFSpecularTints[MAX_LAYERS];
        uniform float BRDFRoughnesses[MAX_LAYERS];  / / roughness
        uniform float BRDFAnisotropics[MAX_LAYERS]; // Anisotropy
        uniform float BRDFSheens[MAX_LAYERS];  //sheen
        uniform float BRDFSheenTints[MAX_LAYERS];
        uniform float BRDFClearcoats[MAX_LAYERS];  / / varnish
        uniform float BRDFClearcoatRoughnesses[MAX_LAYERS];
        uniform float BRDFBrightnesses[MAX_LAYERS];  //
        uniform float BRDFExposures[MAX_LAYERS];
        uniform float BRDFGammas[MAX_LAYERS];
    #endif
    varying vec3 vTangent;
    varying vec3 vBinormal;
#endif
#if (MAX_LAYERS > 0)
    uniform int mapIndex[MAX_LAYERS];  //int is only used to determine which layer is uppermost
    uniform vec3 mColor[MAX_LAYERS];  // Color // These two have some business logic
    uniform vec3 mAmbient[MAX_LAYERS];  // Environment color
    uniform vec3 mSpecular[MAX_LAYERS];  // Highlight color
    uniform vec3 mEmissive[MAX_LAYERS];  // Self-luminous color
    uniform float mShininess[MAX_LAYERS];  
    uniform float mMetal[MAX_LAYERS];
    uniform float mOpacity[MAX_LAYERS];
#endif
vec4 fragColors[MAX_LAYERS];
Copy the code

Multilayer Maps

#if defined( USE_MAP ) 
    if (w == 0) {
        #if defined(SHADER_TEXTURE_LOD) && ( defined(USE_DISPLACEMAP) || (MAX_DISPLACEMAPS > 0) )
            texelColor = texture2DLodEXT( map, mapUv + dispOffsetUv, 0. );
        #else
            texelColor = texture2D( map, mapUv + dispOffsetUv );
        #endif
    }
    #if (MAX_MAPS > 0)
        else {
            for (int z = 0; z<MAX_MAPS; z++) {
                if (z == mdx) {
                    #if defined(SHADER_TEXTURE_LOD) && ( defined(USE_DISPLACEMAP) || (MAX_DISPLACEMAPS > 0) )
                        texelColor = texture2DLodEXT( maps[z], mapUvs[z] + dispOffsetUvs[z], 0. );
                    #else
                        texelColor = texture2D( maps[z], mapUvs[z] + dispOffsetUvs[z] );
                    #endif
                }
            }
            mdx++;
        }
    #endif
    #elif (MAX_MAPS > 0)
    for (int z = 0; z<MAX_MAPS; z++) {
        if (z == mdx) {
            #if defined(SHADER_TEXTURE_LOD) && ( defined(USE_DISPLACEMAP) || (MAX_DISPLACEMAPS > 0) )
                texelColor = texture2DLodEXT( maps[z], mapUvs[z] + dispOffsetUvs[z], 0. );
            #else
                texelColor = texture2D( maps[z], mapUvs[z] + dispOffsetUvs[z] );
            #endif
        }
    }
    mdx++;
#endif
#ifdef GAMMA_INPUT
    texelColor.xyz *= texelColor.xyz;
#endif
fragColor = fragColor * texelColor;
Copy the code
#if (MAX_LAYERS > 0)
    vec4 texelColor = fragColors[0]; / / the first
    float maxAlpha = texelColor.a;  / / the first
    for (int i = 1; i<MAX_LAYERS; i++) {
        vec4 tB = fragColors[i];
        texelColor = vec4(texelColor.rgb * texelColor.a * (1.0 - tB.a) + tB.rgb * tB.a, 1.0);
        maxAlpha = max(tB.a, maxAlpha); 
    }
    gl_FragColor = vec4(texelColor.rgb, maxAlpha);
#endif
Copy the code

Overall architecture (without looking closely)

for (int w = 0; w<9; w++) {
    dispOffsetUvs[w] = vec2(0.0);
}
# if defined (USE_DISPLACEMAP) | | (MAX_DISPLACEMAPS > 0) / / vertex displacement map
#ifdef USE_SPECULARMAP // Specular map
#if (MAX_SPECULARMAPS > 0)
#ifdef USE_BRDF
    #ifdef USE_METALROUGHMAP
    #if (MAX_METALROUGHMAPS > 0)
    #ifdef USE_SPECGLOSSMAP
    #if (MAX_SPECGLOSSMAPS > 0)
vec3 normal = normalize( vNormal ); // Compute normals
#ifdef DOUBLE_SIDED // calculate normals
#ifdef STANDARD_DERIVATIVES // Compute normals
    #if defined( USE_NORMALMAP ) || ( MAX_NORMALMAPS > 0 )
    #if defined( USE_BUMPMAP ) || ( MAX_BUMPMAPS > 0 )
for (int w = 0; w<MAX_LAYERS; w++) {
    #if defined( USE_MAP )
    #ifdef GAMMA_INPUT
    #ifdef USE_BRDF
    #ifdef FORWARD_TILED_RENDER
    #if (MAX_POINT_LIGHTS > 0)
    #if (MAX_SPOT_LIGHTS > 0)
    #if (MAX_DIR_LIGHTS > 0)
    #if (MAX_AREA_LIGHTS > 0)
    #if (MAX_HEMI_LIGHTS > 0)
    #if (MAX_DIR_LIGHTS > 0)
    #if (MAX_HEMI_LIGHTS > 0)
    #if (MAX_POINT_LIGHTS > 0)
    #ifdef FORWARD_TILED_RENDER
    #if (MAX_SPOT_LIGHTS > 0)
    #if (MAX_AREA_LIGHTS > 0)
    #ifdef USE_BRDF
    #if (MAX_SPOT_LIGHT_GELS > 0)
    #if (MAX_AREA_LIGHT_GELS > 0)
    fragColors[w] = fragColor;
}
gl_FragColor = vec4(texelColor.rgb, maxAlpha);/ / summary
#ifdef USE_LIGHTMAP
#ifdef USE_COLOR
#ifdef USE_INSTANCED_ATTRB
#ifdef USE_ENVMAP
#if defined( USE_REFLECTMAP ) || defined( USE_REFRACTMAP )
#ifdef GAMMA_OUTPUT
#ifdef USE_FOG
#ifdef USE_INSTANCED_ATTRB
#ifdef USE_ALPHAMAP
#if (MAX_ALPHAMAPS > 0)
#ifdef ALPHATEST
#ifdef PREMULTIPLIED_ALPHA
Copy the code