Skip to content
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

feat: 499 better memory management #606

Merged
merged 12 commits into from
Apr 24, 2024
Prev Previous commit
Next Next commit
Merge branch 'v4' into feat/499-better-memory-management
  • Loading branch information
alvarosabu committed Apr 10, 2024
commit a7958587ec7223fbe46fa1db14fb0cc982e90a37
2 changes: 1 addition & 1 deletion playground/src/router/routes/performance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export const perfRoutes = [
name: 'Memory',
component: () => import('../../pages/perf/Memory.vue'),
},
]
]
14 changes: 4 additions & 10 deletions src/components/TresCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ import {
getCurrentInstance,
h,
onMounted,
onUnmounted,
provide,
ref,
shallowRef,
watch,
watchEffect,
Fragment,
defineComponent,
h,
getCurrentInstance,
createRenderer,
onUnmounted,
} from 'vue'
import pkg from '../../package.json'

Expand Down Expand Up @@ -134,7 +129,7 @@ const dispose = (context: TresContext, force = false) => {
root: context,
}
mountCustomRenderer(context)
resume()
resume()
/* disposeObject3D(scene.value) */
/* scene.value.children.forEach((child) => {
child.removeFromParent()
Expand Down Expand Up @@ -219,10 +214,9 @@ onMounted(() => {
)
addDefaultCamera()
}

// HMR support
if (import.meta.hot && context.value)
import.meta.hot.on('vite:afterUpdate', () => dispose(context.value as TresContext))
if (import.meta.hot && context.value) { import.meta.hot.on('vite:afterUpdate', () => dispose(context.value as TresContext)) }
})

onUnmounted(() => {
Expand Down
9 changes: 4 additions & 5 deletions src/core/nodeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,13 @@ export const nodeOps: () => RendererOptions<TresObject, TresObject | null> = ()
const ctx = node.__tres
// remove is only called on the node being removed and not on child nodes.
node.parent = node.parent || scene
const {

const {
deregisterObjectAtPointerEventHandler,
deregisterBlockingObjectAtPointerEventHandler,
} = ctx.root

if (node.isObject3D) {

const deregisterAtPointerEventHandlerIfRequired = (object: TresObject) => {
deregisterBlockingObjectAtPointerEventHandler(object as Object3D)
if (
Expand Down Expand Up @@ -174,13 +173,13 @@ export const nodeOps: () => RendererOptions<TresObject, TresObject | null> = ()
deregisterAtPointerEventHandlerIfRequired?.(node as TresObject)
invalidateInstance(node as TresObject)

// Dispose the object if it's disposable, primitives needs to be manually disposed by
// Dispose the object if it's disposable, primitives needs to be manually disposed by
// calling dispose from `@tresjs/core` package like this `dispose(model)`
const isPrimitive = node.__tres.primitive

if (!isPrimitive && node.__tres.disposable) {
disposeObject3D(node as TresObject3D)
}
}
node.dispose?.()
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Material, Texture, Mesh, Object3D } from 'three'
import { MeshBasicMaterial, DoubleSide, Vector3, Scene } from 'three'
import type { Material, Mesh, Object3D, Texture } from 'three'
import { DoubleSide, MeshBasicMaterial, Scene, Vector3 } from 'three'
import { HightlightMesh } from '../devtools/highlight'
import { TresObject3D } from '~/types'

export function toSetMethodName(key: string) {
return `set${key[0].toUpperCase()}${key.slice(1)}`
Expand Down Expand Up @@ -304,4 +303,4 @@ export function disposeObject3D(object: Object3D): void {
}
object.dispose?.()
}
}
}
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.