babylon.glTFSerializer.tests.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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._GLTFMaterial.GetAlphaMode(babylonMaterial);
  39. alphaMode.should.be.equal('OPAQUE');
  40. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND;
  41. alphaMode = BABYLON._GLTFMaterial.GetAlphaMode(babylonMaterial);
  42. alphaMode.should.be.equal('BLEND');
  43. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATESTANDBLEND;
  44. alphaMode = BABYLON._GLTFMaterial.GetAlphaMode(babylonMaterial);
  45. alphaMode.should.be.equal('BLEND');
  46. babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST;
  47. alphaMode = BABYLON._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. const specGloss: BABYLON._IBabylonSpecularGlossiness = {
  56. diffuse: BABYLON.Color3.White(),
  57. specular: BABYLON.Color3.Black(),
  58. glossiness: 0.25,
  59. opacity: 1.0
  60. };
  61. const metalRough = BABYLON._GLTFMaterial.ConvertToMetallicRoughness(specGloss);
  62. metalRough.baseColor.equals(new BABYLON.Color3(1, 1, 1)).should.be.equal(true);
  63. metalRough.metallic.should.be.equal(0);
  64. metalRough.roughness.should.be.equal(0.75);
  65. metalRough.opacity.should.be.equal(1);
  66. });
  67. it('should solve for metallic', () => {
  68. BABYLON._GLTFMaterial.SolveMetallic(1.0, 0.0, 1.0).should.be.equal(0);
  69. BABYLON._GLTFMaterial.SolveMetallic(0.0, 1.0, 1.0).should.be.approximately(1, 1e-6);
  70. });
  71. it('should serialize empty Babylon scene to glTF with only asset property', (done) => {
  72. mocha.timeout(10000);
  73. const scene = new BABYLON.Scene(subject);
  74. scene.executeWhenReady(function () {
  75. const glTFExporter = new BABYLON._GLTF2Exporter(scene);
  76. const glTFData = glTFExporter._generateGLTF('test');
  77. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  78. const jsonData = JSON.parse(jsonString);
  79. Object.keys(jsonData).length.should.be.equal(1);
  80. jsonData.asset.version.should.be.equal("2.0");
  81. jsonData.asset.generator.should.be.equal("BabylonJS");
  82. done();
  83. });
  84. });
  85. it('should serialize sphere geometry in scene to glTF', (done) => {
  86. mocha.timeout(10000);
  87. const scene = new BABYLON.Scene(subject);
  88. BABYLON.Mesh.CreateSphere('sphere', 16, 2, scene);
  89. scene.executeWhenReady(function () {
  90. const glTFExporter = new BABYLON._GLTF2Exporter(scene);
  91. const glTFData = glTFExporter._generateGLTF('test');
  92. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  93. const jsonData = JSON.parse(jsonString);
  94. // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes,
  95. Object.keys(jsonData).length.should.be.equal(8);
  96. // positions, normals, texture coords, indices
  97. jsonData.accessors.length.should.be.equal(4);
  98. // generator, version
  99. Object.keys(jsonData.asset).length.should.be.equal(2);
  100. jsonData.buffers.length.should.be.equal(1);
  101. // positions, normals, texture coords, indices
  102. jsonData.bufferViews.length.should.be.equal(4);
  103. jsonData.meshes.length.should.be.equal(1);
  104. jsonData.nodes.length.should.be.equal(1);
  105. jsonData.scenes.length.should.be.equal(1);
  106. jsonData.scene.should.be.equal(0);
  107. done();
  108. });
  109. });
  110. it('should serialize alpha mode and cutoff', (done) => {
  111. mocha.timeout(10000);
  112. const scene = new BABYLON.Scene(subject);
  113. const plane = BABYLON.Mesh.CreatePlane('plane', 120, scene);
  114. const babylonPBRMetalRoughMaterial = new BABYLON.PBRMetallicRoughnessMaterial('metalRoughMat', scene);
  115. babylonPBRMetalRoughMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND;
  116. const alphaCutoff = 0.8;
  117. babylonPBRMetalRoughMaterial.alphaCutOff = alphaCutoff;
  118. plane.material = babylonPBRMetalRoughMaterial;
  119. scene.executeWhenReady(function () {
  120. const glTFExporter = new BABYLON._GLTF2Exporter(scene);
  121. const glTFData = glTFExporter._generateGLTF('test');
  122. const jsonString = glTFData.glTFFiles['test.gltf'] as string;
  123. const jsonData = JSON.parse(jsonString);
  124. Object.keys(jsonData).length.should.be.equal(9);
  125. jsonData.materials.length.should.be.equal(1);
  126. jsonData.materials[0].alphaMode.should.be.equal('BLEND');
  127. jsonData.materials[0].alphaCutoff.should.be.equal(alphaCutoff);
  128. done();
  129. });
  130. });
  131. });
  132. });