@atom.io/spa 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +254 -6
- package/README.mdx +83 -1
- package/dist/astro-application.d.ts +1 -1
- package/dist/astro-application.js +2 -2
- package/dist/astro-application.js.map +1 -1
- package/dist/astro-client.d.ts +1 -1
- package/dist/astro-client.js +6 -3
- package/dist/astro-client.js.map +1 -1
- package/dist/astro-server.js +2 -1
- package/dist/astro-server.js.map +1 -1
- package/dist/astro-shared-BT45auwR.js.map +1 -1
- package/dist/{astro-shared-DkZaqX7L.d.ts → astro-shared-BkWvVZWZ.d.ts} +4 -2
- package/dist/astro-shared-BkWvVZWZ.d.ts.map +1 -0
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +1 -1
- package/dist/capabilities-BkQ2i0vV.js +63 -0
- package/dist/capabilities-BkQ2i0vV.js.map +1 -0
- package/dist/capabilities-CFP-HIuY.d.ts +115 -0
- package/dist/capabilities-CFP-HIuY.d.ts.map +1 -0
- package/dist/client.min.js +1 -1
- package/dist/{core-Dvqm7w2y.js → core-DCmqLZQP.js} +23 -4
- package/dist/core-DCmqLZQP.js.map +1 -0
- package/dist/development/astro-application.js +2 -2
- package/dist/development/astro-application.js.map +1 -1
- package/dist/development/astro-client.js +6 -3
- package/dist/development/astro-client.js.map +1 -1
- package/dist/development/astro-server.js +2 -1
- package/dist/development/astro-server.js.map +1 -1
- package/dist/development/astro-shared-BT45auwR.js.map +1 -1
- package/dist/development/astro.js +1 -1
- package/dist/development/capabilities-BkQ2i0vV.js +63 -0
- package/dist/development/capabilities-BkQ2i0vV.js.map +1 -0
- package/dist/development/{core-DB3sQz4h.js → core-p9vciX9C.js} +23 -4
- package/dist/development/core-p9vciX9C.js.map +1 -0
- package/dist/development/{dom-D9cY6l1R.js → dom-Ci1XvbVJ.js} +21 -7
- package/dist/development/dom-Ci1XvbVJ.js.map +1 -0
- package/dist/development/index.js +3 -38
- package/dist/development/jsx-runtime.js.map +1 -1
- package/dist/development/konva.js +143 -0
- package/dist/development/konva.js.map +1 -0
- package/dist/development/motion.js +601 -0
- package/dist/development/motion.js.map +1 -0
- package/dist/development/renderer.js +190 -0
- package/dist/development/renderer.js.map +1 -0
- package/dist/development/server.js +4 -2
- package/dist/development/server.js.map +1 -1
- package/dist/development/src-DG0xjuS5.js +38 -0
- package/dist/development/src-DG0xjuS5.js.map +1 -0
- package/dist/development/three.js +452 -0
- package/dist/development/three.js.map +1 -0
- package/dist/development/vite-CSHoxzXU.js +240 -0
- package/dist/development/vite-CSHoxzXU.js.map +1 -0
- package/dist/development/vite.js +1 -1
- package/dist/{dom-BR9o9wZW.js → dom-dHQM0O7Q.js} +21 -7
- package/dist/dom-dHQM0O7Q.js.map +1 -0
- package/dist/index-CJJXcKaw.d.ts +24 -0
- package/dist/index-CJJXcKaw.d.ts.map +1 -0
- package/dist/index.d.ts +4 -24
- package/dist/index.js +3 -38
- package/dist/{jsx-runtime-BzGV18VO.d.ts → jsx-runtime-CREZYKDC.d.ts} +6 -4
- package/dist/jsx-runtime-CREZYKDC.d.ts.map +1 -0
- package/dist/jsx-runtime.d.ts +2 -2
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/konva.d.ts +32 -0
- package/dist/konva.d.ts.map +1 -0
- package/dist/konva.js +143 -0
- package/dist/konva.js.map +1 -0
- package/dist/motion.d.ts +45 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +601 -0
- package/dist/motion.js.map +1 -0
- package/dist/renderer.d.ts +24 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +190 -0
- package/dist/renderer.js.map +1 -0
- package/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +4 -2
- package/dist/server.js.map +1 -1
- package/dist/size.json +16 -16
- package/dist/src-DCQa6qxs.js +38 -0
- package/dist/src-DCQa6qxs.js.map +1 -0
- package/dist/three.d.ts +110 -0
- package/dist/three.d.ts.map +1 -0
- package/dist/three.js +452 -0
- package/dist/three.js.map +1 -0
- package/dist/vite-CSHoxzXU.js +240 -0
- package/dist/vite-CSHoxzXU.js.map +1 -0
- package/dist/vite.js +1 -1
- package/package.json +62 -15
- package/src/astro-application.ts +2 -2
- package/src/astro-client.ts +1 -0
- package/src/astro-server.ts +1 -0
- package/src/astro-shared.ts +3 -0
- package/src/capabilities.ts +107 -0
- package/src/compiler.ts +251 -178
- package/src/core.ts +38 -2
- package/src/dom.ts +38 -4
- package/src/index.ts +5 -0
- package/src/jsx-runtime.ts +4 -0
- package/src/jsx-types.d.ts +3 -1
- package/src/konva.ts +186 -0
- package/src/motion-tween.ts +106 -0
- package/src/motion.ts +519 -0
- package/src/presence.ts +324 -0
- package/src/renderer.ts +278 -0
- package/src/server.ts +3 -0
- package/src/three-events.ts +342 -0
- package/src/three.ts +373 -0
- package/dist/astro-shared-DkZaqX7L.d.ts.map +0 -1
- package/dist/core-BhiA9fFi.d.ts +0 -44
- package/dist/core-BhiA9fFi.d.ts.map +0 -1
- package/dist/core-Dvqm7w2y.js.map +0 -1
- package/dist/development/core-DB3sQz4h.js.map +0 -1
- package/dist/development/dom-D9cY6l1R.js.map +0 -1
- package/dist/development/index.js.map +0 -1
- package/dist/development/vite-CQchIQ_-.js +0 -126
- package/dist/development/vite-CQchIQ_-.js.map +0 -1
- package/dist/dom-BR9o9wZW.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/jsx-runtime-BzGV18VO.d.ts.map +0 -1
- package/dist/vite-CQchIQ_-.js +0 -126
- package/dist/vite-CQchIQ_-.js.map +0 -1
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import * as THREE from "three"
|
|
2
|
+
|
|
3
|
+
export interface ThreePointerEvent<N extends THREE.Object3D = THREE.Object3D> {
|
|
4
|
+
type: string
|
|
5
|
+
nativeEvent: PointerEvent
|
|
6
|
+
target: THREE.Object3D
|
|
7
|
+
currentTarget: N
|
|
8
|
+
intersection: THREE.Intersection
|
|
9
|
+
ray: THREE.Ray
|
|
10
|
+
pointerId: number
|
|
11
|
+
button: number
|
|
12
|
+
buttons: number
|
|
13
|
+
clientX: number
|
|
14
|
+
clientY: number
|
|
15
|
+
stopPropagation(): void
|
|
16
|
+
preventDefault(): void
|
|
17
|
+
setPointerCapture(): void
|
|
18
|
+
releasePointerCapture(): void
|
|
19
|
+
hasPointerCapture(): boolean
|
|
20
|
+
}
|
|
21
|
+
export type ThreeEventHandler<N extends THREE.Object3D = THREE.Object3D> = (
|
|
22
|
+
event: ThreePointerEvent<N>,
|
|
23
|
+
) => void
|
|
24
|
+
export interface ThreeEventAttributes<
|
|
25
|
+
N extends THREE.Object3D = THREE.Object3D,
|
|
26
|
+
> {
|
|
27
|
+
onPointerDown?: ThreeEventHandler<N>
|
|
28
|
+
onPointerUp?: ThreeEventHandler<N>
|
|
29
|
+
onPointerMove?: ThreeEventHandler<N>
|
|
30
|
+
onPointerEnter?: ThreeEventHandler<N>
|
|
31
|
+
onPointerLeave?: ThreeEventHandler<N>
|
|
32
|
+
onPointerOver?: ThreeEventHandler<N>
|
|
33
|
+
onPointerOut?: ThreeEventHandler<N>
|
|
34
|
+
onPointerCancel?: ThreeEventHandler<N>
|
|
35
|
+
onClick?: ThreeEventHandler<N>
|
|
36
|
+
}
|
|
37
|
+
type Hit = { node: THREE.Object3D; intersection: THREE.Intersection }
|
|
38
|
+
type Handler = ThreeEventHandler
|
|
39
|
+
const suspended = new WeakSet<THREE.Object3D>()
|
|
40
|
+
export function suspendThree(node: THREE.Object3D): () => void {
|
|
41
|
+
suspended.add(node)
|
|
42
|
+
for (const manager of managers) manager.remove(node)
|
|
43
|
+
return () => {
|
|
44
|
+
suspended.delete(node)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const listeners = new WeakMap<THREE.Object3D, Map<string, Set<Handler>>>()
|
|
48
|
+
const managers = new Set<{ remove(node: THREE.Object3D): void }>()
|
|
49
|
+
|
|
50
|
+
export function listenThree(
|
|
51
|
+
node: THREE.Object3D,
|
|
52
|
+
type: string,
|
|
53
|
+
handler: Handler,
|
|
54
|
+
): () => void {
|
|
55
|
+
const events = listeners.get(node) ?? new Map<string, Set<Handler>>()
|
|
56
|
+
listeners.set(node, events)
|
|
57
|
+
const handlers = events.get(type) ?? new Set<Handler>()
|
|
58
|
+
events.set(type, handlers)
|
|
59
|
+
handlers.add(handler)
|
|
60
|
+
return () => {
|
|
61
|
+
handlers.delete(handler)
|
|
62
|
+
if (!handlers.size) events.delete(type)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export function forgetThree(node: THREE.Object3D): void {
|
|
66
|
+
const errors: unknown[] = []
|
|
67
|
+
for (const manager of managers) {
|
|
68
|
+
try {
|
|
69
|
+
manager.remove(node)
|
|
70
|
+
} catch (error) {
|
|
71
|
+
errors.push(error)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
listeners.delete(node)
|
|
75
|
+
if (errors.length) throw new AggregateError(errors)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface ThreeEventManager {
|
|
79
|
+
update(): void
|
|
80
|
+
/** Release hover, press, and capture state for a removed subtree. */
|
|
81
|
+
remove(node: THREE.Object3D): void
|
|
82
|
+
dispose(): void
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Attach raycast pointer routing to a canvas; usable with borrowed scene roots. */
|
|
86
|
+
export function createThreeEvents(
|
|
87
|
+
canvas: HTMLCanvasElement,
|
|
88
|
+
scene: THREE.Object3D,
|
|
89
|
+
camera: () => THREE.Camera,
|
|
90
|
+
onError: (error: unknown) => void = (error) => {
|
|
91
|
+
throw error
|
|
92
|
+
},
|
|
93
|
+
): ThreeEventManager {
|
|
94
|
+
const raycaster = new THREE.Raycaster()
|
|
95
|
+
const pointer = new THREE.Vector2()
|
|
96
|
+
const hovering = new Map<number, Hit>()
|
|
97
|
+
const pressed = new Map<number, Hit>()
|
|
98
|
+
const captures = new Map<number, Hit>()
|
|
99
|
+
const last = new Map<number, PointerEvent>()
|
|
100
|
+
let dead = false
|
|
101
|
+
const path = (node: THREE.Object3D) => {
|
|
102
|
+
const nodes: THREE.Object3D[] = []
|
|
103
|
+
for (let at: THREE.Object3D | null = node; at; at = at.parent) {
|
|
104
|
+
nodes.push(at)
|
|
105
|
+
if (at === scene) return nodes
|
|
106
|
+
}
|
|
107
|
+
return []
|
|
108
|
+
}
|
|
109
|
+
const contains = (node: THREE.Object3D, child: THREE.Object3D) => {
|
|
110
|
+
for (let at: THREE.Object3D | null = child; at; at = at.parent)
|
|
111
|
+
if (at === node) return true
|
|
112
|
+
return false
|
|
113
|
+
}
|
|
114
|
+
const eligible = (intersection: THREE.Intersection) => {
|
|
115
|
+
const nodes = path(intersection.object)
|
|
116
|
+
if (
|
|
117
|
+
!nodes.length ||
|
|
118
|
+
nodes.some((node) => !node.visible || suspended.has(node))
|
|
119
|
+
)
|
|
120
|
+
return false
|
|
121
|
+
const material = (intersection.object as THREE.Mesh).material
|
|
122
|
+
return (
|
|
123
|
+
!material ||
|
|
124
|
+
(Array.isArray(material)
|
|
125
|
+
? material[intersection.face?.materialIndex ?? 0]?.visible !== false
|
|
126
|
+
: material.visible)
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
const pick = (event: PointerEvent): Hit | undefined => {
|
|
130
|
+
const bounds = canvas.getBoundingClientRect()
|
|
131
|
+
if (!bounds.width || !bounds.height) return undefined
|
|
132
|
+
const active = camera()
|
|
133
|
+
scene.updateWorldMatrix(true, true)
|
|
134
|
+
active.updateWorldMatrix(true, false)
|
|
135
|
+
pointer.set(
|
|
136
|
+
((event.clientX - bounds.left) / bounds.width) * 2 - 1,
|
|
137
|
+
1 - ((event.clientY - bounds.top) / bounds.height) * 2,
|
|
138
|
+
)
|
|
139
|
+
raycaster.layers.mask = active.layers.mask
|
|
140
|
+
raycaster.setFromCamera(pointer, active)
|
|
141
|
+
if (
|
|
142
|
+
event.clientX < bounds.left ||
|
|
143
|
+
event.clientX > bounds.right ||
|
|
144
|
+
event.clientY < bounds.top ||
|
|
145
|
+
event.clientY > bounds.bottom
|
|
146
|
+
)
|
|
147
|
+
return undefined
|
|
148
|
+
const intersection = raycaster.intersectObject(scene, true).find(eligible)
|
|
149
|
+
return intersection ? { node: intersection.object, intersection } : undefined
|
|
150
|
+
}
|
|
151
|
+
const nativeCapture = (id: number) => {
|
|
152
|
+
if (canvas.hasPointerCapture?.(id)) canvas.releasePointerCapture(id)
|
|
153
|
+
}
|
|
154
|
+
const dispatch = (
|
|
155
|
+
type: string,
|
|
156
|
+
hit: Hit,
|
|
157
|
+
nativeEvent: PointerEvent,
|
|
158
|
+
nodes = path(hit.node),
|
|
159
|
+
) => {
|
|
160
|
+
let stopped = false
|
|
161
|
+
for (const node of nodes) {
|
|
162
|
+
if (dead) break
|
|
163
|
+
const event: ThreePointerEvent = {
|
|
164
|
+
type,
|
|
165
|
+
nativeEvent,
|
|
166
|
+
target: hit.node,
|
|
167
|
+
currentTarget: node,
|
|
168
|
+
intersection: hit.intersection,
|
|
169
|
+
ray: raycaster.ray.clone(),
|
|
170
|
+
pointerId: nativeEvent.pointerId,
|
|
171
|
+
button: nativeEvent.button,
|
|
172
|
+
buttons: nativeEvent.buttons,
|
|
173
|
+
clientX: nativeEvent.clientX,
|
|
174
|
+
clientY: nativeEvent.clientY,
|
|
175
|
+
stopPropagation() {
|
|
176
|
+
stopped = true
|
|
177
|
+
},
|
|
178
|
+
preventDefault() {
|
|
179
|
+
nativeEvent.preventDefault()
|
|
180
|
+
},
|
|
181
|
+
setPointerCapture() {
|
|
182
|
+
canvas.setPointerCapture(nativeEvent.pointerId)
|
|
183
|
+
captures.set(nativeEvent.pointerId, {
|
|
184
|
+
node,
|
|
185
|
+
intersection: hit.intersection,
|
|
186
|
+
})
|
|
187
|
+
},
|
|
188
|
+
releasePointerCapture() {
|
|
189
|
+
if (captures.get(nativeEvent.pointerId)?.node === node) {
|
|
190
|
+
captures.delete(nativeEvent.pointerId)
|
|
191
|
+
nativeCapture(nativeEvent.pointerId)
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
hasPointerCapture() {
|
|
195
|
+
return captures.get(nativeEvent.pointerId)?.node === node
|
|
196
|
+
},
|
|
197
|
+
}
|
|
198
|
+
for (const handler of [...(listeners.get(node)?.get(type) ?? [])])
|
|
199
|
+
if (!dead && listeners.get(node)?.get(type)?.has(handler)) handler(event)
|
|
200
|
+
if (stopped) break
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const hover = (event: PointerEvent, next?: Hit) => {
|
|
204
|
+
const previous = hovering.get(event.pointerId)
|
|
205
|
+
if (previous?.node === next?.node) {
|
|
206
|
+
if (next) hovering.set(event.pointerId, next)
|
|
207
|
+
return
|
|
208
|
+
}
|
|
209
|
+
const oldPath = previous ? path(previous.node) : []
|
|
210
|
+
const newPath = next ? path(next.node) : []
|
|
211
|
+
// Publish state first so handlers may remove nodes or dispose this manager safely.
|
|
212
|
+
if (next) hovering.set(event.pointerId, next)
|
|
213
|
+
else hovering.delete(event.pointerId)
|
|
214
|
+
if (previous) {
|
|
215
|
+
dispatch(`pointerout`, previous, event)
|
|
216
|
+
for (const node of oldPath)
|
|
217
|
+
if (!newPath.includes(node))
|
|
218
|
+
dispatch(`pointerleave`, previous, event, [node])
|
|
219
|
+
}
|
|
220
|
+
if (next && path(next.node).length) {
|
|
221
|
+
dispatch(`pointerover`, next, event)
|
|
222
|
+
for (const node of newPath.reverse())
|
|
223
|
+
if (!oldPath.includes(node))
|
|
224
|
+
dispatch(`pointerenter`, next, event, [node])
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
const cancel = (event: PointerEvent) => {
|
|
228
|
+
const hit = captures.get(event.pointerId) ?? pressed.get(event.pointerId)
|
|
229
|
+
captures.delete(event.pointerId)
|
|
230
|
+
pressed.delete(event.pointerId)
|
|
231
|
+
try {
|
|
232
|
+
if (hit) dispatch(`pointercancel`, hit, event)
|
|
233
|
+
} finally {
|
|
234
|
+
nativeCapture(event.pointerId)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const handle = (event: PointerEvent) => {
|
|
238
|
+
try {
|
|
239
|
+
if (event.type === `lostpointercapture`) {
|
|
240
|
+
cancel(event)
|
|
241
|
+
return
|
|
242
|
+
}
|
|
243
|
+
if (event.type === `pointercancel`) {
|
|
244
|
+
cancel(event)
|
|
245
|
+
hover(event)
|
|
246
|
+
last.delete(event.pointerId)
|
|
247
|
+
return
|
|
248
|
+
}
|
|
249
|
+
if (event.type === `pointerleave`) {
|
|
250
|
+
last.set(event.pointerId, event)
|
|
251
|
+
hover(event)
|
|
252
|
+
return
|
|
253
|
+
}
|
|
254
|
+
last.set(event.pointerId, event)
|
|
255
|
+
const hit = pick(event)
|
|
256
|
+
hover(event, hit)
|
|
257
|
+
if (dead) return
|
|
258
|
+
const capture = captures.get(event.pointerId)
|
|
259
|
+
if (capture && hit && contains(capture.node, hit.node))
|
|
260
|
+
capture.intersection = hit.intersection
|
|
261
|
+
const delivery = capture ?? hit
|
|
262
|
+
if (event.type === `pointerdown`) {
|
|
263
|
+
if (hit) {
|
|
264
|
+
pressed.set(event.pointerId, hit)
|
|
265
|
+
canvas.setPointerCapture?.(event.pointerId)
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (event.type === `pointerup`) {
|
|
269
|
+
const down = pressed.get(event.pointerId)
|
|
270
|
+
pressed.delete(event.pointerId)
|
|
271
|
+
if (down && down.node !== delivery?.node)
|
|
272
|
+
dispatch(`pointercancel`, down, event)
|
|
273
|
+
if (delivery) dispatch(`pointerup`, delivery, event)
|
|
274
|
+
if (down && down.node === hit?.node && event.button === 0)
|
|
275
|
+
dispatch(`click`, hit, event)
|
|
276
|
+
captures.delete(event.pointerId)
|
|
277
|
+
nativeCapture(event.pointerId)
|
|
278
|
+
if (event.pointerType === `touch`) {
|
|
279
|
+
hover(event)
|
|
280
|
+
last.delete(event.pointerId)
|
|
281
|
+
}
|
|
282
|
+
} else if (delivery) dispatch(event.type, delivery, event)
|
|
283
|
+
} catch (error) {
|
|
284
|
+
onError(error)
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const events = [
|
|
288
|
+
`pointerdown`,
|
|
289
|
+
`pointerup`,
|
|
290
|
+
`pointermove`,
|
|
291
|
+
`pointerleave`,
|
|
292
|
+
`pointercancel`,
|
|
293
|
+
`lostpointercapture`,
|
|
294
|
+
] as const
|
|
295
|
+
for (const type of events) canvas.addEventListener(type, handle)
|
|
296
|
+
const manager = {
|
|
297
|
+
/** Re-pick stationary pointers after camera, geometry, or transforms change. */
|
|
298
|
+
update() {
|
|
299
|
+
if (dead) return
|
|
300
|
+
try {
|
|
301
|
+
for (const event of last.values())
|
|
302
|
+
if (event.type !== `pointerleave`) hover(event, pick(event))
|
|
303
|
+
} catch (error) {
|
|
304
|
+
onError(error)
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
remove(node: THREE.Object3D) {
|
|
308
|
+
const errors: unknown[] = []
|
|
309
|
+
for (const [id, hit] of hovering)
|
|
310
|
+
if (contains(node, hit.node)) {
|
|
311
|
+
try {
|
|
312
|
+
hover(last.get(id)!)
|
|
313
|
+
} catch (error) {
|
|
314
|
+
errors.push(error)
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
for (const [id, hit] of [...pressed, ...captures])
|
|
318
|
+
if (contains(node, hit.node)) {
|
|
319
|
+
try {
|
|
320
|
+
cancel(last.get(id)!)
|
|
321
|
+
} catch (error) {
|
|
322
|
+
errors.push(error)
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (errors.length) throw new AggregateError(errors)
|
|
326
|
+
},
|
|
327
|
+
dispose() {
|
|
328
|
+
if (dead) return
|
|
329
|
+
dead = true
|
|
330
|
+
for (const type of events) canvas.removeEventListener(type, handle)
|
|
331
|
+
for (const id of new Set([...pressed.keys(), ...captures.keys()]))
|
|
332
|
+
nativeCapture(id)
|
|
333
|
+
hovering.clear()
|
|
334
|
+
pressed.clear()
|
|
335
|
+
captures.clear()
|
|
336
|
+
last.clear()
|
|
337
|
+
managers.delete(manager)
|
|
338
|
+
},
|
|
339
|
+
}
|
|
340
|
+
managers.add(manager)
|
|
341
|
+
return manager
|
|
342
|
+
}
|
package/src/three.ts
ADDED
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { fail, isPresent, owner } from "./core.ts"
|
|
2
|
+
import type { ThreeEventAttributes, ThreeEventHandler } from "./three-events.ts"
|
|
3
|
+
import {
|
|
4
|
+
createThreeEvents,
|
|
5
|
+
forgetThree,
|
|
6
|
+
listenThree,
|
|
7
|
+
suspendThree,
|
|
8
|
+
} from "./three-events.ts"
|
|
9
|
+
|
|
10
|
+
export type {
|
|
11
|
+
ThreeEventAttributes,
|
|
12
|
+
ThreeEventHandler,
|
|
13
|
+
ThreeEventManager,
|
|
14
|
+
ThreePointerEvent,
|
|
15
|
+
} from "./three-events.ts"
|
|
16
|
+
export { createThreeEvents } from "./three-events.ts"
|
|
17
|
+
import * as THREE from "three"
|
|
18
|
+
|
|
19
|
+
import type { Child, VNode } from "./index.ts"
|
|
20
|
+
import { onCleanup, onMount } from "./index.ts"
|
|
21
|
+
import type { CapabilityAttributes } from "./jsx-runtime.ts"
|
|
22
|
+
import { jsx } from "./jsx-runtime.ts"
|
|
23
|
+
import {
|
|
24
|
+
bindScene,
|
|
25
|
+
createSceneRenderer,
|
|
26
|
+
onSceneFrame,
|
|
27
|
+
sceneComponent,
|
|
28
|
+
} from "./renderer.ts"
|
|
29
|
+
|
|
30
|
+
type Resource = THREE.Object3D | THREE.BufferGeometry | THREE.Material
|
|
31
|
+
type Vector3 = readonly [number, number, number]
|
|
32
|
+
export interface ObjectProps<N extends THREE.Object3D = THREE.Object3D>
|
|
33
|
+
extends CapabilityAttributes<`three`>, ThreeEventAttributes<N> {
|
|
34
|
+
children?: Child
|
|
35
|
+
position?: Vector3
|
|
36
|
+
rotation?: Vector3
|
|
37
|
+
scale?: Vector3 | number
|
|
38
|
+
visible?: boolean
|
|
39
|
+
name?: string
|
|
40
|
+
ref?: (object: N | null) => void
|
|
41
|
+
}
|
|
42
|
+
const constructors = {
|
|
43
|
+
Group: THREE.Group,
|
|
44
|
+
Mesh: THREE.Mesh,
|
|
45
|
+
PerspectiveCamera: THREE.PerspectiveCamera,
|
|
46
|
+
AmbientLight: THREE.AmbientLight,
|
|
47
|
+
DirectionalLight: THREE.DirectionalLight,
|
|
48
|
+
BoxGeometry: THREE.BoxGeometry,
|
|
49
|
+
SphereGeometry: THREE.SphereGeometry,
|
|
50
|
+
PlaneGeometry: THREE.PlaneGeometry,
|
|
51
|
+
MeshStandardMaterial: THREE.MeshStandardMaterial,
|
|
52
|
+
MeshBasicMaterial: THREE.MeshBasicMaterial,
|
|
53
|
+
MeshNormalMaterial: THREE.MeshNormalMaterial,
|
|
54
|
+
}
|
|
55
|
+
const meshDefaults = new WeakMap<
|
|
56
|
+
THREE.Mesh,
|
|
57
|
+
{ geometry: THREE.BufferGeometry; material: THREE.Material }
|
|
58
|
+
>()
|
|
59
|
+
const borrowedMeshDefaults = new WeakMap<
|
|
60
|
+
THREE.Mesh,
|
|
61
|
+
{
|
|
62
|
+
geometry: THREE.BufferGeometry
|
|
63
|
+
material: THREE.Material | THREE.Material[]
|
|
64
|
+
}
|
|
65
|
+
>()
|
|
66
|
+
const constructorArgs = new WeakMap<Resource, unknown[]>()
|
|
67
|
+
const eventHandlers = new WeakMap<Resource, Map<string, () => void>>()
|
|
68
|
+
const defaults = new WeakMap<Resource, Map<string, any>>()
|
|
69
|
+
function writeProperty(node: Resource, key: string, value: any): void {
|
|
70
|
+
const target = node as any
|
|
71
|
+
if (!(key in target)) throw new Error(`spa/three: unknown property ${key}`)
|
|
72
|
+
const current = target[key]
|
|
73
|
+
if (current?.isColor) current.set(value)
|
|
74
|
+
else if (Array.isArray(value) && typeof current?.set === `function`)
|
|
75
|
+
current.set(...value)
|
|
76
|
+
else if (key === `scale` && typeof value === `number`) current.setScalar(value)
|
|
77
|
+
else if (current?.copy && value?.constructor === current.constructor)
|
|
78
|
+
current.copy(value)
|
|
79
|
+
else target[key] = value
|
|
80
|
+
if (node instanceof THREE.PerspectiveCamera) node.updateProjectionMatrix()
|
|
81
|
+
}
|
|
82
|
+
const renderer = createSceneRenderer<Resource>({
|
|
83
|
+
target(node) {
|
|
84
|
+
return {
|
|
85
|
+
kind: `three`,
|
|
86
|
+
node,
|
|
87
|
+
...(node instanceof THREE.Object3D
|
|
88
|
+
? {
|
|
89
|
+
suspend: () => suspendThree(node),
|
|
90
|
+
listen: (type: string, listener: () => void) =>
|
|
91
|
+
listenThree(node, type, listener),
|
|
92
|
+
}
|
|
93
|
+
: {}),
|
|
94
|
+
read(key) {
|
|
95
|
+
const value = (node as any)[key]
|
|
96
|
+
return value?.isColor
|
|
97
|
+
? value.getStyle()
|
|
98
|
+
: value?.isVector3 || value?.isEuler
|
|
99
|
+
? [value.x, value.y, value.z]
|
|
100
|
+
: value
|
|
101
|
+
},
|
|
102
|
+
write(key, value) {
|
|
103
|
+
writeProperty(node, key, value)
|
|
104
|
+
},
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
create(type, props) {
|
|
108
|
+
const Constructor = constructors[type as keyof typeof constructors]
|
|
109
|
+
if (!Constructor) throw new Error(`spa/three: unknown node ${type}`)
|
|
110
|
+
const node = new (Constructor as any)(...(props[`args`] ?? [])) as Resource
|
|
111
|
+
// Mesh's implicit resources are replaced by owned JSX resources below.
|
|
112
|
+
if (node instanceof THREE.Mesh)
|
|
113
|
+
meshDefaults.set(node, {
|
|
114
|
+
geometry: node.geometry,
|
|
115
|
+
material: node.material as THREE.Material,
|
|
116
|
+
})
|
|
117
|
+
constructorArgs.set(node, [...(props[`args`] ?? [])])
|
|
118
|
+
defaults.set(node, new Map())
|
|
119
|
+
eventHandlers.set(node, new Map())
|
|
120
|
+
return node
|
|
121
|
+
},
|
|
122
|
+
update(node, props, previous) {
|
|
123
|
+
if (!equalArgs(constructorArgs.get(node)!, props[`args`]))
|
|
124
|
+
throw new Error(
|
|
125
|
+
`spa/three: change the component key to replace constructor args`,
|
|
126
|
+
)
|
|
127
|
+
const target = node as any
|
|
128
|
+
const saved = defaults.get(node)!
|
|
129
|
+
for (const key of new Set([
|
|
130
|
+
...Object.keys(previous),
|
|
131
|
+
...Object.keys(props),
|
|
132
|
+
])) {
|
|
133
|
+
if (
|
|
134
|
+
/^(key|children|ref|args)$/.test(key) ||
|
|
135
|
+
Object.is(props[key], previous[key])
|
|
136
|
+
)
|
|
137
|
+
continue
|
|
138
|
+
if (key.startsWith(`on`)) {
|
|
139
|
+
if (
|
|
140
|
+
!(node instanceof THREE.Object3D) ||
|
|
141
|
+
!/^on(Pointer(Down|Up|Move|Enter|Leave|Over|Out|Cancel)|Click)$/.test(
|
|
142
|
+
key,
|
|
143
|
+
)
|
|
144
|
+
)
|
|
145
|
+
throw new Error(`spa/three: unknown event ${key}`)
|
|
146
|
+
const handlers = eventHandlers.get(node)!
|
|
147
|
+
handlers.get(key)?.()
|
|
148
|
+
handlers.delete(key)
|
|
149
|
+
if (props[key])
|
|
150
|
+
handlers.set(
|
|
151
|
+
key,
|
|
152
|
+
listenThree(
|
|
153
|
+
node,
|
|
154
|
+
key.slice(2).toLowerCase(),
|
|
155
|
+
props[key] as ThreeEventHandler,
|
|
156
|
+
),
|
|
157
|
+
)
|
|
158
|
+
continue
|
|
159
|
+
}
|
|
160
|
+
if (!(key in target)) throw new Error(`spa/three: unknown property ${key}`)
|
|
161
|
+
const current = target[key]
|
|
162
|
+
if (!saved.has(key))
|
|
163
|
+
saved.set(key, current?.clone ? current.clone() : current)
|
|
164
|
+
const value = props[key] === undefined ? saved.get(key) : props[key]
|
|
165
|
+
writeProperty(node, key, value)
|
|
166
|
+
}
|
|
167
|
+
if (node instanceof THREE.PerspectiveCamera) node.updateProjectionMatrix()
|
|
168
|
+
},
|
|
169
|
+
children(parent, children, activeChildren) {
|
|
170
|
+
if (!(parent instanceof THREE.Object3D)) {
|
|
171
|
+
if (children.length)
|
|
172
|
+
throw new Error(`spa/three: resources cannot contain children`)
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
const objects = children.filter(
|
|
176
|
+
(child): child is THREE.Object3D => child instanceof THREE.Object3D,
|
|
177
|
+
)
|
|
178
|
+
for (const child of [...parent.children])
|
|
179
|
+
if (!objects.includes(child)) parent.remove(child)
|
|
180
|
+
for (const child of objects) if (child.parent !== parent) parent.add(child)
|
|
181
|
+
parent.children.splice(0, parent.children.length, ...objects)
|
|
182
|
+
const resources = children.filter(
|
|
183
|
+
(child) => !(child instanceof THREE.Object3D),
|
|
184
|
+
)
|
|
185
|
+
if (resources.length && !(parent instanceof THREE.Mesh))
|
|
186
|
+
throw new Error(`spa/three: geometry and material require a Mesh parent`)
|
|
187
|
+
if (parent instanceof THREE.Mesh) {
|
|
188
|
+
const geometries = resources.filter(
|
|
189
|
+
(child) => child instanceof THREE.BufferGeometry,
|
|
190
|
+
)
|
|
191
|
+
const materials = resources.filter(
|
|
192
|
+
(child) => child instanceof THREE.Material,
|
|
193
|
+
)
|
|
194
|
+
const activeGeometries = activeChildren.filter(
|
|
195
|
+
(child) => child instanceof THREE.BufferGeometry,
|
|
196
|
+
)
|
|
197
|
+
const activeMaterials = activeChildren.filter(
|
|
198
|
+
(child) => child instanceof THREE.Material,
|
|
199
|
+
)
|
|
200
|
+
if (activeGeometries.length > 1 || activeMaterials.length > 1)
|
|
201
|
+
throw new Error(
|
|
202
|
+
`spa/three: each Mesh accepts one geometry and one material`,
|
|
203
|
+
)
|
|
204
|
+
const owned = meshDefaults.get(parent)
|
|
205
|
+
let baseline = owned ?? borrowedMeshDefaults.get(parent)
|
|
206
|
+
if (!baseline) {
|
|
207
|
+
baseline = { geometry: parent.geometry, material: parent.material }
|
|
208
|
+
borrowedMeshDefaults.set(parent, baseline)
|
|
209
|
+
}
|
|
210
|
+
// Retention owns outgoing resources without occupying incoming attachment slots.
|
|
211
|
+
parent.geometry =
|
|
212
|
+
activeGeometries[0] ??
|
|
213
|
+
geometries.find((geometry) => geometry === parent.geometry) ??
|
|
214
|
+
geometries.at(-1) ??
|
|
215
|
+
baseline.geometry
|
|
216
|
+
parent.material =
|
|
217
|
+
activeMaterials[0] ??
|
|
218
|
+
materials.find((material) => material === parent.material) ??
|
|
219
|
+
materials.at(-1) ??
|
|
220
|
+
baseline.material
|
|
221
|
+
if (!owned && children.length === 0) borrowedMeshDefaults.delete(parent)
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
destroy(node) {
|
|
225
|
+
eventHandlers.get(node)!.clear()
|
|
226
|
+
if (node instanceof THREE.Object3D) {
|
|
227
|
+
try {
|
|
228
|
+
forgetThree(node)
|
|
229
|
+
} finally {
|
|
230
|
+
node.removeFromParent()
|
|
231
|
+
if (node instanceof THREE.Mesh) {
|
|
232
|
+
const owned = meshDefaults.get(node)!
|
|
233
|
+
owned.geometry.dispose()
|
|
234
|
+
owned.material.dispose()
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
} else node.dispose()
|
|
238
|
+
},
|
|
239
|
+
})
|
|
240
|
+
function equalArgs(a: unknown[], b: unknown[] = []): boolean {
|
|
241
|
+
return (
|
|
242
|
+
a.length === b.length &&
|
|
243
|
+
a.every((value, index) => Object.is(value, b[index]))
|
|
244
|
+
)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export interface CanvasProps {
|
|
248
|
+
children?: Child
|
|
249
|
+
width?: number
|
|
250
|
+
height?: number
|
|
251
|
+
camera?: THREE.Camera
|
|
252
|
+
onFrame?: (state: {
|
|
253
|
+
scene: THREE.Scene
|
|
254
|
+
camera: THREE.Camera
|
|
255
|
+
renderer: THREE.WebGLRenderer
|
|
256
|
+
delta: number
|
|
257
|
+
time: number
|
|
258
|
+
}) => void
|
|
259
|
+
}
|
|
260
|
+
/** Browser-only scene boundary. The camera prop is borrowed and never disposed. */
|
|
261
|
+
export function Canvas(props: CanvasProps): VNode {
|
|
262
|
+
let canvas: HTMLCanvasElement | null = null
|
|
263
|
+
onMount(() => {
|
|
264
|
+
const webgl = new THREE.WebGLRenderer({ canvas: canvas!, antialias: true })
|
|
265
|
+
onCleanup(() => {
|
|
266
|
+
webgl.dispose()
|
|
267
|
+
})
|
|
268
|
+
const scene = new THREE.Scene()
|
|
269
|
+
const camera = new THREE.PerspectiveCamera(50, 1, 0.1, 1000)
|
|
270
|
+
camera.position.z = 5
|
|
271
|
+
bindScene(
|
|
272
|
+
() => [props.width ?? 800, props.height ?? 600] as const,
|
|
273
|
+
([width, height]) => {
|
|
274
|
+
webgl.setSize(width, height)
|
|
275
|
+
camera.aspect = width / height
|
|
276
|
+
camera.updateProjectionMatrix()
|
|
277
|
+
},
|
|
278
|
+
)
|
|
279
|
+
renderer.mount(() => props.children, scene)
|
|
280
|
+
const scope = owner!
|
|
281
|
+
const root = scope._root
|
|
282
|
+
const events = createThreeEvents(
|
|
283
|
+
canvas!,
|
|
284
|
+
scene,
|
|
285
|
+
() => props.camera ?? camera,
|
|
286
|
+
(error) => {
|
|
287
|
+
try {
|
|
288
|
+
fail(root, error)
|
|
289
|
+
} catch (failure) {
|
|
290
|
+
root.error(failure)
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
)
|
|
294
|
+
onCleanup(() => {
|
|
295
|
+
events.dispose()
|
|
296
|
+
})
|
|
297
|
+
let last: number | undefined
|
|
298
|
+
onSceneFrame((time) => {
|
|
299
|
+
const active = props.camera ?? camera
|
|
300
|
+
const delta = last === undefined ? 0 : (time - last) / 1000
|
|
301
|
+
last = time
|
|
302
|
+
if (isPresent(scope))
|
|
303
|
+
props.onFrame?.({
|
|
304
|
+
scene,
|
|
305
|
+
camera: active,
|
|
306
|
+
renderer: webgl,
|
|
307
|
+
delta,
|
|
308
|
+
time: time / 1000,
|
|
309
|
+
})
|
|
310
|
+
events.update()
|
|
311
|
+
webgl.render(scene, active)
|
|
312
|
+
}, true)
|
|
313
|
+
})
|
|
314
|
+
return jsx(`canvas`, {
|
|
315
|
+
ref: (element: HTMLCanvasElement | null) => {
|
|
316
|
+
canvas = element
|
|
317
|
+
},
|
|
318
|
+
})
|
|
319
|
+
}
|
|
320
|
+
export const Group = sceneComponent<ObjectProps>(`Group`)
|
|
321
|
+
export const Mesh = sceneComponent<ObjectProps<THREE.Mesh>>(`Mesh`)
|
|
322
|
+
export const PerspectiveCamera = sceneComponent<
|
|
323
|
+
ObjectProps<THREE.PerspectiveCamera> & {
|
|
324
|
+
args?: ConstructorParameters<typeof THREE.PerspectiveCamera>
|
|
325
|
+
fov?: number
|
|
326
|
+
aspect?: number
|
|
327
|
+
near?: number
|
|
328
|
+
far?: number
|
|
329
|
+
}
|
|
330
|
+
>(`PerspectiveCamera`)
|
|
331
|
+
export const AmbientLight = sceneComponent<
|
|
332
|
+
ObjectProps<THREE.Light> & {
|
|
333
|
+
color?: THREE.ColorRepresentation
|
|
334
|
+
intensity?: number
|
|
335
|
+
}
|
|
336
|
+
>(`AmbientLight`)
|
|
337
|
+
export const DirectionalLight = sceneComponent<
|
|
338
|
+
ObjectProps<THREE.Light> & {
|
|
339
|
+
color?: THREE.ColorRepresentation
|
|
340
|
+
intensity?: number
|
|
341
|
+
}
|
|
342
|
+
>(`DirectionalLight`)
|
|
343
|
+
export const BoxGeometry = sceneComponent<{
|
|
344
|
+
args?: ConstructorParameters<typeof THREE.BoxGeometry>
|
|
345
|
+
ref?: (node: THREE.BoxGeometry | null) => void
|
|
346
|
+
}>(`BoxGeometry`)
|
|
347
|
+
export const SphereGeometry = sceneComponent<{
|
|
348
|
+
args?: ConstructorParameters<typeof THREE.SphereGeometry>
|
|
349
|
+
ref?: (node: THREE.SphereGeometry | null) => void
|
|
350
|
+
}>(`SphereGeometry`)
|
|
351
|
+
export const PlaneGeometry = sceneComponent<{
|
|
352
|
+
args?: ConstructorParameters<typeof THREE.PlaneGeometry>
|
|
353
|
+
ref?: (node: THREE.PlaneGeometry | null) => void
|
|
354
|
+
}>(`PlaneGeometry`)
|
|
355
|
+
export const MeshStandardMaterial = sceneComponent<
|
|
356
|
+
THREE.MeshStandardMaterialParameters &
|
|
357
|
+
CapabilityAttributes<`three`> & {
|
|
358
|
+
ref?: (node: THREE.MeshStandardMaterial | null) => void
|
|
359
|
+
}
|
|
360
|
+
>(`MeshStandardMaterial`)
|
|
361
|
+
export const MeshBasicMaterial = sceneComponent<
|
|
362
|
+
THREE.MeshBasicMaterialParameters &
|
|
363
|
+
CapabilityAttributes<`three`> & {
|
|
364
|
+
ref?: (node: THREE.MeshBasicMaterial | null) => void
|
|
365
|
+
}
|
|
366
|
+
>(`MeshBasicMaterial`)
|
|
367
|
+
export const MeshNormalMaterial = sceneComponent<
|
|
368
|
+
THREE.MeshNormalMaterialParameters &
|
|
369
|
+
CapabilityAttributes<`three`> & {
|
|
370
|
+
ref?: (node: THREE.MeshNormalMaterial | null) => void
|
|
371
|
+
}
|
|
372
|
+
>(`MeshNormalMaterial`)
|
|
373
|
+
export { renderer as threeRenderer }
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"astro-shared-DkZaqX7L.d.ts","names":[],"sources":["../src/astro-shared.ts"],"mappings":";;UAMiB;;WAEP;WACA,QAAQ;EACjB,UAAU;;EAEV,IAAI,GAAG,cAAc,kBAAkB,IAAI;;UAE3B;;EAEhB,aAAa;;EAEb,SAAS;;EAET,QAAQ;;EAER,UAAU;;KAEC,kBAAkB,IAAI,wBACjC,OAAO,GACP,SAAS,sBACL,YAAY,QAAQ;KACb,sBAAsB,IAAI,wBACrC,OAAO,WACP,OAAO,GACP,SAAS,6BACE;KACA,OAAO,WAAW,UAAU;EACvC;;EAEA;EACA,cAAc,kBAAkB;EAChC,eAAe,sBAAsB"}
|