babylon.glTFSerializer.tests.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * Describes the test suite
  3. */
  4. describe('Babylon glTF Serializer', () => {
  5. let subject: BABYLON.Engine;
  6. /**
  7. * Loads the dependencies
  8. */
  9. before(function (done) {
  10. this.timeout(180000);
  11. (BABYLONDEVTOOLS).Loader
  12. .useDist()
  13. .load(function () {
  14. // Force apply promise polyfill for consistent behavior between PhantomJS, IE11, and other browsers.
  15. BABYLON.PromisePolyfill.Apply(true);
  16. done();
  17. });
  18. });
  19. /**
  20. * Create a null engine subject before each test.
  21. */
  22. beforeEach(function () {
  23. subject = new BABYLON.NullEngine({
  24. renderHeight: 256,
  25. renderWidth: 256,
  26. textureSize: 256,
  27. deterministicLockstep: false,
  28. lockstepMaxSteps: 1
  29. });
  30. });
  31. /**
  32. * This tests the glTF serializer help functions
  33. */
  34. describe('#GLTF', () => {
  35. it('should convert Babylon standard material to metallic roughness', () => {
  36. const scene = new BABYLON.Scene(subject);
  37. const babylonStandardMaterial = new BABYLON.StandardMaterial("specGloss", scene);
  38. babylonStandardMaterial.diffuseColor = BABYLON.Color3.White();
  39. babylonStandardMaterial.specularColor = BABYLON.Color3.Black();
  40. babylonStandardMaterial.specularPower = 64;
  41. babylonStandardMaterial.alpha = 1;
  42. const materialExporter = new BABYLON.GLTF2._GLTFMaterialExporter(new BABYLON.GLTF2._Exporter(scene));
  43. const metalRough = materialExporter._convertToGLTFPBRMetallicRoughness(babylonStandardMaterial);
  44. metalRough.baseColorFactor.should.deep.equal([0.5, 0.5, 0.5, 1]);
  45. metalRough.metallicFactor.should.be.equal(0);
  46. metalRough.roughnessFactor.should.be.approximately(0.328809, 1e-6);
  47. });
  48. it('should solve for metallic', () => {
  49. BABYLON.GLTF2._GLTFMaterialExporter._SolveMetallic(1.0, 0.0, 1.0).should.be.equal(0);
  50. BABYLON.GLTF2._GLTFMaterialExporter._SolveMetallic(0.0, 1.0, 1.0).should.be.approximately(1, 1e-6);
  51. });
  52. it('should serialize empty Babylon scene to glTF with only asset property', () => {
  53. const scene = new BABYLON.Scene(subject);
  54. return BABYLON.GLTF2Export.GLTFAsync(scene, 'test').then(glTFData => {
  55. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  56. const jsonData = JSON.parse(jsonString);
  57. Object.keys(jsonData).length.should.be.equal(1);
  58. jsonData.asset.version.should.be.equal("2.0");
  59. jsonData.asset.generator.should.be.equal("BabylonJS");
  60. });
  61. });
  62. it('should serialize sphere geometry in scene to glTF', () => {
  63. const scene = new BABYLON.Scene(subject);
  64. BABYLON.Mesh.CreateSphere('sphere', 16, 2, scene);
  65. return BABYLON.GLTF2Export.GLTFAsync(scene, 'test')
  66. .then(glTFData => {
  67. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  68. const jsonData = JSON.parse(jsonString);
  69. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials
  70. Object.keys(jsonData).length.should.be.equal(9);
  71. // positions, normals, indices
  72. jsonData.accessors.length.should.be.equal(3);
  73. // generator, version
  74. Object.keys(jsonData.asset).length.should.be.equal(2);
  75. jsonData.buffers.length.should.be.equal(1);
  76. // positions, normals, texture coords, indices
  77. jsonData.bufferViews.length.should.be.equal(4);
  78. jsonData.meshes.length.should.be.equal(1);
  79. jsonData.nodes.length.should.be.equal(1);
  80. jsonData.scenes.length.should.be.equal(1);
  81. jsonData.scene.should.be.equal(0);
  82. });
  83. });
  84. it('should serialize single component translation animation to glTF', () => {
  85. const scene = new BABYLON.Scene(subject);
  86. const box = BABYLON.Mesh.CreateBox('box', 1, scene);
  87. let keys: BABYLON.IAnimationKey[] = [];
  88. keys.push({
  89. frame: 0,
  90. value: 1
  91. });
  92. keys.push({
  93. frame: 20,
  94. value: 0.2
  95. });
  96. keys.push({
  97. frame: 40,
  98. value: 1
  99. });
  100. let animationBoxT = new BABYLON.Animation('boxAnimation_translation', 'position.y', 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  101. animationBoxT.setKeys(keys);
  102. box.animations.push(animationBoxT);
  103. return BABYLON.GLTF2Export.GLTFAsync(scene, 'test').then(glTFData => {
  104. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  105. const jsonData = JSON.parse(jsonString);
  106. jsonData.animations.length.should.be.equal(1);
  107. const animation = jsonData.animations[0];
  108. animation.channels.length.should.be.equal(1);
  109. animation.channels[0].sampler.should.be.equal(0);
  110. animation.channels[0].target.node.should.be.equal(0);
  111. animation.channels[0].target.path.should.be.equal('translation');
  112. jsonData.animations[0].samplers.length.should.be.equal(1);
  113. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
  114. Object.keys(jsonData).length.should.be.equal(10);
  115. // positions, normals, indices, animation keyframe data, animation data
  116. jsonData.accessors.length.should.be.equal(5);
  117. // generator, version
  118. Object.keys(jsonData.asset).length.should.be.equal(2);
  119. jsonData.buffers.length.should.be.equal(1);
  120. // positions, normals, texture coords, indices, animation keyframe data, animation data
  121. jsonData.bufferViews.length.should.be.equal(6);
  122. jsonData.meshes.length.should.be.equal(1);
  123. jsonData.nodes.length.should.be.equal(1);
  124. jsonData.scenes.length.should.be.equal(1);
  125. jsonData.scene.should.be.equal(0);
  126. });
  127. });
  128. it('should serialize translation animation to glTF', () => {
  129. const scene = new BABYLON.Scene(subject);
  130. const box = BABYLON.Mesh.CreateBox('box', 1, scene);
  131. let keys: BABYLON.IAnimationKey[] = [];
  132. keys.push({
  133. frame: 0,
  134. value: new BABYLON.Vector3(0.1, 0.1, 0.1)
  135. });
  136. keys.push({
  137. frame: 20,
  138. value: BABYLON.Vector3.One()
  139. });
  140. keys.push({
  141. frame: 40,
  142. value: new BABYLON.Vector3(0.1, 0.1, 0.1)
  143. });
  144. let animationBoxT = new BABYLON.Animation('boxAnimation_translation', 'position', 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  145. animationBoxT.setKeys(keys);
  146. box.animations.push(animationBoxT);
  147. return BABYLON.GLTF2Export.GLTFAsync(scene, 'test').then(glTFData => {
  148. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  149. const jsonData = JSON.parse(jsonString);
  150. jsonData.animations.length.should.be.equal(1);
  151. const animation = jsonData.animations[0];
  152. animation.channels.length.should.be.equal(1);
  153. animation.channels[0].sampler.should.be.equal(0);
  154. animation.channels[0].target.node.should.be.equal(0);
  155. animation.channels[0].target.path.should.be.equal('translation');
  156. animation.samplers.length.should.be.equal(1);
  157. animation.samplers[0].interpolation.should.be.equal('LINEAR');
  158. animation.samplers[0].input.should.be.equal(3);
  159. animation.samplers[0].output.should.be.equal(4);
  160. jsonData.animations[0].samplers.length.should.be.equal(1);
  161. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
  162. Object.keys(jsonData).length.should.be.equal(10);
  163. // positions, normals, indices, animation keyframe data, animation data
  164. jsonData.accessors.length.should.be.equal(5);
  165. // generator, version
  166. Object.keys(jsonData.asset).length.should.be.equal(2);
  167. jsonData.buffers.length.should.be.equal(1);
  168. // positions, normals, texture coords, indices, animation keyframe data, animation data
  169. jsonData.bufferViews.length.should.be.equal(6);
  170. jsonData.meshes.length.should.be.equal(1);
  171. jsonData.nodes.length.should.be.equal(1);
  172. jsonData.scenes.length.should.be.equal(1);
  173. jsonData.scene.should.be.equal(0);
  174. });
  175. });
  176. it('should serialize scale animation to glTF', () => {
  177. const scene = new BABYLON.Scene(subject);
  178. const box = BABYLON.Mesh.CreateBox('box', 1, scene);
  179. let keys: BABYLON.IAnimationKey[] = [];
  180. keys.push({
  181. frame: 0,
  182. value: new BABYLON.Vector3(0.1, 0.1, 0.1)
  183. });
  184. keys.push({
  185. frame: 20,
  186. value: BABYLON.Vector3.One()
  187. });
  188. keys.push({
  189. frame: 40,
  190. value: new BABYLON.Vector3(0.1, 0.1, 0.1)
  191. });
  192. let animationBoxT = new BABYLON.Animation('boxAnimation_translation', 'scaling', 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  193. animationBoxT.setKeys(keys);
  194. box.animations.push(animationBoxT);
  195. return BABYLON.GLTF2Export.GLTFAsync(scene, 'test').then(glTFData => {
  196. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  197. const jsonData = JSON.parse(jsonString);
  198. jsonData.animations.length.should.be.equal(1);
  199. const animation = jsonData.animations[0];
  200. animation.channels.length.should.be.equal(1);
  201. animation.channels[0].sampler.should.be.equal(0);
  202. animation.channels[0].target.node.should.be.equal(0);
  203. animation.channels[0].target.path.should.be.equal('scale');
  204. animation.samplers.length.should.be.equal(1);
  205. animation.samplers[0].interpolation.should.be.equal('LINEAR');
  206. animation.samplers[0].input.should.be.equal(3);
  207. animation.samplers[0].output.should.be.equal(4);
  208. jsonData.animations[0].samplers.length.should.be.equal(1);
  209. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
  210. Object.keys(jsonData).length.should.be.equal(10);
  211. // positions, normals, indices, animation keyframe data, animation data
  212. jsonData.accessors.length.should.be.equal(5);
  213. // generator, version
  214. Object.keys(jsonData.asset).length.should.be.equal(2);
  215. jsonData.buffers.length.should.be.equal(1);
  216. // positions, normals, texture coords, indices, animation keyframe data, animation data
  217. jsonData.bufferViews.length.should.be.equal(6);
  218. jsonData.meshes.length.should.be.equal(1);
  219. jsonData.nodes.length.should.be.equal(1);
  220. jsonData.scenes.length.should.be.equal(1);
  221. jsonData.scene.should.be.equal(0);
  222. });
  223. });
  224. it('should serialize rotation quaternion animation to glTF', () => {
  225. const scene = new BABYLON.Scene(subject);
  226. const box = BABYLON.Mesh.CreateBox('box', 1, scene);
  227. let keys: BABYLON.IAnimationKey[] = [];
  228. keys.push({
  229. frame: 0,
  230. value: new BABYLON.Quaternion(0.707, 0.0, 0.0, 0.707)
  231. });
  232. keys.push({
  233. frame: 20,
  234. value: BABYLON.Quaternion.Identity()
  235. });
  236. keys.push({
  237. frame: 40,
  238. value: new BABYLON.Quaternion(0.707, 0.0, 0.0, 0.707)
  239. });
  240. let animationBoxT = new BABYLON.Animation('boxAnimation_translation', 'rotationQuaternion', 30, BABYLON.Animation.ANIMATIONTYPE_QUATERNION, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  241. animationBoxT.setKeys(keys);
  242. box.animations.push(animationBoxT);
  243. return BABYLON.GLTF2Export.GLTFAsync(scene, 'test').then(glTFData => {
  244. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  245. const jsonData = JSON.parse(jsonString);
  246. jsonData.animations.length.should.be.equal(1);
  247. const animation = jsonData.animations[0];
  248. animation.channels.length.should.be.equal(1);
  249. animation.channels[0].sampler.should.be.equal(0);
  250. animation.channels[0].target.node.should.be.equal(0);
  251. animation.channels[0].target.path.should.be.equal('rotation');
  252. animation.samplers.length.should.be.equal(1);
  253. animation.samplers[0].interpolation.should.be.equal('LINEAR');
  254. animation.samplers[0].input.should.be.equal(3);
  255. animation.samplers[0].output.should.be.equal(4);
  256. jsonData.animations[0].samplers.length.should.be.equal(1);
  257. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
  258. Object.keys(jsonData).length.should.be.equal(10);
  259. // positions, normals, indices, animation keyframe data, animation data
  260. jsonData.accessors.length.should.be.equal(5);
  261. // generator, version
  262. Object.keys(jsonData.asset).length.should.be.equal(2);
  263. jsonData.buffers.length.should.be.equal(1);
  264. // positions, normals, texture coords, indices, animation keyframe data, animation data
  265. jsonData.bufferViews.length.should.be.equal(6);
  266. jsonData.meshes.length.should.be.equal(1);
  267. jsonData.nodes.length.should.be.equal(1);
  268. jsonData.scenes.length.should.be.equal(1);
  269. jsonData.scene.should.be.equal(0);
  270. });
  271. });
  272. it('should serialize combined animations to glTF', () => {
  273. const scene = new BABYLON.Scene(subject);
  274. const box = BABYLON.Mesh.CreateBox('box', 1, scene);
  275. const rotationKeyFrames: BABYLON.IAnimationKey[] = [];
  276. rotationKeyFrames.push({
  277. frame: 0,
  278. value: new BABYLON.Quaternion(0.707, 0.0, 0.0, 0.707)
  279. });
  280. rotationKeyFrames.push({
  281. frame: 20,
  282. value: BABYLON.Quaternion.Identity()
  283. });
  284. rotationKeyFrames.push({
  285. frame: 40,
  286. value: new BABYLON.Quaternion(0.707, 0.0, 0.0, 0.707)
  287. });
  288. const scaleKeyFrames: BABYLON.IAnimationKey[] = [];
  289. scaleKeyFrames.push({
  290. frame: 0,
  291. value: new BABYLON.Vector3(0.1, 0.1, 0.1)
  292. });
  293. scaleKeyFrames.push({
  294. frame: 20,
  295. value: BABYLON.Vector3.One()
  296. });
  297. scaleKeyFrames.push({
  298. frame: 40,
  299. value: new BABYLON.Vector3(0.1, 0.1, 0.1)
  300. });
  301. let rotationAnimationBox = new BABYLON.Animation('boxAnimation_rotation', 'rotationQuaternion', 30, BABYLON.Animation.ANIMATIONTYPE_QUATERNION, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  302. rotationAnimationBox.setKeys(rotationKeyFrames);
  303. box.animations.push(rotationAnimationBox);
  304. let scaleAnimationBox = new BABYLON.Animation('boxAnimation_scale', 'scaling', 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  305. scaleAnimationBox.setKeys(scaleKeyFrames);
  306. box.animations.push(scaleAnimationBox);
  307. return BABYLON.GLTF2Export.GLTFAsync(scene, 'test').then(glTFData => {
  308. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  309. const jsonData = JSON.parse(jsonString);
  310. jsonData.animations.length.should.be.equal(2);
  311. let animation = jsonData.animations[0];
  312. animation.channels.length.should.be.equal(1);
  313. animation.channels[0].sampler.should.be.equal(0);
  314. animation.channels[0].target.node.should.be.equal(0);
  315. animation.channels[0].target.path.should.be.equal('rotation');
  316. animation.samplers.length.should.be.equal(1);
  317. animation.samplers[0].interpolation.should.be.equal('LINEAR');
  318. animation.samplers[0].input.should.be.equal(3);
  319. animation.samplers[0].output.should.be.equal(4);
  320. animation = jsonData.animations[1];
  321. animation.channels[0].sampler.should.be.equal(0);
  322. animation.channels[0].target.node.should.be.equal(0);
  323. animation.channels[0].target.path.should.be.equal('scale');
  324. animation.samplers.length.should.be.equal(1);
  325. animation.samplers[0].interpolation.should.be.equal('LINEAR');
  326. animation.samplers[0].input.should.be.equal(5);
  327. animation.samplers[0].output.should.be.equal(6);
  328. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
  329. Object.keys(jsonData).length.should.be.equal(10);
  330. // positions, normals, indices, rotation animation keyframe data, rotation animation data, scale animation keyframe data, scale animation data
  331. jsonData.accessors.length.should.be.equal(7);
  332. // generator, version
  333. Object.keys(jsonData.asset).length.should.be.equal(2);
  334. jsonData.buffers.length.should.be.equal(1);
  335. // positions, normals, texture coords, indices, rotation animation keyframe data, rotation animation data, scale animation keyframe data, scale animation data
  336. jsonData.bufferViews.length.should.be.equal(8);
  337. jsonData.meshes.length.should.be.equal(1);
  338. jsonData.nodes.length.should.be.equal(1);
  339. jsonData.scenes.length.should.be.equal(1);
  340. jsonData.scene.should.be.equal(0);
  341. });
  342. });
  343. });
  344. });