@@ -16,6 +16,19 @@ export interface InternalState {
16
16
maxFrames : number
17
17
}
18
18
19
+ export interface RenderState {
20
+ /**
21
+ * If set to 'on-demand', the scene will only be rendered when the current frame is invalidated
22
+ * If set to 'manual', the scene will only be rendered when advance() is called
23
+ * If set to 'always', the scene will be rendered every frame
24
+ */
25
+ mode : Ref < 'always' | 'on-demand' | 'manual' >
26
+ priority : Ref < number >
27
+ frames : Ref < number >
28
+ maxFrames : number
29
+ canBeInvalidated : ComputedRef < boolean >
30
+ }
31
+
19
32
export interface PerformanceState {
20
33
maxFrames : number
21
34
fps : {
@@ -39,21 +52,14 @@ export interface TresContext {
39
52
renderer : ShallowRef < WebGLRenderer >
40
53
raycaster : ShallowRef < Raycaster >
41
54
perf : PerformanceState
42
- /**
43
- * If set to 'on-demand', the scene will only be rendered when the current frame is invalidated
44
- * If set to 'manual', the scene will only be rendered when advance() is called
45
- * If set to 'always', the scene will be rendered every frame
46
- */
47
- renderMode : Ref < 'always' | 'on-demand' | 'manual' >
48
- canBeInvalidated : ComputedRef < boolean >
49
- internal : InternalState
55
+ render : RenderState
50
56
/**
51
57
* Invalidates the current frame when renderMode === 'on-demand'
52
58
*/
53
59
invalidate : ( ) => void
54
60
/**
55
- * Advance one frame when renderMode === 'manual'
56
- */
61
+ * Advance one frame when renderMode === 'manual'
62
+ */
57
63
advance : ( ) => void
58
64
registerCamera : ( camera : Camera ) => void
59
65
setCameraActive : ( cameraOrUuid : Camera | string ) => void
@@ -112,17 +118,20 @@ export function useTresContextProvider({
112
118
setCameraActive,
113
119
} = useCamera ( { sizes, scene } )
114
120
115
- // Initialize internal state
116
- const internal : InternalState = {
121
+ // Render state
122
+
123
+ const render : RenderState = {
124
+ mode : ref < 'always' | 'on-demand' | 'manual' > ( rendererOptions . renderMode || 'always' ) ,
117
125
priority : ref ( 0 ) ,
118
126
frames : ref ( 0 ) ,
119
127
maxFrames : 60 ,
128
+ canBeInvalidated : computed ( ( ) => render . mode . value === 'on-demand' && render . frames . value === 0 ) ,
120
129
}
121
130
122
131
function invalidate ( frames = 1 ) {
123
132
// Increase the frame count, ensuring not to exceed a maximum if desired
124
133
if ( rendererOptions . renderMode === 'on-demand' ) {
125
- internal . frames . value = Math . min ( internal . maxFrames , internal . frames . value + frames )
134
+ render . frames . value = Math . min ( render . maxFrames , render . frames . value + frames )
126
135
}
127
136
else {
128
137
logWarning ( '`invalidate` can only be used when `renderMode` is set to `on-demand`' )
@@ -131,7 +140,7 @@ export function useTresContextProvider({
131
140
132
141
function advance ( ) {
133
142
if ( rendererOptions . renderMode === 'manual' ) {
134
- internal . frames . value = 1
143
+ render . frames . value = 1
135
144
}
136
145
else {
137
146
logWarning ( '`advance` can only be used when `renderMode` is set to `manual`' )
@@ -144,13 +153,12 @@ export function useTresContextProvider({
144
153
canvas,
145
154
options : rendererOptions ,
146
155
emit,
147
- contextParts : { sizes, camera, internal, invalidate, advance } ,
156
+ // TODO: replace contextParts with full ctx at https://github.com/Tresjs/tres/issues/516
157
+ contextParts : { sizes, camera, render, invalidate, advance } ,
148
158
disableRender,
149
159
} )
150
160
151
- const renderMode = ref < 'always' | 'on-demand' | 'manual' > ( rendererOptions . renderMode || 'always' )
152
-
153
- const toProvide : TresContext = {
161
+ const ctx : TresContext = {
154
162
sizes,
155
163
scene : localScene ,
156
164
camera,
@@ -170,9 +178,7 @@ export function useTresContextProvider({
170
178
accumulator : [ ] ,
171
179
} ,
172
180
} ,
173
- renderMode,
174
- canBeInvalidated : computed ( ( ) => renderMode . value === 'on-demand' && internal . frames . value === 0 ) ,
175
- internal,
181
+ render,
176
182
advance,
177
183
extend,
178
184
invalidate,
@@ -181,10 +187,10 @@ export function useTresContextProvider({
181
187
deregisterCamera,
182
188
}
183
189
184
- provide ( 'useTres' , toProvide )
190
+ provide ( 'useTres' , ctx )
185
191
186
192
// Add context to scene.userData
187
- toProvide . scene . value . userData . tres__context = toProvide
193
+ ctx . scene . value . userData . tres__context = ctx
188
194
189
195
// Performance
190
196
const updateInterval = 100 // Update interval in milliseconds
@@ -197,33 +203,33 @@ export function useTresContextProvider({
197
203
198
204
// Update WebGL Memory Usage (Placeholder for actual logic)
199
205
// perf.memory.value = calculateMemoryUsage(gl)
200
- if ( toProvide . scene . value ) {
201
- toProvide . perf . memory . allocatedMem = calculateMemoryUsage ( toProvide . scene . value as unknown as TresObject )
206
+ if ( ctx . scene . value ) {
207
+ ctx . perf . memory . allocatedMem = calculateMemoryUsage ( ctx . scene . value as unknown as TresObject )
202
208
}
203
209
204
210
// Update memory usage
205
211
if ( timestamp - lastUpdateTime >= updateInterval ) {
206
212
lastUpdateTime = timestamp
207
213
208
214
// Update FPS
209
- toProvide . perf . fps . accumulator . push ( fps . value as never )
215
+ ctx . perf . fps . accumulator . push ( fps . value as never )
210
216
211
- if ( toProvide . perf . fps . accumulator . length > maxFrames ) {
212
- toProvide . perf . fps . accumulator . shift ( )
217
+ if ( ctx . perf . fps . accumulator . length > maxFrames ) {
218
+ ctx . perf . fps . accumulator . shift ( )
213
219
}
214
220
215
- toProvide . perf . fps . value = fps . value
221
+ ctx . perf . fps . value = fps . value
216
222
217
223
// Update memory
218
224
if ( isSupported . value && memory . value ) {
219
- toProvide . perf . memory . accumulator . push ( memory . value . usedJSHeapSize / 1024 / 1024 as never )
225
+ ctx . perf . memory . accumulator . push ( memory . value . usedJSHeapSize / 1024 / 1024 as never )
220
226
221
- if ( toProvide . perf . memory . accumulator . length > maxFrames ) {
222
- toProvide . perf . memory . accumulator . shift ( )
227
+ if ( ctx . perf . memory . accumulator . length > maxFrames ) {
228
+ ctx . perf . memory . accumulator . shift ( )
223
229
}
224
230
225
- toProvide . perf . memory . currentMem
226
- = toProvide . perf . memory . accumulator . reduce ( ( a , b ) => a + b , 0 ) / toProvide . perf . memory . accumulator . length
231
+ ctx . perf . memory . currentMem
232
+ = ctx . perf . memory . accumulator . reduce ( ( a , b ) => a + b , 0 ) / ctx . perf . memory . accumulator . length
227
233
228
234
}
229
235
}
@@ -243,7 +249,7 @@ export function useTresContextProvider({
243
249
244
250
// Check if the accumulated time is greater than or equal to the interval
245
251
if ( accumulatedTime >= interval ) {
246
- window . __TRES__DEVTOOLS__ . cb ( toProvide )
252
+ window . __TRES__DEVTOOLS__ . cb ( ctx )
247
253
248
254
// Reset the accumulated time
249
255
accumulatedTime = 0
@@ -255,7 +261,7 @@ export function useTresContextProvider({
255
261
pause ( )
256
262
} )
257
263
258
- return toProvide
264
+ return ctx
259
265
}
260
266
261
267
export function useTresContext ( ) : TresContext {
0 commit comments