All files / engine/Source/Scene/GltfPipeline moveTechniquesToExtension.js

98.14% Statements 53/54
72.22% Branches 13/18
100% Functions 6/6
98.14% Lines 53/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                              8x 8x 8x 8x 8x 4x             4x 4x   4x 4x               4x     4x 4x           4x     20x 20x                 20x 4x   20x       4x 4x   4x             4x 4x   4x 4x   4x 4x           4x           4x 4x 4x     4x 4x 4x       8x 5x 4x       4x 12x 4x     12x 12x 12x       4x 4x     4x     5x 5x     8x 8x 8x   8x        
import addExtensionsUsed from "./addExtensionsUsed.js";
import addExtensionsRequired from "./addExtensionsRequired.js";
import addToArray from "./addToArray.js";
import ForEach from "./ForEach.js";
import defined from "../../Core/defined.js";
 
/**
 * Move glTF 1.0 material techniques to glTF 2.0 KHR_techniques_webgl extension.
 *
 * @param {object} gltf A javascript object containing a glTF asset.
 * @returns {object} The updated glTF asset.
 *
 * @private
 */
function moveTechniquesToExtension(gltf) {
  const techniquesLegacy = gltf.techniques;
  const mappedUniforms = {};
  const updatedTechniqueIndices = {};
  const seenPrograms = {};
  if (defined(techniquesLegacy)) {
    const extension = {
      programs: [],
      shaders: [],
      techniques: [],
    };
 
    // Some 1.1 models have a glExtensionsUsed property that can be transferred to program.glExtensions
    const glExtensions = gltf.glExtensionsUsed;
    delete gltf.glExtensionsUsed;
 
    ForEach.technique(gltf, function (techniqueLegacy, techniqueId) {
      const technique = {
        name: techniqueLegacy.name,
        program: undefined,
        attributes: {},
        uniforms: {},
      };
 
      let parameterLegacy;
      ForEach.techniqueAttribute(
        techniqueLegacy,
        function (parameterName, attributeName) {
          parameterLegacy = techniqueLegacy.parameters[parameterName];
          technique.attributes[attributeName] = {
            semantic: parameterLegacy.semantic,
          };
        },
      );
 
      ForEach.techniqueUniform(
        techniqueLegacy,
        function (parameterName, uniformName) {
          parameterLegacy = techniqueLegacy.parameters[parameterName];
          technique.uniforms[uniformName] = {
            count: parameterLegacy.count,
            node: parameterLegacy.node,
            type: parameterLegacy.type,
            semantic: parameterLegacy.semantic,
            value: parameterLegacy.value,
          };
 
          // Store the name of the uniform to update material values.
          if (!defined(mappedUniforms[techniqueId])) {
            mappedUniforms[techniqueId] = {};
          }
          mappedUniforms[techniqueId][parameterName] = uniformName;
        },
      );
 
      if (!defined(seenPrograms[techniqueLegacy.program])) {
        const programLegacy = gltf.programs[techniqueLegacy.program];
 
        const program = {
          name: programLegacy.name,
          fragmentShader: undefined,
          vertexShader: undefined,
          glExtensions: glExtensions,
        };
 
        const fs = gltf.shaders[programLegacy.fragmentShader];
        program.fragmentShader = addToArray(extension.shaders, fs, true);
 
        const vs = gltf.shaders[programLegacy.vertexShader];
        program.vertexShader = addToArray(extension.shaders, vs, true);
 
        technique.program = addToArray(extension.programs, program);
        seenPrograms[techniqueLegacy.program] = technique.program;
      } else E{
        technique.program = seenPrograms[techniqueLegacy.program];
      }
 
      // Store the index of the new technique to reference instead.
      updatedTechniqueIndices[techniqueId] = addToArray(
        extension.techniques,
        technique,
      );
    });
 
    Eif (extension.techniques.length > 0) {
      Eif (!defined(gltf.extensions)) {
        gltf.extensions = {};
      }
 
      gltf.extensions.KHR_techniques_webgl = extension;
      addExtensionsUsed(gltf, "KHR_techniques_webgl");
      addExtensionsRequired(gltf, "KHR_techniques_webgl");
    }
  }
 
  ForEach.material(gltf, function (material) {
    if (defined(material.technique)) {
      const materialExtension = {
        technique: updatedTechniqueIndices[material.technique],
      };
 
      ForEach.objectLegacy(material.values, function (value, parameterName) {
        if (!defined(materialExtension.values)) {
          materialExtension.values = {};
        }
 
        const uniformName = mappedUniforms[material.technique][parameterName];
        Eif (defined(uniformName)) {
          materialExtension.values[uniformName] = value;
        }
      });
 
      Eif (!defined(material.extensions)) {
        material.extensions = {};
      }
 
      material.extensions.KHR_techniques_webgl = materialExtension;
    }
 
    delete material.technique;
    delete material.values;
  });
 
  delete gltf.techniques;
  delete gltf.programs;
  delete gltf.shaders;
 
  return gltf;
}
 
export default moveTechniquesToExtension;