@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/dom/webgpu.ts
DELETED
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* three.js's `WebGPURenderer` behind the core's renderer contract: WebGPU where the browser
|
|
3
|
-
* has it, otherwise its WebGL 2 backend (or always, with `forceWebGL`), drawing the same
|
|
4
|
-
* node materials either way. It carries the WebGPU render kit (the sky, shader grid and
|
|
5
|
-
* contact shadows in TSL), post-processes with `RenderPipeline`, and presents in WebXR.
|
|
6
|
-
* It starts asynchronously (`init()`), which the runtime waits for.
|
|
7
|
-
*
|
|
8
|
-
* Its kit shades with TSL, so labels come from a label engine's `createTsl` (troika's is
|
|
9
|
-
* `troikaLabels` from `@opsydyn/effect-three-troika/webgpu`); an engine without one draws
|
|
10
|
-
* no labels here.
|
|
11
|
-
*
|
|
12
|
-
* @module
|
|
13
|
-
*/
|
|
14
|
-
import type { RendererFactory, RenderSettings, RenderTarget } from '@opsydyn/effect-three';
|
|
15
|
-
import { webgpuKit } from '@opsydyn/effect-three/webgpu';
|
|
16
|
-
import {
|
|
17
|
-
type Camera,
|
|
18
|
-
HalfFloatType,
|
|
19
|
-
LinearFilter,
|
|
20
|
-
type Object3D,
|
|
21
|
-
RenderTarget as ThreeRenderTarget,
|
|
22
|
-
type Scene,
|
|
23
|
-
Vector2,
|
|
24
|
-
} from 'three';
|
|
25
|
-
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
|
|
26
|
-
import { PMREMGenerator, WebGPURenderer } from 'three/webgpu';
|
|
27
|
-
|
|
28
|
-
import {
|
|
29
|
-
colorSpaces,
|
|
30
|
-
forgetPresentable,
|
|
31
|
-
recompileMaterials,
|
|
32
|
-
registerPresentable,
|
|
33
|
-
shadowTypes,
|
|
34
|
-
toneMappings,
|
|
35
|
-
} from './renderSettings.ts';
|
|
36
|
-
import { createWebGPUPostProcessing } from './webgpuPostprocessing.ts';
|
|
37
|
-
|
|
38
|
-
/** Options for three's `WebGPURenderer`. The canvas is always the scene's own. */
|
|
39
|
-
export type WebGPUOptions = {
|
|
40
|
-
/** Use the WebGL 2 backend even where WebGPU is available. */
|
|
41
|
-
readonly forceWebGL?: boolean;
|
|
42
|
-
readonly alpha?: boolean;
|
|
43
|
-
readonly antialias?: boolean;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
/** Applies scene render settings; returns whether shadows were switched on or off. */
|
|
47
|
-
const configure = (renderer: WebGPURenderer, settings: RenderSettings): boolean => {
|
|
48
|
-
const shadowMap = renderer.shadowMap;
|
|
49
|
-
const enabled = settings.shadows !== 'off';
|
|
50
|
-
const switched = shadowMap.enabled !== enabled;
|
|
51
|
-
shadowMap.enabled = enabled;
|
|
52
|
-
if (enabled) shadowMap.type = shadowTypes[settings.shadows];
|
|
53
|
-
renderer.toneMapping = toneMappings[settings.toneMapping];
|
|
54
|
-
renderer.toneMappingExposure = settings.exposure;
|
|
55
|
-
renderer.outputColorSpace = colorSpaces[settings.colorSpace];
|
|
56
|
-
return switched;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const drawingSize = new Vector2();
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* A render texture's target, as the WebGL renderer makes one (Drei's `useFBO`): half-float
|
|
63
|
-
* colour, linear filtering, multisampled (4, WebGPU's only multisample count), with depth.
|
|
64
|
-
*/
|
|
65
|
-
const createTarget = (
|
|
66
|
-
targets: WeakMap<RenderTarget, ThreeRenderTarget>,
|
|
67
|
-
width: number,
|
|
68
|
-
height: number,
|
|
69
|
-
): RenderTarget => {
|
|
70
|
-
const target = new ThreeRenderTarget(width, height, {
|
|
71
|
-
minFilter: LinearFilter,
|
|
72
|
-
magFilter: LinearFilter,
|
|
73
|
-
type: HalfFloatType,
|
|
74
|
-
samples: 4,
|
|
75
|
-
});
|
|
76
|
-
const handle = { texture: target.texture, dispose: () => target.dispose() };
|
|
77
|
-
targets.set(handle, target);
|
|
78
|
-
return handle;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
/** three.js's `RoomEnvironment`, prefiltered by the WebGPU renderer's PMREM generator. */
|
|
82
|
-
const createRoom = (renderer: WebGPURenderer): RenderTarget => {
|
|
83
|
-
const generator = new PMREMGenerator(renderer);
|
|
84
|
-
const room = new RoomEnvironment();
|
|
85
|
-
const target = generator.fromScene(room, 0.04);
|
|
86
|
-
room.dispose();
|
|
87
|
-
generator.dispose();
|
|
88
|
-
return { texture: target.texture, dispose: () => target.dispose() };
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Draws a frame of an immersive session. In AR the room shows through, so a background
|
|
93
|
-
* (an environment shown as one) is left out of the frame, as the WebGL renderer does.
|
|
94
|
-
*/
|
|
95
|
-
const drawPresenting = (renderer: WebGPURenderer, scene: Scene, camera: Camera): void => {
|
|
96
|
-
const passthrough = renderer.xr.getEnvironmentBlendMode() !== 'opaque';
|
|
97
|
-
if (!passthrough || scene.background === null) {
|
|
98
|
-
renderer.render(scene, camera);
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const background = scene.background;
|
|
102
|
-
scene.background = null;
|
|
103
|
-
renderer.render(scene, camera);
|
|
104
|
-
scene.background = background;
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
/** A factory for three's `WebGPURenderer` drawing into `canvas`. */
|
|
108
|
-
export const webgpuRendererFactory = (
|
|
109
|
-
canvas: HTMLCanvasElement,
|
|
110
|
-
options: WebGPUOptions = {},
|
|
111
|
-
): RendererFactory => ({
|
|
112
|
-
// The TSL parts of a scene (sky, shader grid, contact shadows) go with this renderer.
|
|
113
|
-
kit: webgpuKit,
|
|
114
|
-
create: () => {
|
|
115
|
-
// Opaque by default, as three's WebGLRenderer is (WebGPURenderer's own default is alpha).
|
|
116
|
-
const renderer = new WebGPURenderer({ antialias: true, alpha: false, ...options, canvas });
|
|
117
|
-
registerPresentable(canvas, renderer, {
|
|
118
|
-
xr: renderer.xr,
|
|
119
|
-
setXrLoop: (callback) => {
|
|
120
|
-
void renderer.setAnimationLoop(callback);
|
|
121
|
-
},
|
|
122
|
-
});
|
|
123
|
-
const post = createWebGPUPostProcessing(renderer);
|
|
124
|
-
const targets = new WeakMap<RenderTarget, ThreeRenderTarget>();
|
|
125
|
-
let pixelRatio = 1;
|
|
126
|
-
let resolution = 1;
|
|
127
|
-
let scissored = false;
|
|
128
|
-
let shadowSwitches = 0;
|
|
129
|
-
const compiledAt = new WeakMap<Object3D, number>();
|
|
130
|
-
const applyPixelRatio = (): void => {
|
|
131
|
-
if (renderer.getPixelRatio() !== pixelRatio * resolution) {
|
|
132
|
-
renderer.setPixelRatio(pixelRatio * resolution);
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
return {
|
|
136
|
-
ready: () => renderer.init().then(() => undefined),
|
|
137
|
-
render: (scene, camera, area, target) => {
|
|
138
|
-
if (shadowSwitches > 0 && compiledAt.get(scene) !== shadowSwitches) {
|
|
139
|
-
recompileMaterials(scene);
|
|
140
|
-
compiledAt.set(scene, shadowSwitches);
|
|
141
|
-
}
|
|
142
|
-
const offscreen = target === undefined ? undefined : targets.get(target);
|
|
143
|
-
if (offscreen !== undefined) {
|
|
144
|
-
// As the WebGL renderer: into the target, transparent, never from the headset.
|
|
145
|
-
const previous = renderer.getRenderTarget();
|
|
146
|
-
const clearAlpha = renderer.getClearAlpha();
|
|
147
|
-
const xrEnabled = renderer.xr.enabled;
|
|
148
|
-
renderer.xr.enabled = false;
|
|
149
|
-
renderer.setClearAlpha(0);
|
|
150
|
-
renderer.setRenderTarget(offscreen);
|
|
151
|
-
renderer.render(scene, camera);
|
|
152
|
-
renderer.setRenderTarget(previous);
|
|
153
|
-
renderer.setClearAlpha(clearAlpha);
|
|
154
|
-
renderer.xr.enabled = xrEnabled;
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
// Presenting in XR, three draws the headset's views itself: no areas, no effects.
|
|
158
|
-
if (renderer.xr.isPresenting) {
|
|
159
|
-
drawPresenting(renderer, scene, camera);
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
const { width, height } = renderer.getSize(drawingSize);
|
|
163
|
-
if (area === undefined) {
|
|
164
|
-
if (scissored) {
|
|
165
|
-
renderer.setScissorTest(false);
|
|
166
|
-
renderer.setViewport(0, 0, width, height);
|
|
167
|
-
scissored = false;
|
|
168
|
-
}
|
|
169
|
-
if (!post.render(scene, camera)) renderer.render(scene, camera);
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
// three's viewport and scissor origin is the bottom left; areas are from the top.
|
|
173
|
-
const bottom = height - area.y - area.height;
|
|
174
|
-
renderer.setViewport(area.x, bottom, area.width, area.height);
|
|
175
|
-
renderer.setScissor(area.x, bottom, area.width, area.height);
|
|
176
|
-
renderer.setScissorTest(true);
|
|
177
|
-
scissored = true;
|
|
178
|
-
renderer.render(scene, camera);
|
|
179
|
-
},
|
|
180
|
-
renderOver: (scene, camera) => {
|
|
181
|
-
if (renderer.xr.isPresenting) return;
|
|
182
|
-
if (scissored) {
|
|
183
|
-
const { width, height } = renderer.getSize(drawingSize);
|
|
184
|
-
renderer.setScissorTest(false);
|
|
185
|
-
renderer.setViewport(0, 0, width, height);
|
|
186
|
-
scissored = false;
|
|
187
|
-
}
|
|
188
|
-
// As Drei's Hud: keep what is drawn, clear depth so the HUD is never hidden by it.
|
|
189
|
-
const autoClear = renderer.autoClear;
|
|
190
|
-
renderer.autoClear = false;
|
|
191
|
-
renderer.clearDepth();
|
|
192
|
-
renderer.render(scene, camera);
|
|
193
|
-
renderer.autoClear = autoClear;
|
|
194
|
-
},
|
|
195
|
-
createTarget: (width, height) => createTarget(targets, width, height),
|
|
196
|
-
createEnvironment: () => createRoom(renderer),
|
|
197
|
-
configure: (settings) => {
|
|
198
|
-
if (configure(renderer, settings)) shadowSwitches++;
|
|
199
|
-
resolution = settings.resolution;
|
|
200
|
-
post.configure(settings.effects);
|
|
201
|
-
applyPixelRatio();
|
|
202
|
-
},
|
|
203
|
-
setViewport: (viewport) => {
|
|
204
|
-
pixelRatio = viewport.pixelRatio;
|
|
205
|
-
applyPixelRatio();
|
|
206
|
-
renderer.setSize(viewport.width, viewport.height, false);
|
|
207
|
-
},
|
|
208
|
-
memory: () => ({
|
|
209
|
-
geometries: renderer.info.memory.geometries,
|
|
210
|
-
textures: renderer.info.memory.textures,
|
|
211
|
-
}),
|
|
212
|
-
dispose: () => {
|
|
213
|
-
forgetPresentable(canvas);
|
|
214
|
-
void renderer.xr.getSession()?.end();
|
|
215
|
-
post.dispose();
|
|
216
|
-
void renderer.dispose();
|
|
217
|
-
},
|
|
218
|
-
};
|
|
219
|
-
},
|
|
220
|
-
});
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Post-processing for the WebGPU renderer: three.js's `RenderPipeline` with TSL nodes for
|
|
3
|
-
* the same effects the WebGL renderer composes (`postprocessing.ts`): bloom
|
|
4
|
-
* (`tsl/display/BloomNode.js`, added to the scene as three's examples add it), film
|
|
5
|
-
* (`tsl/display/FilmNode.js`'s grain, written here, with grayscale by luminance as
|
|
6
|
-
* `FilmPass` offers) and
|
|
7
|
-
* vignette (three's `VignetteShader`, as TSL). The pipeline's output transform applies
|
|
8
|
-
* tone mapping and the output colour space, as the WebGL composer's `OutputPass` does.
|
|
9
|
-
*
|
|
10
|
-
* @module
|
|
11
|
-
*/
|
|
12
|
-
import { effectsEqual, type PostEffect } from '@opsydyn/effect-three';
|
|
13
|
-
import type { Camera, Scene } from 'three';
|
|
14
|
-
import { bloom } from 'three/addons/tsl/display/BloomNode.js';
|
|
15
|
-
import {
|
|
16
|
-
clamp,
|
|
17
|
-
dot,
|
|
18
|
-
fract,
|
|
19
|
-
luminance,
|
|
20
|
-
max,
|
|
21
|
-
mix,
|
|
22
|
-
pass,
|
|
23
|
-
rand,
|
|
24
|
-
screenUV,
|
|
25
|
-
time,
|
|
26
|
-
uv,
|
|
27
|
-
vec3,
|
|
28
|
-
vec4,
|
|
29
|
-
} from 'three/tsl';
|
|
30
|
-
import { type Node, RenderPipeline, type WebGPURenderer } from 'three/webgpu';
|
|
31
|
-
|
|
32
|
-
import type { PostProcessing } from './postprocessing.ts';
|
|
33
|
-
|
|
34
|
-
type Colour = Node<'vec4'>;
|
|
35
|
-
|
|
36
|
-
/** One effect applied to the colour so far. */
|
|
37
|
-
const applied = (input: Colour, effect: PostEffect): Colour => {
|
|
38
|
-
switch (effect._tag) {
|
|
39
|
-
case 'Bloom': {
|
|
40
|
-
// BloomNode's output is opaque (alpha 1), which would fill a transparent canvas with
|
|
41
|
-
// black. Keep the scene's alpha and let the glow add its own where it shines, as the
|
|
42
|
-
// WebGL composer's additive bloom does.
|
|
43
|
-
const glow = bloom(input, effect.strength, effect.radius, effect.threshold);
|
|
44
|
-
const shine = max(glow.r, max(glow.g, glow.b));
|
|
45
|
-
return vec4(input.rgb.add(glow.rgb), clamp(input.a.add(shine), 0, 1));
|
|
46
|
-
}
|
|
47
|
-
case 'Vignette': {
|
|
48
|
-
// three's VignetteShader: towards `1 − darkness` with distance from the middle.
|
|
49
|
-
const centred = screenUV.sub(0.5).mul(effect.offset);
|
|
50
|
-
return vec4(mix(input.rgb, vec3(1 - effect.darkness), dot(centred, centred)), input.a);
|
|
51
|
-
}
|
|
52
|
-
case 'Film': {
|
|
53
|
-
// three's FilmNode: noise added in proportion to the colour, mixed in by intensity.
|
|
54
|
-
const base = input.rgb;
|
|
55
|
-
const noise = rand(fract(uv().add(time)));
|
|
56
|
-
const grained = mix(base, base.add(base.mul(clamp(noise.add(0.1), 0, 1))), effect.intensity);
|
|
57
|
-
return vec4(effect.grayscale ? vec3(luminance(grained)) : grained, input.a);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export const createWebGPUPostProcessing = (renderer: WebGPURenderer): PostProcessing => {
|
|
63
|
-
let effects: ReadonlyArray<PostEffect> = [];
|
|
64
|
-
let pipeline: RenderPipeline | undefined;
|
|
65
|
-
// The scene and camera the pipeline's scene pass draws: a new one rebuilds it.
|
|
66
|
-
let drawn: { readonly scene: Scene; readonly camera: Camera } | undefined;
|
|
67
|
-
|
|
68
|
-
const dispose = (): void => {
|
|
69
|
-
pipeline?.dispose();
|
|
70
|
-
pipeline = undefined;
|
|
71
|
-
drawn = undefined;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const build = (scene: Scene, camera: Camera): RenderPipeline => {
|
|
75
|
-
dispose();
|
|
76
|
-
const output = effects.reduce<Colour>(applied, pass(scene, camera));
|
|
77
|
-
const next = new RenderPipeline(renderer, output);
|
|
78
|
-
pipeline = next;
|
|
79
|
-
drawn = { scene, camera };
|
|
80
|
-
return next;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
return {
|
|
84
|
-
configure: (next) => {
|
|
85
|
-
if (effectsEqual(next, effects)) return;
|
|
86
|
-
effects = next;
|
|
87
|
-
dispose();
|
|
88
|
-
},
|
|
89
|
-
// The pipeline follows the renderer's size.
|
|
90
|
-
setSize: () => undefined,
|
|
91
|
-
render: (scene, camera) => {
|
|
92
|
-
if (effects.length === 0) return false;
|
|
93
|
-
const current =
|
|
94
|
-
pipeline !== undefined && drawn?.scene === scene && drawn.camera === camera
|
|
95
|
-
? pipeline
|
|
96
|
-
: build(scene, camera);
|
|
97
|
-
current.render();
|
|
98
|
-
return true;
|
|
99
|
-
},
|
|
100
|
-
dispose,
|
|
101
|
-
};
|
|
102
|
-
};
|
package/src/dom/xr.ts
DELETED
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Presenting a scene in a WebXR session through three.js's `WebXRManager`.
|
|
3
|
-
*
|
|
4
|
-
* Behavioural references: React Three Fiber `core/configuration.ts` at `d74622e` (on
|
|
5
|
-
* `sessionstart` enable XR and hand frames to `xr.setAnimationLoop`; on `sessionend` take
|
|
6
|
-
* them back and draw again), and pmndrs/xr at `0e1acd8` (`store.ts` `setupXRManager`:
|
|
7
|
-
* reference space type and framebuffer scale before `setSession`; `init.ts`
|
|
8
|
-
* `buildXRSessionInit`: the reference space required, other features optional). The
|
|
9
|
-
* player's origin is an offset reference space, as three.js's teleport example moves
|
|
10
|
-
* the viewer. Controller selects become rays, as `@react-three/xr`'s pointers hit-test
|
|
11
|
-
* along each controller's target ray.
|
|
12
|
-
*
|
|
13
|
-
* @module
|
|
14
|
-
*/
|
|
15
|
-
import type { Xr, XrInput } from '@opsydyn/effect-three';
|
|
16
|
-
import { Group, type Object3D } from 'three';
|
|
17
|
-
|
|
18
|
-
import type { PresentableRenderer } from './renderSettings.ts';
|
|
19
|
-
import { floorFacing, type GlidePose, glideFrame, startGlider } from './xrGlide.ts';
|
|
20
|
-
import { rayOf, trackInputs, type XrInputs } from './xrInputs.ts';
|
|
21
|
-
import { type Follow, type LendGeometry, trackSpaces } from './xrSpaces.ts';
|
|
22
|
-
|
|
23
|
-
/** Where an immersive session can come from: `navigator.xr`, or an emulator's. */
|
|
24
|
-
export type XrSystem = Pick<XRSystem, 'requestSession' | 'isSessionSupported'>;
|
|
25
|
-
|
|
26
|
-
/** The immersive session modes. */
|
|
27
|
-
export type XrMode = 'immersive-vr' | 'immersive-ar';
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* The session request for a scene's settings: its reference space required, other
|
|
31
|
-
* floors, hand tracking and anchors optional, and plane detection, a DOM overlay (on
|
|
32
|
-
* `overlay`, the scene's element) and in AR hit testing when the scene asks for them
|
|
33
|
-
* (pmndrs/xr's `buildXRSessionInit` asks for every feature it may use, optionally, and
|
|
34
|
-
* roots the overlay on the element it is given). Anchors are asked for whether or not
|
|
35
|
-
* the scene has any yet: it may place them mid-session.
|
|
36
|
-
*/
|
|
37
|
-
export const sessionInit = (
|
|
38
|
-
settings: Xr,
|
|
39
|
-
mode: XrMode = 'immersive-vr',
|
|
40
|
-
overlay?: Element,
|
|
41
|
-
): XRSessionInit => ({
|
|
42
|
-
requiredFeatures: [settings.referenceSpace],
|
|
43
|
-
optionalFeatures: [
|
|
44
|
-
...['local-floor', 'bounded-floor', 'hand-tracking', 'anchors'].filter(
|
|
45
|
-
(feature) => feature !== settings.referenceSpace,
|
|
46
|
-
),
|
|
47
|
-
...(settings.planes ? ['plane-detection'] : []),
|
|
48
|
-
...(settings.meshes ? ['mesh-detection'] : []),
|
|
49
|
-
...(settings.domOverlay && overlay !== undefined ? ['dom-overlay'] : []),
|
|
50
|
-
...(mode === 'immersive-ar' && settings.hitTest ? ['hit-test'] : []),
|
|
51
|
-
],
|
|
52
|
-
...(settings.domOverlay && overlay !== undefined ? { domOverlay: { root: overlay } } : {}),
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* The offset that puts the player at `origin` facing `heading`: a reference space whose
|
|
57
|
-
* poses are the device's, rotated by the heading and moved to the origin.
|
|
58
|
-
*/
|
|
59
|
-
const originOffset = (settings: GlidePose): XRRigidTransform => {
|
|
60
|
-
const [x, y, z] = settings.origin;
|
|
61
|
-
const half = -settings.heading / 2;
|
|
62
|
-
const cos = Math.cos(-settings.heading);
|
|
63
|
-
const sin = Math.sin(-settings.heading);
|
|
64
|
-
// The inverse of "rotate by heading, then move to origin": rotate by −heading the point −origin.
|
|
65
|
-
return new XRRigidTransform(
|
|
66
|
-
{ x: -x * cos - z * sin, y: -y, z: x * sin - z * cos },
|
|
67
|
-
{ x: 0, y: Math.sin(half), z: 0, w: Math.cos(half) },
|
|
68
|
-
);
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Forwards each source's select as a ray in the presented space (the scene's, since the
|
|
73
|
-
* reference space is offset to the player's origin); a select ending in AR also reports
|
|
74
|
-
* the surface its ray met. Returns a function that stops.
|
|
75
|
-
*/
|
|
76
|
-
const listenForSelects = (
|
|
77
|
-
session: XRSession,
|
|
78
|
-
space: () => XRReferenceSpace | null,
|
|
79
|
-
inputs: XrInputs,
|
|
80
|
-
input: (input: XrInput) => void,
|
|
81
|
-
): (() => void) => {
|
|
82
|
-
const forward = (tag: 'SelectStart' | 'SelectEnd') => (event: XRInputSourceEvent) => {
|
|
83
|
-
const reference = space();
|
|
84
|
-
const pose =
|
|
85
|
-
reference === null
|
|
86
|
-
? undefined
|
|
87
|
-
: event.frame.getPose(event.inputSource.targetRaySpace, reference);
|
|
88
|
-
if (pose !== undefined && pose !== null) {
|
|
89
|
-
input({ _tag: tag, source: inputs.idOf(event.inputSource), ray: rayOf(pose) });
|
|
90
|
-
}
|
|
91
|
-
if (tag === 'SelectEnd') inputs.selected(event.inputSource);
|
|
92
|
-
};
|
|
93
|
-
const started = forward('SelectStart');
|
|
94
|
-
const ended = forward('SelectEnd');
|
|
95
|
-
session.addEventListener('selectstart', started);
|
|
96
|
-
session.addEventListener('selectend', ended);
|
|
97
|
-
return () => {
|
|
98
|
-
session.removeEventListener('selectstart', started);
|
|
99
|
-
session.removeEventListener('selectend', ended);
|
|
100
|
-
};
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
export type Presentation = {
|
|
104
|
-
/** Applies new settings mid-session: a new origin or heading moves the player. */
|
|
105
|
-
readonly update: (settings: Xr) => void;
|
|
106
|
-
readonly session: XRSession;
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
/** What a session presents: the scene, and how it is drawn, followed and told of input. */
|
|
110
|
-
export type Presented = {
|
|
111
|
-
readonly scene: Object3D;
|
|
112
|
-
readonly draw: () => void;
|
|
113
|
-
/** Places the groups that follow tracked spaces (`group({ follow })`). */
|
|
114
|
-
readonly follow: Follow;
|
|
115
|
-
/** Lends detected meshes' geometry to the scene (`xrMesh(id)`). */
|
|
116
|
-
readonly lend: LendGeometry;
|
|
117
|
-
readonly input: (input: XrInput) => void;
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Presents into `session`: frames come from the session (each read for input and the
|
|
122
|
-
* spaces groups follow, then drawn) until it ends, when `ended` is called and the page's
|
|
123
|
-
* own frames take over again. Input sources are drawn into the scene while it presents.
|
|
124
|
-
*/
|
|
125
|
-
export const present = async (
|
|
126
|
-
renderer: PresentableRenderer,
|
|
127
|
-
session: XRSession,
|
|
128
|
-
settings: Xr,
|
|
129
|
-
presented: Presented,
|
|
130
|
-
ended: () => void,
|
|
131
|
-
): Promise<Presentation> => {
|
|
132
|
-
const { scene, draw, input } = presented;
|
|
133
|
-
const { xr } = renderer;
|
|
134
|
-
xr.enabled = true;
|
|
135
|
-
xr.setReferenceSpaceType(settings.referenceSpace);
|
|
136
|
-
xr.setFramebufferScaleFactor(settings.framebufferScale);
|
|
137
|
-
let inputs: XrInputs | undefined;
|
|
138
|
-
const spaces = trackSpaces(session, () => current, presented.follow, input, presented.lend);
|
|
139
|
-
let current = settings;
|
|
140
|
-
// Gliding (`xr({ glide })`) moves the player from the description's place; the
|
|
141
|
-
// description wins again whenever it changes.
|
|
142
|
-
let glider = startGlider({ origin: settings.origin, heading: settings.heading });
|
|
143
|
-
let lastTime: number | undefined;
|
|
144
|
-
let base: XRReferenceSpace | null = null;
|
|
145
|
-
const place = (pose: GlidePose): void => {
|
|
146
|
-
if (base !== null) xr.setReferenceSpace(base.getOffsetReferenceSpace(originOffset(pose)));
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
/** One frame of gliding, when the description asks for it. */
|
|
150
|
-
const glideStep = (time: number, frame: XRFrame, space: XRReferenceSpace): void => {
|
|
151
|
-
const seconds = Math.min(0.1, Math.max(0, (time - (lastTime ?? time)) / 1000));
|
|
152
|
-
lastTime = time;
|
|
153
|
-
if (current.glide === null || inputs === undefined) return;
|
|
154
|
-
const head = frame.getViewerPose(space);
|
|
155
|
-
const facing = floorFacing(head?.transform.orientation ?? { x: 0, y: 0, z: 0, w: 1 });
|
|
156
|
-
const { left, right } = inputs.sticks();
|
|
157
|
-
const next = glideFrame(glider, current.glide, { move: left, turn: right }, facing, seconds);
|
|
158
|
-
if (next.glider.pose !== glider.pose) place(next.glider.pose);
|
|
159
|
-
glider = next.glider;
|
|
160
|
-
if (next.stopped !== undefined) input({ _tag: 'Moved', ...next.stopped });
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
/** One session frame: its input read, then drawn. Frames outside the session are skipped. */
|
|
164
|
-
function sessionFrame(time: number, frame?: XRFrame): void {
|
|
165
|
-
if (!xr.isPresenting) return;
|
|
166
|
-
const space = xr.getReferenceSpace();
|
|
167
|
-
if (frame !== undefined && space !== null) {
|
|
168
|
-
inputs?.frame(frame, space);
|
|
169
|
-
glideStep(time, frame, space);
|
|
170
|
-
// After gliding, so the spaces are where the player now stands.
|
|
171
|
-
spaces.frame(frame, xr.getReferenceSpace() ?? space);
|
|
172
|
-
}
|
|
173
|
-
draw();
|
|
174
|
-
}
|
|
175
|
-
// Set before the session starts (WebGPU's XR manager takes the loop then).
|
|
176
|
-
renderer.setXrLoop(sessionFrame);
|
|
177
|
-
await xr.setSession(session);
|
|
178
|
-
base = xr.getReferenceSpace();
|
|
179
|
-
place(glider.pose);
|
|
180
|
-
const shown = new Group();
|
|
181
|
-
shown.name = 'xr-inputs';
|
|
182
|
-
scene.add(shown);
|
|
183
|
-
const tracked = trackInputs(session, shown, () => current, input);
|
|
184
|
-
inputs = tracked;
|
|
185
|
-
const stopSelects = listenForSelects(session, () => xr.getReferenceSpace(), tracked, input);
|
|
186
|
-
session.addEventListener(
|
|
187
|
-
'end',
|
|
188
|
-
() => {
|
|
189
|
-
stopSelects();
|
|
190
|
-
tracked.stop();
|
|
191
|
-
spaces.stop();
|
|
192
|
-
shown.removeFromParent();
|
|
193
|
-
renderer.setXrLoop(null);
|
|
194
|
-
xr.enabled = false;
|
|
195
|
-
ended();
|
|
196
|
-
},
|
|
197
|
-
{ once: true },
|
|
198
|
-
);
|
|
199
|
-
return {
|
|
200
|
-
session,
|
|
201
|
-
update: (next) => {
|
|
202
|
-
const moved =
|
|
203
|
-
next.origin.some((value, i) => value !== current.origin[i]) ||
|
|
204
|
-
next.heading !== current.heading;
|
|
205
|
-
current = next;
|
|
206
|
-
if (!moved) return;
|
|
207
|
-
glider = startGlider({ origin: next.origin, heading: next.heading });
|
|
208
|
-
place(glider.pose);
|
|
209
|
-
},
|
|
210
|
-
};
|
|
211
|
-
};
|
package/src/dom/xrGlide.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Gliding with the thumbsticks while presenting (`xr({ glide })`), one frame at a time.
|
|
3
|
-
*
|
|
4
|
-
* Behavioural reference: pmndrs/xr at `0e1acd8`, `packages/xr/src/controller-locomotion.ts`:
|
|
5
|
-
* the left stick moves the player along the head's facing, flattened onto the floor, at
|
|
6
|
-
* `speed` metres per second at full stick (forward is the stick pushed away, negative y);
|
|
7
|
-
* the right stick turns, smoothly at `turnSpeed`, or by `snapDegrees` once per push past
|
|
8
|
-
* halfway, ready again once back under it. Unlike there, a still stick is a still player
|
|
9
|
-
* (a small dead zone keeps resting thumbsticks from drifting), and the frame that stops
|
|
10
|
-
* says where the player ended, for the Model.
|
|
11
|
-
*
|
|
12
|
-
* @module
|
|
13
|
-
*/
|
|
14
|
-
import type { Vec3, XrGlide } from '@opsydyn/effect-three';
|
|
15
|
-
|
|
16
|
-
/** Where the player stands: the reference space's origin in the scene, and its heading. */
|
|
17
|
-
export type GlidePose = { readonly origin: Vec3; readonly heading: number };
|
|
18
|
-
|
|
19
|
-
/** A thumbstick, `[x, y]`, each −1 to 1 (`y` negative pushed away). */
|
|
20
|
-
export type Stick = readonly [x: number, y: number];
|
|
21
|
-
|
|
22
|
-
/** Gliding's state between frames. */
|
|
23
|
-
export type Glider = {
|
|
24
|
-
readonly pose: GlidePose;
|
|
25
|
-
/** Whether a snap turn is ready (the turning stick has been back under halfway). */
|
|
26
|
-
readonly snapReady: boolean;
|
|
27
|
-
/** Whether the pose moved since the last report. */
|
|
28
|
-
readonly moved: boolean;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/** Thumbsticks rest a little off centre; under this they are still. */
|
|
32
|
-
const deadZone = 0.15;
|
|
33
|
-
/** Past this the turning stick turns (pmndrs/xr's default dead zone, 0.5). */
|
|
34
|
-
const turnZone = 0.5;
|
|
35
|
-
|
|
36
|
-
export const startGlider = (pose: GlidePose): Glider => ({ pose, snapReady: true, moved: false });
|
|
37
|
-
|
|
38
|
-
/** The head's facing on the floor, from its orientation quaternion: −z turned, flattened. */
|
|
39
|
-
export const floorFacing = (q: {
|
|
40
|
-
readonly x: number;
|
|
41
|
-
readonly y: number;
|
|
42
|
-
readonly z: number;
|
|
43
|
-
readonly w: number;
|
|
44
|
-
}): readonly [x: number, z: number] => {
|
|
45
|
-
const x = -2 * (q.x * q.z + q.w * q.y);
|
|
46
|
-
const z = -(1 - 2 * (q.x * q.x + q.y * q.y));
|
|
47
|
-
const length = Math.hypot(x, z);
|
|
48
|
-
return length < 1e-6 ? [0, -1] : [x / length, z / length];
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const still = (stick: Stick, zone: number): boolean =>
|
|
52
|
-
Math.abs(stick[0]) < zone && Math.abs(stick[1]) < zone;
|
|
53
|
-
|
|
54
|
-
/** One step of moving: along the head's facing and its right, by the stick. */
|
|
55
|
-
const moveBy = (
|
|
56
|
-
origin: Vec3,
|
|
57
|
-
stick: Stick,
|
|
58
|
-
facing: readonly [number, number],
|
|
59
|
-
distance: number,
|
|
60
|
-
): Vec3 => {
|
|
61
|
-
const [fx, fz] = facing;
|
|
62
|
-
// The right of the facing (fx, fz) on the floor, y up: (−fz, fx).
|
|
63
|
-
const forward = -stick[1] * distance;
|
|
64
|
-
const right = stick[0] * distance;
|
|
65
|
-
return [origin[0] + fx * forward - fz * right, origin[1], origin[2] + fz * forward + fx * right];
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
/** The heading after the turning stick, and whether a snap is ready next frame. */
|
|
69
|
-
const turnBy = (
|
|
70
|
-
glider: Glider,
|
|
71
|
-
glide: XrGlide,
|
|
72
|
-
stick: Stick,
|
|
73
|
-
seconds: number,
|
|
74
|
-
): { readonly heading: number; readonly snapReady: boolean } => {
|
|
75
|
-
const { heading } = glider.pose;
|
|
76
|
-
const pushed = Math.abs(stick[0]) >= turnZone;
|
|
77
|
-
if (glide.turn === 'smooth') {
|
|
78
|
-
return {
|
|
79
|
-
heading: pushed ? heading - stick[0] * glide.turnSpeed * seconds : heading,
|
|
80
|
-
snapReady: true,
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
if (!pushed) return { heading, snapReady: true };
|
|
84
|
-
if (!glider.snapReady) return { heading, snapReady: false };
|
|
85
|
-
// Pushed right turns right: the heading (about +y) goes down.
|
|
86
|
-
return {
|
|
87
|
-
heading: heading - (Math.sign(stick[0]) * (glide.snapDegrees * Math.PI)) / 180,
|
|
88
|
-
snapReady: false,
|
|
89
|
-
};
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* One frame of gliding. Returns the next state, and the pose to report when the sticks
|
|
94
|
-
* have just come to rest after moving it.
|
|
95
|
-
*/
|
|
96
|
-
export const glideFrame = (
|
|
97
|
-
glider: Glider,
|
|
98
|
-
glide: XrGlide,
|
|
99
|
-
sticks: { readonly move: Stick; readonly turn: Stick },
|
|
100
|
-
facing: readonly [number, number],
|
|
101
|
-
seconds: number,
|
|
102
|
-
): { readonly glider: Glider; readonly stopped: GlidePose | undefined } => {
|
|
103
|
-
const moving = !still(sticks.move, deadZone);
|
|
104
|
-
const origin = moving
|
|
105
|
-
? moveBy(glider.pose.origin, sticks.move, facing, glide.speed * seconds)
|
|
106
|
-
: glider.pose.origin;
|
|
107
|
-
const { heading, snapReady } = turnBy(glider, glide, sticks.turn, seconds);
|
|
108
|
-
const changed = moving || heading !== glider.pose.heading;
|
|
109
|
-
const resting = !moving && still(sticks.turn, turnZone);
|
|
110
|
-
const moved = glider.moved || changed;
|
|
111
|
-
const pose = { origin, heading };
|
|
112
|
-
if (resting && moved) return { glider: { pose, snapReady, moved: false }, stopped: pose };
|
|
113
|
-
return { glider: { pose, snapReady, moved }, stopped: undefined };
|
|
114
|
-
};
|