babylon.glTFSerializer.tests.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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([1,1,1,1]);
  59. metalRough.metallicFactor.should.be.equal(0);
  60. metalRough.roughnessFactor.should.be.equal(0.75);
  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,
  90. Object.keys(jsonData).length.should.be.equal(8);
  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_ALPHABLEND;
  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. Object.keys(jsonData).length.should.be.equal(9);
  120. jsonData.materials.length.should.be.equal(1);
  121. jsonData.materials[0].alphaMode.should.be.equal('BLEND');
  122. jsonData.materials[0].alphaCutoff.should.be.equal(alphaCutoff);
  123. done();
  124. });
  125. });
  126. });
  127. });