@opsydyn/effect-three-foldkit 0.6.4 → 1.0.0-next.0
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 +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/mount.ts
CHANGED
|
@@ -1,328 +1,72 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* @module
|
|
7
|
-
*/
|
|
8
|
-
import {
|
|
9
|
-
createGatedFrames,
|
|
10
|
-
FrameSource,
|
|
11
|
-
type GatedFrames,
|
|
12
|
-
type PhysicsEngine,
|
|
13
|
-
type PointerInput,
|
|
14
|
-
ResourceRegistry,
|
|
15
|
-
type SceneEvent,
|
|
16
|
-
type SceneDescription,
|
|
17
|
-
SceneRuntime,
|
|
18
|
-
SceneSchema,
|
|
19
|
-
} from '@opsydyn/effect-three';
|
|
20
|
-
import { type Cause, Data, Effect, Option, Queue, Schema, Stream } from 'effect';
|
|
21
|
-
import { Mount } from 'foldkit';
|
|
22
|
-
import type { ViewState } from 'foldkit/mount';
|
|
1
|
+
/** Connects an Effect UI mount scope and arguments to the browser scene host. */
|
|
2
|
+
import { Mount } from '@effect/ui';
|
|
3
|
+
import { SceneSchema, type SceneDescription } from '@opsydyn/effect-three';
|
|
4
|
+
import { mountSceneHost, type Environment } from '@opsydyn/effect-three/browser';
|
|
5
|
+
import { Effect, Schema, type Scope, Stream } from 'effect';
|
|
23
6
|
|
|
24
|
-
import {
|
|
25
|
-
canvasIn,
|
|
26
|
-
reconciledDispatch,
|
|
27
|
-
removal,
|
|
28
|
-
sceneErrorDispatch,
|
|
29
|
-
sceneEventDispatch,
|
|
30
|
-
dispatchAssetNotice,
|
|
31
|
-
} from './dom/canvas.ts';
|
|
32
|
-
import { applying, createLatch, type Latch, routedWhileOpen } from './dom/latch.ts';
|
|
33
|
-
import { containOverlays, createOverlayWriter, type OverlayWriter } from './dom/overlays.ts';
|
|
34
|
-
import { claimsGestures, listenForPointer, touchActionSync } from './dom/pointer.ts';
|
|
35
|
-
import { interceptProperty } from './dom/property.ts';
|
|
36
|
-
import { typedScene } from './dom/typedScenes.ts';
|
|
37
|
-
import { afterUpdate } from './dom/updates.ts';
|
|
38
|
-
import {
|
|
39
|
-
measureViewport,
|
|
40
|
-
observePixelRatio,
|
|
41
|
-
observeReducedMotion,
|
|
42
|
-
observeSize,
|
|
43
|
-
observeVisibility,
|
|
44
|
-
} from './dom/viewport.ts';
|
|
45
|
-
import { createFrameGate, type FrameGate, xrTarget } from './dom/xrTarget.ts';
|
|
46
7
|
import { SceneLifecycle } from './element.ts';
|
|
47
|
-
import type { Environment } from './environment.ts';
|
|
48
|
-
|
|
49
|
-
type SceneHandle = SceneRuntime.SceneHandle;
|
|
50
|
-
|
|
51
|
-
/** Everything one mounted scene needs, gathered so helpers take one argument. */
|
|
52
|
-
type Mounted = {
|
|
53
|
-
readonly environment: Environment;
|
|
54
|
-
readonly element: Element;
|
|
55
|
-
readonly canvas: HTMLCanvasElement;
|
|
56
|
-
readonly overlays: OverlayWriter;
|
|
57
|
-
readonly handle: SceneHandle;
|
|
58
|
-
readonly live: Latch;
|
|
59
|
-
readonly frames: GatedFrames;
|
|
60
|
-
readonly gate: FrameGate;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const decodeUnknownScene = Schema.decodeUnknownEffect(SceneSchema.SceneDescription);
|
|
64
|
-
|
|
65
|
-
/** Typed scenes from `view` skip validation; anything else is Schema-decoded. */
|
|
66
|
-
const decodeScene = (value: unknown) =>
|
|
67
|
-
Option.fromNullishOr(typedScene(value)).pipe(
|
|
68
|
-
Option.match({ onNone: () => decodeUnknownScene(value), onSome: Effect.succeed }),
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
const isLive = Schema.is(Schema.Literal('Live'));
|
|
72
|
-
|
|
73
|
-
const acquireCanvas = (element: Element) => {
|
|
74
|
-
const create = Effect.sync(canvasIn(element));
|
|
75
|
-
return Effect.acquireRelease(create, (canvas) => Effect.sync(removal(canvas)));
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const acquireSceneUpdates = () => {
|
|
79
|
-
const create = Queue.sliding<unknown>(1);
|
|
80
|
-
return Effect.acquireRelease(create, Queue.shutdown);
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const interception = (element: Element, updates: Queue.Queue<unknown>) => () =>
|
|
84
|
-
interceptProperty(element, 'scene', (value) => Queue.offerUnsafe(updates, value));
|
|
85
|
-
|
|
86
|
-
const acquireInterception = (element: Element, updates: Queue.Queue<unknown>) => {
|
|
87
|
-
const intercept = Effect.sync(interception(element, updates));
|
|
88
|
-
return Effect.acquireRelease(intercept, (intercepted) => Effect.sync(intercepted.restore));
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const pointerListening =
|
|
92
|
-
(
|
|
93
|
-
canvas: HTMLElement,
|
|
94
|
-
onInput: (input: PointerInput) => ReadonlyArray<SceneEvent>,
|
|
95
|
-
claimsGestures: () => boolean,
|
|
96
|
-
) =>
|
|
97
|
-
() =>
|
|
98
|
-
listenForPointer(canvas, onInput, claimsGestures);
|
|
99
|
-
|
|
100
|
-
const acquirePointer = (canvas: HTMLElement, mounted: Mounted) => {
|
|
101
|
-
const listen = Effect.sync(
|
|
102
|
-
pointerListening(
|
|
103
|
-
canvas,
|
|
104
|
-
routedWhileOpen(mounted.live, mounted.handle.pointer),
|
|
105
|
-
claimsGestures(mounted.handle.graph),
|
|
106
|
-
),
|
|
107
|
-
);
|
|
108
|
-
return Effect.acquireRelease(listen, Effect.sync);
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
/** Makes the scene presentable in the page's immersive sessions while it is mounted. */
|
|
112
|
-
const xrRegistration = (mounted: Mounted) => () =>
|
|
113
|
-
mounted.environment.xrHub.register(
|
|
114
|
-
xrTarget({
|
|
115
|
-
element: mounted.element,
|
|
116
|
-
canvas: mounted.canvas,
|
|
117
|
-
live: mounted.live,
|
|
118
|
-
gate: mounted.gate,
|
|
119
|
-
settings: mounted.handle.graph.xr,
|
|
120
|
-
draw: mounted.handle.drawNow,
|
|
121
|
-
scene: mounted.handle.graph.scene,
|
|
122
|
-
follow: mounted.handle.graph.follow,
|
|
123
|
-
lend: mounted.handle.graph.store.lendGeometry,
|
|
124
|
-
input: mounted.handle.xrInput,
|
|
125
|
-
}),
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
const acquireXrTarget = (mounted: Mounted) => {
|
|
129
|
-
const register = Effect.sync(xrRegistration(mounted));
|
|
130
|
-
return Effect.acquireRelease(register, Effect.sync);
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
/** A stream of values from a DOM observer that returns a disconnect function. */
|
|
134
|
-
const connectObserver =
|
|
135
|
-
<A>(observe: (emit: (value: A) => void) => () => void) =>
|
|
136
|
-
(queue: Queue.Queue<A, Cause.Done>) => {
|
|
137
|
-
const connect = Effect.sync(applying(observe, (value: A) => Queue.offerUnsafe(queue, value)));
|
|
138
|
-
return Effect.acquireRelease(connect, Effect.sync);
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
const observed = <A>(observe: (emit: (value: A) => void) => () => void) =>
|
|
142
|
-
Stream.callback<A>(connectObserver(observe));
|
|
143
|
-
|
|
144
|
-
/** What the scene asks for now, given to the environment's asset registry. */
|
|
145
|
-
const asking = (mounted: Mounted, scene: SceneDescription) => () =>
|
|
146
|
-
mounted.environment.assetRegistry.want(mounted.element, scene.assets, (notice) =>
|
|
147
|
-
dispatchAssetNotice(mounted.element, notice),
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
const lettingGo = (mounted: Mounted) => () =>
|
|
151
|
-
mounted.environment.assetRegistry.release(mounted.element);
|
|
152
|
-
|
|
153
|
-
/** The scene's assets, asked for while it is mounted: going, it asks for none. */
|
|
154
|
-
const acquireAssets = (mounted: Mounted, initial: SceneDescription) => {
|
|
155
|
-
const ask = Effect.sync(asking(mounted, initial));
|
|
156
|
-
return Effect.acquireRelease(ask, () => Effect.sync(lettingGo(mounted)));
|
|
157
|
-
};
|
|
158
8
|
|
|
159
|
-
/**
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
9
|
+
/** Arguments supplied by a component whenever it renders the scene. */
|
|
10
|
+
export type SceneMountArgs = {
|
|
11
|
+
readonly scene: SceneDescription;
|
|
12
|
+
readonly live?: boolean;
|
|
163
13
|
};
|
|
164
14
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const decoded = decodeScene(value);
|
|
172
|
-
const applied = decoded.pipe(
|
|
173
|
-
Effect.flatMap(updated(mounted)),
|
|
174
|
-
Effect.flatMap((ops) => Effect.sync(reportReconciled(ops))),
|
|
175
|
-
);
|
|
176
|
-
return applied.pipe(
|
|
177
|
-
Effect.catchTags({
|
|
178
|
-
SchemaError: (error) =>
|
|
179
|
-
Effect.sync(reportError({ _tag: 'MalformedScene', issue: error.message })),
|
|
180
|
-
InvalidScene: ({ error }) => Effect.sync(reportError(error)),
|
|
181
|
-
}),
|
|
182
|
-
);
|
|
183
|
-
};
|
|
15
|
+
export type SceneMount<R = never> = Mount.Factory<
|
|
16
|
+
SceneMountArgs,
|
|
17
|
+
SceneLifecycle,
|
|
18
|
+
HTMLElement,
|
|
19
|
+
R | Scope.Scope
|
|
20
|
+
>;
|
|
184
21
|
|
|
185
|
-
const
|
|
186
|
-
mounted.handle.resize(
|
|
187
|
-
measureViewport(
|
|
188
|
-
mounted.element,
|
|
189
|
-
mounted.environment.host.devicePixelRatio,
|
|
190
|
-
mounted.environment.pixelRatio,
|
|
191
|
-
),
|
|
192
|
-
);
|
|
22
|
+
const lifecycleFailure = (reason: string) => SceneLifecycle.FailedScene({ reason });
|
|
193
23
|
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
) =>
|
|
199
|
-
const dispatchEvent = sceneEventDispatch(mounted.element, mounted.live);
|
|
200
|
-
const sizes = observed<void>((emit) => observeSize(mounted.element, emit));
|
|
201
|
-
const ratios = observed<void>((emit) => observePixelRatio(mounted.environment.host, emit));
|
|
202
|
-
const visibility = observed<boolean>((emit) => observeVisibility(mounted.element, emit));
|
|
203
|
-
const reducedMotion = observed<boolean>((emit) =>
|
|
204
|
-
observeReducedMotion(mounted.environment.host, emit),
|
|
205
|
-
);
|
|
206
|
-
return [
|
|
207
|
-
Stream.runForEach(Stream.fromQueue(updates), applyUpdate(mounted)),
|
|
208
|
-
Stream.runForEach(mounted.handle.events, (event) => Effect.sync(dispatchEvent(event))),
|
|
209
|
-
Stream.runForEach(viewStateChanges, (state) =>
|
|
210
|
-
Effect.sync(applying(mounted.live.set, isLive(state))),
|
|
211
|
-
),
|
|
212
|
-
Stream.runForEach(sizes, resize(mounted)),
|
|
213
|
-
Stream.runForEach(ratios, resize(mounted)),
|
|
214
|
-
Stream.runForEach(visibility, (visible) =>
|
|
215
|
-
Effect.sync(applying(mounted.gate.setVisible, visible)),
|
|
216
|
-
),
|
|
217
|
-
Stream.runForEach(reducedMotion, (reduced) => mounted.handle.setAnimating(!reduced)),
|
|
218
|
-
];
|
|
24
|
+
const recoverStartup = {
|
|
25
|
+
InvalidScene: ({ error }: { readonly error: { readonly _tag: string } }) =>
|
|
26
|
+
Effect.succeed(lifecycleFailure(`invalid scene: ${error._tag}`)),
|
|
27
|
+
RendererUnavailable: () => Effect.succeed(lifecycleFailure('renderer unavailable')),
|
|
28
|
+
PhysicsUnavailable: () => Effect.succeed(lifecycleFailure('physics unavailable')),
|
|
219
29
|
};
|
|
220
30
|
|
|
221
|
-
|
|
222
|
-
class PhysicsUnavailable extends Data.TaggedError('PhysicsUnavailable')<{
|
|
223
|
-
readonly cause: unknown;
|
|
224
|
-
}> {}
|
|
225
|
-
|
|
226
|
-
const sceneRuntimeFor =
|
|
227
|
-
(
|
|
228
|
-
environment: Environment,
|
|
229
|
-
element: Element,
|
|
230
|
-
canvas: HTMLCanvasElement,
|
|
231
|
-
overlays: OverlayWriter,
|
|
232
|
-
physics: PhysicsEngine | undefined,
|
|
233
|
-
) =>
|
|
234
|
-
(scene: SceneDescription) => {
|
|
235
|
-
const frames = createGatedFrames(environment.frames);
|
|
236
|
-
const started = SceneRuntime.make({
|
|
237
|
-
scene,
|
|
238
|
-
renderer: environment.renderer(canvas),
|
|
239
|
-
behaviours: environment.behaviours,
|
|
240
|
-
physics,
|
|
241
|
-
labels: environment.labels,
|
|
242
|
-
placeOverlays: overlays.place,
|
|
243
|
-
viewport: measureViewport(element, environment.host.devicePixelRatio, environment.pixelRatio),
|
|
244
|
-
}).pipe(
|
|
245
|
-
Effect.provideService(FrameSource.FrameSource, FrameSource.fromScheduler(frames)),
|
|
246
|
-
Effect.provideService(ResourceRegistry.ResourceRegistry, { store: environment.store }),
|
|
247
|
-
);
|
|
248
|
-
return Effect.map(started, (handle) => ({ handle, frames }));
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
function* runScene(
|
|
31
|
+
const startScene = (
|
|
252
32
|
environment: Environment,
|
|
253
|
-
element:
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
const updates =
|
|
258
|
-
const
|
|
259
|
-
const
|
|
260
|
-
const overlays = createOverlayWriter(element);
|
|
261
|
-
yield* Effect.sync(containOverlays(element));
|
|
262
|
-
const physics = yield* Effect.tryPromise({
|
|
263
|
-
try: () => environment.physics,
|
|
264
|
-
catch: (cause) => new PhysicsUnavailable({ cause }),
|
|
265
|
-
});
|
|
266
|
-
const { handle, frames } = yield* sceneRuntimeFor(
|
|
267
|
-
environment,
|
|
268
|
-
element,
|
|
269
|
-
canvas,
|
|
270
|
-
overlays,
|
|
271
|
-
physics,
|
|
272
|
-
)(initial);
|
|
273
|
-
const mounted: Mounted = {
|
|
33
|
+
element: HTMLElement,
|
|
34
|
+
{ scene, live }: SceneMountArgs,
|
|
35
|
+
changes: Stream.Stream<SceneMountArgs>,
|
|
36
|
+
) => {
|
|
37
|
+
const updates = Stream.map(changes, (args) => args.scene);
|
|
38
|
+
const liveChanges = Stream.map(changes, (args) => args.live ?? true);
|
|
39
|
+
const startup = mountSceneHost({
|
|
274
40
|
environment,
|
|
275
41
|
element,
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
yield* acquirePointer(canvas, mounted);
|
|
285
|
-
yield* acquireXrTarget(mounted);
|
|
286
|
-
yield* Effect.sync(touchActionSync(canvas, handle.graph));
|
|
287
|
-
yield* Effect.sync(reconciledDispatch(element, environment.store)(handle.startupOps));
|
|
288
|
-
const forks = watchers(mounted, updates, viewStateChanges).map((watcher) =>
|
|
289
|
-
Effect.forkScoped(watcher),
|
|
42
|
+
initial: scene,
|
|
43
|
+
initialLive: live ?? true,
|
|
44
|
+
updates,
|
|
45
|
+
liveChanges,
|
|
46
|
+
});
|
|
47
|
+
return startup.pipe(
|
|
48
|
+
Effect.map(() => SceneLifecycle.StartedScene()),
|
|
49
|
+
Effect.catchTags(recoverStartup),
|
|
290
50
|
);
|
|
291
|
-
yield* Effect.all(forks, { concurrency: 'unbounded' });
|
|
292
|
-
return SceneLifecycle.StartedScene();
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
const failure = (reason: string) => SceneLifecycle.FailedScene({ reason });
|
|
296
|
-
|
|
297
|
-
const recoverStart = {
|
|
298
|
-
SchemaError: (error: Schema.SchemaError) =>
|
|
299
|
-
Effect.succeed(failure(`malformed scene: ${error.message}`)),
|
|
300
|
-
InvalidScene: ({ error }: { readonly error: { readonly _tag: string } }) =>
|
|
301
|
-
Effect.succeed(failure(`invalid scene: ${error._tag}`)),
|
|
302
|
-
RendererUnavailable: () => Effect.succeed(failure('renderer unavailable')),
|
|
303
|
-
PhysicsUnavailable: () => Effect.succeed(failure('physics unavailable')),
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
type ExecuteInput = {
|
|
307
|
-
readonly element: Element;
|
|
308
|
-
readonly viewStateChanges: Stream.Stream<ViewState>;
|
|
309
51
|
};
|
|
310
52
|
|
|
311
|
-
const
|
|
312
|
-
(environment: Environment) =>
|
|
313
|
-
({ element, viewStateChanges }: ExecuteInput) => {
|
|
314
|
-
const scene = Effect.gen(() => runScene(environment, element, viewStateChanges));
|
|
315
|
-
return scene.pipe(Effect.catchTags(recoverStart));
|
|
316
|
-
};
|
|
53
|
+
const mountArgs = { scene: SceneSchema.SceneDescription, live: Schema.optionalKey(Schema.Boolean) };
|
|
317
54
|
|
|
318
55
|
/**
|
|
319
|
-
* Defines
|
|
320
|
-
*
|
|
56
|
+
* Defines a scene mount with access to the component's environment. The provided
|
|
57
|
+
* Effect can read a shared service or acquire an environment scoped to the scene.
|
|
321
58
|
*/
|
|
322
|
-
export const
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
})
|
|
59
|
+
export const defineScopedSceneMount = <R>(
|
|
60
|
+
// oxlint-disable-next-line linteffect/no-manual-effect-channels -- Retain the caller's environment requirements.
|
|
61
|
+
environment: Effect.Effect<Environment, never, R>,
|
|
62
|
+
): SceneMount<R> =>
|
|
63
|
+
Mount.make('EffectThreeScene', { args: mountArgs }, (element, args, changes) =>
|
|
64
|
+
Effect.flatMap(environment, (resolved) => startScene(resolved, element, args, changes)),
|
|
65
|
+
);
|
|
327
66
|
|
|
328
|
-
|
|
67
|
+
/** Defines a scene mount for a shared environment owned by the caller. */
|
|
68
|
+
export const makeSceneMount = (environment: Environment): SceneMount => {
|
|
69
|
+
// oxlint-disable-next-line linteffect/no-effect-succeed-variable -- The caller has already acquired the shared environment.
|
|
70
|
+
const shared = Effect.succeed(environment);
|
|
71
|
+
return defineScopedSceneMount(shared);
|
|
72
|
+
};
|
package/src/overlayView.ts
CHANGED
|
@@ -1,71 +1,50 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* element the scene moves to the overlay's point every frame it draws.
|
|
4
|
-
*
|
|
5
|
-
* @module
|
|
6
|
-
*/
|
|
1
|
+
import type { View } from '@effect/ui';
|
|
2
|
+
import type { Html } from '@effect/ui/html';
|
|
7
3
|
import type { Overlay, SceneDescription } from '@opsydyn/effect-three';
|
|
8
|
-
import
|
|
4
|
+
import { overlayData } from '@opsydyn/effect-three/browser';
|
|
9
5
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
/** HTML shown at a point in the scene, as Drei's `Html`: where, and what. */
|
|
13
|
-
export type OverlayView = {
|
|
6
|
+
/** HTML shown at a point in the scene. */
|
|
7
|
+
export type OverlayView<Message> = {
|
|
14
8
|
readonly overlay: Overlay;
|
|
15
|
-
readonly content:
|
|
16
|
-
/**
|
|
17
|
-
* `'auto'` (the default, as Drei): the HTML takes pointer events. `'none'`: they pass
|
|
18
|
-
* through to the canvas, so the scene under a label stays pickable.
|
|
19
|
-
*/
|
|
9
|
+
readonly content: View.Input<Message, never>;
|
|
20
10
|
readonly pointerEvents?: 'auto' | 'none';
|
|
21
11
|
};
|
|
22
12
|
|
|
23
|
-
|
|
24
|
-
export const withOverlays = (
|
|
13
|
+
export const withOverlays = <Message>(
|
|
25
14
|
scene: SceneDescription,
|
|
26
|
-
overlays: ReadonlyArray<OverlayView
|
|
15
|
+
overlays: ReadonlyArray<OverlayView<Message>>,
|
|
27
16
|
): SceneDescription =>
|
|
28
17
|
overlays.length === 0
|
|
29
18
|
? scene
|
|
30
|
-
: { ...scene, overlays: [...scene.overlays, ...overlays.map((
|
|
31
|
-
|
|
32
|
-
// The layer covers the canvas, lets events through, and keeps Drei's large z-indexes
|
|
33
|
-
// inside the scene element rather than competing with the page.
|
|
34
|
-
const layerStyle = {
|
|
35
|
-
position: 'absolute',
|
|
36
|
-
inset: '0',
|
|
37
|
-
overflow: 'hidden',
|
|
38
|
-
pointerEvents: 'none',
|
|
39
|
-
isolation: 'isolate',
|
|
40
|
-
};
|
|
19
|
+
: { ...scene, overlays: [...scene.overlays, ...overlays.map(({ overlay }) => overlay)] };
|
|
41
20
|
|
|
42
|
-
/** The overlay layer, or nothing when there are no overlays. */
|
|
43
21
|
export const overlayLayer = <Message>(
|
|
44
|
-
h:
|
|
45
|
-
overlays: ReadonlyArray<OverlayView
|
|
46
|
-
)
|
|
22
|
+
h: Html.H<Message>,
|
|
23
|
+
overlays: ReadonlyArray<OverlayView<Message>>,
|
|
24
|
+
) =>
|
|
47
25
|
overlays.length === 0
|
|
48
|
-
?
|
|
49
|
-
:
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
26
|
+
? null
|
|
27
|
+
: h.div
|
|
28
|
+
.style({
|
|
29
|
+
position: 'absolute',
|
|
30
|
+
inset: '0',
|
|
31
|
+
overflow: 'hidden',
|
|
32
|
+
pointerEvents: 'none',
|
|
33
|
+
isolation: 'isolate',
|
|
34
|
+
})
|
|
35
|
+
.children(
|
|
36
|
+
...overlays.map(({ overlay, content, pointerEvents = 'auto' }) =>
|
|
37
|
+
h.div
|
|
38
|
+
.key(overlay.key)
|
|
39
|
+
.attr(overlayData.attribute, overlay.key)
|
|
40
|
+
.style({
|
|
41
|
+
position: 'absolute',
|
|
42
|
+
top: '0',
|
|
43
|
+
left: '0',
|
|
44
|
+
transformOrigin: '0 0',
|
|
45
|
+
visibility: 'hidden',
|
|
46
|
+
pointerEvents,
|
|
47
|
+
})
|
|
48
|
+
.children(content),
|
|
69
49
|
),
|
|
70
|
-
)
|
|
71
|
-
];
|
|
50
|
+
);
|
package/src/pivotControls.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Pivot controls as
|
|
2
|
+
* Pivot controls as an Effect UI submodel (roadmap, "Beyond parity" 5): the core's pure
|
|
3
3
|
* `pivotNodes`, `pressPivot` and `dragPivot` (after Drei's `PivotControls`) with
|
|
4
4
|
* Messages for the scene's pointer events and OutMessages for what the parent does with
|
|
5
5
|
* them, as `TransformGizmo` is for three's controls. The parent owns where its objects
|
|
@@ -17,102 +17,16 @@
|
|
|
17
17
|
*
|
|
18
18
|
* @module
|
|
19
19
|
*/
|
|
20
|
-
import {
|
|
21
|
-
dragPivot,
|
|
22
|
-
hoverPivot,
|
|
23
|
-
type Pivot,
|
|
24
|
-
pivot,
|
|
25
|
-
type PivotHandle,
|
|
26
|
-
pivotHandleOf,
|
|
27
|
-
pivotNodes,
|
|
28
|
-
type Placement,
|
|
29
|
-
pressPivot,
|
|
30
|
-
releasePivot,
|
|
31
|
-
SceneSchema,
|
|
32
|
-
type SceneNode,
|
|
33
|
-
type Vec3,
|
|
34
|
-
} from '@opsydyn/effect-three';
|
|
35
|
-
import { Option, Schema } from 'effect';
|
|
36
|
-
import { defineMessageUnion } from 'foldkit/message';
|
|
37
|
-
import type { ReturnWithOutMessage } from 'foldkit/update';
|
|
38
20
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
key: Schema.String,
|
|
42
|
-
pivot: SceneSchema.Pivot,
|
|
43
|
-
/** The placement the drag last reported, so `Released` can report where it ended. */
|
|
44
|
-
last: Schema.Option(SceneSchema.Placement),
|
|
45
|
-
});
|
|
46
|
-
export type Model = typeof Model.Type;
|
|
21
|
+
import { UiSchema } from '@effect/ui';
|
|
22
|
+
import { PivotControls as Controller } from '@opsydyn/effect-three/browser';
|
|
47
23
|
|
|
48
|
-
const
|
|
24
|
+
export const { Model, init, handleOf, held, nodes } = Controller;
|
|
25
|
+
export type Model = Controller.Model;
|
|
49
26
|
|
|
50
|
-
export const Message =
|
|
51
|
-
Pressed: { handle: Handle, point: SceneSchema.Vec3, placement: SceneSchema.Placement },
|
|
52
|
-
Dragged: { origin: SceneSchema.Vec3, direction: SceneSchema.Vec3, snap: Schema.Boolean },
|
|
53
|
-
Released: {},
|
|
54
|
-
Hovered: { handle: Schema.NullOr(Handle) },
|
|
55
|
-
});
|
|
27
|
+
export const Message = UiSchema.TaggedEnum(Controller.MessageFields);
|
|
56
28
|
export type Message = typeof Message.Type;
|
|
57
|
-
|
|
58
|
-
export const OutMessage = defineMessageUnion({
|
|
59
|
-
/** The object's placement now, during a drag. */
|
|
60
|
-
Moved: { placement: SceneSchema.Placement },
|
|
61
|
-
/** The drag is over, the object here: a step for history. */
|
|
62
|
-
Finished: { placement: SceneSchema.Placement },
|
|
63
|
-
});
|
|
29
|
+
export const OutMessage = UiSchema.TaggedEnum(Controller.OutMessageFields);
|
|
64
30
|
export type OutMessage = typeof OutMessage.Type;
|
|
65
31
|
|
|
66
|
-
|
|
67
|
-
export const init = (key: string, options: Parameters<typeof pivot>[0] = {}): Model => ({
|
|
68
|
-
key,
|
|
69
|
-
pivot: pivot(options),
|
|
70
|
-
last: Option.none(),
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
type Return = ReturnWithOutMessage<Model, Message, OutMessage>;
|
|
74
|
-
|
|
75
|
-
const withPivot = (model: Model, next: Pivot): Model => ({ ...model, pivot: next });
|
|
76
|
-
|
|
77
|
-
const finished = (model: Model): Return => ({
|
|
78
|
-
model: { ...withPivot(model, releasePivot(model.pivot)), last: Option.none() },
|
|
79
|
-
...Option.match(model.last, {
|
|
80
|
-
onNone: () => ({}),
|
|
81
|
-
onSome: (placement) => ({ outMessage: OutMessage.Finished({ placement }) }),
|
|
82
|
-
}),
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
const dragged = (model: Model, origin: Vec3, direction: Vec3, snap: boolean): Return => {
|
|
86
|
-
const placement = Option.fromNullishOr(dragPivot(model.pivot, { origin, direction }, { snap }));
|
|
87
|
-
return Option.match(placement, {
|
|
88
|
-
onNone: () => ({ model }),
|
|
89
|
-
onSome: (placement) => ({
|
|
90
|
-
model: { ...model, last: Option.some(placement) },
|
|
91
|
-
outMessage: OutMessage.Moved({ placement }),
|
|
92
|
-
}),
|
|
93
|
-
});
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
export const update = (model: Model, message: Message): Return =>
|
|
97
|
-
Message.match<Return>(message, {
|
|
98
|
-
Pressed: ({ handle, point, placement }) => ({
|
|
99
|
-
model: {
|
|
100
|
-
...withPivot(model, pressPivot(model.pivot, placement, handle, point)),
|
|
101
|
-
last: Option.none(),
|
|
102
|
-
},
|
|
103
|
-
}),
|
|
104
|
-
Dragged: ({ origin, direction, snap }) => dragged(model, origin, direction, snap),
|
|
105
|
-
Released: () => finished(model),
|
|
106
|
-
Hovered: ({ handle }) => ({ model: withPivot(model, hoverPivot(model.pivot, handle)) }),
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
/** Whether a hit `key` is one of these controls' handles, and which. */
|
|
110
|
-
export const handleOf = (model: Model, key: string): Option.Option<PivotHandle> =>
|
|
111
|
-
Option.fromNullishOr(pivotHandleOf(model.key, key));
|
|
112
|
-
|
|
113
|
-
/** Whether a handle is held: the parent leaves the camera alone while it is. */
|
|
114
|
-
export const held = (model: Model): boolean => model.pivot.grab !== null;
|
|
115
|
-
|
|
116
|
-
/** The handles for `placement`, for the parent's scene. */
|
|
117
|
-
export const nodes = (model: Model, placement: Placement): SceneNode =>
|
|
118
|
-
pivotNodes(model.key, model.pivot, placement);
|
|
32
|
+
export const update = Controller.update;
|