forked from google/model-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce to unique materials (google#1072)
* Express material names in the 3DOM scene graph * Remove vestigial logging, stricter test * Refactor GLTF preparation and cloning * Get Constructor from utilities * Remove vestigial comments * Fix GLTFInstance preparation in IE11 * Remove vestigial debug code * Lightly refactor test suite
- Loading branch information
Christopher Joel
authored
Mar 4, 2020
1 parent
ae70902
commit 31664d7
Showing
13 changed files
with
621 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/model-viewer/src/test/three-components/GLTFInstance-spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* @license | ||
* Copyright 2020 Google LLC. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the 'License'); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an 'AS IS' BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {GLTF} from 'three/examples/jsm/loaders/GLTFLoader.js'; | ||
|
||
import {$prepared, GLTFInstance, PreparedGLTF} from '../../three-components/GLTFInstance.js'; | ||
import {createFakeGLTF} from '../helpers.js'; | ||
|
||
|
||
const expect = chai.expect; | ||
|
||
suite('GLTFInstance', () => { | ||
let rawGLTF: GLTF; | ||
let preparedGLTF: PreparedGLTF; | ||
|
||
setup(async () => { | ||
rawGLTF = createFakeGLTF(); | ||
preparedGLTF = GLTFInstance.prepare(rawGLTF); | ||
}); | ||
|
||
suite('with a prepared GLTF', () => { | ||
test('exposes the same scene as the GLTF', () => { | ||
const gltfInstance = new GLTFInstance(preparedGLTF); | ||
expect(gltfInstance.scene).to.be.equal(preparedGLTF.scene); | ||
}); | ||
|
||
suite('when cloned', () => { | ||
test('creates a unique scene', () => { | ||
const gltfInstance = new GLTFInstance(preparedGLTF); | ||
const cloneInstance = gltfInstance.clone(); | ||
|
||
expect(cloneInstance.scene).to.be.ok; | ||
expect(cloneInstance.scene).to.not.be.equal(gltfInstance.scene); | ||
}); | ||
}); | ||
}); | ||
|
||
suite('preparing the GLTF', () => { | ||
test('creates a prepared GLTF', () => { | ||
expect(preparedGLTF).to.not.be.equal(rawGLTF); | ||
expect(preparedGLTF[$prepared]).to.be.equal(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...ages/model-viewer/src/test/three-components/gltf-instance/ModelViewerGLTFInstance-spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* @license | ||
* Copyright 2020 Google LLC. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the 'License'); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an 'AS IS' BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {BufferGeometry, DoubleSide, Mesh, MeshStandardMaterial} from 'three'; | ||
import {GLTF} from 'three/examples/jsm/loaders/GLTFLoader.js'; | ||
|
||
import {ModelViewerGLTFInstance} from '../../../three-components/gltf-instance/ModelViewerGLTFInstance.js'; | ||
import {PreparedGLTF} from '../../../three-components/GLTFInstance.js'; | ||
import {createFakeGLTF} from '../../helpers.js'; | ||
|
||
|
||
const expect = chai.expect; | ||
|
||
suite('ModelViewerGLTFInstance', () => { | ||
let rawGLTF: GLTF; | ||
let preparedGLTF: PreparedGLTF; | ||
|
||
setup(async () => { | ||
rawGLTF = createFakeGLTF(); | ||
|
||
const materialOne = new MeshStandardMaterial(); | ||
const materialTwo = new MeshStandardMaterial(); | ||
|
||
materialTwo.transparent = true; | ||
materialTwo.side = DoubleSide; | ||
|
||
const meshOne = new Mesh(new BufferGeometry(), materialOne); | ||
const meshTwo = new Mesh(new BufferGeometry(), materialOne); | ||
const meshThree = new Mesh(new BufferGeometry(), materialTwo); | ||
|
||
rawGLTF.scene.add(meshOne, meshTwo, meshThree); | ||
|
||
preparedGLTF = ModelViewerGLTFInstance.prepare(rawGLTF); | ||
}); | ||
|
||
suite('with a prepared GLTF', () => { | ||
suite('when cloned', () => { | ||
let cloneInstance: ModelViewerGLTFInstance; | ||
let gltfInstance: ModelViewerGLTFInstance; | ||
|
||
setup(() => { | ||
gltfInstance = new ModelViewerGLTFInstance(preparedGLTF); | ||
cloneInstance = gltfInstance.clone(); | ||
}); | ||
|
||
teardown(() => { | ||
gltfInstance.dispose(); | ||
cloneInstance.dispose(); | ||
}); | ||
|
||
test('clones materials in a mesh', () => { | ||
const [originalMeshOne, originalMeshTwo, originalMeshThree] = | ||
gltfInstance.scene.children as [Mesh, Mesh, Mesh]; | ||
const [meshOne, meshTwo, meshThree] = | ||
cloneInstance.scene.children as [Mesh, Mesh, Mesh]; | ||
|
||
expect(originalMeshOne.material).to.not.be.equal(meshOne.material); | ||
expect(originalMeshTwo.material).to.not.be.equal(meshTwo.material); | ||
expect(originalMeshThree.material).to.not.be.equal(meshThree.material); | ||
}); | ||
|
||
test('only clones a discrete material once', () => { | ||
const [meshOne, meshTwo, meshThree] = | ||
cloneInstance.scene.children as [Mesh, Mesh, Mesh]; | ||
|
||
expect(meshOne.material).to.be.equal(meshTwo.material); | ||
expect(meshOne.material).to.not.be.equal(meshThree.material); | ||
}); | ||
}); | ||
}); | ||
|
||
suite('preparing the GLTF', () => { | ||
test('duplicates a transparent, double-sided mesh', () => { | ||
const meshThree = preparedGLTF.scene.children[2] as Mesh; | ||
expect(meshThree.children[0]).to.be.ok; | ||
expect((meshThree.children[0] as Mesh).isMesh).to.be.ok; | ||
}); | ||
|
||
test('sets meshes to cast shadows', () => { | ||
(preparedGLTF.scene.children as [Mesh, Mesh, Mesh]) | ||
.forEach(mesh => expect(mesh.castShadow).to.be.true); | ||
}); | ||
|
||
test('disables frustum culling on meshes', () => { | ||
(preparedGLTF.scene.children as [Mesh, Mesh, Mesh]) | ||
.forEach(mesh => expect(mesh.frustumCulled).to.be.false); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.