babylon.glTFSerializer.tests.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. done();
  15. });
  16. });
  17. /**
  18. * Create a null engine subject before each test.
  19. */
  20. beforeEach(function () {
  21. subject = new BABYLON.NullEngine({
  22. renderHeight: 256,
  23. renderWidth: 256,
  24. textureSize: 256,
  25. deterministicLockstep: false,
  26. lockstepMaxSteps: 1
  27. });
  28. });
  29. /**
  30. * This tests the glTF serializer help functions
  31. */
  32. describe('#GLTF', () => {
  33. it('should get alpha mode from Babylon metallic roughness', () => {
  34. let alphaMode: string;
  35. const scene = new BABYLON.Scene(subject);
  36. const babylonMaterial = new BABYLON.PBRMetallicRoughnessMaterial("metallicroughness", scene);
  37. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
  38. alphaMode = BABYLON.GLTF2._GLTFMaterial._GetAlphaMode(babylonMaterial);
  39. alphaMode.should.be.equal('OPAQUE');
  40. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND;
  41. alphaMode = BABYLON.GLTF2._GLTFMaterial._GetAlphaMode(babylonMaterial);
  42. alphaMode.should.be.equal('BLEND');
  43. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATESTANDBLEND;
  44. alphaMode = BABYLON.GLTF2._GLTFMaterial._GetAlphaMode(babylonMaterial);
  45. alphaMode.should.be.equal('BLEND');
  46. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST;
  47. alphaMode = BABYLON.GLTF2._GLTFMaterial._GetAlphaMode(babylonMaterial);
  48. alphaMode.should.be.equal('MASK');
  49. });
  50. it('should convert Babylon standard material to metallic roughness', () => {
  51. const scene = new BABYLON.Scene(subject);
  52. const babylonStandardMaterial = new BABYLON.StandardMaterial("specGloss", scene);
  53. babylonStandardMaterial.diffuseColor = BABYLON.Color3.White();
  54. babylonStandardMaterial.specularColor = BABYLON.Color3.Black();
  55. babylonStandardMaterial.specularPower = 64;
  56. babylonStandardMaterial.alpha = 1;
  57. const metalRough = BABYLON.GLTF2._GLTFMaterial._ConvertToGLTFPBRMetallicRoughness(babylonStandardMaterial);
  58. metalRough.baseColorFactor.should.deep.equal([0.5, 0.5, 0.5, 1]);
  59. metalRough.metallicFactor.should.be.equal(0);
  60. metalRough.roughnessFactor.should.be.approximately(0.328809, 1e-6);
  61. });
  62. it('should solve for metallic', () => {
  63. BABYLON.GLTF2._GLTFMaterial._SolveMetallic(1.0, 0.0, 1.0).should.be.equal(0);
  64. BABYLON.GLTF2._GLTFMaterial._SolveMetallic(0.0, 1.0, 1.0).should.be.approximately(1, 1e-6);
  65. });
  66. it('should serialize empty Babylon scene to glTF with only asset property', (done) => {
  67. mocha.timeout(10000);
  68. const scene = new BABYLON.Scene(subject);
  69. scene.executeWhenReady(function () {
  70. const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
  71. const glTFData = glTFExporter._generateGLTF('test');
  72. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  73. const jsonData = JSON.parse(jsonString);
  74. Object.keys(jsonData).length.should.be.equal(1);
  75. jsonData.asset.version.should.be.equal("2.0");
  76. jsonData.asset.generator.should.be.equal("BabylonJS");
  77. done();
  78. });
  79. });
  80. it('should serialize sphere geometry in scene to glTF', (done) => {
  81. mocha.timeout(10000);
  82. const scene = new BABYLON.Scene(subject);
  83. BABYLON.Mesh.CreateSphere('sphere', 16, 2, scene);
  84. scene.executeWhenReady(function () {
  85. const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
  86. const glTFData = glTFExporter._generateGLTF('test');
  87. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  88. const jsonData = JSON.parse(jsonString);
  89. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials
  90. Object.keys(jsonData).length.should.be.equal(9);
  91. // positions, normals, texture coords, indices
  92. jsonData.accessors.length.should.be.equal(4);
  93. // generator, version
  94. Object.keys(jsonData.asset).length.should.be.equal(2);
  95. jsonData.buffers.length.should.be.equal(1);
  96. // positions, normals, texture coords, indices
  97. jsonData.bufferViews.length.should.be.equal(4);
  98. jsonData.meshes.length.should.be.equal(1);
  99. jsonData.nodes.length.should.be.equal(1);
  100. jsonData.scenes.length.should.be.equal(1);
  101. jsonData.scene.should.be.equal(0);
  102. done();
  103. });
  104. });
  105. it('should serialize alpha mode and cutoff', (done) => {
  106. mocha.timeout(10000);
  107. const scene = new BABYLON.Scene(subject);
  108. const plane = BABYLON.Mesh.CreatePlane('plane', 120, scene);
  109. const babylonPBRMetalRoughMaterial = new BABYLON.PBRMetallicRoughnessMaterial('metalRoughMat', scene);
  110. babylonPBRMetalRoughMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST;
  111. const alphaCutoff = 0.8;
  112. babylonPBRMetalRoughMaterial.alphaCutOff = alphaCutoff;
  113. plane.material = babylonPBRMetalRoughMaterial;
  114. scene.executeWhenReady(function () {
  115. const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
  116. const glTFData = glTFExporter._generateGLTF('test');
  117. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  118. const jsonData = JSON.parse(jsonString);
  119. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials
  120. Object.keys(jsonData).length.should.be.equal(9);
  121. jsonData.materials.length.should.be.equal(2);
  122. jsonData.materials[0].alphaMode.should.be.equal('MASK');
  123. jsonData.materials[0].alphaCutoff.should.be.equal(alphaCutoff);
  124. done();
  125. });
  126. });
  127. it('should serialize single component translation animation to glTF', (done) => {
  128. mocha.timeout(10000);
  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: 1
  135. });
  136. keys.push({
  137. frame: 20,
  138. value: 0.2
  139. });
  140. keys.push({
  141. frame: 40,
  142. value: 1
  143. });
  144. let animationBoxT = new BABYLON.Animation('boxAnimation_translation', 'position.y', 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  145. animationBoxT.setKeys(keys);
  146. box.animations.push(animationBoxT);
  147. scene.executeWhenReady(function () {
  148. const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
  149. const glTFData = glTFExporter._generateGLTF('test');
  150. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  151. const jsonData = JSON.parse(jsonString);
  152. jsonData.animations.length.should.be.equal(1);
  153. const animation = jsonData.animations[0];
  154. animation.channels.length.should.be.equal(1);
  155. animation.channels[0].sampler.should.be.equal(0);
  156. animation.channels[0].target.node.should.be.equal(0);
  157. animation.channels[0].target.path.should.be.equal('translation');
  158. jsonData.animations[0].samplers.length.should.be.equal(1);
  159. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
  160. Object.keys(jsonData).length.should.be.equal(10);
  161. // positions, normals, texture coords, indices, animation keyframe data, animation data
  162. jsonData.accessors.length.should.be.equal(6);
  163. // generator, version
  164. Object.keys(jsonData.asset).length.should.be.equal(2);
  165. jsonData.buffers.length.should.be.equal(1);
  166. // positions, normals, texture coords, indices, animation keyframe data, animation data
  167. jsonData.bufferViews.length.should.be.equal(6);
  168. jsonData.meshes.length.should.be.equal(1);
  169. jsonData.nodes.length.should.be.equal(1);
  170. jsonData.scenes.length.should.be.equal(1);
  171. jsonData.scene.should.be.equal(0);
  172. done();
  173. });
  174. });
  175. it('should serialize translation animation to glTF', (done) => {
  176. mocha.timeout(10000);
  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', 'position', 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  193. animationBoxT.setKeys(keys);
  194. box.animations.push(animationBoxT);
  195. scene.executeWhenReady(function () {
  196. const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
  197. const glTFData = glTFExporter._generateGLTF('test');
  198. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  199. const jsonData = JSON.parse(jsonString);
  200. jsonData.animations.length.should.be.equal(1);
  201. const animation = jsonData.animations[0];
  202. animation.channels.length.should.be.equal(1);
  203. animation.channels[0].sampler.should.be.equal(0);
  204. animation.channels[0].target.node.should.be.equal(0);
  205. animation.channels[0].target.path.should.be.equal('translation');
  206. animation.samplers.length.should.be.equal(1);
  207. animation.samplers[0].interpolation.should.be.equal('LINEAR');
  208. animation.samplers[0].input.should.be.equal(4);
  209. animation.samplers[0].output.should.be.equal(5);
  210. jsonData.animations[0].samplers.length.should.be.equal(1);
  211. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
  212. Object.keys(jsonData).length.should.be.equal(10);
  213. // positions, normals, texture coords, indices, animation keyframe data, animation data
  214. jsonData.accessors.length.should.be.equal(6);
  215. // generator, version
  216. Object.keys(jsonData.asset).length.should.be.equal(2);
  217. jsonData.buffers.length.should.be.equal(1);
  218. // positions, normals, texture coords, indices, animation keyframe data, animation data
  219. jsonData.bufferViews.length.should.be.equal(6);
  220. jsonData.meshes.length.should.be.equal(1);
  221. jsonData.nodes.length.should.be.equal(1);
  222. jsonData.scenes.length.should.be.equal(1);
  223. jsonData.scene.should.be.equal(0);
  224. done();
  225. });
  226. });
  227. it('should serialize scale animation to glTF', (done) => {
  228. mocha.timeout(10000);
  229. const scene = new BABYLON.Scene(subject);
  230. const box = BABYLON.Mesh.CreateBox('box', 1, scene);
  231. let keys: BABYLON.IAnimationKey[] = [];
  232. keys.push({
  233. frame: 0,
  234. value: new BABYLON.Vector3(0.1, 0.1, 0.1)
  235. });
  236. keys.push({
  237. frame: 20,
  238. value: BABYLON.Vector3.One()
  239. });
  240. keys.push({
  241. frame: 40,
  242. value: new BABYLON.Vector3(0.1, 0.1, 0.1)
  243. });
  244. let animationBoxT = new BABYLON.Animation('boxAnimation_translation', 'scaling', 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  245. animationBoxT.setKeys(keys);
  246. box.animations.push(animationBoxT);
  247. scene.executeWhenReady(function () {
  248. const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
  249. const glTFData = glTFExporter._generateGLTF('test');
  250. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  251. const jsonData = JSON.parse(jsonString);
  252. jsonData.animations.length.should.be.equal(1);
  253. const animation = jsonData.animations[0];
  254. animation.channels.length.should.be.equal(1);
  255. animation.channels[0].sampler.should.be.equal(0);
  256. animation.channels[0].target.node.should.be.equal(0);
  257. animation.channels[0].target.path.should.be.equal('scale');
  258. animation.samplers.length.should.be.equal(1);
  259. animation.samplers[0].interpolation.should.be.equal('LINEAR');
  260. animation.samplers[0].input.should.be.equal(4);
  261. animation.samplers[0].output.should.be.equal(5);
  262. jsonData.animations[0].samplers.length.should.be.equal(1);
  263. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
  264. Object.keys(jsonData).length.should.be.equal(10);
  265. // positions, normals, texture coords, indices, animation keyframe data, animation data
  266. jsonData.accessors.length.should.be.equal(6);
  267. // generator, version
  268. Object.keys(jsonData.asset).length.should.be.equal(2);
  269. jsonData.buffers.length.should.be.equal(1);
  270. // positions, normals, texture coords, indices, animation keyframe data, animation data
  271. jsonData.bufferViews.length.should.be.equal(6);
  272. jsonData.meshes.length.should.be.equal(1);
  273. jsonData.nodes.length.should.be.equal(1);
  274. jsonData.scenes.length.should.be.equal(1);
  275. jsonData.scene.should.be.equal(0);
  276. done();
  277. });
  278. });
  279. it('should serialize rotation quaternion animation to glTF', (done) => {
  280. mocha.timeout(10000);
  281. const scene = new BABYLON.Scene(subject);
  282. const box = BABYLON.Mesh.CreateBox('box', 1, scene);
  283. let keys: BABYLON.IAnimationKey[] = [];
  284. keys.push({
  285. frame: 0,
  286. value: new BABYLON.Quaternion(0.707, 0.0, 0.0, 0.707)
  287. });
  288. keys.push({
  289. frame: 20,
  290. value: BABYLON.Quaternion.Identity()
  291. });
  292. keys.push({
  293. frame: 40,
  294. value: new BABYLON.Quaternion(0.707, 0.0, 0.0, 0.707)
  295. });
  296. let animationBoxT = new BABYLON.Animation('boxAnimation_translation', 'rotationQuaternion', 30, BABYLON.Animation.ANIMATIONTYPE_QUATERNION, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  297. animationBoxT.setKeys(keys);
  298. box.animations.push(animationBoxT);
  299. scene.executeWhenReady(function () {
  300. const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
  301. const glTFData = glTFExporter._generateGLTF('test');
  302. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  303. const jsonData = JSON.parse(jsonString);
  304. jsonData.animations.length.should.be.equal(1);
  305. const animation = jsonData.animations[0];
  306. animation.channels.length.should.be.equal(1);
  307. animation.channels[0].sampler.should.be.equal(0);
  308. animation.channels[0].target.node.should.be.equal(0);
  309. animation.channels[0].target.path.should.be.equal('rotation');
  310. animation.samplers.length.should.be.equal(1);
  311. animation.samplers[0].interpolation.should.be.equal('LINEAR');
  312. animation.samplers[0].input.should.be.equal(4);
  313. animation.samplers[0].output.should.be.equal(5);
  314. jsonData.animations[0].samplers.length.should.be.equal(1);
  315. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
  316. Object.keys(jsonData).length.should.be.equal(10);
  317. // positions, normals, texture coords, indices, animation keyframe data, animation data
  318. jsonData.accessors.length.should.be.equal(6);
  319. // generator, version
  320. Object.keys(jsonData.asset).length.should.be.equal(2);
  321. jsonData.buffers.length.should.be.equal(1);
  322. // positions, normals, texture coords, indices, animation keyframe data, animation data
  323. jsonData.bufferViews.length.should.be.equal(6);
  324. jsonData.meshes.length.should.be.equal(1);
  325. jsonData.nodes.length.should.be.equal(1);
  326. jsonData.scenes.length.should.be.equal(1);
  327. jsonData.scene.should.be.equal(0);
  328. done();
  329. });
  330. });
  331. it('should serialize combined animations to glTF', (done) => {
  332. mocha.timeout(10000);
  333. const scene = new BABYLON.Scene(subject);
  334. const box = BABYLON.Mesh.CreateBox('box', 1, scene);
  335. const rotationKeyFrames: BABYLON.IAnimationKey[] = [];
  336. rotationKeyFrames.push({
  337. frame: 0,
  338. value: new BABYLON.Quaternion(0.707, 0.0, 0.0, 0.707)
  339. });
  340. rotationKeyFrames.push({
  341. frame: 20,
  342. value: BABYLON.Quaternion.Identity()
  343. });
  344. rotationKeyFrames.push({
  345. frame: 40,
  346. value: new BABYLON.Quaternion(0.707, 0.0, 0.0, 0.707)
  347. });
  348. const scaleKeyFrames: BABYLON.IAnimationKey[] = [];
  349. scaleKeyFrames.push({
  350. frame: 0,
  351. value: new BABYLON.Vector3(0.1, 0.1, 0.1)
  352. });
  353. scaleKeyFrames.push({
  354. frame: 20,
  355. value: BABYLON.Vector3.One()
  356. });
  357. scaleKeyFrames.push({
  358. frame: 40,
  359. value: new BABYLON.Vector3(0.1, 0.1, 0.1)
  360. });
  361. let rotationAnimationBox = new BABYLON.Animation('boxAnimation_rotation', 'rotationQuaternion', 30, BABYLON.Animation.ANIMATIONTYPE_QUATERNION, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  362. rotationAnimationBox.setKeys(rotationKeyFrames);
  363. box.animations.push(rotationAnimationBox);
  364. let scaleAnimationBox = new BABYLON.Animation('boxAnimation_scale', 'scaling', 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  365. scaleAnimationBox.setKeys(scaleKeyFrames);
  366. box.animations.push(scaleAnimationBox);
  367. scene.executeWhenReady(function () {
  368. const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
  369. const glTFData = glTFExporter._generateGLTF('test');
  370. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  371. const jsonData = JSON.parse(jsonString);
  372. jsonData.animations.length.should.be.equal(2);
  373. let animation = jsonData.animations[0];
  374. animation.channels.length.should.be.equal(1);
  375. animation.channels[0].sampler.should.be.equal(0);
  376. animation.channels[0].target.node.should.be.equal(0);
  377. animation.channels[0].target.path.should.be.equal('rotation');
  378. animation.samplers.length.should.be.equal(1);
  379. animation.samplers[0].interpolation.should.be.equal('LINEAR');
  380. animation.samplers[0].input.should.be.equal(4);
  381. animation.samplers[0].output.should.be.equal(5);
  382. animation = jsonData.animations[1];
  383. animation.channels[0].sampler.should.be.equal(0);
  384. animation.channels[0].target.node.should.be.equal(0);
  385. animation.channels[0].target.path.should.be.equal('scale');
  386. animation.samplers.length.should.be.equal(1);
  387. animation.samplers[0].interpolation.should.be.equal('LINEAR');
  388. animation.samplers[0].input.should.be.equal(6);
  389. animation.samplers[0].output.should.be.equal(7);
  390. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
  391. Object.keys(jsonData).length.should.be.equal(10);
  392. // positions, normals, texture coords, indices, rotation animation keyframe data, rotation animation data, scale animation keyframe data, scale animation data
  393. jsonData.accessors.length.should.be.equal(8);
  394. // generator, version
  395. Object.keys(jsonData.asset).length.should.be.equal(2);
  396. jsonData.buffers.length.should.be.equal(1);
  397. // positions, normals, texture coords, indices, rotation animation keyframe data, rotation animation data, scale animation keyframe data, scale animation data
  398. jsonData.bufferViews.length.should.be.equal(8);
  399. jsonData.meshes.length.should.be.equal(1);
  400. jsonData.nodes.length.should.be.equal(1);
  401. jsonData.scenes.length.should.be.equal(1);
  402. jsonData.scene.should.be.equal(0);
  403. done();
  404. });
  405. });
  406. });
  407. });