forEachTextureInMaterial.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import ForEach from "./ForEach.js";
  2. import Check from "../../Core/Check.js";
  3. import defined from "../../Core/defined.js";
  4. /**
  5. * Calls the provider handler function on each texture used by the material.
  6. * Mimics the behavior of functions in gltf-pipeline ForEach.
  7. * @param {Object} material The glTF material.
  8. * @param {forEachTextureInMaterial~handler} handler Function that is called for each texture in the material.
  9. *
  10. * @private
  11. */
  12. function forEachTextureInMaterial(material, handler) {
  13. Check.typeOf.object("material", material);
  14. Check.defined("handler", handler);
  15. // Metallic roughness
  16. const pbrMetallicRoughness = material.pbrMetallicRoughness;
  17. if (defined(pbrMetallicRoughness)) {
  18. if (defined(pbrMetallicRoughness.baseColorTexture)) {
  19. const textureInfo = pbrMetallicRoughness.baseColorTexture;
  20. const value = handler(textureInfo.index, textureInfo);
  21. if (defined(value)) {
  22. return value;
  23. }
  24. }
  25. if (defined(pbrMetallicRoughness.metallicRoughnessTexture)) {
  26. const textureInfo = pbrMetallicRoughness.metallicRoughnessTexture;
  27. const value = handler(textureInfo.index, textureInfo);
  28. if (defined(value)) {
  29. return value;
  30. }
  31. }
  32. }
  33. if (defined(material.extensions)) {
  34. // Spec gloss extension
  35. const pbrSpecularGlossiness =
  36. material.extensions.KHR_materials_pbrSpecularGlossiness;
  37. if (defined(pbrSpecularGlossiness)) {
  38. if (defined(pbrSpecularGlossiness.diffuseTexture)) {
  39. const textureInfo = pbrSpecularGlossiness.diffuseTexture;
  40. const value = handler(textureInfo.index, textureInfo);
  41. if (defined(value)) {
  42. return value;
  43. }
  44. }
  45. if (defined(pbrSpecularGlossiness.specularGlossinessTexture)) {
  46. const textureInfo = pbrSpecularGlossiness.specularGlossinessTexture;
  47. const value = handler(textureInfo.index, textureInfo);
  48. if (defined(value)) {
  49. return value;
  50. }
  51. }
  52. }
  53. // Materials common extension (may be present in models converted from glTF 1.0)
  54. const materialsCommon = material.extensions.KHR_materials_common;
  55. if (defined(materialsCommon)) {
  56. const diffuse = materialsCommon.values.diffuse;
  57. const ambient = materialsCommon.values.ambient;
  58. const emission = materialsCommon.values.emission;
  59. const specular = materialsCommon.values.specular;
  60. if (defined(diffuse) && defined(diffuse.index)) {
  61. const value = handler(diffuse.index, diffuse);
  62. if (defined(value)) {
  63. return value;
  64. }
  65. }
  66. if (defined(ambient) && defined(ambient.index)) {
  67. const value = handler(ambient.index, ambient);
  68. if (defined(value)) {
  69. return value;
  70. }
  71. }
  72. if (defined(emission) && defined(emission.index)) {
  73. const value = handler(emission.index, emission);
  74. if (defined(value)) {
  75. return value;
  76. }
  77. }
  78. if (defined(specular) && defined(specular.index)) {
  79. const value = handler(specular.index, specular);
  80. if (defined(value)) {
  81. return value;
  82. }
  83. }
  84. }
  85. }
  86. // KHR_techniques_webgl extension
  87. const value = ForEach.materialValue(material, function (materialValue) {
  88. if (defined(materialValue.index)) {
  89. const value = handler(materialValue.index, materialValue);
  90. if (defined(value)) {
  91. return value;
  92. }
  93. }
  94. });
  95. if (defined(value)) {
  96. return value;
  97. }
  98. // Top level textures
  99. if (defined(material.emissiveTexture)) {
  100. const textureInfo = material.emissiveTexture;
  101. const value = handler(textureInfo.index, textureInfo);
  102. if (defined(value)) {
  103. return value;
  104. }
  105. }
  106. if (defined(material.normalTexture)) {
  107. const textureInfo = material.normalTexture;
  108. const value = handler(textureInfo.index, textureInfo);
  109. if (defined(value)) {
  110. return value;
  111. }
  112. }
  113. if (defined(material.occlusionTexture)) {
  114. const textureInfo = material.occlusionTexture;
  115. const value = handler(textureInfo.index, textureInfo);
  116. if (defined(value)) {
  117. return value;
  118. }
  119. }
  120. }
  121. /**
  122. * Function that is called for each texture in the material. If this function returns a value the for each stops and returns that value.
  123. * @callback forEachTextureInMaterial~handler
  124. * @param {Number} The texture index.
  125. * @param {Object} The texture info object.
  126. *
  127. * @private
  128. */
  129. export default forEachTextureInMaterial;