<commit_before>pc.gfx.programlib.phong = {
    generateKey: function (device, options) {
        var key = "phong";
        if (options.skin)                key += "_skin";
        switch (options.fog) {
            case 'none':
                key += "_fogn";
                break;
            case 'linear':
                key += "_fogl";
                break;
            case 'exp2':
                key += "_foge";
                break;
        }
        if (options.numDirs > 0)         key += "_" + options.numDirs + "dir";
        if (options.numPnts > 0)         key += "_" + options.numPnts + "pnt";
        if (options.numSpts > 0)         key += "_" + options.numSpts + "spt";
        if (options.numSDirs > 0)        key += "_" + options.numSDirs + "sdir";
        if (options.numSPnts > 0)        key += "_" + options.numSPnts + "spnt";
        if (options.numSSpts > 0)        key += "_" + options.numSSpts + "sspt";
        if (options.vertexColors)        key += "_vcol";

        if (options.diffuseMapTransform) {
            key += "_difx"; // Diffuse map with texture transform
        } else if (options.diffuseMap) {
            key += "_difm"; // Diffuse map
        } else {
            key += "_difc"; // Diffuse color
        }

        if (options.specularMapTransform) {
            key += "_spex"; // Specular map with texture transform
        } else if (options.specularMap) {
            key += "_spem"; // Specular map
        } else {
            key += "_spec"; // Specular color
        }

        if (options.glossMapTransform) {
            key += "_glox"; // Gloss map with texture transform
        } else if (options.glossMap) {
            key += "_glom"; // Gloss map
        }

        if (options.emissiveMapTransform) {
            key += "_emix"; // Emissive map with texture transform
        } else if (options.emissiveMap) {
            key += "_emim"; // Emissive map
        } else {
            key += "_emic"; // Emissive color
        }

        if (options.opacityMapTransform) {
            key += "_opax"; // Opacity map with texture transform
        } else if (options.opacityMap) {
            key += "_opam"; // Opacity map
        }

        if (options.normalMapTransform) {
            key += "_norx"; // Normal map with texture transform
        } else if (options.normalMap) {
            key += "_norm"; // Normal map
        }

        if (options.heightMapTransform) {
            key += "_hgtx"; // Height map with texture transform
        } else if (options.heightMap) {
            key += "_hgtm"; // Height map
        }

        if (options.sphereMap)           key += "_sphr";
        if (options.cubeMap)             key += "_cube";
        if (options.lightMap)            key += "_lght";

        return key;
    },

    createShaderDefinition: function (device, options) {
        var i;
        var numNormalLights = options.numDirs + options.numPnts + options.numSpts;
        var numShadowLights = options.numSDirs + options.numSPnts + options.numSSpts;
        var totalDirs = options.numDirs + options.numSDirs;
        var totalPnts = options.numPnts + options.numSPnts;
        var totalSpts = options.numSpts + options.numSSpts;
        var totalLights = numNormalLights + numShadowLights;
        var lighting = totalLights > 0;
        var mapWithoutTransform =
           ((options.diffuseMap && !options.diffuseMapTransform) ||
            (options.specularMap && !options.specularMapTransform) ||
            (options.glossMap && !options.glossMapTransform) ||
            (options.emissiveMap && !options.emissiveMapTransform) ||
            (options.opacityMap && !options.opacityMapTransform) ||
            (options.normalMap && !options.normalMapTransform) ||
            (options.heightMap && !options.heightMapTransform));
        var useTangents = pc.gfx.precalculatedTangents;

        /////////////////////////
        // GENERATE ATTRIBUTES //
        /////////////////////////
        var attributes = {
            vertex_position: pc.gfx.SEMANTIC_POSITION
        }
        if (lighting || options.cubeMap || options.sphereMap) {
            attributes.vertex_normal = pc.gfx.SEMANTIC_NORMAL;
            if (options.normalMap && useTangents) {
                attributes.vertex_tangent = pc.gfx.SEMANTIC_TANGENT;
            }
        }
        if (options.diffuseMap || options.specularMap || options.glossMap ||
            options.emissiveMap || options.normalMap || options.heightMap || options.opacityMap) {
            attributes.vertex_texCoord0 = pc.gfx.SEMANTIC_TEXCOORD0;
        }
        if (options.lightMap) {
            attributes.vertex_texCoord1 = pc.gfx.SEMANTIC_TEXCOORD1;
        }
        if (options.vertexColors) {
            attributes.vertex_color = pc.gfx.SEMANTIC_COLOR;
        }
        if (options.skin) {
            attributes.vertex_boneWeights = pc.gfx.SEMANTIC_BLENDWEIGHT;
            attributes.vertex_boneIndices = pc.gfx.SEMANTIC_BLENDINDICES;
        }

        ////////////////////////////
        // GENERATE VERTEX SHADER //
        ////////////////////////////
        var getSnippet = pc.gfx.programlib.getSnippet;
        var code = '';

        // VERTEX SHADER INPUTS: ATTRIBUTES
        code += "attribute vec3 vertex_position;\n";
        if (lighting) {
            code += "attribute vec3 vertex_normal;\n";
            if (options.normalMap && useTangents) {
                code += "attribute vec4 vertex_tangent;\n";
            }
        }
        if (options.diffuseMap || options.specularMap || options.glossMap ||
            options.emissiveMap || options.normalMap || options.heightMap || options.opacityMap) {
            code += "attribute vec2 vertex_texCoord0;\n";
        }
        if (options.lightMap) {
            code += "attribute vec2 vertex_texCoord1;\n";
        }
        if (options.vertexColors) {
            code += "attribute vec4 vertex_color;\n";
        }
        code += "\n";

        // VERTEX SHADER INPUTS: UNIFORMS
        code += "uniform mat4 matrix_viewProjection;\n";
        code += "uniform mat4 matrix_model;\n";
        if (lighting) {
            code += "uniform mat3 matrix_normal;\n";
        }
        for (i = 0; i < totalLights; i++) {
            if (i < totalDirs) {
                code += "uniform vec3 light" + i + "_direction;\n";
            }
            if (i >= totalDirs) {
                code += "uniform vec3 light" + i + "_position;\n";
            }
            if (i >= totalDirs + totalPnts) {
                code += "uniform vec3 light" + i + "_spotDirection;\n";
            }
            if ((i >= options.numDirs && i < totalDirs) || 
                (i >= totalDirs + options.numPnts && i < totalDirs + totalPnts) || 
                (i >= totalDirs + totalPnts + options.numSpts && i < totalLights)) {
                code += "uniform mat4 light" + i + "_shadowMatrix;\n";
            }
        }
        if (lighting) {
            code += "uniform vec3 view_position;\n";
        }
        if (options.diffuseMap && options.diffuseMapTransform) {
            code += "uniform mat4 texture_diffuseMapTransform;\n";
        }
        if (options.normalMap && options.normalMapTransform) {
            code += "uniform mat4 texture_normalMapTransform;\n";
        }
        if (options.heightMap && options.heightMapTransform) {
            code += "uniform mat4 texture_heightMapTransform;\n";
        }
        if (options.opacityMap && options.opacityMapTransform) {
            code += "uniform mat4 texture_opacityMapTransform;\n";
        }
        if (options.specularMap && options.specularMapTransform) {
            code += "uniform mat4 texture_specularMapTransform;\n";
        }
        if (options.glossMap && options.glossMapTransform) {
            code += "uniform mat4 texture_glossMapTransform;\n";
        }
        if (options.emissiveMap && options.emissiveMapTransform) {
            code += "uniform mat4 texture_emissiveMapTransform;\n";
        }
        code += "\n";

        // VERTEX SHADER OUTPUTS
        if (lighting) {
            if (!(options.normalMap && useTangents)) {
                code += "varying vec3 vNormalW;\n";
            }
            code += "varying vec3 vViewDirW;\n";

            for (i = 0; i < totalLights; i++) {
                code += "varying vec3 vLight" + i + "DirW;\n";
                if (i >= totalDirs + totalPnts) {
                    code += "varying vec3 vLight" + i + "SpotDirW;\n";
                }
                if ((i >= options.numDirs && i < totalDirs) || 
                    (i >= totalDirs + options.numPnts && i < totalDirs + totalPnts) || 
                    (i >= totalDirs + totalPnts + options.numSpts && i < totalLights)) {
                    code += "varying vec4 vLight" + i + "ShadowCoord;\n";
                }
            }
        }

        if (mapWithoutTransform) {
            code += "varying vec2 vUv0;\n";
        }
        if (options.diffuseMap && options.diffuseMapTransform) {
            code += "varying vec2 vUvDiffuseMap;\n";
        }
        if (lighting) {
            if (options.specularMap && options.specularMapTransform) {
                code += "varying vec2 vUvSpecularMap;\n";
            }
            if (options.glossMap && options.glossMapTransform) {
                code += "varying vec2 vUvGlossMap;\n";
            }
            if ((options.normalMap && options.normalMapTransform) || (options.heightMap && options.heightMapTransform)) {
                code += "varying vec2 vUvBumpMap;\n";
            }
        }
        if (options.emissiveMap && options.emissiveMapTransform) {
            code += "varying vec2 vUvEmissiveMap;\n";
        }
        if (options.opacityMap && options.opacityMapTransform) {
            code += "varying vec2 vUvOpacityMap;\n";
        }
        if (options.lightMap) {
            code += "varying vec2 vUvLightMap;\n";
        }

        if (options.vertexColors) {
            code += "varying vec4 vVertexColor;\n";
        }
        code += "\n";

        if (options.skin) {
            code += getSnippet(device, 'vs_skin_decl');
        }

        // VERTEX SHADER BODY
        code += "void main(void)\n";
        code += "{\n";
        // SKINNING
        if (options.skin) {
            code += "    mat4 modelMatrix = vertex_boneWeights.x * getBoneMatrix(vertex_boneIndices.x) +\n";
            code += "                       vertex_boneWeights.y * getBoneMatrix(vertex_boneIndices.y) +\n";
            code += "                       vertex_boneWeights.z * getBoneMatrix(vertex_boneIndices.z) +\n";
            code += "                       vertex_boneWeights.w * getBoneMatrix(vertex_boneIndices.w);\n";
            code += "    mat3 normalMatrix = mat3(modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[2].xyz);\n";
        } else {
            code += "    mat4 modelMatrix = matrix_model;\n";
            code += "    mat3 normalMatrix = matrix_normal;\n";
        }
        code += "\n";

        // TRANSFORM
        code += "    vec4 positionW = modelMatrix * vec4(vertex_position, 1.0);\n";
        code += "    gl_Position = matrix_viewProjection * positionW;\n\n";
        if (lighting) {
            code += "    vec3 normalW = normalMatrix * vertex_normal;\n";
            code += "    normalW = normalize(normalW);\n";
            if (options.normalMap && useTangents) {
                code += "    vec3 tangentW = normalMatrix * vertex_tangent.xyz;\n";
                code += "    tangentW  = normalize(tangentW);\n";
            }
        }
        code += "\n";<commit_msg>[FIX] Phong shader generation where there are no lights enabled.<commit_after>pc.gfx.programlib.phong = {
    generateKey: function (device, options) {
        var key = "phong";
        if (options.skin)                key += "_skin";
        switch (options.fog) {
            case 'none':
                key += "_fogn";
                break;
            case 'linear':
                key += "_fogl";
                break;
            case 'exp2':
                key += "_foge";
                break;
        }
        if (options.numDirs > 0)         key += "_" + options.numDirs + "dir";
        if (options.numPnts > 0)         key += "_" + options.numPnts + "pnt";
        if (options.numSpts > 0)         key += "_" + options.numSpts + "spt";
        if (options.numSDirs > 0)        key += "_" + options.numSDirs + "sdir";
        if (options.numSPnts > 0)        key += "_" + options.numSPnts + "spnt";
        if (options.numSSpts > 0)        key += "_" + options.numSSpts + "sspt";
        if (options.vertexColors)        key += "_vcol";

        if (options.diffuseMapTransform) {
            key += "_difx"; // Diffuse map with texture transform
        } else if (options.diffuseMap) {
            key += "_difm"; // Diffuse map
        } else {
            key += "_difc"; // Diffuse color
        }

        if (options.specularMapTransform) {
            key += "_spex"; // Specular map with texture transform
        } else if (options.specularMap) {
            key += "_spem"; // Specular map
        } else {
            key += "_spec"; // Specular color
        }

        if (options.glossMapTransform) {
            key += "_glox"; // Gloss map with texture transform
        } else if (options.glossMap) {
            key += "_glom"; // Gloss map
        }

        if (options.emissiveMapTransform) {
            key += "_emix"; // Emissive map with texture transform
        } else if (options.emissiveMap) {
            key += "_emim"; // Emissive map
        } else {
            key += "_emic"; // Emissive color
        }

        if (options.opacityMapTransform) {
            key += "_opax"; // Opacity map with texture transform
        } else if (options.opacityMap) {
            key += "_opam"; // Opacity map
        }

        if (options.normalMapTransform) {
            key += "_norx"; // Normal map with texture transform
        } else if (options.normalMap) {
            key += "_norm"; // Normal map
        }

        if (options.heightMapTransform) {
            key += "_hgtx"; // Height map with texture transform
        } else if (options.heightMap) {
            key += "_hgtm"; // Height map
        }

        if (options.sphereMap)           key += "_sphr";
        if (options.cubeMap)             key += "_cube";
        if (options.lightMap)            key += "_lght";

        return key;
    },

    createShaderDefinition: function (device, options) {
        var i;
        var numNormalLights = options.numDirs + options.numPnts + options.numSpts;
        var numShadowLights = options.numSDirs + options.numSPnts + options.numSSpts;
        var totalDirs = options.numDirs + options.numSDirs;
        var totalPnts = options.numPnts + options.numSPnts;
        var totalSpts = options.numSpts + options.numSSpts;
        var totalLights = numNormalLights + numShadowLights;
        var lighting = totalLights > 0;
        var mapWithoutTransform =
           ((options.diffuseMap && !options.diffuseMapTransform) ||
            (options.specularMap && !options.specularMapTransform) ||
            (options.glossMap && !options.glossMapTransform) ||
            (options.emissiveMap && !options.emissiveMapTransform) ||
            (options.opacityMap && !options.opacityMapTransform) ||
            (options.normalMap && !options.normalMapTransform) ||
            (options.heightMap && !options.heightMapTransform));
        var useTangents = pc.gfx.precalculatedTangents;

        /////////////////////////
        // GENERATE ATTRIBUTES //
        /////////////////////////
        var attributes = {
            vertex_position: pc.gfx.SEMANTIC_POSITION
        }
        if (lighting || options.cubeMap || options.sphereMap) {
            attributes.vertex_normal = pc.gfx.SEMANTIC_NORMAL;
            if (options.normalMap && useTangents) {
                attributes.vertex_tangent = pc.gfx.SEMANTIC_TANGENT;
            }
        }
        if (options.diffuseMap || options.specularMap || options.glossMap ||
            options.emissiveMap || options.normalMap || options.heightMap || options.opacityMap) {
            attributes.vertex_texCoord0 = pc.gfx.SEMANTIC_TEXCOORD0;
        }
        if (options.lightMap) {
            attributes.vertex_texCoord1 = pc.gfx.SEMANTIC_TEXCOORD1;
        }
        if (options.vertexColors) {
            attributes.vertex_color = pc.gfx.SEMANTIC_COLOR;
        }
        if (options.skin) {
            attributes.vertex_boneWeights = pc.gfx.SEMANTIC_BLENDWEIGHT;
            attributes.vertex_boneIndices = pc.gfx.SEMANTIC_BLENDINDICES;
        }

        ////////////////////////////
        // GENERATE VERTEX SHADER //
        ////////////////////////////
        var getSnippet = pc.gfx.programlib.getSnippet;
        var code = '';

        // VERTEX SHADER INPUTS: ATTRIBUTES
        code += "attribute vec3 vertex_position;\n";
        if (lighting) {
            code += "attribute vec3 vertex_normal;\n";
            if (options.normalMap && useTangents) {
                code += "attribute vec4 vertex_tangent;\n";
            }
        }
        if (options.diffuseMap || options.specularMap || options.glossMap ||
            options.emissiveMap || options.normalMap || options.heightMap || options.opacityMap) {
            code += "attribute vec2 vertex_texCoord0;\n";
        }
        if (options.lightMap) {
            code += "attribute vec2 vertex_texCoord1;\n";
        }
        if (options.vertexColors) {
            code += "attribute vec4 vertex_color;\n";
        }
        code += "\n";

        // VERTEX SHADER INPUTS: UNIFORMS
        code += "uniform mat4 matrix_viewProjection;\n";
        code += "uniform mat4 matrix_model;\n";
        if (lighting) {
            code += "uniform mat3 matrix_normal;\n";
        }
        for (i = 0; i < totalLights; i++) {
            if (i < totalDirs) {
                code += "uniform vec3 light" + i + "_direction;\n";
            }
            if (i >= totalDirs) {
                code += "uniform vec3 light" + i + "_position;\n";
            }
            if (i >= totalDirs + totalPnts) {
                code += "uniform vec3 light" + i + "_spotDirection;\n";
            }
            if ((i >= options.numDirs && i < totalDirs) || 
                (i >= totalDirs + options.numPnts && i < totalDirs + totalPnts) || 
                (i >= totalDirs + totalPnts + options.numSpts && i < totalLights)) {
                code += "uniform mat4 light" + i + "_shadowMatrix;\n";
            }
        }
        if (lighting) {
            code += "uniform vec3 view_position;\n";
        }
        if (options.diffuseMap && options.diffuseMapTransform) {
            code += "uniform mat4 texture_diffuseMapTransform;\n";
        }
        if (options.normalMap && options.normalMapTransform) {
            code += "uniform mat4 texture_normalMapTransform;\n";
        }
        if (options.heightMap && options.heightMapTransform) {
            code += "uniform mat4 texture_heightMapTransform;\n";
        }
        if (options.opacityMap && options.opacityMapTransform) {
            code += "uniform mat4 texture_opacityMapTransform;\n";
        }
        if (options.specularMap && options.specularMapTransform) {
            code += "uniform mat4 texture_specularMapTransform;\n";
        }
        if (options.glossMap && options.glossMapTransform) {
            code += "uniform mat4 texture_glossMapTransform;\n";
        }
        if (options.emissiveMap && options.emissiveMapTransform) {
            code += "uniform mat4 texture_emissiveMapTransform;\n";
        }
        code += "\n";

        // VERTEX SHADER OUTPUTS
        if (lighting) {
            if (!(options.normalMap && useTangents)) {
                code += "varying vec3 vNormalW;\n";
            }
            code += "varying vec3 vViewDirW;\n";

            for (i = 0; i < totalLights; i++) {
                code += "varying vec3 vLight" + i + "DirW;\n";
                if (i >= totalDirs + totalPnts) {
                    code += "varying vec3 vLight" + i + "SpotDirW;\n";
                }
                if ((i >= options.numDirs && i < totalDirs) || 
                    (i >= totalDirs + options.numPnts && i < totalDirs + totalPnts) || 
                    (i >= totalDirs + totalPnts + options.numSpts && i < totalLights)) {
                    code += "varying vec4 vLight" + i + "ShadowCoord;\n";
                }
            }
        }

        if (mapWithoutTransform) {
            code += "varying vec2 vUv0;\n";
        }
        if (options.diffuseMap && options.diffuseMapTransform) {
            code += "varying vec2 vUvDiffuseMap;\n";
        }
        if (lighting) {
            if (options.specularMap && options.specularMapTransform) {
                code += "varying vec2 vUvSpecularMap;\n";
            }
            if (options.glossMap && options.glossMapTransform) {
                code += "varying vec2 vUvGlossMap;\n";
            }
            if ((options.normalMap && options.normalMapTransform) || (options.heightMap && options.heightMapTransform)) {
                code += "varying vec2 vUvBumpMap;\n";
            }
        }
        if (options.emissiveMap && options.emissiveMapTransform) {
            code += "varying vec2 vUvEmissiveMap;\n";
        }
        if (options.opacityMap && options.opacityMapTransform) {
            code += "varying vec2 vUvOpacityMap;\n";
        }
        if (options.lightMap) {
            code += "varying vec2 vUvLightMap;\n";
        }

        if (options.vertexColors) {
            code += "varying vec4 vVertexColor;\n";
        }
        code += "\n";

        if (options.skin) {
            code += getSnippet(device, 'vs_skin_decl');
        }

        // VERTEX SHADER BODY
        code += "void main(void)\n";
        code += "{\n";
        // SKINNING
        if (options.skin) {
            code += "    mat4 modelMatrix = vertex_boneWeights.x * getBoneMatrix(vertex_boneIndices.x) +\n";
            code += "                       vertex_boneWeights.y * getBoneMatrix(vertex_boneIndices.y) +\n";
            code += "                       vertex_boneWeights.z * getBoneMatrix(vertex_boneIndices.z) +\n";
            code += "                       vertex_boneWeights.w * getBoneMatrix(vertex_boneIndices.w);\n";
            if (lighting) {
                code += "    mat3 normalMatrix = mat3(modelMatrix[0].xyz, modelMatrix[1].xyz, modelMatrix[2].xyz);\n";
            }
        } else {
            code += "    mat4 modelMatrix = matrix_model;\n";
            if (lighting) {
                code += "    mat3 normalMatrix = matrix_normal;\n";
            }
        }
        code += "\n";

        // TRANSFORM
        code += "    vec4 positionW = modelMatrix * vec4(vertex_position, 1.0);\n";
        code += "    gl_Position = matrix_viewProjection * positionW;\n\n";
        if (lighting) {
            code += "    vec3 normalW = normalMatrix * vertex_normal;\n";
            code += "    normalW = normalize(normalW);\n";
            if (options.normalMap && useTangents) {
                code += "    vec3 tangentW = normalMatrix * vertex_tangent.xyz;\n";
                code += "    tangentW  = normalize(tangentW);\n";
            }
        }
        code += "\n";
{'fraction_non_alphanumeric': 0.13093660496729792, 'fraction_numerical': 0.005642713632283161, 'mean_word_length': 1.6258839375912, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '814236', 'n_tokens_mistral': 6906, 'n_tokens_neox': 6717, 'n_words': 1420}