moveTechniqueRenderStates.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import addExtensionsUsed from "./addExtensionsUsed.js";
  2. import ForEach from "./ForEach.js";
  3. import defaultValue from "../../Core/defaultValue.js";
  4. import defined from "../../Core/defined.js";
  5. import WebGLConstants from "../../Core/WebGLConstants.js";
  6. const defaultBlendEquation = [WebGLConstants.FUNC_ADD, WebGLConstants.FUNC_ADD];
  7. const defaultBlendFactors = [
  8. WebGLConstants.ONE,
  9. WebGLConstants.ZERO,
  10. WebGLConstants.ONE,
  11. WebGLConstants.ZERO,
  12. ];
  13. function isStateEnabled(renderStates, state) {
  14. const enabled = renderStates.enable;
  15. if (!defined(enabled)) {
  16. return false;
  17. }
  18. return enabled.indexOf(state) > -1;
  19. }
  20. const supportedBlendFactors = [
  21. WebGLConstants.ZERO,
  22. WebGLConstants.ONE,
  23. WebGLConstants.SRC_COLOR,
  24. WebGLConstants.ONE_MINUS_SRC_COLOR,
  25. WebGLConstants.SRC_ALPHA,
  26. WebGLConstants.ONE_MINUS_SRC_ALPHA,
  27. WebGLConstants.DST_ALPHA,
  28. WebGLConstants.ONE_MINUS_DST_ALPHA,
  29. WebGLConstants.DST_COLOR,
  30. WebGLConstants.ONE_MINUS_DST_COLOR,
  31. ];
  32. // If any of the blend factors are not supported, return the default
  33. function getSupportedBlendFactors(value, defaultValue) {
  34. if (!defined(value)) {
  35. return defaultValue;
  36. }
  37. for (let i = 0; i < 4; i++) {
  38. if (supportedBlendFactors.indexOf(value[i]) === -1) {
  39. return defaultValue;
  40. }
  41. }
  42. return value;
  43. }
  44. /**
  45. * Move glTF 1.0 technique render states to glTF 2.0 materials properties and KHR_blend extension.
  46. *
  47. * @param {Object} gltf A javascript object containing a glTF asset.
  48. * @returns {Object} The updated glTF asset.
  49. *
  50. * @private
  51. */
  52. function moveTechniqueRenderStates(gltf) {
  53. const blendingForTechnique = {};
  54. const materialPropertiesForTechnique = {};
  55. const techniquesLegacy = gltf.techniques;
  56. if (!defined(techniquesLegacy)) {
  57. return gltf;
  58. }
  59. ForEach.technique(gltf, function (techniqueLegacy, techniqueIndex) {
  60. const renderStates = techniqueLegacy.states;
  61. if (defined(renderStates)) {
  62. const materialProperties = (materialPropertiesForTechnique[
  63. techniqueIndex
  64. ] = {});
  65. // If BLEND is enabled, the material should have alpha mode BLEND
  66. if (isStateEnabled(renderStates, WebGLConstants.BLEND)) {
  67. materialProperties.alphaMode = "BLEND";
  68. const blendFunctions = renderStates.functions;
  69. if (
  70. defined(blendFunctions) &&
  71. (defined(blendFunctions.blendEquationSeparate) ||
  72. defined(blendFunctions.blendFuncSeparate))
  73. ) {
  74. blendingForTechnique[techniqueIndex] = {
  75. blendEquation: defaultValue(
  76. blendFunctions.blendEquationSeparate,
  77. defaultBlendEquation
  78. ),
  79. blendFactors: getSupportedBlendFactors(
  80. blendFunctions.blendFuncSeparate,
  81. defaultBlendFactors
  82. ),
  83. };
  84. }
  85. }
  86. // If CULL_FACE is not enabled, the material should be doubleSided
  87. if (!isStateEnabled(renderStates, WebGLConstants.CULL_FACE)) {
  88. materialProperties.doubleSided = true;
  89. }
  90. delete techniqueLegacy.states;
  91. }
  92. });
  93. if (Object.keys(blendingForTechnique).length > 0) {
  94. if (!defined(gltf.extensions)) {
  95. gltf.extensions = {};
  96. }
  97. addExtensionsUsed(gltf, "KHR_blend");
  98. }
  99. ForEach.material(gltf, function (material) {
  100. if (defined(material.technique)) {
  101. const materialProperties =
  102. materialPropertiesForTechnique[material.technique];
  103. ForEach.objectLegacy(materialProperties, function (value, property) {
  104. material[property] = value;
  105. });
  106. const blending = blendingForTechnique[material.technique];
  107. if (defined(blending)) {
  108. if (!defined(material.extensions)) {
  109. material.extensions = {};
  110. }
  111. material.extensions.KHR_blend = blending;
  112. }
  113. }
  114. });
  115. return gltf;
  116. }
  117. export default moveTechniqueRenderStates;