@@ -110,7 +110,7 @@ const scene = shallowRef<TresScene | Scene>(new Scene())
110
110
const instance = getCurrentInstance ()?.appContext .app
111
111
extend (THREE )
112
112
113
- const createInternalComponent = (context : TresContext ) =>
113
+ const createInternalComponent = (context : TresContext , empty = false ) =>
114
114
defineComponent ({
115
115
setup() {
116
116
const ctx = getCurrentInstance ()?.appContext
@@ -121,12 +121,12 @@ const createInternalComponent = (context: TresContext) =>
121
121
if (typeof window !== ' undefined' ) {
122
122
registerTresDevtools (ctx ?.app , context )
123
123
}
124
- return () => h (Fragment , null , slots ?. default ? slots .default () : [])
124
+ return () => h (Fragment , null , ! empty ? slots .default () : [])
125
125
},
126
126
})
127
127
128
- const mountCustomRenderer = (context : TresContext ) => {
129
- const InternalComponent = createInternalComponent (context )
128
+ const mountCustomRenderer = (context : TresContext , empty = false ) => {
129
+ const InternalComponent = createInternalComponent (context , empty )
130
130
const { render } = createRenderer (nodeOps (context ))
131
131
render (h (InternalComponent ), scene .value as unknown as TresObject )
132
132
}
@@ -141,7 +141,6 @@ const dispose = (context: TresContext, force = false) => {
141
141
(scene .value as TresScene ).__tres = {
142
142
root: context ,
143
143
}
144
- mountCustomRenderer (context )
145
144
}
146
145
147
146
const disableRender = computed (() => props .disableRender )
@@ -150,6 +149,16 @@ const context = shallowRef<TresContext | null>(null)
150
149
151
150
defineExpose ({ context , dispose : () => dispose (context .value as TresContext , true ) })
152
151
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
+
153
162
onMounted (() => {
154
163
const existingCanvas = canvas as Ref <HTMLCanvasElement >
155
164
@@ -209,12 +218,10 @@ onMounted(() => {
209
218
}
210
219
211
220
// 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 )) }
213
222
})
214
223
215
- onUnmounted (() => {
216
- dispose (context .value as TresContext )
217
- })
224
+ onUnmounted (unmountCanvas )
218
225
</script >
219
226
220
227
<template >
0 commit comments