moveTechniqueRenderStates.js 4.4 KB

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