@@ -110,7 +110,7 @@ const scene = shallowRef<TresScene | Scene>(new Scene())
110110const instance = getCurrentInstance ()?.appContext .app
111111extend (THREE )
112112
113- const createInternalComponent = (context : TresContext ) =>
113+ const createInternalComponent = (context : TresContext , empty = false ) =>
114114 defineComponent ({
115115 setup() {
116116 const ctx = getCurrentInstance ()?.appContext
@@ -121,12 +121,12 @@ const createInternalComponent = (context: TresContext) =>
121121 if (typeof window !== ' undefined' ) {
122122 registerTresDevtools (ctx ?.app , context )
123123 }
124- return () => h (Fragment , null , slots ?. default ? slots .default () : [])
124+ return () => h (Fragment , null , ! empty ? slots .default () : [])
125125 },
126126 })
127127
128- const mountCustomRenderer = (context : TresContext ) => {
129- const InternalComponent = createInternalComponent (context )
128+ const mountCustomRenderer = (context : TresContext , empty = false ) => {
129+ const InternalComponent = createInternalComponent (context , empty )
130130 const { render } = createRenderer (nodeOps (context ))
131131 render (h (InternalComponent ), scene .value as unknown as TresObject )
132132}
@@ -141,7 +141,6 @@ const dispose = (context: TresContext, force = false) => {
141141 (scene .value as TresScene ).__tres = {
142142 root: context ,
143143 }
144- mountCustomRenderer (context )
145144}
146145
147146const disableRender = computed (() => props .disableRender )
@@ -150,6 +149,16 @@ const context = shallowRef<TresContext | null>(null)
150149
151150defineExpose ({ context , dispose : () => dispose (context .value as TresContext , true ) })
152151
152+ const handleHMR = (context : TresContext ) => {
153+ dispose (context )
154+ mountCustomRenderer (context )
155+ }
156+
157+ const unmountCanvas = () => {
158+ dispose (context .value as TresContext )
159+ mountCustomRenderer (context .value as TresContext , true )
160+ }
161+
153162onMounted (() => {
154163 const existingCanvas = canvas as Ref <HTMLCanvasElement >
155164
@@ -209,12 +218,10 @@ onMounted(() => {
209218 }
210219
211220 // HMR support
212- if (import .meta .hot && context .value ) { import .meta .hot .on (' vite:afterUpdate' , () => dispose (context .value as TresContext )) }
221+ if (import .meta .hot && context .value ) { import .meta .hot .on (' vite:afterUpdate' , () => handleHMR (context .value as TresContext )) }
213222})
214223
215- onUnmounted (() => {
216- dispose (context .value as TresContext )
217- })
224+ onUnmounted (unmountCanvas )
218225 </script >
219226
220227<template >
0 commit comments