addDefaults.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. if (technique !== "CONSTANT") {
  78. values.diffuse = defined(values.diffuse)
  79. ? values.diffuse
  80. : [0.0, 0.0, 0.0, 1.0];
  81. if (technique !== "LAMBERT") {
  82. values.specular = defined(values.specular)
  83. ? values.specular
  84. : [0.0, 0.0, 0.0, 1.0];
  85. values.shininess = defaultValue(values.shininess, 0.0);
  86. }
  87. }
  88. // These actually exist on the extension object, not the values object despite what's shown in the spec
  89. materialsCommon.transparent = defaultValue(
  90. materialsCommon.transparent,
  91. false
  92. );
  93. materialsCommon.doubleSided = defaultValue(
  94. materialsCommon.doubleSided,
  95. false
  96. );
  97. return;
  98. }
  99. material.emissiveFactor = defaultValue(
  100. material.emissiveFactor,
  101. [0.0, 0.0, 0.0]
  102. );
  103. material.alphaMode = defaultValue(material.alphaMode, "OPAQUE");
  104. material.doubleSided = defaultValue(material.doubleSided, false);
  105. if (material.alphaMode === "MASK") {
  106. material.alphaCutoff = defaultValue(material.alphaCutoff, 0.5);
  107. }
  108. const techniquesExtension = extensions.KHR_techniques_webgl;
  109. if (defined(techniquesExtension)) {
  110. ForEach.materialValue(material, function (materialValue) {
  111. // Check if material value is a TextureInfo object
  112. if (defined(materialValue.index)) {
  113. addTextureDefaults(materialValue);
  114. }
  115. });
  116. }
  117. addTextureDefaults(material.emissiveTexture);
  118. addTextureDefaults(material.normalTexture);
  119. addTextureDefaults(material.occlusionTexture);
  120. const pbrMetallicRoughness = material.pbrMetallicRoughness;
  121. if (defined(pbrMetallicRoughness)) {
  122. pbrMetallicRoughness.baseColorFactor = defaultValue(
  123. pbrMetallicRoughness.baseColorFactor,
  124. [1.0, 1.0, 1.0, 1.0]
  125. );
  126. pbrMetallicRoughness.metallicFactor = defaultValue(
  127. pbrMetallicRoughness.metallicFactor,
  128. 1.0
  129. );
  130. pbrMetallicRoughness.roughnessFactor = defaultValue(
  131. pbrMetallicRoughness.roughnessFactor,
  132. 1.0
  133. );
  134. addTextureDefaults(pbrMetallicRoughness.baseColorTexture);
  135. addTextureDefaults(pbrMetallicRoughness.metallicRoughnessTexture);
  136. }
  137. const pbrSpecularGlossiness =
  138. extensions.KHR_materials_pbrSpecularGlossiness;
  139. if (defined(pbrSpecularGlossiness)) {
  140. pbrSpecularGlossiness.diffuseFactor = defaultValue(
  141. pbrSpecularGlossiness.diffuseFactor,
  142. [1.0, 1.0, 1.0, 1.0]
  143. );
  144. pbrSpecularGlossiness.specularFactor = defaultValue(
  145. pbrSpecularGlossiness.specularFactor,
  146. [1.0, 1.0, 1.0]
  147. );
  148. pbrSpecularGlossiness.glossinessFactor = defaultValue(
  149. pbrSpecularGlossiness.glossinessFactor,
  150. 1.0
  151. );
  152. addTextureDefaults(pbrSpecularGlossiness.specularGlossinessTexture);
  153. }
  154. });
  155. ForEach.animation(gltf, function (animation) {
  156. ForEach.animationSampler(animation, function (sampler) {
  157. sampler.interpolation = defaultValue(sampler.interpolation, "LINEAR");
  158. });
  159. });
  160. const animatedNodes = getAnimatedNodes(gltf);
  161. ForEach.node(gltf, function (node, id) {
  162. const animated = defined(animatedNodes[id]);
  163. if (
  164. animated ||
  165. defined(node.translation) ||
  166. defined(node.rotation) ||
  167. defined(node.scale)
  168. ) {
  169. node.translation = defaultValue(node.translation, [0.0, 0.0, 0.0]);
  170. node.rotation = defaultValue(node.rotation, [0.0, 0.0, 0.0, 1.0]);
  171. node.scale = defaultValue(node.scale, [1.0, 1.0, 1.0]);
  172. } else {
  173. node.matrix = defaultValue(
  174. node.matrix,
  175. [
  176. 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,
  177. 0.0, 1.0,
  178. ]
  179. );
  180. }
  181. });
  182. ForEach.sampler(gltf, function (sampler) {
  183. sampler.wrapS = defaultValue(sampler.wrapS, WebGLConstants.REPEAT);
  184. sampler.wrapT = defaultValue(sampler.wrapT, WebGLConstants.REPEAT);
  185. });
  186. if (defined(gltf.scenes) && !defined(gltf.scene)) {
  187. gltf.scene = 0;
  188. }
  189. return gltf;
  190. }
  191. function getAnimatedNodes(gltf) {
  192. const nodes = {};
  193. ForEach.animation(gltf, function (animation) {
  194. ForEach.animationChannel(animation, function (channel) {
  195. const target = channel.target;
  196. const nodeId = target.node;
  197. const path = target.path;
  198. // Ignore animations that target 'weights'
  199. if (path === "translation" || path === "rotation" || path === "scale") {
  200. nodes[nodeId] = true;
  201. }
  202. });
  203. });
  204. return nodes;
  205. }
  206. function addTextureDefaults(texture) {
  207. if (defined(texture)) {
  208. texture.texCoord = defaultValue(texture.texCoord, 0);
  209. }
  210. }
  211. export default addDefaults;