123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- /**
- * Describes the test suite
- */
- describe('Babylon glTF Serializer', () => {
- let subject: BABYLON.Engine;
- /**
- * Loads the dependencies
- */
- before(function (done) {
- this.timeout(180000);
- (BABYLONDEVTOOLS).Loader
- .useDist()
- .load(function () {
- done();
- });
- });
- /**
- * Create a null engine subject before each test.
- */
- beforeEach(function () {
- subject = new BABYLON.NullEngine({
- renderHeight: 256,
- renderWidth: 256,
- textureSize: 256,
- deterministicLockstep: false,
- lockstepMaxSteps: 1
- });
- });
- /**
- * This tests the glTF serializer help functions
- */
- describe('#GLTF', () => {
- it('should get alpha mode from Babylon metallic roughness', () => {
- let alphaMode: string;
- const scene = new BABYLON.Scene(subject);
- const babylonMaterial = new BABYLON.PBRMetallicRoughnessMaterial("metallicroughness", scene);
- babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE;
- alphaMode = BABYLON.GLTF2._GLTFMaterial._GetAlphaMode(babylonMaterial);
- alphaMode.should.be.equal('OPAQUE');
- babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND;
- alphaMode = BABYLON.GLTF2._GLTFMaterial._GetAlphaMode(babylonMaterial);
- alphaMode.should.be.equal('BLEND');
- babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATESTANDBLEND;
- alphaMode = BABYLON.GLTF2._GLTFMaterial._GetAlphaMode(babylonMaterial);
- alphaMode.should.be.equal('BLEND');
- babylonMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST;
- alphaMode = BABYLON.GLTF2._GLTFMaterial._GetAlphaMode(babylonMaterial);
- alphaMode.should.be.equal('MASK');
- });
- it('should convert Babylon standard material to metallic roughness', () => {
- const scene = new BABYLON.Scene(subject);
- const babylonStandardMaterial = new BABYLON.StandardMaterial("specGloss", scene);
- babylonStandardMaterial.diffuseColor = BABYLON.Color3.White();
- babylonStandardMaterial.specularColor = BABYLON.Color3.Black();
- babylonStandardMaterial.specularPower = 64;
- babylonStandardMaterial.alpha = 1;
- const metalRough = BABYLON.GLTF2._GLTFMaterial._ConvertToGLTFPBRMetallicRoughness(babylonStandardMaterial);
- metalRough.baseColorFactor.should.deep.equal([0.5, 0.5, 0.5, 1]);
- metalRough.metallicFactor.should.be.equal(0);
- metalRough.roughnessFactor.should.be.approximately(0.328809, 1e-6);
- });
- it('should solve for metallic', () => {
- BABYLON.GLTF2._GLTFMaterial._SolveMetallic(1.0, 0.0, 1.0).should.be.equal(0);
- BABYLON.GLTF2._GLTFMaterial._SolveMetallic(0.0, 1.0, 1.0).should.be.approximately(1, 1e-6);
- });
- it('should serialize empty Babylon scene to glTF with only asset property', (done) => {
- mocha.timeout(10000);
- const scene = new BABYLON.Scene(subject);
- scene.executeWhenReady(function () {
- const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
- const glTFData = glTFExporter._generateGLTF('test');
- const jsonString = glTFData.glTFFiles['test.gltf'] as string;
- const jsonData = JSON.parse(jsonString);
- Object.keys(jsonData).length.should.be.equal(1);
- jsonData.asset.version.should.be.equal("2.0");
- jsonData.asset.generator.should.be.equal("BabylonJS");
- done();
- });
- });
- it('should serialize sphere geometry in scene to glTF', (done) => {
- mocha.timeout(10000);
- const scene = new BABYLON.Scene(subject);
- BABYLON.Mesh.CreateSphere('sphere', 16, 2, scene);
- scene.executeWhenReady(function () {
- const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
- const glTFData = glTFExporter._generateGLTF('test');
- const jsonString = glTFData.glTFFiles['test.gltf'] as string;
- const jsonData = JSON.parse(jsonString);
- // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials
- Object.keys(jsonData).length.should.be.equal(9);
- // positions, normals, texture coords, indices
- jsonData.accessors.length.should.be.equal(4);
- // generator, version
- Object.keys(jsonData.asset).length.should.be.equal(2);
- jsonData.buffers.length.should.be.equal(1);
- // positions, normals, texture coords, indices
- jsonData.bufferViews.length.should.be.equal(4);
- jsonData.meshes.length.should.be.equal(1);
- jsonData.nodes.length.should.be.equal(1);
- jsonData.scenes.length.should.be.equal(1);
- jsonData.scene.should.be.equal(0);
- done();
- });
- });
- it('should serialize alpha mode and cutoff', (done) => {
- mocha.timeout(10000);
- const scene = new BABYLON.Scene(subject);
- const plane = BABYLON.Mesh.CreatePlane('plane', 120, scene);
- const babylonPBRMetalRoughMaterial = new BABYLON.PBRMetallicRoughnessMaterial('metalRoughMat', scene);
- babylonPBRMetalRoughMaterial.transparencyMode = BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST;
- const alphaCutoff = 0.8;
- babylonPBRMetalRoughMaterial.alphaCutOff = alphaCutoff;
- plane.material = babylonPBRMetalRoughMaterial;
- scene.executeWhenReady(function () {
- const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
- const glTFData = glTFExporter._generateGLTF('test');
- const jsonString = glTFData.glTFFiles['test.gltf'] as string;
- const jsonData = JSON.parse(jsonString);
- // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials
- Object.keys(jsonData).length.should.be.equal(9);
- jsonData.materials.length.should.be.equal(2);
- jsonData.materials[0].alphaMode.should.be.equal('MASK');
- jsonData.materials[0].alphaCutoff.should.be.equal(alphaCutoff);
- done();
- });
- });
- it('should serialize single component translation animation to glTF', (done) => {
- mocha.timeout(10000);
- const scene = new BABYLON.Scene(subject);
- const box = BABYLON.Mesh.CreateBox('box', 1, scene);
- let keys: BABYLON.IAnimationKey[] = [];
- keys.push({
- frame: 0,
- value: 1
- });
- keys.push({
- frame: 20,
- value: 0.2
- });
- keys.push({
- frame: 40,
- value: 1
- });
- let animationBoxT = new BABYLON.Animation('boxAnimation_translation', 'position.y', 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
- animationBoxT.setKeys(keys);
- box.animations.push(animationBoxT);
- scene.executeWhenReady(function () {
- const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
- const glTFData = glTFExporter._generateGLTF('test');
- const jsonString = glTFData.glTFFiles['test.gltf'] as string;
- const jsonData = JSON.parse(jsonString);
- jsonData.animations.length.should.be.equal(1);
- const animation = jsonData.animations[0];
- animation.channels.length.should.be.equal(1);
- animation.channels[0].sampler.should.be.equal(0);
- animation.channels[0].target.node.should.be.equal(0);
- animation.channels[0].target.path.should.be.equal('translation');
- jsonData.animations[0].samplers.length.should.be.equal(1);
- // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
- Object.keys(jsonData).length.should.be.equal(10);
- // positions, normals, texture coords, indices, animation keyframe data, animation data
- jsonData.accessors.length.should.be.equal(6);
- // generator, version
- Object.keys(jsonData.asset).length.should.be.equal(2);
- jsonData.buffers.length.should.be.equal(1);
- // positions, normals, texture coords, indices, animation keyframe data, animation data
- jsonData.bufferViews.length.should.be.equal(6);
- jsonData.meshes.length.should.be.equal(1);
- jsonData.nodes.length.should.be.equal(1);
- jsonData.scenes.length.should.be.equal(1);
- jsonData.scene.should.be.equal(0);
- done();
- });
- });
- it('should serialize translation animation to glTF', (done) => {
- mocha.timeout(10000);
- const scene = new BABYLON.Scene(subject);
- const box = BABYLON.Mesh.CreateBox('box', 1, scene);
- let keys: BABYLON.IAnimationKey[] = [];
- keys.push({
- frame: 0,
- value: new BABYLON.Vector3(0.1, 0.1, 0.1)
- });
- keys.push({
- frame: 20,
- value: BABYLON.Vector3.One()
- });
- keys.push({
- frame: 40,
- value: new BABYLON.Vector3(0.1, 0.1, 0.1)
- });
- let animationBoxT = new BABYLON.Animation('boxAnimation_translation', 'position', 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
- animationBoxT.setKeys(keys);
- box.animations.push(animationBoxT);
- scene.executeWhenReady(function () {
- const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
- const glTFData = glTFExporter._generateGLTF('test');
- const jsonString = glTFData.glTFFiles['test.gltf'] as string;
- const jsonData = JSON.parse(jsonString);
- jsonData.animations.length.should.be.equal(1);
- const animation = jsonData.animations[0];
- animation.channels.length.should.be.equal(1);
- animation.channels[0].sampler.should.be.equal(0);
- animation.channels[0].target.node.should.be.equal(0);
- animation.channels[0].target.path.should.be.equal('translation');
- animation.samplers.length.should.be.equal(1);
- animation.samplers[0].interpolation.should.be.equal('LINEAR');
- animation.samplers[0].input.should.be.equal(4);
- animation.samplers[0].output.should.be.equal(5);
- jsonData.animations[0].samplers.length.should.be.equal(1);
- // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
- Object.keys(jsonData).length.should.be.equal(10);
- // positions, normals, texture coords, indices, animation keyframe data, animation data
- jsonData.accessors.length.should.be.equal(6);
- // generator, version
- Object.keys(jsonData.asset).length.should.be.equal(2);
- jsonData.buffers.length.should.be.equal(1);
- // positions, normals, texture coords, indices, animation keyframe data, animation data
- jsonData.bufferViews.length.should.be.equal(6);
- jsonData.meshes.length.should.be.equal(1);
- jsonData.nodes.length.should.be.equal(1);
- jsonData.scenes.length.should.be.equal(1);
- jsonData.scene.should.be.equal(0);
- done();
- });
- });
- it('should serialize scale animation to glTF', (done) => {
- mocha.timeout(10000);
- const scene = new BABYLON.Scene(subject);
- const box = BABYLON.Mesh.CreateBox('box', 1, scene);
- let keys: BABYLON.IAnimationKey[] = [];
- keys.push({
- frame: 0,
- value: new BABYLON.Vector3(0.1, 0.1, 0.1)
- });
- keys.push({
- frame: 20,
- value: BABYLON.Vector3.One()
- });
- keys.push({
- frame: 40,
- value: new BABYLON.Vector3(0.1, 0.1, 0.1)
- });
- let animationBoxT = new BABYLON.Animation('boxAnimation_translation', 'scaling', 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
- animationBoxT.setKeys(keys);
- box.animations.push(animationBoxT);
- scene.executeWhenReady(function () {
- const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
- const glTFData = glTFExporter._generateGLTF('test');
- const jsonString = glTFData.glTFFiles['test.gltf'] as string;
- const jsonData = JSON.parse(jsonString);
- jsonData.animations.length.should.be.equal(1);
- const animation = jsonData.animations[0];
- animation.channels.length.should.be.equal(1);
- animation.channels[0].sampler.should.be.equal(0);
- animation.channels[0].target.node.should.be.equal(0);
- animation.channels[0].target.path.should.be.equal('scale');
- animation.samplers.length.should.be.equal(1);
- animation.samplers[0].interpolation.should.be.equal('LINEAR');
- animation.samplers[0].input.should.be.equal(4);
- animation.samplers[0].output.should.be.equal(5);
- jsonData.animations[0].samplers.length.should.be.equal(1);
- // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
- Object.keys(jsonData).length.should.be.equal(10);
- // positions, normals, texture coords, indices, animation keyframe data, animation data
- jsonData.accessors.length.should.be.equal(6);
- // generator, version
- Object.keys(jsonData.asset).length.should.be.equal(2);
- jsonData.buffers.length.should.be.equal(1);
- // positions, normals, texture coords, indices, animation keyframe data, animation data
- jsonData.bufferViews.length.should.be.equal(6);
- jsonData.meshes.length.should.be.equal(1);
- jsonData.nodes.length.should.be.equal(1);
- jsonData.scenes.length.should.be.equal(1);
- jsonData.scene.should.be.equal(0);
- done();
- });
- });
- it('should serialize rotation quaternion animation to glTF', (done) => {
- mocha.timeout(10000);
- const scene = new BABYLON.Scene(subject);
- const box = BABYLON.Mesh.CreateBox('box', 1, scene);
- let keys: BABYLON.IAnimationKey[] = [];
- keys.push({
- frame: 0,
- value: new BABYLON.Quaternion(0.707, 0.0, 0.0, 0.707)
- });
- keys.push({
- frame: 20,
- value: BABYLON.Quaternion.Identity()
- });
- keys.push({
- frame: 40,
- value: new BABYLON.Quaternion(0.707, 0.0, 0.0, 0.707)
- });
- let animationBoxT = new BABYLON.Animation('boxAnimation_translation', 'rotationQuaternion', 30, BABYLON.Animation.ANIMATIONTYPE_QUATERNION, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
- animationBoxT.setKeys(keys);
- box.animations.push(animationBoxT);
- scene.executeWhenReady(function () {
- const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
- const glTFData = glTFExporter._generateGLTF('test');
- const jsonString = glTFData.glTFFiles['test.gltf'] as string;
- const jsonData = JSON.parse(jsonString);
- jsonData.animations.length.should.be.equal(1);
- const animation = jsonData.animations[0];
- animation.channels.length.should.be.equal(1);
- animation.channels[0].sampler.should.be.equal(0);
- animation.channels[0].target.node.should.be.equal(0);
- animation.channels[0].target.path.should.be.equal('rotation');
- animation.samplers.length.should.be.equal(1);
- animation.samplers[0].interpolation.should.be.equal('LINEAR');
- animation.samplers[0].input.should.be.equal(4);
- animation.samplers[0].output.should.be.equal(5);
- jsonData.animations[0].samplers.length.should.be.equal(1);
- // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
- Object.keys(jsonData).length.should.be.equal(10);
- // positions, normals, texture coords, indices, animation keyframe data, animation data
- jsonData.accessors.length.should.be.equal(6);
- // generator, version
- Object.keys(jsonData.asset).length.should.be.equal(2);
- jsonData.buffers.length.should.be.equal(1);
- // positions, normals, texture coords, indices, animation keyframe data, animation data
- jsonData.bufferViews.length.should.be.equal(6);
- jsonData.meshes.length.should.be.equal(1);
- jsonData.nodes.length.should.be.equal(1);
- jsonData.scenes.length.should.be.equal(1);
- jsonData.scene.should.be.equal(0);
- done();
- });
- });
- it('should serialize combined animations to glTF', (done) => {
- mocha.timeout(10000);
- const scene = new BABYLON.Scene(subject);
- const box = BABYLON.Mesh.CreateBox('box', 1, scene);
- const rotationKeyFrames: BABYLON.IAnimationKey[] = [];
- rotationKeyFrames.push({
- frame: 0,
- value: new BABYLON.Quaternion(0.707, 0.0, 0.0, 0.707)
- });
- rotationKeyFrames.push({
- frame: 20,
- value: BABYLON.Quaternion.Identity()
- });
- rotationKeyFrames.push({
- frame: 40,
- value: new BABYLON.Quaternion(0.707, 0.0, 0.0, 0.707)
- });
- const scaleKeyFrames: BABYLON.IAnimationKey[] = [];
- scaleKeyFrames.push({
- frame: 0,
- value: new BABYLON.Vector3(0.1, 0.1, 0.1)
- });
- scaleKeyFrames.push({
- frame: 20,
- value: BABYLON.Vector3.One()
- });
- scaleKeyFrames.push({
- frame: 40,
- value: new BABYLON.Vector3(0.1, 0.1, 0.1)
- });
- let rotationAnimationBox = new BABYLON.Animation('boxAnimation_rotation', 'rotationQuaternion', 30, BABYLON.Animation.ANIMATIONTYPE_QUATERNION, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
- rotationAnimationBox.setKeys(rotationKeyFrames);
- box.animations.push(rotationAnimationBox);
- let scaleAnimationBox = new BABYLON.Animation('boxAnimation_scale', 'scaling', 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
- scaleAnimationBox.setKeys(scaleKeyFrames);
- box.animations.push(scaleAnimationBox);
- scene.executeWhenReady(function () {
- const glTFExporter = new BABYLON.GLTF2._Exporter(scene);
- const glTFData = glTFExporter._generateGLTF('test');
- const jsonString = glTFData.glTFFiles['test.gltf'] as string;
- const jsonData = JSON.parse(jsonString);
- jsonData.animations.length.should.be.equal(2);
-
- let animation = jsonData.animations[0];
- animation.channels.length.should.be.equal(1);
- animation.channels[0].sampler.should.be.equal(0);
- animation.channels[0].target.node.should.be.equal(0);
- animation.channels[0].target.path.should.be.equal('rotation');
- animation.samplers.length.should.be.equal(1);
- animation.samplers[0].interpolation.should.be.equal('LINEAR');
- animation.samplers[0].input.should.be.equal(4);
- animation.samplers[0].output.should.be.equal(5);
- animation = jsonData.animations[1];
- animation.channels[0].sampler.should.be.equal(0);
- animation.channels[0].target.node.should.be.equal(0);
- animation.channels[0].target.path.should.be.equal('scale');
- animation.samplers.length.should.be.equal(1);
- animation.samplers[0].interpolation.should.be.equal('LINEAR');
- animation.samplers[0].input.should.be.equal(6);
- animation.samplers[0].output.should.be.equal(7);
- // accessors, asset, buffers, bufferViews, meshes, nodes, scene, scenes, materials, animations
- Object.keys(jsonData).length.should.be.equal(10);
- // positions, normals, texture coords, indices, rotation animation keyframe data, rotation animation data, scale animation keyframe data, scale animation data
- jsonData.accessors.length.should.be.equal(8);
- // generator, version
- Object.keys(jsonData.asset).length.should.be.equal(2);
- jsonData.buffers.length.should.be.equal(1);
- // positions, normals, texture coords, indices, rotation animation keyframe data, rotation animation data, scale animation keyframe data, scale animation data
- jsonData.bufferViews.length.should.be.equal(8);
- jsonData.meshes.length.should.be.equal(1);
- jsonData.nodes.length.should.be.equal(1);
- jsonData.scenes.length.should.be.equal(1);
- jsonData.scene.should.be.equal(0);
- done();
- });
- });
-
- });
- });
|