1
- import { toValue , useElementSize , useFps , useMemory , useRafFn , useWindowSize } from '@vueuse/core'
2
- import { inject , provide , readonly , shallowRef , computed , ref , onUnmounted } from 'vue'
1
+ import { toValue , useElementSize , useFps , useMemory , useRafFn , useWindowSize , refDebounced } from '@vueuse/core'
2
+ import { inject , provide , readonly , shallowRef , computed , ref , onUnmounted , watchEffect } from 'vue'
3
3
import type { Camera , EventDispatcher , Scene , WebGLRenderer } from 'three'
4
4
import { Raycaster } from 'three'
5
5
import type { ComputedRef , DeepReadonly , MaybeRef , MaybeRefOrGetter , Ref , ShallowRef } from 'vue'
@@ -55,14 +55,23 @@ export function useTresContextProvider({
55
55
: useElementSize ( toValue ( canvas ) . parentElement ) ,
56
56
)
57
57
58
- const width = computed ( ( ) => elementSize . value . width . value )
59
- const height = computed ( ( ) => elementSize . value . height . value )
58
+ const reactiveSize = shallowRef ( {
59
+ width : 0 ,
60
+ height : 0 ,
61
+ } )
62
+ const debouncedReactiveSize = refDebounced ( reactiveSize , 10 )
63
+ const unWatchSize = watchEffect ( ( ) => {
64
+ reactiveSize . value = {
65
+ width : elementSize . value . width . value ,
66
+ height : elementSize . value . height . value ,
67
+ }
68
+ } )
60
69
61
- const aspectRatio = computed ( ( ) => width . value / height . value )
70
+ const aspectRatio = computed ( ( ) => debouncedReactiveSize . value . width / debouncedReactiveSize . value . height )
62
71
63
72
const sizes = {
64
- height,
65
- width,
73
+ height : computed ( ( ) => debouncedReactiveSize . value . height ) ,
74
+ width : computed ( ( ) => debouncedReactiveSize . value . width ) ,
66
75
aspectRatio,
67
76
}
68
77
const localScene = shallowRef < Scene > ( scene )
@@ -113,7 +122,7 @@ export function useTresContextProvider({
113
122
114
123
// Performance
115
124
const updateInterval = 100 // Update interval in milliseconds
116
- const fps = useFps ( { every : updateInterval } )
125
+ const fps = useFps ( { every : updateInterval } )
117
126
const { isSupported, memory } = useMemory ( { interval : updateInterval } )
118
127
const maxFrames = 160
119
128
let lastUpdateTime = performance . now ( )
@@ -125,7 +134,7 @@ export function useTresContextProvider({
125
134
if ( toProvide . scene . value ) {
126
135
toProvide . perf . memory . allocatedMem = calculateMemoryUsage ( toProvide . scene . value as unknown as TresObject )
127
136
}
128
-
137
+
129
138
// Update memory usage
130
139
if ( timestamp - lastUpdateTime >= updateInterval ) {
131
140
lastUpdateTime = timestamp
@@ -147,35 +156,36 @@ export function useTresContextProvider({
147
156
toProvide . perf . memory . accumulator . shift ( )
148
157
}
149
158
150
- toProvide . perf . memory . currentMem
159
+ toProvide . perf . memory . currentMem
151
160
= toProvide . perf . memory . accumulator . reduce ( ( a , b ) => a + b , 0 ) / toProvide . perf . memory . accumulator . length
152
-
161
+
153
162
}
154
163
}
155
164
}
156
165
157
166
// Devtools
158
167
let accumulatedTime = 0
159
168
const interval = 1 // Interval in milliseconds, e.g., 1000 ms = 1 second
160
-
169
+
161
170
const { pause, resume } = useRafFn ( ( { delta } ) => {
162
171
if ( ! window . __TRES__DEVTOOLS__ ) return
163
172
164
173
updatePerformanceData ( { timestamp : performance . now ( ) } )
165
-
174
+
166
175
// Accumulate the delta time
167
176
accumulatedTime += delta
168
-
177
+
169
178
// Check if the accumulated time is greater than or equal to the interval
170
179
if ( accumulatedTime >= interval ) {
171
180
window . __TRES__DEVTOOLS__ . cb ( toProvide )
172
-
181
+
173
182
// Reset the accumulated time
174
183
accumulatedTime = 0
175
184
}
176
- } , { immediate : true } )
177
-
185
+ } , { immediate : true } )
186
+
178
187
onUnmounted ( ( ) => {
188
+ unWatchSize ( )
179
189
pause ( )
180
190
} )
181
191
0 commit comments