@@ -10,8 +10,14 @@ export type UpdateCallbackRef = MutableRefObject<UpdateCallback>
10
10
type Store = UseBoundStore < RootState , StoreApi < RootState > >
11
11
export type UpdateSubscription = { ref : UpdateCallbackRef ; store : Store }
12
12
13
- export type FixedStageOptions = { fixedStep ?: number ; maxSubSteps ?: number }
14
-
13
+ export type FixedStageOptions = { fixedStep ?: number ; maxSubsteps ?: number }
14
+ export type FixedStageProps = { fixedStep : number ; maxSubsteps : number ; accumulator : number ; alpha : number }
15
+
16
+ /**
17
+ * Class representing a stage that updates every frame.
18
+ * Stages are used to build a lifecycle of effects for an app's frameloop.
19
+ * @param {string } name - Name of the stage.
20
+ */
15
21
export class Stage {
16
22
name : string
17
23
subscribers : UpdateSubscription [ ]
@@ -21,6 +27,11 @@ export class Stage {
21
27
this . subscribers = [ ]
22
28
}
23
29
30
+ /**
31
+ * Executes all callback subscriptions on the stage.
32
+ * @param {number } delta - Delta time between frame calls.
33
+ * @param {THREE.XRFrame | undefined } [frame] - The XR frame if it exists.
34
+ */
24
35
frame ( delta : number , frame ?: THREE . XRFrame | undefined ) {
25
36
const subs = this . subscribers
26
37
@@ -29,6 +40,12 @@ export class Stage {
29
40
}
30
41
}
31
42
43
+ /**
44
+ * Adds a callback subscriber to the stage.
45
+ * @param {UpdateCallbackRef } ref - The mutable callback reference.
46
+ * @param {Store } store - The store to be used with the callback execution.
47
+ * @returns {() => void } A function to remove the subscription.
48
+ */
32
49
add ( ref : UpdateCallbackRef , store : Store ) {
33
50
this . subscribers . push ( { ref, store } )
34
51
@@ -40,9 +57,17 @@ export class Stage {
40
57
}
41
58
}
42
59
43
- // Using Unity's default here .
60
+ // Using Unity's fixedStep default .
44
61
const FPS_50 = 1 / 50
45
62
63
+ /**
64
+ * Class representing a stage that updates every frame at a fixed rate.
65
+ * @param {string } name - Name of the stage.
66
+ * @param {FixedStageOptions } [options] - Options for the fixed stage.
67
+ * @param {number } [options.fixedStep] - Fixed step rate.
68
+ * @param {number } [options.maxSubsteps] - Maximum number of substeps.
69
+ * @extends Stage
70
+ */
46
71
export class FixedStage extends Stage {
47
72
private fixedStep : number
48
73
private maxSubsteps : number
@@ -58,6 +83,11 @@ export class FixedStage extends Stage {
58
83
this . alpha = 0
59
84
}
60
85
86
+ /**
87
+ * Executes all callback subscriptions on the stage.
88
+ * @param {number } delta - Delta time between frame calls.
89
+ * @param {THREE.XRFrame | undefined } [frame] - The XR frame if it exists.
90
+ */
61
91
frame ( delta : number , frame ?: THREE . XRFrame | undefined ) {
62
92
const initialTime = performance . now ( )
63
93
let substeps = 0
@@ -77,19 +107,33 @@ export class FixedStage extends Stage {
77
107
}
78
108
79
109
// The accumulator will only be larger than the fixed step if we had to
80
- // bail early due to hitting the max substep limit. In that case, we want to
81
- // shave off the excess so we don't fall behind next frame.
110
+ // bail early due to hitting the max substep limit or execution time lagging.
111
+ // In that case, we want to shave off the excess so we don't fall behind next frame.
82
112
this . accumulator = this . accumulator % this . fixedStep
83
113
this . alpha = this . accumulator / this . fixedStep
84
114
}
85
115
116
+ /**
117
+ * Set the fixed stage properties.
118
+ * @param {FixedStageOptions } options - Options for the fixed stage.
119
+ * @param {number } [options.fixedStep] - Fixed step rate.
120
+ * @param {number } [options.maxSubsteps] - Maximum number of substeps.
121
+ */
86
122
set ( options : FixedStageOptions ) {
87
- const { fixedStep, maxSubSteps } = options
123
+ const { fixedStep, maxSubsteps } = options
88
124
if ( fixedStep ) this . fixedStep = fixedStep
89
- if ( maxSubSteps ) this . maxSubsteps = maxSubSteps
125
+ if ( maxSubsteps ) this . maxSubsteps = maxSubsteps
90
126
}
91
127
92
- get ( ) {
128
+ /**
129
+ * Get the fixed stage properties.
130
+ * @returns {FixedStageProps } props The fixed stage properties.
131
+ * @returns {number } props.fixedStep The fixed step rate.
132
+ * @returns {number } props.maxSubsteps The maximum number of substeps.
133
+ * @returns {number } props.accumulator The time left over in the accumulator.
134
+ * @returns {number } props.alpha The ratio of remaining time and step size. Useful for interpolation.
135
+ */
136
+ get ( ) : FixedStageProps {
93
137
return {
94
138
fixedStep : this . fixedStep ,
95
139
maxSubsteps : this . maxSubsteps ,
@@ -106,8 +150,8 @@ const Late = new Stage('late')
106
150
const Render = new Stage ( 'render' )
107
151
const After = new Stage ( 'after' )
108
152
109
- export const Standard = { Early, Fixed, Update, Late, Render, After }
110
- export const StandardStages = [ Early , Fixed , Update , Late , Render , After ]
153
+ export const StandardStages = { Early, Fixed, Update, Late, Render, After }
154
+ export const StandardLifecycle = [ Early , Fixed , Update , Late , Render , After ]
111
155
112
156
export enum Stages {
113
157
Early = 'early' ,
0 commit comments