babylon.sceneLoader.tests.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * Describes the test suite.
  3. */
  4. describe('Babylon Scene Loader', function () {
  5. let subject: BABYLON.Engine;
  6. this.timeout(10000);
  7. /**
  8. * Loads the dependencies.
  9. */
  10. before(function (done) {
  11. this.timeout(180000);
  12. (BABYLONDEVTOOLS).Loader
  13. .useDist()
  14. .load(function () {
  15. // Force apply promise polyfill for consistent behavior between PhantomJS, IE11, and other browsers.
  16. BABYLON.PromisePolyfill.Apply(true);
  17. done();
  18. });
  19. });
  20. /**
  21. * Create a new engine subject before each test.
  22. */
  23. beforeEach(function () {
  24. subject = new BABYLON.NullEngine({
  25. renderHeight: 256,
  26. renderWidth: 256,
  27. textureSize: 256,
  28. deterministicLockstep: false,
  29. lockstepMaxSteps: 1
  30. });
  31. });
  32. /**
  33. * Integration tests for loading glTF assets.
  34. */
  35. describe('#glTF', () => {
  36. it('Load BoomBox', () => {
  37. const scene = new BABYLON.Scene(subject);
  38. return BABYLON.SceneLoader.AppendAsync("/Playground/scenes/BoomBox/", "BoomBox.gltf", scene).then(scene => {
  39. expect(scene.meshes.length, "scene.meshes.length").to.equal(2);
  40. expect(scene.materials.length, "scene.materials.length").to.equal(1);
  41. });
  42. });
  43. it('Load BoomBox GLB', () => {
  44. const scene = new BABYLON.Scene(subject);
  45. return BABYLON.SceneLoader.AppendAsync("/Playground/scenes/", "BoomBox.glb", scene).then(scene => {
  46. expect(scene.meshes.length, "scene.meshes.length").to.equal(2);
  47. expect(scene.materials.length, "scene.materials.length").to.equal(1);
  48. });
  49. });
  50. it('Load BoomBox with ImportMesh', () => {
  51. const scene = new BABYLON.Scene(subject);
  52. return BABYLON.SceneLoader.ImportMeshAsync(null, "/Playground/scenes/BoomBox/", "BoomBox.gltf", scene).then(result => {
  53. expect(result.meshes.length, "meshes.length").to.equal(scene.meshes.length);
  54. expect(result.particleSystems.length, "particleSystems.length").to.equal(0);
  55. expect(result.skeletons.length, "skeletons.length").to.equal(0);
  56. });
  57. });
  58. it('Load BoomBox with callbacks', () => {
  59. let parsedCount = 0;
  60. let meshCount = 0;
  61. let materialCount = 0;
  62. let textureCounts: { [name: string]: number } = {};
  63. let ready = false;
  64. const promises = new Array<Promise<void>>();
  65. BABYLON.SceneLoader.OnPluginActivatedObservable.add((loader: BABYLON.GLTFFileLoader) => {
  66. loader.onParsed = data => {
  67. parsedCount++;
  68. };
  69. loader.onMeshLoaded = mesh => {
  70. meshCount++;
  71. };
  72. loader.onMaterialLoaded = material => {
  73. materialCount++;
  74. };
  75. loader.onTextureLoaded = texture => {
  76. textureCounts[texture.name] = textureCounts[texture.name] || 0;
  77. textureCounts[texture.name]++;
  78. };
  79. promises.push(loader.whenCompleteAsync().then(() => {
  80. expect(ready, "ready").to.be.true;
  81. }));
  82. }, undefined, undefined, undefined, true);
  83. const scene = new BABYLON.Scene(subject);
  84. promises.push(BABYLON.SceneLoader.AppendAsync("/Playground/scenes/BoomBox/", "BoomBox.gltf", scene).then(() => {
  85. ready = true;
  86. expect(parsedCount, "parsedCount").to.equal(1);
  87. expect(meshCount, "meshCount").to.equal(scene.meshes.length);
  88. expect(materialCount, "materialCount").to.equal(scene.materials.length);
  89. const expectedTextureLoadCounts = {
  90. "baseColor": 1,
  91. "occlusionRoughnessMetallic": 2,
  92. "normal": 1,
  93. "emissive": 1
  94. };
  95. expect(Object.keys(textureCounts), "Object.keys(textureCounts)").to.have.lengthOf(Object.keys(expectedTextureLoadCounts).length);
  96. for (const textureName in expectedTextureLoadCounts) {
  97. expect(textureCounts, "textureCounts").to.have.property(textureName, expectedTextureLoadCounts[textureName]);
  98. }
  99. }));
  100. return Promise.all(promises);
  101. });
  102. it('Load BoomBox with dispose', () => {
  103. let ready = false;
  104. let disposed = false;
  105. const promises = new Array<Promise<void>>();
  106. BABYLON.SceneLoader.OnPluginActivatedObservable.add((loader: BABYLON.GLTFFileLoader) => {
  107. loader.onDispose = () => {
  108. disposed = true;
  109. };
  110. promises.push(BABYLON.Tools.DelayAsync(50).then(() => {
  111. loader.dispose();
  112. expect(ready, "ready").to.be.false;
  113. expect(disposed, "disposed").to.be.true;
  114. }));
  115. }, undefined, undefined, undefined, true);
  116. const scene = new BABYLON.Scene(subject);
  117. promises.push(BABYLON.SceneLoader.AppendAsync("/Playground/scenes/BoomBox/", "BoomBox2.gltf", scene).then(() => {
  118. ready = true;
  119. }));
  120. return Promise.race(promises);
  121. });
  122. it('Load BoomBox with compileMaterials', () => {
  123. let createShaderProgramSpy: sinon.SinonSpy;
  124. const promises = new Array<Promise<void>>();
  125. BABYLON.SceneLoader.OnPluginActivatedObservable.add((loader: BABYLON.GLTFFileLoader) => {
  126. loader.compileMaterials = true;
  127. promises.push(loader.whenCompleteAsync().then(() => {
  128. try {
  129. expect(createShaderProgramSpy.called, "createShaderProgramSpy.called").to.be.false;
  130. }
  131. finally {
  132. createShaderProgramSpy.restore();
  133. }
  134. }));
  135. }, undefined, undefined, undefined, true);
  136. const scene = new BABYLON.Scene(subject);
  137. promises.push(BABYLON.SceneLoader.AppendAsync("/Playground/scenes/BoomBox/", "BoomBox.gltf", scene).then(() => {
  138. for (const mesh of scene.meshes) {
  139. if (mesh.material) {
  140. expect(mesh.material.isReady(mesh)).to.be.true;
  141. }
  142. }
  143. createShaderProgramSpy = sinon.spy(subject, "createShaderProgram");
  144. }));
  145. promises.push(scene.whenReadyAsync());
  146. return Promise.all(promises);
  147. });
  148. it('Load BoomBox with rootMesh.isEnabled check', () => {
  149. const scene = new BABYLON.Scene(subject);
  150. let rootMesh: BABYLON.AbstractMesh;
  151. subject.runRenderLoop(() => {
  152. if (!rootMesh) {
  153. for (const mesh of scene.meshes) {
  154. if (!mesh.parent) {
  155. rootMesh = mesh;
  156. break;
  157. }
  158. }
  159. }
  160. if (rootMesh) {
  161. expect(rootMesh.isEnabled(), "rootMesh.isEnabled").to.be.false;
  162. }
  163. });
  164. return BABYLON.SceneLoader.AppendAsync("/Playground/scenes/BoomBox/", "BoomBox.gltf", scene).then(scene => {
  165. expect(rootMesh.isEnabled(), "rootMesh.isEnabled").to.be.true;
  166. subject.stopRenderLoop();
  167. });
  168. });
  169. it('Load Alien', () => {
  170. const scene = new BABYLON.Scene(subject);
  171. return BABYLON.SceneLoader.ImportMeshAsync(null, "/Playground/scenes/Alien/", "Alien.gltf", scene).then(result => {
  172. expect(result.skeletons.length, "skeletons.length").to.equal(scene.skeletons.length);
  173. const mapping = {
  174. "AlienHead": "skeleton0",
  175. "Collar": "skeleton1",
  176. "LeftEye": "skeleton2",
  177. "RightEye": "skeleton3",
  178. "CollarClasp": "skeleton1",
  179. "Shirt": "skeleton1",
  180. "ShirtPlate": "skeleton1",
  181. "Teeth": "skeleton1",
  182. };
  183. for (const meshName in mapping) {
  184. const skeletonName = mapping[meshName];
  185. expect(scene.getMeshByName(meshName).skeleton.name, `skeleton name of mesh '${meshName}'`).to.equal(skeletonName);
  186. }
  187. });
  188. });
  189. it('Load TwoQuads with LODs', () => {
  190. const scene = new BABYLON.Scene(subject);
  191. const promises = new Array<Promise<void>>();
  192. subject.runRenderLoop(() => {
  193. for (const mesh of scene.meshes) {
  194. if (mesh.material && mesh.isEnabled()) {
  195. expect(mesh.material.isReady(mesh), "mesh material is ready").to.be.true;
  196. }
  197. }
  198. });
  199. BABYLON.SceneLoader.OnPluginActivatedObservable.add((loader: BABYLON.GLTFFileLoader) => {
  200. loader.compileMaterials = true;
  201. promises.push(loader.whenCompleteAsync().then(() => {
  202. const meshes = [
  203. scene.getMeshByName("node0"),
  204. scene.getMeshByName("node1")
  205. ];
  206. expect(meshes[0].material.name, "Material for node 0").to.equal("LOD0");
  207. expect(meshes[1].material.name, "Material for node 1").to.equal("LOD0");
  208. expect(scene.materials, "scene.materials").to.have.lengthOf(1);
  209. const materials = [
  210. scene.getMaterialByName("LOD0")
  211. ];
  212. expect(materials[0].isReady(meshes[0]), "Material of LOD 0 is ready for node 0").to.be.true;
  213. expect(materials[0].isReady(meshes[1]), "Material of LOD 0 is ready for node 1").to.be.true;
  214. }));
  215. }, undefined, undefined, undefined, true);
  216. promises.push(BABYLON.SceneLoader.AppendAsync("/Playground/scenes/TwoQuads/", "TwoQuads.gltf", scene).then(() => {
  217. const meshes = [
  218. scene.getMeshByName("node0"),
  219. scene.getMeshByName("node1")
  220. ];
  221. expect(meshes[0].material.name, "Material for node 0").to.equal("LOD2");
  222. expect(meshes[1].material.name, "Material for node 1").to.equal("LOD2");
  223. expect(scene.materials, "scene.materials").to.have.lengthOf(3);
  224. const materials = [
  225. scene.getMaterialByName("LOD0"),
  226. scene.getMaterialByName("LOD1"),
  227. scene.getMaterialByName("LOD2")
  228. ];
  229. expect(materials[0].isReady(meshes[0]), "Material of LOD 0 is ready for node 0").to.be.false;
  230. expect(materials[0].isReady(meshes[1]), "Material of LOD 0 is ready for node 1").to.be.false;
  231. expect(materials[1].isReady(meshes[0]), "Material of LOD 1 is ready for node 0").to.be.false;
  232. expect(materials[1].isReady(meshes[1]), "Material of LOD 1 is ready for node 1").to.be.false;
  233. expect(materials[2].isReady(meshes[0]), "Material of LOD 2 is ready for node 0").to.be.true;
  234. expect(materials[2].isReady(meshes[1]), "Material of LOD 2 is ready for node 1").to.be.true;
  235. }));
  236. return Promise.all(promises);
  237. });
  238. // TODO: test animation group callback
  239. // TODO: test material instancing
  240. // TODO: test ImportMesh with specific node name
  241. // TODO: test KHR_materials_pbrSpecularGlossiness
  242. // TODO: test KHR_lights
  243. });
  244. describe('#AssetContainer', () => {
  245. it('should be loaded from BoomBox GLTF', () => {
  246. var scene = new BABYLON.Scene(subject);
  247. return BABYLON.SceneLoader.LoadAssetContainerAsync("/Playground/scenes/BoomBox/", "BoomBox.gltf", scene).then(container => {
  248. expect(container.meshes.length).to.eq(2);
  249. });
  250. });
  251. it('should be adding and removing objects from scene', () => {
  252. // Create a scene with some assets
  253. var scene = new BABYLON.Scene(subject);
  254. var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
  255. var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
  256. var sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);
  257. var ground = BABYLON.Mesh.CreateGround("ground1", 6, 6, 2, scene);
  258. // Move all the assets from the scene into a container
  259. var container = new BABYLON.AssetContainer(scene);
  260. var keepAssets = new BABYLON.KeepAssets();
  261. keepAssets.cameras.push(camera);
  262. container.moveAllFromScene(keepAssets);
  263. expect(scene.cameras.length).to.eq(1);
  264. expect(scene.meshes.length).to.eq(0);
  265. expect(scene.lights.length).to.eq(0);
  266. expect(container.cameras.length).to.eq(0);
  267. expect(container.meshes.length).to.eq(2);
  268. expect(container.lights.length).to.eq(1);
  269. // Add them back and then remove again
  270. container.addAllToScene();
  271. expect(scene.cameras.length).to.eq(1);
  272. expect(scene.meshes.length).to.eq(2);
  273. expect(scene.lights.length).to.eq(1);
  274. container.removeAllFromScene();
  275. expect(scene.cameras.length).to.eq(1);
  276. expect(scene.meshes.length).to.eq(0);
  277. expect(scene.lights.length).to.eq(0);
  278. });
  279. });
  280. });