|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import { GLTF } from 'three/examples/jsm/loaders/GLTFLoader.js'; |
| 9 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 10 | +import { CacheManager } from '../../src/asset/cache-manager.js'; |
| 11 | +import { GLTFAssetLoader } from '../../src/asset/loaders/gltf-loader.js'; |
| 12 | +import { |
| 13 | + AnimationClip, |
| 14 | + Bone, |
| 15 | + BufferAttribute, |
| 16 | + BufferGeometry, |
| 17 | + Group, |
| 18 | + Mesh, |
| 19 | + MeshStandardMaterial, |
| 20 | + Skeleton, |
| 21 | + SkinnedMesh, |
| 22 | + SphereGeometry, |
| 23 | +} from '../../src/runtime/three.js'; |
| 24 | + |
| 25 | +function makeFakeGLTF(): GLTF { |
| 26 | + const geometry = new SphereGeometry(); |
| 27 | + const material = new MeshStandardMaterial(); |
| 28 | + const mesh = new Mesh(geometry, material); |
| 29 | + mesh.name = 'TestMesh'; |
| 30 | + |
| 31 | + const scene = new Group(); |
| 32 | + scene.name = 'TestScene'; |
| 33 | + scene.add(mesh); |
| 34 | + |
| 35 | + const animations: AnimationClip[] = []; |
| 36 | + return { |
| 37 | + scene, |
| 38 | + scenes: [scene], |
| 39 | + animations, |
| 40 | + cameras: [], |
| 41 | + asset: { version: '2.0' }, |
| 42 | + parser: undefined as unknown as GLTF['parser'], |
| 43 | + userData: {}, |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +describe('GLTFAssetLoader.getGLTF', () => { |
| 48 | + beforeEach(() => { |
| 49 | + CacheManager.clear(); |
| 50 | + const gltf = makeFakeGLTF(); |
| 51 | + CacheManager.setKeyToUrl('test', 'https://example.invalid/test.glb'); |
| 52 | + CacheManager.setAsset('https://example.invalid/test.glb', gltf); |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + CacheManager.clear(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns null for unknown keys', () => { |
| 60 | + expect(GLTFAssetLoader.getGLTF('missing')).toBeNull(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('returns a fresh scene clone on every call by default', () => { |
| 64 | + const a = GLTFAssetLoader.getGLTF('test'); |
| 65 | + const b = GLTFAssetLoader.getGLTF('test'); |
| 66 | + expect(a).not.toBeNull(); |
| 67 | + expect(b).not.toBeNull(); |
| 68 | + expect(a!.scene).not.toBe(b!.scene); |
| 69 | + expect(a!.scenes[0]).not.toBe(b!.scenes[0]); |
| 70 | + }); |
| 71 | + |
| 72 | + it('shares geometries, materials, and animations across clones', () => { |
| 73 | + const a = GLTFAssetLoader.getGLTF('test')!; |
| 74 | + const b = GLTFAssetLoader.getGLTF('test')!; |
| 75 | + const meshA = a.scene.children[0] as Mesh; |
| 76 | + const meshB = b.scene.children[0] as Mesh; |
| 77 | + expect(meshA.geometry).toBe(meshB.geometry); |
| 78 | + expect(meshA.material).toBe(meshB.material); |
| 79 | + expect(a.animations).toBe(b.animations); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns the cached instance when { shared: true }', () => { |
| 83 | + const a = GLTFAssetLoader.getGLTF('test', { shared: true }); |
| 84 | + const b = GLTFAssetLoader.getGLTF('test', { shared: true }); |
| 85 | + expect(a).not.toBeNull(); |
| 86 | + expect(a).toBe(b); |
| 87 | + expect(a!.scene).toBe(b!.scene); |
| 88 | + }); |
| 89 | + |
| 90 | + it('returns scenes with no parent so multi-entity placement does not steal ownership', () => { |
| 91 | + const parentA = new Group(); |
| 92 | + const parentB = new Group(); |
| 93 | + |
| 94 | + const a = GLTFAssetLoader.getGLTF('test')!; |
| 95 | + parentA.add(a.scene); |
| 96 | + |
| 97 | + const b = GLTFAssetLoader.getGLTF('test')!; |
| 98 | + parentB.add(b.scene); |
| 99 | + |
| 100 | + expect(a.scene.parent).toBe(parentA); |
| 101 | + expect(b.scene.parent).toBe(parentB); |
| 102 | + expect(parentA.children).toContain(a.scene); |
| 103 | + expect(parentB.children).toContain(b.scene); |
| 104 | + }); |
| 105 | + |
| 106 | + it('rebinds SkinnedMesh skeletons so each clone has its own bones', () => { |
| 107 | + const root = new Group(); |
| 108 | + root.name = 'SkinnedRoot'; |
| 109 | + |
| 110 | + const bone = new Bone(); |
| 111 | + bone.name = 'Bone0'; |
| 112 | + root.add(bone); |
| 113 | + |
| 114 | + const geometry = new BufferGeometry(); |
| 115 | + geometry.setAttribute( |
| 116 | + 'skinIndex', |
| 117 | + new BufferAttribute(new Uint16Array(4), 4), |
| 118 | + ); |
| 119 | + geometry.setAttribute( |
| 120 | + 'skinWeight', |
| 121 | + new BufferAttribute(new Float32Array(4), 4), |
| 122 | + ); |
| 123 | + const skinnedMesh = new SkinnedMesh(geometry, new MeshStandardMaterial()); |
| 124 | + skinnedMesh.name = 'SkinnedTestMesh'; |
| 125 | + skinnedMesh.bind(new Skeleton([bone])); |
| 126 | + root.add(skinnedMesh); |
| 127 | + |
| 128 | + CacheManager.setAsset('https://example.invalid/test.glb', { |
| 129 | + scene: root, |
| 130 | + scenes: [root], |
| 131 | + animations: [], |
| 132 | + cameras: [], |
| 133 | + asset: { version: '2.0' }, |
| 134 | + parser: undefined as unknown as GLTF['parser'], |
| 135 | + userData: {}, |
| 136 | + }); |
| 137 | + |
| 138 | + const a = GLTFAssetLoader.getGLTF('test')!; |
| 139 | + const b = GLTFAssetLoader.getGLTF('test')!; |
| 140 | + |
| 141 | + const meshA = a.scene.getObjectByName('SkinnedTestMesh') as SkinnedMesh; |
| 142 | + const meshB = b.scene.getObjectByName('SkinnedTestMesh') as SkinnedMesh; |
| 143 | + const boneA = a.scene.getObjectByName('Bone0') as Bone; |
| 144 | + const boneB = b.scene.getObjectByName('Bone0') as Bone; |
| 145 | + |
| 146 | + expect(meshA).toBeInstanceOf(SkinnedMesh); |
| 147 | + expect(meshB).toBeInstanceOf(SkinnedMesh); |
| 148 | + expect(meshA).not.toBe(meshB); |
| 149 | + |
| 150 | + expect(boneA).not.toBe(boneB); |
| 151 | + expect(boneA.parent).toBe(a.scene); |
| 152 | + expect(boneB.parent).toBe(b.scene); |
| 153 | + |
| 154 | + expect(meshA.skeleton).not.toBe(meshB.skeleton); |
| 155 | + expect(meshA.skeleton.bones[0]).toBe(boneA); |
| 156 | + expect(meshB.skeleton.bones[0]).toBe(boneB); |
| 157 | + }); |
| 158 | +}); |
0 commit comments