-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
store.ts
358 lines (316 loc) Β· 11 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import * as THREE from 'three'
import * as React from 'react'
import * as ReactThreeFiber from '../three-types'
import create, { GetState, SetState, UseStore } from 'zustand'
import shallow from 'zustand/shallow'
import { prepare, Instance, InstanceProps } from './renderer'
import { DomEvent, EventManager } from './events'
export interface Intersection extends THREE.Intersection {
eventObject: THREE.Object3D
}
export type Subscription = {
ref: React.MutableRefObject<RenderCallback>
priority: number
}
export type Dpr = number | [min: number, max: number]
export type Size = { width: number; height: number }
export type Viewport = Size & {
initialDpr: number
dpr: number
factor: number
distance: number
aspect: number
}
export type Camera = THREE.OrthographicCamera | THREE.PerspectiveCamera
export type Raycaster = THREE.Raycaster & {
enabled: boolean
filter?: FilterFunction
computeOffsets?: ComputeOffsetsFunction
}
export type RenderCallback = (state: RootState, delta: number) => void
export type Performance = {
current: number
min: number
max: number
debounce: number
regress: () => void
}
export const isRenderer = (def: THREE.WebGLRenderer): def is THREE.WebGLRenderer =>
def && !!(def as THREE.WebGLRenderer).render
export const isOrthographicCamera = (def: THREE.Camera): def is THREE.OrthographicCamera =>
def && (def as THREE.OrthographicCamera).isOrthographicCamera
export type InternalState = {
active: boolean
priority: number
frames: number
lastProps: StoreProps
interaction: THREE.Object3D[]
hovered: Map<string, DomEvent>
subscribers: Subscription[]
captured: Intersection[] | undefined
initialClick: [x: number, y: number]
initialHits: THREE.Object3D[]
subscribe: (callback: React.MutableRefObject<RenderCallback>, priority?: number) => () => void
}
export type RootState = {
gl: THREE.WebGLRenderer
scene: THREE.Scene
camera: Camera
raycaster: Raycaster
mouse: THREE.Vector2
clock: THREE.Clock
vr: boolean
linear: boolean
flat: boolean
frameloop: 'always' | 'demand' | 'never'
performance: Performance
size: Size
viewport: Viewport & {
getCurrentViewport: (camera?: Camera, target?: THREE.Vector3, size?: Size) => Omit<Viewport, 'dpr' | 'initialDpr'>
}
set: SetState<RootState>
get: GetState<RootState>
invalidate: () => void
advance: (timestamp: number, runGlobalEffects?: boolean) => void
setSize: (width: number, height: number) => void
setDpr: (dpr: Dpr) => void
onPointerMissed?: () => void
events: EventManager<any>
internal: InternalState
}
export type FilterFunction = (items: THREE.Intersection[], state: RootState) => THREE.Intersection[]
export type ComputeOffsetsFunction = (event: any, state: RootState) => { offsetX: number; offsetY: number }
export type StoreProps = {
gl: THREE.WebGLRenderer
size: Size
vr?: boolean
shadows?: boolean | Partial<THREE.WebGLShadowMap>
linear?: boolean
flat?: boolean
orthographic?: boolean
frameloop?: 'always' | 'demand' | 'never'
performance?: Partial<Omit<Performance, 'regress'>>
dpr?: Dpr
clock?: THREE.Clock
raycaster?: Partial<Raycaster>
camera?:
| Camera
| Partial<
ReactThreeFiber.Object3DNode<THREE.Camera, typeof THREE.Camera> &
ReactThreeFiber.Object3DNode<THREE.PerspectiveCamera, typeof THREE.PerspectiveCamera> &
ReactThreeFiber.Object3DNode<THREE.OrthographicCamera, typeof THREE.OrthographicCamera>
>
onPointerMissed?: () => void
}
export type ApplyProps = (
instance: Instance,
newProps: InstanceProps,
oldProps?: InstanceProps,
accumulative?: boolean,
) => void
const context = React.createContext<UseStore<RootState>>(null!)
const createStore = (
applyProps: ApplyProps,
invalidate: (state?: RootState) => void,
advance: (timestamp: number, runGlobalEffects?: boolean, state?: RootState) => void,
props: StoreProps,
): UseStore<RootState> => {
const {
gl,
size,
shadows = false,
linear = false,
flat = false,
vr = false,
orthographic = false,
frameloop = 'always',
dpr = 1,
performance,
clock = new THREE.Clock(),
raycaster: raycastOptions,
camera: cameraOptions,
onPointerMissed,
} = props
// Set shadowmap
if (shadows) {
gl.shadowMap.enabled = true
if (typeof shadows === 'object') Object.assign(gl.shadowMap, shadows)
else gl.shadowMap.type = THREE.PCFSoftShadowMap
}
// Set color management
if (!linear) {
if (!flat) gl.toneMapping = THREE.ACESFilmicToneMapping
gl.outputEncoding = THREE.sRGBEncoding
}
const rootState = create<RootState>((set, get) => {
// Create custom raycaster
const raycaster = new THREE.Raycaster() as Raycaster
const { params, ...options } = raycastOptions || {}
applyProps(raycaster as any, { enabled: true, ...options, params: { ...raycaster.params, ...params } }, {})
// Create default camera
const isCamera = cameraOptions instanceof THREE.Camera
const camera = isCamera
? (cameraOptions as Camera)
: orthographic
? new THREE.OrthographicCamera(0, 0, 0, 0, 0.1, 1000)
: new THREE.PerspectiveCamera(75, 0, 0.1, 1000)
if (!isCamera) {
camera.position.z = 5
if (cameraOptions) applyProps(camera as any, cameraOptions as any, {})
// Always look at center by default
camera.lookAt(0, 0, 0)
}
function setDpr(dpr: Dpr) {
return Array.isArray(dpr) ? Math.min(Math.max(dpr[0], window.devicePixelRatio), dpr[1]) : dpr
}
const initialDpr = setDpr(dpr)
const position = new THREE.Vector3()
const defaultTarget = new THREE.Vector3()
function getCurrentViewport(
camera: Camera = get().camera,
target: THREE.Vector3 = defaultTarget,
size: Size = get().size,
) {
const { width, height } = size
const aspect = width / height
const distance = camera.getWorldPosition(position).distanceTo(target)
if (isOrthographicCamera(camera)) {
return { width: width / camera.zoom, height: height / camera.zoom, factor: 1, distance, aspect }
} else {
const fov = (camera.fov * Math.PI) / 180 // convert vertical fov to radians
const h = 2 * Math.tan(fov / 2) * distance // visible height
const w = h * (width / height)
return { width: w, height: h, factor: width / w, distance, aspect }
}
}
let performanceTimeout: ReturnType<typeof setTimeout> | undefined = undefined
const setPerformanceCurrent = (current: number) =>
set((state) => ({ performance: { ...state.performance, current } }))
return {
gl,
set,
get,
invalidate: () => invalidate(get()),
advance: (timestamp: number, runGlobalEffects?: boolean) => advance(timestamp, runGlobalEffects, get()),
linear,
flat,
scene: prepare<THREE.Scene>(new THREE.Scene()),
camera,
raycaster,
clock,
mouse: new THREE.Vector2(),
vr,
frameloop,
onPointerMissed,
performance: {
current: 1,
min: 0.5,
max: 1,
debounce: 200,
...performance,
regress: () => {
const state = get()
// Clear timeout
if (performanceTimeout) clearTimeout(performanceTimeout)
// Set lower bound performance
if (state.performance.current !== state.performance.min) setPerformanceCurrent(state.performance.min)
// Go back to upper bound performance after a while unless something regresses meanwhile
performanceTimeout = setTimeout(
() => setPerformanceCurrent(get().performance.max),
state.performance.debounce,
)
},
},
size: { width: 0, height: 0 },
viewport: {
initialDpr,
dpr: initialDpr,
width: 0,
height: 0,
aspect: 0,
distance: 0,
factor: 0,
getCurrentViewport,
},
setSize: (width: number, height: number) => {
const size = { width, height }
set((state) => ({ size, viewport: { ...state.viewport, ...getCurrentViewport(camera, defaultTarget, size) } }))
},
setDpr: (dpr: Dpr) => set((state) => ({ viewport: { ...state.viewport, dpr: setDpr(dpr) } })),
events: { connected: false },
internal: {
active: false,
priority: 0,
frames: 0,
lastProps: props,
interaction: [],
hovered: new Map<string, DomEvent>(),
subscribers: [],
captured: undefined,
initialClick: [0, 0],
initialHits: [],
subscribe: (ref: React.MutableRefObject<RenderCallback>, priority = 0) => {
set(({ internal }) => ({
internal: {
...internal,
// If this subscription was given a priority, it takes rendering into its own hands
// For that reason we switch off automatic rendering and increase the manual flag
// As long as this flag is positive (there could be multiple render subscription)
// ..there can be no internal rendering at all
priority: internal.priority + (priority ? 1 : 0),
// Register subscriber and sort layers from lowest to highest, meaning,
// highest priority renders last (on top of the other frames)
subscribers: [...internal.subscribers, { ref, priority }].sort((a, b) => a.priority - b.priority),
},
}))
return () => {
set(({ internal }) => ({
internal: {
...internal,
// Decrease manual flag if this subscription had a priority
priority: internal.priority - (priority ? 1 : 0),
// Remove subscriber from list
subscribers: internal.subscribers.filter((s) => s.ref !== ref),
},
}))
}
},
},
}
})
// Resize camera and renderer on changes to size and pixelratio
rootState.subscribe(
() => {
const { camera, size, viewport, internal } = rootState.getState()
// https://github.com/pmndrs/react-three-fiber/issues/92
// Do not mess with the camera if it belongs to the user
if (!(internal.lastProps.camera instanceof THREE.Camera)) {
if (isOrthographicCamera(camera)) {
camera.left = size.width / -2
camera.right = size.width / 2
camera.top = size.height / 2
camera.bottom = size.height / -2
} else {
camera.aspect = size.width / size.height
}
camera.updateProjectionMatrix()
// https://github.com/pmndrs/react-three-fiber/issues/178
// Update matrix world since the renderer is a frame late
camera.updateMatrixWorld()
}
// Update renderer
gl.setPixelRatio(viewport.dpr)
gl.setSize(size.width, size.height)
},
(state) => [state.viewport.dpr, state.size],
shallow,
)
const state = rootState.getState()
// Update size
if (size) state.setSize(size.width, size.height)
// Invalidate on any change
rootState.subscribe((state) => invalidate(state))
// Return root state
return rootState
}
export { createStore, context }