@opsydyn/effect-three-foldkit 0.6.4 → 1.0.0-next.1
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/CHANGELOG.md +14 -0
- package/README.md +26 -21
- package/dist/boxSelect.d.ts +91 -117
- package/dist/boxSelect.d.ts.map +1 -1
- package/dist/browser.d.ts +3 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +32 -0
- package/dist/browser.js.map +10 -0
- package/dist/element.d.ts +11 -909
- package/dist/element.d.ts.map +1 -1
- package/dist/index.d.ts +17 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +173 -2565
- package/dist/index.js.map +14 -39
- package/dist/inspector.d.ts +4 -21
- package/dist/inspector.d.ts.map +1 -1
- package/dist/measuringTape.d.ts +39 -46
- package/dist/measuringTape.d.ts.map +1 -1
- package/dist/mount.d.ts +17 -11
- package/dist/mount.d.ts.map +1 -1
- package/dist/overlayView.d.ts +7 -18
- package/dist/overlayView.d.ts.map +1 -1
- package/dist/pivotControls.d.ts +64 -76
- package/dist/pivotControls.d.ts.map +1 -1
- package/dist/transformGizmo.d.ts +113 -125
- package/dist/transformGizmo.d.ts.map +1 -1
- package/dist/view.d.ts +21 -4
- package/dist/view.d.ts.map +1 -1
- package/dist/viewportGizmo.d.ts +107 -119
- package/dist/viewportGizmo.d.ts.map +1 -1
- package/package.json +15 -10
- package/src/boxSelect.ts +12 -133
- package/src/browser.ts +16 -0
- package/src/element.ts +19 -110
- package/src/index.ts +32 -16
- package/src/inspector.ts +70 -93
- package/src/measuringTape.ts +28 -92
- package/src/mount.ts +53 -309
- package/src/overlayView.ts +36 -57
- package/src/pivotControls.ts +8 -94
- package/src/transformGizmo.ts +8 -108
- package/src/view.ts +73 -54
- package/src/viewportGizmo.ts +8 -97
- package/dist/assetRegistry.d.ts +0 -49
- package/dist/assetRegistry.d.ts.map +0 -1
- package/dist/assets.d.ts +0 -66
- package/dist/assets.d.ts.map +0 -1
- package/dist/dom/animationFrames.d.ts +0 -12
- package/dist/dom/animationFrames.d.ts.map +0 -1
- package/dist/dom/canvas.d.ts +0 -33
- package/dist/dom/canvas.d.ts.map +0 -1
- package/dist/dom/latch.d.ts +0 -15
- package/dist/dom/latch.d.ts.map +0 -1
- package/dist/dom/models.d.ts +0 -10
- package/dist/dom/models.d.ts.map +0 -1
- package/dist/dom/overlays.d.ts +0 -24
- package/dist/dom/overlays.d.ts.map +0 -1
- package/dist/dom/pointer.d.ts +0 -49
- package/dist/dom/pointer.d.ts.map +0 -1
- package/dist/dom/postprocessing.d.ts +0 -22
- package/dist/dom/postprocessing.d.ts.map +0 -1
- package/dist/dom/property.d.ts +0 -15
- package/dist/dom/property.d.ts.map +0 -1
- package/dist/dom/reconciled.d.ts +0 -10
- package/dist/dom/reconciled.d.ts.map +0 -1
- package/dist/dom/renderSettings.d.ts +0 -58
- package/dist/dom/renderSettings.d.ts.map +0 -1
- package/dist/dom/typedScenes.d.ts +0 -14
- package/dist/dom/typedScenes.d.ts.map +0 -1
- package/dist/dom/updates.d.ts +0 -8
- package/dist/dom/updates.d.ts.map +0 -1
- package/dist/dom/viewport.d.ts +0 -40
- package/dist/dom/viewport.d.ts.map +0 -1
- package/dist/dom/webgl.d.ts +0 -12
- package/dist/dom/webgl.d.ts.map +0 -1
- package/dist/dom/webgpu.d.ts +0 -24
- package/dist/dom/webgpu.d.ts.map +0 -1
- package/dist/dom/webgpuPostprocessing.d.ts +0 -4
- package/dist/dom/webgpuPostprocessing.d.ts.map +0 -1
- package/dist/dom/xr.d.ts +0 -53
- package/dist/dom/xr.d.ts.map +0 -1
- package/dist/dom/xrGlide.d.ts +0 -49
- package/dist/dom/xrGlide.d.ts.map +0 -1
- package/dist/dom/xrHub.d.ts +0 -45
- package/dist/dom/xrHub.d.ts.map +0 -1
- package/dist/dom/xrInputs.d.ts +0 -40
- package/dist/dom/xrInputs.d.ts.map +0 -1
- package/dist/dom/xrSpaces.d.ts +0 -33
- package/dist/dom/xrSpaces.d.ts.map +0 -1
- package/dist/dom/xrTarget.d.ts +0 -36
- package/dist/dom/xrTarget.d.ts.map +0 -1
- package/dist/environment.d.ts +0 -70
- package/dist/environment.d.ts.map +0 -1
- package/dist/xr.d.ts +0 -34
- package/dist/xr.d.ts.map +0 -1
- package/src/assetRegistry.ts +0 -218
- package/src/assets.ts +0 -208
- package/src/dom/animationFrames.ts +0 -32
- package/src/dom/canvas.ts +0 -66
- package/src/dom/latch.ts +0 -36
- package/src/dom/models.ts +0 -37
- package/src/dom/overlays.ts +0 -63
- package/src/dom/pointer.ts +0 -176
- package/src/dom/postprocessing.ts +0 -116
- package/src/dom/property.ts +0 -43
- package/src/dom/reconciled.ts +0 -51
- package/src/dom/renderSettings.ts +0 -117
- package/src/dom/typedScenes.ts +0 -21
- package/src/dom/updates.ts +0 -14
- package/src/dom/viewport.ts +0 -97
- package/src/dom/webgl.ts +0 -231
- package/src/dom/webgpu.ts +0 -220
- package/src/dom/webgpuPostprocessing.ts +0 -102
- package/src/dom/xr.ts +0 -211
- package/src/dom/xrGlide.ts +0 -114
- package/src/dom/xrHub.ts +0 -118
- package/src/dom/xrInputs.ts +0 -309
- package/src/dom/xrSpaces.ts +0 -287
- package/src/dom/xrTarget.ts +0 -71
- package/src/environment.ts +0 -83
- package/src/xr.ts +0 -36
package/src/dom/xrSpaces.ts
DELETED
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* What a session tracks for the scene's description, each frame: where every space a
|
|
3
|
-
* group follows is (`group({ follow })`), the anchors the scene placed
|
|
4
|
-
* (`xr({ anchors })`), and the planes the session detected (`xr({ planes: true })`).
|
|
5
|
-
*
|
|
6
|
-
* Behavioural reference: pmndrs/xr at `0e1acd8`: `space.ts` `createGetXRSpaceMatrix` (an
|
|
7
|
-
* object on a space takes the space's pose in the reference space each frame, and none
|
|
8
|
-
* while the frame has no pose for it), `anchor.ts` `requestXRAnchor` (`frame.createAnchor`
|
|
9
|
-
* with a rigid transform in the origin's reference space), `store.ts` `updateSession` (the
|
|
10
|
-
* frame's `detectedPlanes` and `detectedMeshes` read each frame, passed on when the set
|
|
11
|
-
* changes), `plane.ts` (a plane is made again once it changes) and `mesh.ts` (a detected
|
|
12
|
-
* mesh's geometry built from its `vertices` and `indices`, again when they change).
|
|
13
|
-
*
|
|
14
|
-
* @module
|
|
15
|
-
*/
|
|
16
|
-
import {
|
|
17
|
-
type BodyPose,
|
|
18
|
-
type Vec3,
|
|
19
|
-
type Xr,
|
|
20
|
-
type XrAnchor,
|
|
21
|
-
type XrInput,
|
|
22
|
-
type XrMesh,
|
|
23
|
-
xrMeshGeometryId,
|
|
24
|
-
type XrPlane,
|
|
25
|
-
type XrSpace,
|
|
26
|
-
} from '@opsydyn/effect-three';
|
|
27
|
-
import { BufferAttribute, BufferGeometry, Euler, Quaternion } from 'three';
|
|
28
|
-
|
|
29
|
-
/** Places the scene's following groups where `locate` finds their spaces. */
|
|
30
|
-
export type Follow = (locate: (space: XrSpace) => BodyPose | undefined) => void;
|
|
31
|
-
|
|
32
|
-
/** Lends a geometry to the scene's store under `id`; returns a function that withdraws it. */
|
|
33
|
-
export type LendGeometry = (id: string, geometry: BufferGeometry) => () => void;
|
|
34
|
-
|
|
35
|
-
export type XrSpaces = {
|
|
36
|
-
/** Brings anchors and planes up to date in this frame, then places the following groups. */
|
|
37
|
-
readonly frame: (frame: XRFrame, space: XRReferenceSpace) => void;
|
|
38
|
-
/** Ends tracking: anchors are deleted, and every following group is hidden. */
|
|
39
|
-
readonly stop: () => void;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const turn = new Quaternion();
|
|
43
|
-
const angles = new Euler();
|
|
44
|
-
|
|
45
|
-
const poseOf = (pose: XRPose | null | undefined): BodyPose | undefined => {
|
|
46
|
-
if (pose === null || pose === undefined) return undefined;
|
|
47
|
-
const { position: p, orientation: o } = pose.transform;
|
|
48
|
-
return { position: [p.x, p.y, p.z], quaternion: [o.x, o.y, o.z, o.w] };
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const sameVec3 = (a: Vec3, b: Vec3): boolean => a.every((value, i) => value === b[i]);
|
|
52
|
-
|
|
53
|
-
const sameAnchor = (a: XrAnchor, b: XrAnchor): boolean =>
|
|
54
|
-
sameVec3(a.position, b.position) && sameVec3(a.rotation, b.rotation);
|
|
55
|
-
|
|
56
|
-
/** An anchor's place as a rigid transform: its position, its Euler rotation turned. */
|
|
57
|
-
const transformOf = ({ position: [x, y, z], rotation }: XrAnchor): XRRigidTransform => {
|
|
58
|
-
turn.setFromEuler(angles.fromArray([...rotation]));
|
|
59
|
-
return new XRRigidTransform({ x, y, z }, { x: turn.x, y: turn.y, z: turn.z, w: turn.w });
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
/** A plane as the scene's data: its pose in the scene, its outline in its own space. */
|
|
63
|
-
const planeOf = (id: string, plane: XRPlane, pose: BodyPose): XrPlane => {
|
|
64
|
-
angles.setFromQuaternion(turn.fromArray([...pose.quaternion]));
|
|
65
|
-
return {
|
|
66
|
-
id,
|
|
67
|
-
orientation: plane.orientation,
|
|
68
|
-
label: plane.semanticLabel ?? '',
|
|
69
|
-
position: pose.position,
|
|
70
|
-
rotation: [angles.x, angles.y, angles.z],
|
|
71
|
-
polygon: plane.polygon.map((point): Vec3 => [point.x, point.y, point.z]),
|
|
72
|
-
};
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
/** A plane's report in a few characters, to tell when one changes. */
|
|
76
|
-
const signatureOf = (plane: XrPlane): string =>
|
|
77
|
-
[plane.id, plane.orientation, plane.label, ...plane.position, ...plane.rotation]
|
|
78
|
-
.concat(plane.polygon.flat())
|
|
79
|
-
.join(',');
|
|
80
|
-
|
|
81
|
-
/** A detected mesh's report in a few characters, to tell when one changes. */
|
|
82
|
-
const meshSignatureOf = (mesh: XrMesh): string =>
|
|
83
|
-
[mesh.id, mesh.label, ...mesh.position, ...mesh.rotation].join(',');
|
|
84
|
-
|
|
85
|
-
/** A detected mesh's geometry, as pmndrs/xr's `updateXRMeshGeometry` builds it. */
|
|
86
|
-
const meshGeometryOf = (mesh: XRMesh): BufferGeometry => {
|
|
87
|
-
const geometry = new BufferGeometry();
|
|
88
|
-
geometry.setIndex(new BufferAttribute(mesh.indices, 1));
|
|
89
|
-
geometry.setAttribute('position', new BufferAttribute(mesh.vertices, 3));
|
|
90
|
-
geometry.computeVertexNormals();
|
|
91
|
-
return geometry;
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
/** A detected mesh's lent geometry: what it was built from, and how to take it back. */
|
|
95
|
-
type Lent = {
|
|
96
|
-
readonly vertices: Float32Array;
|
|
97
|
-
readonly indices: Uint32Array;
|
|
98
|
-
readonly geometry: BufferGeometry;
|
|
99
|
-
readonly withdraw: () => void;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const withdrawLent = (lent: Lent): void => {
|
|
103
|
-
lent.withdraw();
|
|
104
|
-
lent.geometry.dispose();
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
type Made = { readonly spec: XrAnchor; anchor: XRAnchor | undefined };
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Tracks the spaces `session` can locate for the description `settings()`, placing its
|
|
111
|
-
* following groups with `follow` and reporting detected planes to `input`.
|
|
112
|
-
*/
|
|
113
|
-
export const trackSpaces = (
|
|
114
|
-
session: XRSession,
|
|
115
|
-
settings: () => Xr | null,
|
|
116
|
-
follow: Follow,
|
|
117
|
-
input: (input: XrInput) => void,
|
|
118
|
-
lend: LendGeometry,
|
|
119
|
-
): XrSpaces => {
|
|
120
|
-
const anchors = new Map<string, Made>();
|
|
121
|
-
const planeIds = new WeakMap<XRPlane, string>();
|
|
122
|
-
const planes = new Map<string, XRPlane>();
|
|
123
|
-
let nextPlane = 0;
|
|
124
|
-
/** The planes last reported. */
|
|
125
|
-
let reported: string | undefined;
|
|
126
|
-
const meshIds = new WeakMap<XRMesh, string>();
|
|
127
|
-
const meshes = new Map<string, XRMesh>();
|
|
128
|
-
const lentMeshes = new Map<string, Lent>();
|
|
129
|
-
let nextMesh = 0;
|
|
130
|
-
let reportedMeshes: string | undefined;
|
|
131
|
-
|
|
132
|
-
/** Anchors made, made again where they moved, and deleted where they went. */
|
|
133
|
-
const syncAnchors = (frame: XRFrame, space: XRReferenceSpace): void => {
|
|
134
|
-
const wanted = settings()?.anchors ?? [];
|
|
135
|
-
const seen = new Set<string>();
|
|
136
|
-
for (const spec of wanted) {
|
|
137
|
-
seen.add(spec.id);
|
|
138
|
-
const made = anchors.get(spec.id);
|
|
139
|
-
if (made !== undefined && sameAnchor(made.spec, spec)) continue;
|
|
140
|
-
made?.anchor?.delete();
|
|
141
|
-
const entry: Made = { spec, anchor: undefined };
|
|
142
|
-
anchors.set(spec.id, entry);
|
|
143
|
-
// Refused where the session lacks anchors (they are optional): never found, then.
|
|
144
|
-
void frame.createAnchor?.(transformOf(spec), space).then(
|
|
145
|
-
(anchor) => {
|
|
146
|
-
if (anchors.get(spec.id) === entry) entry.anchor = anchor;
|
|
147
|
-
else anchor.delete();
|
|
148
|
-
},
|
|
149
|
-
() => undefined,
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
for (const [id, made] of anchors) {
|
|
153
|
-
if (seen.has(id)) continue;
|
|
154
|
-
made.anchor?.delete();
|
|
155
|
-
anchors.delete(id);
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Reports the detected planes when what is reported changes: one found or lost,
|
|
161
|
-
* reshaped, or seen from where the player has moved to. (Compared by content, not
|
|
162
|
-
* `lastChangedTime`, which some runtimes move on every frame.)
|
|
163
|
-
*/
|
|
164
|
-
const syncPlanes = (frame: XRFrame, space: XRReferenceSpace): void => {
|
|
165
|
-
planes.clear();
|
|
166
|
-
if (settings()?.planes !== true || frame.detectedPlanes === undefined) return;
|
|
167
|
-
const found: Array<XrPlane> = [];
|
|
168
|
-
for (const plane of frame.detectedPlanes) {
|
|
169
|
-
const id = planeIds.get(plane) ?? `plane-${nextPlane++}`;
|
|
170
|
-
planeIds.set(plane, id);
|
|
171
|
-
planes.set(id, plane);
|
|
172
|
-
const pose = poseOf(frame.getPose(plane.planeSpace, space));
|
|
173
|
-
if (pose !== undefined) found.push(planeOf(id, plane, pose));
|
|
174
|
-
}
|
|
175
|
-
const now = found.map(signatureOf).join(' ');
|
|
176
|
-
if (now === reported) return;
|
|
177
|
-
reported = now;
|
|
178
|
-
input({ _tag: 'Planes', planes: found });
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
/** Lends each detected mesh's geometry, again when its vertices change; takes back the lost. */
|
|
182
|
-
const lendMesh = (id: string, mesh: XRMesh): void => {
|
|
183
|
-
const lent = lentMeshes.get(id);
|
|
184
|
-
// Runtimes hand over new arrays when a mesh changes (IWER moves `lastChangedTime` every
|
|
185
|
-
// frame, keeping them): rebuild only then.
|
|
186
|
-
if (lent?.vertices === mesh.vertices && lent.indices === mesh.indices) return;
|
|
187
|
-
if (lent !== undefined) withdrawLent(lent);
|
|
188
|
-
const geometry = meshGeometryOf(mesh);
|
|
189
|
-
lentMeshes.set(id, {
|
|
190
|
-
vertices: mesh.vertices,
|
|
191
|
-
indices: mesh.indices,
|
|
192
|
-
geometry,
|
|
193
|
-
withdraw: lend(xrMeshGeometryId(id), geometry),
|
|
194
|
-
});
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
/** Reports the detected meshes when what is reported changes, and lends their geometry. */
|
|
198
|
-
const syncMeshes = (frame: XRFrame, space: XRReferenceSpace): void => {
|
|
199
|
-
meshes.clear();
|
|
200
|
-
if (settings()?.meshes === true && frame.detectedMeshes !== undefined) {
|
|
201
|
-
const found: Array<XrMesh> = [];
|
|
202
|
-
for (const mesh of frame.detectedMeshes) {
|
|
203
|
-
const id = meshIds.get(mesh) ?? `mesh-${nextMesh++}`;
|
|
204
|
-
meshIds.set(mesh, id);
|
|
205
|
-
meshes.set(id, mesh);
|
|
206
|
-
lendMesh(id, mesh);
|
|
207
|
-
const pose = poseOf(frame.getPose(mesh.meshSpace, space));
|
|
208
|
-
if (pose !== undefined) {
|
|
209
|
-
angles.setFromQuaternion(turn.fromArray([...pose.quaternion]));
|
|
210
|
-
found.push({
|
|
211
|
-
id,
|
|
212
|
-
label: mesh.semanticLabel ?? '',
|
|
213
|
-
position: pose.position,
|
|
214
|
-
rotation: [angles.x, angles.y, angles.z],
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
const now = found.map(meshSignatureOf).join(' ');
|
|
219
|
-
if (now !== reportedMeshes) {
|
|
220
|
-
reportedMeshes = now;
|
|
221
|
-
input({ _tag: 'Meshes', meshes: found });
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
for (const [id, lent] of lentMeshes) {
|
|
225
|
-
if (meshes.has(id)) continue;
|
|
226
|
-
withdrawLent(lent);
|
|
227
|
-
lentMeshes.delete(id);
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
const sourceOf = (hand: string, tracked: boolean): XRInputSource | undefined => {
|
|
232
|
-
for (const source of session.inputSources) {
|
|
233
|
-
if (source.handedness === hand && (source.hand !== undefined) === tracked) return source;
|
|
234
|
-
}
|
|
235
|
-
return undefined;
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
const locateIn =
|
|
239
|
-
(frame: XRFrame, space: XRReferenceSpace) =>
|
|
240
|
-
(target: XrSpace): BodyPose | undefined => {
|
|
241
|
-
switch (target._tag) {
|
|
242
|
-
case 'Head':
|
|
243
|
-
return poseOf(frame.getViewerPose(space));
|
|
244
|
-
case 'Joint': {
|
|
245
|
-
const joint = sourceOf(target.hand, true)?.hand?.get(target.joint);
|
|
246
|
-
return joint === undefined ? undefined : poseOf(frame.getJointPose?.(joint, space));
|
|
247
|
-
}
|
|
248
|
-
case 'Controller': {
|
|
249
|
-
const source = sourceOf(target.hand, false);
|
|
250
|
-
const located = target.space === 'grip' ? source?.gripSpace : source?.targetRaySpace;
|
|
251
|
-
return located === undefined ? undefined : poseOf(frame.getPose(located, space));
|
|
252
|
-
}
|
|
253
|
-
case 'Anchor': {
|
|
254
|
-
const anchor = anchors.get(target.id)?.anchor;
|
|
255
|
-
return anchor === undefined
|
|
256
|
-
? undefined
|
|
257
|
-
: poseOf(frame.getPose(anchor.anchorSpace, space));
|
|
258
|
-
}
|
|
259
|
-
case 'Plane': {
|
|
260
|
-
const plane = planes.get(target.id);
|
|
261
|
-
return plane === undefined ? undefined : poseOf(frame.getPose(plane.planeSpace, space));
|
|
262
|
-
}
|
|
263
|
-
case 'Mesh': {
|
|
264
|
-
const mesh = meshes.get(target.id);
|
|
265
|
-
return mesh === undefined ? undefined : poseOf(frame.getPose(mesh.meshSpace, space));
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
};
|
|
269
|
-
|
|
270
|
-
return {
|
|
271
|
-
frame: (frame, space) => {
|
|
272
|
-
syncAnchors(frame, space);
|
|
273
|
-
syncPlanes(frame, space);
|
|
274
|
-
syncMeshes(frame, space);
|
|
275
|
-
follow(locateIn(frame, space));
|
|
276
|
-
},
|
|
277
|
-
stop: () => {
|
|
278
|
-
for (const made of anchors.values()) made.anchor?.delete();
|
|
279
|
-
anchors.clear();
|
|
280
|
-
planes.clear();
|
|
281
|
-
meshes.clear();
|
|
282
|
-
for (const lent of lentMeshes.values()) withdrawLent(lent);
|
|
283
|
-
lentMeshes.clear();
|
|
284
|
-
follow(() => undefined);
|
|
285
|
-
},
|
|
286
|
-
};
|
|
287
|
-
};
|
package/src/dom/xrTarget.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A mounted scene as an immersive session target: its frames handed to the session while
|
|
3
|
-
* it presents, and the session's start and end dispatched as `scene-xr` events.
|
|
4
|
-
*
|
|
5
|
-
* @module
|
|
6
|
-
*/
|
|
7
|
-
import type { GatedFrames, SceneEvent, Xr, XrInput } from '@opsydyn/effect-three';
|
|
8
|
-
import type { Object3D } from 'three';
|
|
9
|
-
|
|
10
|
-
import type { Latch } from './latch.ts';
|
|
11
|
-
import type { XrMode, XrTarget } from './xrHub.ts';
|
|
12
|
-
import type { Follow, LendGeometry } from './xrSpaces.ts';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The page's frames for a scene: open while it is visible and no session presents it
|
|
16
|
-
* (a session brings its own frames, as React Three Fiber's loop stands aside).
|
|
17
|
-
*/
|
|
18
|
-
export const createFrameGate = (frames: GatedFrames) => {
|
|
19
|
-
let visible = true;
|
|
20
|
-
let presenting = false;
|
|
21
|
-
const apply = (): void => frames.setOpen(visible && !presenting);
|
|
22
|
-
return {
|
|
23
|
-
setVisible: (next: boolean): void => {
|
|
24
|
-
visible = next;
|
|
25
|
-
apply();
|
|
26
|
-
},
|
|
27
|
-
setPresenting: (next: boolean): void => {
|
|
28
|
-
presenting = next;
|
|
29
|
-
apply();
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
export type FrameGate = ReturnType<typeof createFrameGate>;
|
|
35
|
-
|
|
36
|
-
/** The `scene-xr` event: the mode presenting, or `null` when the session ended. */
|
|
37
|
-
export const dispatchXrSession = (element: Element, live: Latch, mode: XrMode | null): void => {
|
|
38
|
-
if (live.isOpen()) element.dispatchEvent(new CustomEvent('scene-xr', { detail: { mode } }));
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/** A scene as a target for the page's immersive sessions. */
|
|
42
|
-
export const xrTarget = (scene: {
|
|
43
|
-
readonly element: Element;
|
|
44
|
-
readonly canvas: HTMLCanvasElement;
|
|
45
|
-
readonly live: Latch;
|
|
46
|
-
readonly gate: FrameGate;
|
|
47
|
-
readonly settings: () => Xr | null;
|
|
48
|
-
readonly draw: () => void;
|
|
49
|
-
readonly scene: Object3D;
|
|
50
|
-
readonly follow: Follow;
|
|
51
|
-
readonly lend: LendGeometry;
|
|
52
|
-
readonly input: (input: XrInput) => ReadonlyArray<SceneEvent>;
|
|
53
|
-
}): XrTarget => ({
|
|
54
|
-
canvas: scene.canvas,
|
|
55
|
-
element: scene.element,
|
|
56
|
-
follow: scene.follow,
|
|
57
|
-
lend: scene.lend,
|
|
58
|
-
settings: scene.settings,
|
|
59
|
-
draw: scene.draw,
|
|
60
|
-
scene: scene.scene,
|
|
61
|
-
// Dropped while time travel pauses the scene, as pointer input is.
|
|
62
|
-
input: (input) => {
|
|
63
|
-
if (scene.live.isOpen()) scene.input(input);
|
|
64
|
-
},
|
|
65
|
-
presenting: (mode) => {
|
|
66
|
-
scene.gate.setPresenting(mode !== null);
|
|
67
|
-
// Back on the page's frames: draw once, as React Three Fiber invalidates on session end.
|
|
68
|
-
if (mode === null) scene.draw();
|
|
69
|
-
dispatchXrSession(scene.element, scene.live, mode);
|
|
70
|
-
},
|
|
71
|
-
});
|
package/src/environment.ts
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* App-wide state shared by every scene element: one GPU resource store and one
|
|
3
|
-
* animation-frame scheduler. Create it once at the application entry point.
|
|
4
|
-
*
|
|
5
|
-
* @module
|
|
6
|
-
*/
|
|
7
|
-
import {
|
|
8
|
-
type Behaviour,
|
|
9
|
-
createResourceStore,
|
|
10
|
-
type LabelEngine,
|
|
11
|
-
type PhysicsEngine,
|
|
12
|
-
type RendererFactory,
|
|
13
|
-
type ResourceStore,
|
|
14
|
-
} from '@opsydyn/effect-three';
|
|
15
|
-
|
|
16
|
-
import { makeAssetRegistry } from './assetRegistry.ts';
|
|
17
|
-
import { assetLoader, makeAssets } from './assets.ts';
|
|
18
|
-
import { animationFrameScheduler, type AnimationFrameHost } from './dom/animationFrames.ts';
|
|
19
|
-
import { defaultPixelRatio, type PixelRatio } from './dom/viewport.ts';
|
|
20
|
-
import { webglRendererFactory } from './dom/webgl.ts';
|
|
21
|
-
import { createXrHub } from './dom/xrHub.ts';
|
|
22
|
-
import { makeXr } from './xr.ts';
|
|
23
|
-
|
|
24
|
-
/** The browser APIs the adapter uses; normally `window`. */
|
|
25
|
-
export type EnvironmentHost = AnimationFrameHost &
|
|
26
|
-
Pick<Window, 'matchMedia' | 'devicePixelRatio'> &
|
|
27
|
-
Partial<Pick<Window, 'navigator'>>;
|
|
28
|
-
|
|
29
|
-
export type EnvironmentOptions = {
|
|
30
|
-
/** Creates each scene's renderer from its canvas. Defaults to WebGL2. */
|
|
31
|
-
readonly renderer?: (canvas: HTMLCanvasElement) => RendererFactory;
|
|
32
|
-
/** Drawing-buffer pixel ratio: fixed, or `[min, max]` around the device's. Default `[1, 2]`. */
|
|
33
|
-
readonly pixelRatio?: PixelRatio;
|
|
34
|
-
/** Behaviours scenes may name with `behave(name)`, beside the built-in `float`. */
|
|
35
|
-
readonly behaviours?: Readonly<Record<string, Behaviour>>;
|
|
36
|
-
/**
|
|
37
|
-
* The physics engine for scenes with `physics`, or the promise of one: pass
|
|
38
|
-
* `loadRapier()` from `@opsydyn/effect-three-rapier` as it is, and scenes start once it
|
|
39
|
-
* has loaded. Without one, bodies stay where they are described.
|
|
40
|
-
*/
|
|
41
|
-
readonly physics?: PhysicsEngine | Promise<PhysicsEngine>;
|
|
42
|
-
/**
|
|
43
|
-
* Draws `label` nodes: pass `troikaLabels()` from `@opsydyn/effect-three-troika`.
|
|
44
|
-
* Without one, labels are empty.
|
|
45
|
-
*/
|
|
46
|
-
readonly labels?: LabelEngine;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
/** Creates the shared environment for `host`. Call `dispose` when the app shuts down. */
|
|
50
|
-
export const makeEnvironment = (host: EnvironmentHost, options: EnvironmentOptions = {}) => {
|
|
51
|
-
const store: ResourceStore = createResourceStore();
|
|
52
|
-
const assets = makeAssets(store);
|
|
53
|
-
const assetRegistry = makeAssetRegistry(assetLoader(store));
|
|
54
|
-
const xrHub = createXrHub(() => host.navigator?.xr);
|
|
55
|
-
return {
|
|
56
|
-
host,
|
|
57
|
-
store,
|
|
58
|
-
frames: animationFrameScheduler(host),
|
|
59
|
-
renderer: options.renderer ?? ((canvas: HTMLCanvasElement) => webglRendererFactory(canvas)),
|
|
60
|
-
pixelRatio: options.pixelRatio ?? defaultPixelRatio,
|
|
61
|
-
behaviours: options.behaviours ?? {},
|
|
62
|
-
/** The physics engine, once loaded; `undefined` when there is none. */
|
|
63
|
-
physics: Promise.resolve(options.physics),
|
|
64
|
-
/** The label engine; `undefined` when there is none. */
|
|
65
|
-
labels: options.labels,
|
|
66
|
-
/** Loads textures and glTF models by URL and lends them to the store. */
|
|
67
|
-
assets,
|
|
68
|
-
/** The assets scenes ask for in their descriptions: loaded, shared, cancelled, freed. */
|
|
69
|
-
assetRegistry,
|
|
70
|
-
/** Enters and leaves immersive (WebXR) sessions, from Commands. */
|
|
71
|
-
xr: makeXr(xrHub),
|
|
72
|
-
/** The page's presentable scenes and its session; scene mounts register here. */
|
|
73
|
-
xrHub,
|
|
74
|
-
/** Disposes every loaded asset and every GPU resource still held. */
|
|
75
|
-
dispose: () => {
|
|
76
|
-
assetRegistry.dispose();
|
|
77
|
-
assets.dispose();
|
|
78
|
-
store.dispose();
|
|
79
|
-
},
|
|
80
|
-
};
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export type Environment = ReturnType<typeof makeEnvironment>;
|
package/src/xr.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Immersive sessions as Effects an application runs from Foldkit Commands: whether a mode
|
|
3
|
-
* is supported, entering it from a user gesture, and leaving it. The scene presented is
|
|
4
|
-
* the mounted one whose description has `xr`; while presenting, the session drives its
|
|
5
|
-
* frames and the view's `onXRSession` reports the session starting and ending.
|
|
6
|
-
*
|
|
7
|
-
* @module
|
|
8
|
-
*/
|
|
9
|
-
import { Data, Effect } from 'effect';
|
|
10
|
-
|
|
11
|
-
import type { XrHub, XrMode } from './dom/xrHub.ts';
|
|
12
|
-
|
|
13
|
-
/** A session could not start: no WebXR, no scene describing `xr`, or the request refused. */
|
|
14
|
-
export class XrUnavailable extends Data.TaggedError('XrUnavailable')<{
|
|
15
|
-
readonly mode: XrMode;
|
|
16
|
-
readonly cause: unknown;
|
|
17
|
-
}> {}
|
|
18
|
-
|
|
19
|
-
/** The immersive session controls for an environment's hub. */
|
|
20
|
-
export const makeXr = (hub: XrHub) => ({
|
|
21
|
-
/** Whether the browser can present `mode` (false without WebXR). */
|
|
22
|
-
supported: (mode: XrMode) => Effect.promise(() => hub.supported(mode)),
|
|
23
|
-
/**
|
|
24
|
-
* Enters `mode`, presenting the mounted scene that describes `xr`. Run it from the
|
|
25
|
-
* Command a click (or other user gesture) produces: browsers require one.
|
|
26
|
-
*/
|
|
27
|
-
enter: (mode: XrMode) =>
|
|
28
|
-
Effect.tryPromise({
|
|
29
|
-
try: () => hub.enter(mode),
|
|
30
|
-
catch: (cause) => new XrUnavailable({ mode, cause }),
|
|
31
|
-
}),
|
|
32
|
-
/** Ends the session, if there is one. */
|
|
33
|
-
exit: Effect.promise(() => hub.exit()),
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
export type XrControls = ReturnType<typeof makeXr>;
|