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

56.52% Statements 26/46
29.03% Branches 9/31
66.66% Functions 4/6
57.77% Lines 26/45

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          1x   1x               8x 8x       8x     1x                                                                         8x 8x 8x 8x 4x     4x 4x 4x 4x         4x                                         4x       4x       4x               4x 4x   4x 4x       4x 4x                   4x        
import addExtensionsUsed from "./addExtensionsUsed.js";
import ForEach from "./ForEach.js";
import defined from "../../Core/defined.js";
import WebGLConstants from "../../Core/WebGLConstants.js";
 
const defaultBlendEquation = [WebGLConstants.FUNC_ADD, WebGLConstants.FUNC_ADD];
 
const defaultBlendFactors = [
  WebGLConstants.ONE,
  WebGLConstants.ZERO,
  WebGLConstants.ONE,
  WebGLConstants.ZERO,
];
 
function isStateEnabled(renderStates, state) {
  const enabled = renderStates.enable;
  Iif (!defined(enabled)) {
    return false;
  }
 
  return enabled.indexOf(state) > -1;
}
 
const supportedBlendFactors = [
  WebGLConstants.ZERO,
  WebGLConstants.ONE,
  WebGLConstants.SRC_COLOR,
  WebGLConstants.ONE_MINUS_SRC_COLOR,
  WebGLConstants.SRC_ALPHA,
  WebGLConstants.ONE_MINUS_SRC_ALPHA,
  WebGLConstants.DST_ALPHA,
  WebGLConstants.ONE_MINUS_DST_ALPHA,
  WebGLConstants.DST_COLOR,
  WebGLConstants.ONE_MINUS_DST_COLOR,
];
 
// If any of the blend factors are not supported, return the default
function getSupportedBlendFactors(value, defaultValue) {
  if (!defined(value)) {
    return defaultValue;
  }
 
  for (let i = 0; i < 4; i++) {
    if (supportedBlendFactors.indexOf(value[i]) === -1) {
      return defaultValue;
    }
  }
 
  return value;
}
 
/**
 * Move glTF 1.0 technique render states to glTF 2.0 materials properties and KHR_blend extension.
 *
 * @param {object} gltf A javascript object containing a glTF asset.
 * @returns {object} The updated glTF asset.
 *
 * @private
 */
function moveTechniqueRenderStates(gltf) {
  const blendingForTechnique = {};
  const materialPropertiesForTechnique = {};
  const techniquesLegacy = gltf.techniques;
  if (!defined(techniquesLegacy)) {
    return gltf;
  }
 
  ForEach.technique(gltf, function (techniqueLegacy, techniqueIndex) {
    const renderStates = techniqueLegacy.states;
    Eif (defined(renderStates)) {
      const materialProperties = (materialPropertiesForTechnique[
        techniqueIndex
      ] = {});
 
      // If BLEND is enabled, the material should have alpha mode BLEND
      Iif (isStateEnabled(renderStates, WebGLConstants.BLEND)) {
        materialProperties.alphaMode = "BLEND";
 
        const blendFunctions = renderStates.functions;
        if (
          defined(blendFunctions) &&
          (defined(blendFunctions.blendEquationSeparate) ||
            defined(blendFunctions.blendFuncSeparate))
        ) {
          blendingForTechnique[techniqueIndex] = {
            blendEquation:
              blendFunctions.blendEquationSeparate ?? defaultBlendEquation,
            blendFactors: getSupportedBlendFactors(
              blendFunctions.blendFuncSeparate,
              defaultBlendFactors,
            ),
          };
        }
      }
 
      // If CULL_FACE is not enabled, the material should be doubleSided
      Iif (!isStateEnabled(renderStates, WebGLConstants.CULL_FACE)) {
        materialProperties.doubleSided = true;
      }
 
      delete techniqueLegacy.states;
    }
  });
 
  Iif (Object.keys(blendingForTechnique).length > 0) {
    if (!defined(gltf.extensions)) {
      gltf.extensions = {};
    }
 
    addExtensionsUsed(gltf, "KHR_blend");
  }
 
  ForEach.material(gltf, function (material) {
    Eif (defined(material.technique)) {
      const materialProperties =
        materialPropertiesForTechnique[material.technique];
      ForEach.objectLegacy(materialProperties, function (value, property) {
        material[property] = value;
      });
 
      const blending = blendingForTechnique[material.technique];
      Iif (defined(blending)) {
        if (!defined(material.extensions)) {
          material.extensions = {};
        }
 
        material.extensions.KHR_blend = blending;
      }
    }
  });
 
  return gltf;
}
 
export default moveTechniqueRenderStates;