-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
perf: tighten viewer runtime ownership #675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,17 @@ | ||
| import type { Object3D } from 'three' | ||
|
|
||
| function isFlameObject(object: Object3D): boolean { | ||
| return Boolean( | ||
| object.userData.cabinetFlameJet || | ||
| object.userData.cabinetFlamePulse || | ||
| object.userData.cabinetFlameMaterialPulse, | ||
| ) | ||
| } | ||
|
|
||
| export function collectCabinetFlameObjects(root: Object3D): Object3D[] { | ||
| const objects: Object3D[] = [] | ||
| root.traverse((object) => { | ||
| if (isFlameObject(object)) objects.push(object) | ||
| }) | ||
| return objects | ||
| } |
This file contains hidden or 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,31 @@ | ||
| import { describe, expect, test } from 'bun:test' | ||
| import { Group, Mesh } from 'three' | ||
| import { collectCabinetFlameObjects } from './flame-index' | ||
| import { animateCabinetFlames } from './system' | ||
|
|
||
| describe('collectCabinetFlameObjects', () => { | ||
| test('indexes only animated flame descendants', () => { | ||
| const root = new Group() | ||
| const staticMesh = new Mesh() | ||
| const flame = new Mesh() | ||
| flame.userData.cabinetFlamePulse = { phase: 0, amplitude: 0.1, base: 1 } | ||
| const nested = new Group() | ||
| nested.add(flame) | ||
| root.add(staticMesh, nested) | ||
|
|
||
| expect(collectCabinetFlameObjects(root)).toEqual([flame]) | ||
| }) | ||
| }) | ||
|
|
||
| describe('animateCabinetFlames', () => { | ||
| test('continues animating after a throttled flame jet', () => { | ||
| const jet = new Mesh() | ||
| jet.userData.cabinetFlameJet = { seed: {}, burnerR: 0.1 } | ||
| const pulse = new Mesh() | ||
| pulse.userData.cabinetFlamePulse = { phase: Math.PI / 2, amplitude: 0.2, base: 1 } | ||
|
|
||
| animateCabinetFlames([jet, pulse], 0, false) | ||
|
|
||
| expect(pulse.scale.x).toBeCloseTo(1.2) | ||
| }) | ||
| }) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,36 @@ | ||
| import { describe, expect, test } from 'bun:test' | ||
| import { BoxGeometry, Group, Mesh, MeshBasicMaterial } from 'three' | ||
| import { disposeObject3DResources } from './dispose-object3d' | ||
|
|
||
| describe('disposeObject3DResources', () => { | ||
| test('disposes nested geometry and materials once', () => { | ||
| const root = new Group() | ||
| const nested = new Group() | ||
| const geometry = new BoxGeometry() | ||
| const material = new MeshBasicMaterial() | ||
| let geometryDisposals = 0 | ||
| let materialDisposals = 0 | ||
| geometry.addEventListener('dispose', () => geometryDisposals++) | ||
| material.addEventListener('dispose', () => materialDisposals++) | ||
| nested.add(new Mesh(geometry, material), new Mesh(geometry, material)) | ||
| root.add(nested) | ||
|
|
||
| disposeObject3DResources(root) | ||
|
|
||
| expect(geometryDisposals).toBe(1) | ||
| expect(materialDisposals).toBe(1) | ||
| }) | ||
|
|
||
| test('preserves Pascal material-cache ownership', () => { | ||
| const root = new Group() | ||
| const material = new MeshBasicMaterial() | ||
| material.userData.__pascalCachedMaterial = true | ||
| let materialDisposals = 0 | ||
| material.addEventListener('dispose', () => materialDisposals++) | ||
| root.add(new Mesh(new BoxGeometry(), material)) | ||
|
|
||
| disposeObject3DResources(root) | ||
|
|
||
| expect(materialDisposals).toBe(0) | ||
| }) | ||
| }) |
This file contains hidden or 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,30 @@ | ||
| import type { BufferGeometry, Material, Object3D } from 'three' | ||
|
|
||
| function isCachedMaterial(material: Material): boolean { | ||
| return Boolean(material.userData?.__pascalCachedMaterial) | ||
| } | ||
|
|
||
| /** Dispose geometry and non-cached materials owned by an Object3D subtree. */ | ||
| export function disposeObject3DResources(root: Object3D): void { | ||
| const geometries = new Set<BufferGeometry>() | ||
| const materials = new Set<Material>() | ||
|
|
||
| root.traverse((object) => { | ||
| const renderable = object as Object3D & { | ||
| geometry?: BufferGeometry | ||
| material?: Material | Material[] | ||
| } | ||
| if (renderable.geometry) geometries.add(renderable.geometry) | ||
| const objectMaterials = renderable.material | ||
| if (Array.isArray(objectMaterials)) { | ||
| for (const material of objectMaterials) materials.add(material) | ||
| } else if (objectMaterials) { | ||
| materials.add(objectMaterials) | ||
| } | ||
| }) | ||
|
|
||
| for (const geometry of geometries) geometry.dispose() | ||
| for (const material of materials) { | ||
| if (!isCachedMaterial(material)) material.dispose() | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.