gltfDefaults.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*global define*/
  2. define([
  3. '../Core/defaultValue',
  4. '../Core/defined'
  5. ], function(
  6. defaultValue,
  7. defined) {
  8. "use strict";
  9. function accessorDefaults(gltf) {
  10. if (!defined(gltf.accessors)) {
  11. gltf.accessors = {};
  12. }
  13. var accessors = gltf.accessors;
  14. for (var name in accessors) {
  15. if (accessors.hasOwnProperty(name)) {
  16. var accessor = accessors[name];
  17. accessor.byteStride = defaultValue(accessor.byteStride, 0);
  18. }
  19. }
  20. }
  21. function animationDefaults(gltf) {
  22. if (!defined(gltf.animations)) {
  23. gltf.animations = {};
  24. }
  25. var animations = gltf.animations;
  26. for (var name in animations) {
  27. if (animations.hasOwnProperty(name)) {
  28. var animation = animations[name];
  29. if (!defined(animation.channels)) {
  30. animation.channels = [];
  31. }
  32. if (!defined(animation.parameters)) {
  33. animation.parameters = {};
  34. }
  35. if (!defined(animation.samplers)) {
  36. animation.samplers = {};
  37. }
  38. var samplers = animations.samplers;
  39. for (var samplerName in samplers) {
  40. if (samplers.hasOwnProperty(samplerName)) {
  41. var sampler = samplers[samplerName];
  42. sampler.interpolation = defaultValue(sampler.interpolation, 'LINEAR');
  43. }
  44. }
  45. }
  46. }
  47. }
  48. function assetDefaults(gltf) {
  49. if (!defined(gltf.asset)) {
  50. gltf.asset = {};
  51. }
  52. gltf.asset.premultipliedAlpha = defaultValue(gltf.asset.premultipliedAlpha, false);
  53. }
  54. function bufferDefaults(gltf) {
  55. if (!defined(gltf.buffers)) {
  56. gltf.buffers = {};
  57. }
  58. var buffers = gltf.buffers;
  59. for (var name in buffers) {
  60. if (buffers.hasOwnProperty(name)) {
  61. var buffer = buffers[name];
  62. buffer.type = defaultValue(buffer.type, 'arraybuffer');
  63. }
  64. }
  65. }
  66. function bufferViewDefaults(gltf) {
  67. if (!defined(gltf.bufferViews)) {
  68. gltf.bufferViews = {};
  69. }
  70. }
  71. function cameraDefaults(gltf) {
  72. if (!defined(gltf.cameras)) {
  73. gltf.cameras = {};
  74. }
  75. }
  76. function imageDefaults(gltf) {
  77. if (!defined(gltf.images)) {
  78. gltf.images = {};
  79. }
  80. }
  81. function lightDefaults(gltf) {
  82. if (!defined(gltf.lights)) {
  83. gltf.lights = {};
  84. }
  85. var lights = gltf.lights;
  86. for (var name in lights) {
  87. if (lights.hasOwnProperty(name)) {
  88. var light = lights[name];
  89. if (light.type === 'ambient') {
  90. if (!defined(light.ambient)) {
  91. light.ambient = {};
  92. }
  93. var ambientLight = light.ambient;
  94. if (!defined(ambientLight.color)) {
  95. ambientLight.color = [1.0, 1.0, 1.0];
  96. }
  97. } else if (light.type === 'directional') {
  98. if (!defined(light.directional)) {
  99. light.directional = {};
  100. }
  101. var directionalLight = light.directional;
  102. if (!defined(directionalLight.color)) {
  103. directionalLight.color = [1.0, 1.0, 1.0];
  104. }
  105. } else if (light.type === 'point') {
  106. if (!defined(light.point)) {
  107. light.point = {};
  108. }
  109. var pointLight = light.point;
  110. if (!defined(pointLight.color)) {
  111. pointLight.color = [1.0, 1.0, 1.0];
  112. }
  113. pointLight.constantAttenuation = defaultValue(pointLight.constantAttenuation, 1.0);
  114. pointLight.linearAttenuation = defaultValue(pointLight.linearAttenuation, 0.0);
  115. pointLight.quadraticAttenuation = defaultValue(pointLight.quadraticAttenuation, 0.0);
  116. } else if (light.type === 'spot') {
  117. if (!defined(light.spot)) {
  118. light.spot = {};
  119. }
  120. var spotLight = light.spot;
  121. if (!defined(spotLight.color)) {
  122. spotLight.color = [1.0, 1.0, 1.0];
  123. }
  124. spotLight.constantAttenuation = defaultValue(spotLight.constantAttenuation, 1.0);
  125. spotLight.fallOffAngle = defaultValue(spotLight.fallOffAngle, 3.14159265);
  126. spotLight.fallOffExponent = defaultValue(spotLight.fallOffExponent, 0.0);
  127. spotLight.linearAttenuation = defaultValue(spotLight.linearAttenuation, 0.0);
  128. spotLight.quadraticAttenuation = defaultValue(spotLight.quadraticAttenuation, 0.0);
  129. }
  130. }
  131. }
  132. }
  133. function materialDefaults(gltf) {
  134. if (!defined(gltf.materials)) {
  135. gltf.materials = {};
  136. }
  137. var materials = gltf.materials;
  138. for (var name in materials) {
  139. if (materials.hasOwnProperty(name)) {
  140. var instanceTechnique = materials[name].instanceTechnique;
  141. if (!defined(instanceTechnique.values)) {
  142. instanceTechnique.values = {};
  143. }
  144. }
  145. }
  146. }
  147. function meshDefaults(gltf) {
  148. if (!defined(gltf.meshes)) {
  149. gltf.meshes = {};
  150. }
  151. var meshes = gltf.meshes;
  152. for (var name in meshes) {
  153. if (meshes.hasOwnProperty(name)) {
  154. var mesh = meshes[name];
  155. if (!defined(mesh.primitives)) {
  156. mesh.primitives = [];
  157. }
  158. var primitives = mesh.primitives.length;
  159. var length = primitives.length;
  160. for (var i = 0; i < length; ++i) {
  161. var primitive = primitives[i];
  162. if (!defined(primitive.attributes)) {
  163. primitive.attributes = {};
  164. }
  165. primitive.primitive = defaultValue(primitive.primitive, WebGLRenderingContext.TRIANGLES);
  166. }
  167. }
  168. }
  169. }
  170. function nodeDefaults(gltf) {
  171. if (!defined(gltf.nodes)) {
  172. gltf.nodes = {};
  173. }
  174. var nodes = gltf.nodes;
  175. for (var name in nodes) {
  176. if (nodes.hasOwnProperty(name)) {
  177. var node = nodes[name];
  178. if (!defined(node.children)) {
  179. node.children = [];
  180. }
  181. if (!defined(node.matrix)) {
  182. // Add default identity matrix if there is no matrix property and no TRS properties
  183. if (!(defined(node.translation) && defined(node.rotation) && defined(node.scale))) {
  184. node.matrix = [
  185. 1.0, 0.0, 0.0, 0.0,
  186. 0.0, 1.0, 0.0, 0.0,
  187. 0.0, 0.0, 1.0, 0.0,
  188. 0.0, 0.0, 0.0, 1.0
  189. ];
  190. } else {
  191. if (!defined(node.translation)) {
  192. node.translation = [0.0, 0.0, 0.0];
  193. }
  194. if (!defined(node.rotation)) {
  195. // GLTF_SPEC: What is the default? https://github.com/KhronosGroup/glTF/issues/197
  196. node.rotation = [1.0, 0.0, 0.0, 0.0];
  197. }
  198. if (!defined(node.scale)) {
  199. node.scale = [1.0, 1.0, 1.0];
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }
  206. function programDefaults(gltf) {
  207. if (!defined(gltf.programs)) {
  208. gltf.programs = {};
  209. }
  210. var programs = gltf.programs;
  211. for (var name in programs) {
  212. if (programs.hasOwnProperty(name)) {
  213. var program = programs[name];
  214. if (!defined(program.attributes)) {
  215. program.attributes = [];
  216. }
  217. }
  218. }
  219. }
  220. function samplerDefaults(gltf) {
  221. if (!defined(gltf.samplers)) {
  222. gltf.samplers = {};
  223. }
  224. var samplers = gltf.samplers;
  225. for (var name in samplers) {
  226. if (samplers.hasOwnProperty(name)) {
  227. var sampler = samplers[name];
  228. sampler.magFilter = defaultValue(sampler.magFilter, WebGLRenderingContext.LINEAR);
  229. sampler.minFilter = defaultValue(sampler.minFilter, WebGLRenderingContext.NEAREST_MIPMAP_LINEAR);
  230. sampler.wrapS = defaultValue(sampler.wrapS, WebGLRenderingContext.REPEAT);
  231. sampler.wrapT = defaultValue(sampler.wrapT, WebGLRenderingContext.REPEAT);
  232. }
  233. }
  234. }
  235. function sceneDefaults(gltf) {
  236. if (!defined(gltf.scenes)) {
  237. gltf.scenes = {};
  238. }
  239. var scenes = gltf.scenes;
  240. for (var name in scenes) {
  241. if (scenes.hasOwnProperty(name)) {
  242. var scene = scenes[name];
  243. if (!defined(scene.node)) {
  244. scene.node = [];
  245. }
  246. }
  247. }
  248. }
  249. function shaderDefaults(gltf) {
  250. if (!defined(gltf.shaders)) {
  251. gltf.shaders = {};
  252. }
  253. }
  254. function skinDefaults(gltf) {
  255. if (!defined(gltf.skins)) {
  256. gltf.skins = {};
  257. }
  258. var skins = gltf.skins;
  259. for (var name in skins) {
  260. if (skins.hasOwnProperty(name)) {
  261. var skin = skins[name];
  262. if (defined(skin.bindShapeMatrix)) {
  263. skin.bindShapeMatrix = [
  264. 1.0, 0.0, 0.0, 0.0,
  265. 0.0, 1.0, 0.0, 0.0,
  266. 0.0, 0.0, 1.0, 0.0,
  267. 0.0, 0.0, 0.0, 1.0
  268. ];
  269. }
  270. }
  271. }
  272. }
  273. function statesDefaults(states) {
  274. if (!defined(states.enable)) {
  275. states.enable = [];
  276. }
  277. if (!defined(states.disable)) {
  278. states.disable = [];
  279. }
  280. }
  281. function techniqueDefaults(gltf) {
  282. if (!defined(gltf.techniques)) {
  283. gltf.techniques = {};
  284. }
  285. var techniques = gltf.techniques;
  286. for (var name in techniques) {
  287. if (techniques.hasOwnProperty(name)) {
  288. var technique = techniques[name];
  289. if (!defined(technique.parameters)) {
  290. technique.parameters = {};
  291. }
  292. var passes = technique.passes;
  293. for (var passName in passes) {
  294. if (passes.hasOwnProperty(passName)) {
  295. var pass = passes[passName];
  296. var instanceProgram = pass.instanceProgram;
  297. if (!defined(instanceProgram.attributes)) {
  298. instanceProgram.attributes = {};
  299. }
  300. if (!defined(instanceProgram.uniforms)) {
  301. instanceProgram.uniforms = {};
  302. }
  303. if (!defined(pass.states)) {
  304. pass.states = {};
  305. }
  306. statesDefaults(pass.states);
  307. }
  308. }
  309. }
  310. }
  311. }
  312. function textureDefaults(gltf) {
  313. if (!defined(gltf.textures)) {
  314. gltf.textures = {};
  315. }
  316. var textures = gltf.textures;
  317. for (var name in textures) {
  318. if (textures.hasOwnProperty(name)) {
  319. var texture = textures[name];
  320. texture.format = defaultValue(texture.format, WebGLRenderingContext.RGBA);
  321. texture.internalFormat = defaultValue(texture.internalFormat, texture.format);
  322. texture.target = defaultValue(texture.target, WebGLRenderingContext.TEXTURE_2D);
  323. texture.type = defaultValue(texture.type, WebGLRenderingContext.UNSIGNED_BYTE);
  324. }
  325. }
  326. }
  327. /**
  328. * Modifies gltf in place.
  329. *
  330. * @private
  331. */
  332. var gltfDefaults = function(gltf) {
  333. if (!defined(gltf)) {
  334. return undefined;
  335. }
  336. if (!defined(gltf.allExtensions)) {
  337. gltf.allExtensions = [];
  338. }
  339. accessorDefaults(gltf);
  340. animationDefaults(gltf);
  341. assetDefaults(gltf);
  342. bufferDefaults(gltf);
  343. bufferViewDefaults(gltf);
  344. cameraDefaults(gltf);
  345. imageDefaults(gltf);
  346. lightDefaults(gltf);
  347. materialDefaults(gltf);
  348. meshDefaults(gltf);
  349. nodeDefaults(gltf);
  350. programDefaults(gltf);
  351. samplerDefaults(gltf);
  352. sceneDefaults(gltf);
  353. shaderDefaults(gltf);
  354. skinDefaults(gltf);
  355. techniqueDefaults(gltf);
  356. gltf.profile = defaultValue(gltf.profile, 'WebGL 1.0.2');
  357. gltf.version = defaultValue(gltf.version, '1.0');
  358. textureDefaults(gltf);
  359. return gltf;
  360. };
  361. return gltfDefaults;
  362. });