addDefaults.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import addToArray from "./addToArray.js";
  2. import ForEach from "./ForEach.js";
  3. import getAccessorByteStride from "./getAccessorByteStride.js";
  4. import defaultValue from "../../Core/defaultValue.js";
  5. import defined from "../../Core/defined.js";
  6. import WebGLConstants from "../../Core/WebGLConstants.js";
  7. /**
  8. * Adds default glTF values if they don't exist.
  9. *
  10. * @param {Object} gltf A javascript object containing a glTF asset.
  11. * @returns {Object} The modified glTF.
  12. *
  13. * @private
  14. */
  15. function addDefaults(gltf) {
  16. ForEach.accessor(gltf, function (accessor) {
  17. if (defined(accessor.bufferView)) {
  18. accessor.byteOffset = defaultValue(accessor.byteOffset, 0);
  19. }
  20. });
  21. ForEach.bufferView(gltf, function (bufferView) {
  22. if (defined(bufferView.buffer)) {
  23. bufferView.byteOffset = defaultValue(bufferView.byteOffset, 0);
  24. }
  25. });
  26. ForEach.mesh(gltf, function (mesh) {
  27. ForEach.meshPrimitive(mesh, function (primitive) {
  28. primitive.mode = defaultValue(primitive.mode, WebGLConstants.TRIANGLES);
  29. if (!defined(primitive.material)) {
  30. if (!defined(gltf.materials)) {
  31. gltf.materials = [];
  32. }
  33. const defaultMaterial = {
  34. name: "default",
  35. };
  36. primitive.material = addToArray(gltf.materials, defaultMaterial);
  37. }
  38. });
  39. });
  40. ForEach.accessorContainingVertexAttributeData(gltf, function (accessorId) {
  41. const accessor = gltf.accessors[accessorId];
  42. const bufferViewId = accessor.bufferView;
  43. accessor.normalized = defaultValue(accessor.normalized, false);
  44. if (defined(bufferViewId)) {
  45. const bufferView = gltf.bufferViews[bufferViewId];
  46. bufferView.byteStride = getAccessorByteStride(gltf, accessor);
  47. bufferView.target = WebGLConstants.ARRAY_BUFFER;
  48. }
  49. });
  50. ForEach.accessorContainingIndexData(gltf, function (accessorId) {
  51. const accessor = gltf.accessors[accessorId];
  52. const bufferViewId = accessor.bufferView;
  53. if (defined(bufferViewId)) {
  54. const bufferView = gltf.bufferViews[bufferViewId];
  55. bufferView.target = WebGLConstants.ELEMENT_ARRAY_BUFFER;
  56. }
  57. });
  58. ForEach.material(gltf, function (material) {
  59. const extensions = defaultValue(
  60. material.extensions,
  61. defaultValue.EMPTY_OBJECT
  62. );
  63. const materialsCommon = extensions.KHR_materials_common;
  64. if (defined(materialsCommon)) {
  65. const technique = materialsCommon.technique;
  66. const values = defined(materialsCommon.values)
  67. ? materialsCommon.values
  68. : {};
  69. materialsCommon.values = values;
  70. values.ambient = defined(values.ambient)
  71. ? values.ambient
  72. : [0.0, 0.0, 0.0, 1.0];
  73. values.emission = defined(values.emission)
  74. ? values.emission
  75. : [0.0, 0.0, 0.0, 1.0];
  76. values.transparency = defaultValue(values.transparency, 1.0);
  77. values.transparent = defaultValue(values.transparent, false);
  78. values.doubleSided = defaultValue(values.doubleSided, false);
  79. if (technique !== "CONSTANT") {
  80. values.diffuse = defined(values.diffuse)
  81. ? values.diffuse
  82. : [0.0, 0.0, 0.0, 1.0];
  83. if (technique !== "LAMBERT") {
  84. values.specular = defined(values.specular)
  85. ? values.specular
  86. : [0.0, 0.0, 0.0, 1.0];
  87. values.shininess = defaultValue(values.shininess, 0.0);
  88. }
  89. }
  90. return;
  91. }
  92. material.emissiveFactor = defaultValue(
  93. material.emissiveFactor,
  94. [0.0, 0.0, 0.0]
  95. );
  96. material.alphaMode = defaultValue(material.alphaMode, "OPAQUE");
  97. material.doubleSided = defaultValue(material.doubleSided, false);
  98. if (material.alphaMode === "MASK") {
  99. material.alphaCutoff = defaultValue(material.alphaCutoff, 0.5);
  100. }
  101. const techniquesExtension = extensions.KHR_techniques_webgl;
  102. if (defined(techniquesExtension)) {
  103. ForEach.materialValue(material, function (materialValue) {
  104. // Check if material value is a TextureInfo object
  105. if (defined(materialValue.index)) {
  106. addTextureDefaults(materialValue);
  107. }
  108. });
  109. }
  110. addTextureDefaults(material.emissiveTexture);
  111. addTextureDefaults(material.normalTexture);
  112. addTextureDefaults(material.occlusionTexture);
  113. const pbrMetallicRoughness = material.pbrMetallicRoughness;
  114. if (defined(pbrMetallicRoughness)) {
  115. pbrMetallicRoughness.baseColorFactor = defaultValue(
  116. pbrMetallicRoughness.baseColorFactor,
  117. [1.0, 1.0, 1.0, 1.0]
  118. );
  119. pbrMetallicRoughness.metallicFactor = defaultValue(
  120. pbrMetallicRoughness.metallicFactor,
  121. 1.0
  122. );
  123. pbrMetallicRoughness.roughnessFactor = defaultValue(
  124. pbrMetallicRoughness.roughnessFactor,
  125. 1.0
  126. );
  127. addTextureDefaults(pbrMetallicRoughness.baseColorTexture);
  128. addTextureDefaults(pbrMetallicRoughness.metallicRoughnessTexture);
  129. }
  130. const pbrSpecularGlossiness =
  131. extensions.KHR_materials_pbrSpecularGlossiness;
  132. if (defined(pbrSpecularGlossiness)) {
  133. pbrSpecularGlossiness.diffuseFactor = defaultValue(
  134. pbrSpecularGlossiness.diffuseFactor,
  135. [1.0, 1.0, 1.0, 1.0]
  136. );
  137. pbrSpecularGlossiness.specularFactor = defaultValue(
  138. pbrSpecularGlossiness.specularFactor,
  139. [1.0, 1.0, 1.0]
  140. );
  141. pbrSpecularGlossiness.glossinessFactor = defaultValue(
  142. pbrSpecularGlossiness.glossinessFactor,
  143. 1.0
  144. );
  145. addTextureDefaults(pbrSpecularGlossiness.specularGlossinessTexture);
  146. }
  147. });
  148. ForEach.animation(gltf, function (animation) {
  149. ForEach.animationSampler(animation, function (sampler) {
  150. sampler.interpolation = defaultValue(sampler.interpolation, "LINEAR");
  151. });
  152. });
  153. const animatedNodes = getAnimatedNodes(gltf);
  154. ForEach.node(gltf, function (node, id) {
  155. const animated = defined(animatedNodes[id]);
  156. if (
  157. animated ||
  158. defined(node.translation) ||
  159. defined(node.rotation) ||
  160. defined(node.scale)
  161. ) {
  162. node.translation = defaultValue(node.translation, [0.0, 0.0, 0.0]);
  163. node.rotation = defaultValue(node.rotation, [0.0, 0.0, 0.0, 1.0]);
  164. node.scale = defaultValue(node.scale, [1.0, 1.0, 1.0]);
  165. } else {
  166. node.matrix = defaultValue(
  167. node.matrix,
  168. [
  169. 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
  170. 0.0, 1.0,
  171. ]
  172. );
  173. }
  174. });
  175. ForEach.sampler(gltf, function (sampler) {
  176. sampler.wrapS = defaultValue(sampler.wrapS, WebGLConstants.REPEAT);
  177. sampler.wrapT = defaultValue(sampler.wrapT, WebGLConstants.REPEAT);
  178. });
  179. if (defined(gltf.scenes) && !defined(gltf.scene)) {
  180. gltf.scene = 0;
  181. }
  182. return gltf;
  183. }
  184. function getAnimatedNodes(gltf) {
  185. const nodes = {};
  186. ForEach.animation(gltf, function (animation) {
  187. ForEach.animationChannel(animation, function (channel) {
  188. const target = channel.target;
  189. const nodeId = target.node;
  190. const path = target.path;
  191. // Ignore animations that target 'weights'
  192. if (path === "translation" || path === "rotation" || path === "scale") {
  193. nodes[nodeId] = true;
  194. }
  195. });
  196. });
  197. return nodes;
  198. }
  199. function addTextureDefaults(texture) {
  200. if (defined(texture)) {
  201. texture.texCoord = defaultValue(texture.texCoord, 0);
  202. }
  203. }
  204. export default addDefaults;