depthview-r3f-bridge 0.0.0-stage → 0.3.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 +171 -2
- package/dist/babel.cjs +123 -0
- package/dist/cameraPath.js +168 -0
- package/dist/controls.js +87 -0
- package/dist/controls.prod.js +15 -0
- package/dist/controlsReact.js +99 -0
- package/dist/controlsReact.prod.js +21 -0
- package/dist/index.dev.js +41 -0
- package/dist/index.prod.js +7 -0
- package/dist/look.js +2223 -0
- package/dist/lookReact.js +2239 -0
- package/dist/path.js +181 -0
- package/dist/types/babel.d.ts +8 -0
- package/dist/types/cameraPath.d.ts +30 -0
- package/dist/types/controls.d.ts +69 -0
- package/dist/types/controls.prod.d.ts +8 -0
- package/dist/types/controlsReact.d.ts +14 -0
- package/dist/types/controlsReact.prod.d.ts +5 -0
- package/dist/types/index.dev.d.ts +10 -0
- package/dist/types/index.prod.d.ts +4 -0
- package/dist/types/look.d.ts +454 -0
- package/dist/types/lookApply.d.ts +110 -0
- package/dist/types/lookRaster.d.ts +6 -0
- package/dist/types/lookReact.d.ts +16 -0
- package/dist/types/lookRuntime.d.ts +49 -0
- package/dist/types/path.d.ts +64 -0
- package/dist/types/plugin.d.ts +21 -0
- package/dist/types/types.d.ts +15 -0
- package/dist/types/vite.d.ts +19 -0
- package/dist/vite.js +154 -0
- package/package.json +88 -4
package/README.md
CHANGED
|
@@ -1,3 +1,172 @@
|
|
|
1
|
-
#
|
|
1
|
+
# depthview-r3f-bridge
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Bridge between a three.js / React Three Fiber project and the Depthview browser extension.
|
|
4
|
+
|
|
5
|
+
- **Source locations.** A build plugin adds `userData.__depthview = { file, line, column, component }`
|
|
6
|
+
to every JSX three element (`<mesh>`, `<directionalLight>`, `<meshStandardMaterial>`, drei cameras…),
|
|
7
|
+
so an edit made in the inspector can be written back to the exact JSX prop.
|
|
8
|
+
- **Exact scene + scroll access.** `<DepthviewBridge />` hands the R3F store to the extension and,
|
|
9
|
+
if you pass a `timeline`, lets it sample the camera path exactly instead of recording a scroll.
|
|
10
|
+
|
|
11
|
+
- **Lens effects in the inspector.** The Vite plugin lets Depthview preview ambient occlusion,
|
|
12
|
+
bloom, depth of field and smooth edges with your project's own three.js.
|
|
13
|
+
- **[Looks](#look)** and **[camera paths](#camera-path)** saved from the inspector, applied by
|
|
14
|
+
your site.
|
|
15
|
+
- **[Site controls](#site-controls):** values of your scene, declared in your code, tuned in the
|
|
16
|
+
inspector and saved back.
|
|
17
|
+
|
|
18
|
+
Nothing of the development tooling ships to production: the Vite plugin only applies to the dev
|
|
19
|
+
server, the Babel plugin does nothing unless `NODE_ENV === 'development'`, and the bridge
|
|
20
|
+
component and the site controls have production entries that are empty stubs. Only what you
|
|
21
|
+
ask your site to run does: `camera-path` and `look`.
|
|
22
|
+
|
|
23
|
+
## Vite
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
// vite.config.ts
|
|
27
|
+
import { depthviewVite } from 'depthview-r3f-bridge/vite';
|
|
28
|
+
import react from '@vitejs/plugin-react';
|
|
29
|
+
|
|
30
|
+
export default defineConfig({ plugins: [depthviewVite(), react()] });
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The plugin also adds a few lines to every page of the dev server (`window.__DEPTHVIEW_LOOK__`),
|
|
34
|
+
through which the inspector loads three.js's effect passes when you turn a lens effect on.
|
|
35
|
+
`depthviewVite({ look: false })` leaves that out.
|
|
36
|
+
|
|
37
|
+
## Next.js (Babel)
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
// babel.config.js
|
|
41
|
+
module.exports = { presets: ['next/babel'], plugins: ['depthview-r3f-bridge/babel'] };
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Camera path
|
|
45
|
+
|
|
46
|
+
Depthview can edit a site's scroll camera path and save it as `camera-path.json`. This hook
|
|
47
|
+
drives the camera from that file. Unlike the rest of this package, it is meant for production:
|
|
48
|
+
it is plain three.js maths with no inspector code.
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
import { useCameraPath } from 'depthview-r3f-bridge/camera-path';
|
|
52
|
+
import path from './camera-path.json';
|
|
53
|
+
|
|
54
|
+
function ScrollCamera() {
|
|
55
|
+
useCameraPath(path); // window scroll by default
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
<Canvas>
|
|
60
|
+
<Scene />
|
|
61
|
+
<ScrollCamera />
|
|
62
|
+
</Canvas>;
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Each key is a scroll `percent` (0 top, 100 bottom) with a camera `position`, a look-at `target`
|
|
66
|
+
and a `fov`; between keys the camera follows a smooth curve through them. Pass `progress` for
|
|
67
|
+
another scroll source, for example drei's `ScrollControls`:
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
const scroll = useScroll();
|
|
71
|
+
useCameraPath(path, { progress: () => scroll.offset });
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Not using React? `evaluatePath` and `pathKeys` are in `depthview-r3f-bridge/path`, with no
|
|
75
|
+
dependencies: `evaluatePath(pathKeys(file), scrollPercent)` gives the pose to put the camera in.
|
|
76
|
+
|
|
77
|
+
`<CameraPath path={path} />` is the same as a component. `evaluatePath(keys, percent)` returns
|
|
78
|
+
the pose at a scroll position if you drive the camera yourself.
|
|
79
|
+
|
|
80
|
+
Two keys in a row with the same position, target and fov are a **hold**: the camera stays
|
|
81
|
+
still between them (Depthview writes these for a view with **Hold here** on).
|
|
82
|
+
|
|
83
|
+
## Look
|
|
84
|
+
|
|
85
|
+
A look is what the inspector's **Realism** tab tunes: tone mapping and exposure, pixel ratio,
|
|
86
|
+
world lighting (a studio of soft light boxes, built as an environment map), soft shadows,
|
|
87
|
+
surface materials, bevelled edges and lens effects. **Save to project** (or your AI agent's
|
|
88
|
+
`save_look`) writes it as `depthview-look.json`. Your site applies it:
|
|
89
|
+
|
|
90
|
+
```tsx
|
|
91
|
+
// React Three Fiber, inside <Canvas>
|
|
92
|
+
import look from './depthview-look.json';
|
|
93
|
+
import { DepthviewLook } from 'depthview-r3f-bridge/look/react';
|
|
94
|
+
|
|
95
|
+
<Canvas>
|
|
96
|
+
<Scene />
|
|
97
|
+
<DepthviewLook look={look} />
|
|
98
|
+
</Canvas>;
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
// Plain three.js
|
|
103
|
+
import look from './depthview-look.json';
|
|
104
|
+
import { applyLook } from 'depthview-r3f-bridge/look';
|
|
105
|
+
|
|
106
|
+
const applied = applyLook({ renderer, scene, camera }, look);
|
|
107
|
+
renderer.setAnimationLoop(() => applied.render()); // in place of renderer.render(scene, camera)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
- Needs `three >= 0.160`. Lens effects use the passes that ship with three.js
|
|
111
|
+
(`three/examples/jsm/postprocessing`): nothing else is installed.
|
|
112
|
+
- Surfaces and bevels find their objects by **name**: name the meshes you tuned.
|
|
113
|
+
- `applied.notes` lists anything the scene could not do (a clear coat on a material that is
|
|
114
|
+
not `MeshPhysicalMaterial`, a shape that cannot be bevelled). `applied.update(look)` swaps in
|
|
115
|
+
a changed look, `applied.dispose()` puts everything back.
|
|
116
|
+
- `performance: { adaptive: true, targetFps: 60 }` in the look makes the site hold its frame
|
|
117
|
+
rate: on a device that cannot keep up it steps down shadow detail, pixel ratio and the
|
|
118
|
+
heavier lens effects, once, without stepping back up. `applied.lightened` lists the steps
|
|
119
|
+
it took.
|
|
120
|
+
- The look follows the scene (`adapt: { follow: true }`, the default): when a model finishes
|
|
121
|
+
loading after `applyLook` ran, its meshes cast and receive shadows, the shadow area grows
|
|
122
|
+
to hold it, its textures get anisotropic filtering, and surfaces and bevels matched by name
|
|
123
|
+
are applied to it. The check is one walk over the scene every 30 frames.
|
|
124
|
+
`adapt: { textures: true }` also corrects texture colour spaces on physically based
|
|
125
|
+
materials, and `models` holds the scale, height and reflection strength of whole models by
|
|
126
|
+
name.
|
|
127
|
+
- A surface's `detail` (brushed, rings, hammered, knurl, engraved text) is drawn into a normal
|
|
128
|
+
map when the look is applied: no texture files to ship.
|
|
129
|
+
- With lens effects on, `<DepthviewLook />` takes over drawing (render priority 1), like an
|
|
130
|
+
`EffectComposer`. Do not combine it with another composer.
|
|
131
|
+
|
|
132
|
+
## Site controls
|
|
133
|
+
|
|
134
|
+
Values you keep tuning, declared where they are used:
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
import { depthviewControls } from 'depthview-r3f-bridge/controls';
|
|
138
|
+
|
|
139
|
+
const coin = depthviewControls('Coin', {
|
|
140
|
+
roughness: { value: 0.32, min: 0, max: 1 },
|
|
141
|
+
spin: true,
|
|
142
|
+
tint: '#c9ccd1',
|
|
143
|
+
});
|
|
144
|
+
// coin.roughness is updated in place: read it in your render loop, or pass a third
|
|
145
|
+
// argument, (values, key) => { ... }, to react when one changes.
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
```tsx
|
|
149
|
+
import { useDepthviewControls } from 'depthview-r3f-bridge/controls/react';
|
|
150
|
+
|
|
151
|
+
const { roughness } = useDepthviewControls('Coin', { roughness: { value: 0.32, min: 0, max: 1 } });
|
|
152
|
+
<meshStandardMaterial roughness={roughness} />;
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
They show in the inspector under **Realism › Your site's controls**, in a panel named after
|
|
156
|
+
the group: a slider when there is a `min` and `max`, a switch for booleans, a colour picker
|
|
157
|
+
for `#rrggbb`. Changing one updates the page at once. **Save to code** (paired with
|
|
158
|
+
`depthview-mcp`) writes the new value back into this call, and only that literal. A production
|
|
159
|
+
build gets the values written in the code and nothing else.
|
|
160
|
+
|
|
161
|
+
## Bridge component
|
|
162
|
+
|
|
163
|
+
```tsx
|
|
164
|
+
import { DepthviewBridge } from 'depthview-r3f-bridge';
|
|
165
|
+
|
|
166
|
+
<Canvas>
|
|
167
|
+
<Scene />
|
|
168
|
+
<DepthviewBridge timeline={{ getProgress: () => progress, setProgress: (p) => (progress = p) }} />
|
|
169
|
+
</Canvas>;
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
`line` is 1-based and `column` 0-based, pointing at the `<` of the JSX element.
|
package/dist/babel.cjs
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/babel.ts
|
|
21
|
+
var babel_exports = {};
|
|
22
|
+
__export(babel_exports, {
|
|
23
|
+
default: () => depthviewBabelPlugin
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(babel_exports);
|
|
26
|
+
|
|
27
|
+
// src/plugin.ts
|
|
28
|
+
var SOURCE_KEY = "__depthview";
|
|
29
|
+
var ATTR = `userData-${SOURCE_KEY}`;
|
|
30
|
+
var OBJECT_TAGS = /* @__PURE__ */ new Set([
|
|
31
|
+
"mesh",
|
|
32
|
+
"group",
|
|
33
|
+
"points",
|
|
34
|
+
"sprite",
|
|
35
|
+
"scene",
|
|
36
|
+
"bone",
|
|
37
|
+
"lOD",
|
|
38
|
+
"object3D",
|
|
39
|
+
"line",
|
|
40
|
+
"lineLoop",
|
|
41
|
+
"lineSegments",
|
|
42
|
+
"instancedMesh",
|
|
43
|
+
"skinnedMesh",
|
|
44
|
+
"batchedMesh"
|
|
45
|
+
]);
|
|
46
|
+
var OBJECT_SUFFIX = /(Light|Camera|Material|Helper)$/;
|
|
47
|
+
var DREI_COMPONENTS = /* @__PURE__ */ new Set([
|
|
48
|
+
"PerspectiveCamera",
|
|
49
|
+
"OrthographicCamera",
|
|
50
|
+
"Box",
|
|
51
|
+
"Sphere",
|
|
52
|
+
"Plane",
|
|
53
|
+
"Circle",
|
|
54
|
+
"Cone",
|
|
55
|
+
"Cylinder",
|
|
56
|
+
"Torus",
|
|
57
|
+
"TorusKnot",
|
|
58
|
+
"Ring",
|
|
59
|
+
"RoundedBox",
|
|
60
|
+
"Icosahedron",
|
|
61
|
+
"Octahedron",
|
|
62
|
+
"Dodecahedron",
|
|
63
|
+
"Tetrahedron",
|
|
64
|
+
"Capsule"
|
|
65
|
+
]);
|
|
66
|
+
var isThreeTag = (tag) => OBJECT_TAGS.has(tag) || OBJECT_SUFFIX.test(tag);
|
|
67
|
+
function componentName(path) {
|
|
68
|
+
for (let p = path.parentPath; p; p = p.parentPath) {
|
|
69
|
+
let name;
|
|
70
|
+
if (p.isFunctionDeclaration() || p.isClassDeclaration()) name = p.node.id?.name;
|
|
71
|
+
else if (p.isArrowFunctionExpression() || p.isFunctionExpression() || p.isClassExpression()) {
|
|
72
|
+
const parent = p.parentPath;
|
|
73
|
+
if (parent?.isVariableDeclarator() && parent.node.id.type === "Identifier")
|
|
74
|
+
name = parent.node.id.name;
|
|
75
|
+
else if (parent?.isCallExpression()) {
|
|
76
|
+
const outer = parent.parentPath;
|
|
77
|
+
if (outer?.isVariableDeclarator() && outer.node.id.type === "Identifier")
|
|
78
|
+
name = outer.node.id.name;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (name && /^[A-Z]/.test(name)) return name;
|
|
82
|
+
}
|
|
83
|
+
return "";
|
|
84
|
+
}
|
|
85
|
+
function depthviewBabelPlugin(api, options = {}) {
|
|
86
|
+
const t = api.types;
|
|
87
|
+
const enabled = options.enabled ?? process.env.NODE_ENV === "development";
|
|
88
|
+
return {
|
|
89
|
+
name: "depthview-source-locations",
|
|
90
|
+
visitor: {
|
|
91
|
+
Program(path, state) {
|
|
92
|
+
if (!enabled) return;
|
|
93
|
+
const names = /* @__PURE__ */ new Set();
|
|
94
|
+
for (const node of path.node.body) {
|
|
95
|
+
if (node.type !== "ImportDeclaration" || node.source.value !== "@react-three/drei")
|
|
96
|
+
continue;
|
|
97
|
+
for (const spec of node.specifiers) {
|
|
98
|
+
if (spec.type !== "ImportSpecifier") continue;
|
|
99
|
+
const imported = spec.imported.type === "Identifier" ? spec.imported.name : spec.imported.value;
|
|
100
|
+
if (DREI_COMPONENTS.has(imported)) names.add(spec.local.name);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
state.dreiNames = names;
|
|
104
|
+
},
|
|
105
|
+
JSXOpeningElement(path, state) {
|
|
106
|
+
if (!enabled) return;
|
|
107
|
+
const { node } = path;
|
|
108
|
+
if (node.name.type !== "JSXIdentifier" || !node.loc) return;
|
|
109
|
+
const tag = node.name.name;
|
|
110
|
+
if (!(isThreeTag(tag) || state.dreiNames?.has(tag))) return;
|
|
111
|
+
if (node.attributes.some((a) => a.type === "JSXAttribute" && a.name.name === ATTR)) return;
|
|
112
|
+
const meta = t.objectExpression([
|
|
113
|
+
t.objectProperty(t.identifier("file"), t.stringLiteral(state.filename ?? "unknown")),
|
|
114
|
+
t.objectProperty(t.identifier("line"), t.numericLiteral(node.loc.start.line)),
|
|
115
|
+
t.objectProperty(t.identifier("column"), t.numericLiteral(node.loc.start.column)),
|
|
116
|
+
t.objectProperty(t.identifier("component"), t.stringLiteral(componentName(path)))
|
|
117
|
+
]);
|
|
118
|
+
node.attributes.push(t.jsxAttribute(t.jsxIdentifier(ATTR), t.jsxExpressionContainer(meta)));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
module.exports = module.exports.default;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// src/cameraPath.tsx
|
|
2
|
+
import { useFrame, useThree } from "@react-three/fiber";
|
|
3
|
+
import { useRef } from "react";
|
|
4
|
+
|
|
5
|
+
// src/path.ts
|
|
6
|
+
var sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
7
|
+
var scale = (a, s) => [a[0] * s, a[1] * s, a[2] * s];
|
|
8
|
+
var len = (a) => Math.hypot(a[0], a[1], a[2]);
|
|
9
|
+
var norm = (a) => {
|
|
10
|
+
const l = len(a);
|
|
11
|
+
return l > 1e-12 ? scale(a, 1 / l) : [0, 0, 0];
|
|
12
|
+
};
|
|
13
|
+
var cross = (a, b) => [
|
|
14
|
+
a[1] * b[2] - a[2] * b[1],
|
|
15
|
+
a[2] * b[0] - a[0] * b[2],
|
|
16
|
+
a[0] * b[1] - a[1] * b[0]
|
|
17
|
+
];
|
|
18
|
+
function pathKeys(input) {
|
|
19
|
+
const list = Array.isArray(input) ? input : input && typeof input === "object" && Array.isArray(input.keys) ? input.keys : [];
|
|
20
|
+
const vec = (v) => Array.isArray(v) && v.length === 3 && v.every((n) => typeof n === "number" && Number.isFinite(n));
|
|
21
|
+
const out = [];
|
|
22
|
+
for (const item of list) {
|
|
23
|
+
const k = item;
|
|
24
|
+
if (!k || typeof k.percent !== "number" || !vec(k.position) || !vec(k.target)) continue;
|
|
25
|
+
out.push({
|
|
26
|
+
percent: k.percent,
|
|
27
|
+
position: [...k.position],
|
|
28
|
+
target: [...k.target],
|
|
29
|
+
fov: typeof k.fov === "number" && Number.isFinite(k.fov) ? k.fov : 50
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
function sortKeys(keys) {
|
|
35
|
+
const sorted = [...keys].sort((a, b) => a.percent - b.percent);
|
|
36
|
+
return sorted.filter((k, i) => i === 0 || k.percent - sorted[i - 1].percent > 1e-6);
|
|
37
|
+
}
|
|
38
|
+
var sameVec = (a, b) => Math.abs(a[0] - b[0]) < 1e-6 && Math.abs(a[1] - b[1]) < 1e-6 && Math.abs(a[2] - b[2]) < 1e-6;
|
|
39
|
+
function samePose(a, b) {
|
|
40
|
+
return sameVec(a.position, b.position) && sameVec(a.target, b.target) && Math.abs(a.fov - b.fov) < 1e-6;
|
|
41
|
+
}
|
|
42
|
+
function catmull(p0, p1, p2, p3, t) {
|
|
43
|
+
const t2 = t * t;
|
|
44
|
+
const t3 = t2 * t;
|
|
45
|
+
const out = [0, 0, 0];
|
|
46
|
+
for (let i = 0; i < 3; i++) {
|
|
47
|
+
out[i] = 0.5 * (2 * p1[i] + (-p0[i] + p2[i]) * t + (2 * p0[i] - 5 * p1[i] + 4 * p2[i] - p3[i]) * t2 + (-p0[i] + 3 * p1[i] - 3 * p2[i] + p3[i]) * t3);
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
function evaluatePath(input, percent) {
|
|
52
|
+
const keys = sortKeys(input);
|
|
53
|
+
if (!keys.length) return null;
|
|
54
|
+
const first = keys[0];
|
|
55
|
+
const last = keys[keys.length - 1];
|
|
56
|
+
const pick = (k) => ({
|
|
57
|
+
position: [...k.position],
|
|
58
|
+
target: [...k.target],
|
|
59
|
+
fov: k.fov
|
|
60
|
+
});
|
|
61
|
+
if (keys.length === 1 || percent <= first.percent) return pick(first);
|
|
62
|
+
if (percent >= last.percent) return pick(last);
|
|
63
|
+
let i = 0;
|
|
64
|
+
while (i < keys.length - 2 && percent > keys[i + 1].percent) i++;
|
|
65
|
+
const k1 = keys[i];
|
|
66
|
+
const k2 = keys[i + 1];
|
|
67
|
+
if (samePose(k1, k2)) return pick(k1);
|
|
68
|
+
const before = keys[i - 1];
|
|
69
|
+
const after = keys[i + 2];
|
|
70
|
+
const k0 = before && !samePose(before, k1) ? before : void 0;
|
|
71
|
+
const k3 = after && !samePose(after, k2) ? after : void 0;
|
|
72
|
+
const t = (percent - k1.percent) / (k2.percent - k1.percent);
|
|
73
|
+
const ends = (field) => [
|
|
74
|
+
k0 ? k0[field] : sub(scale(k1[field], 2), k2[field]),
|
|
75
|
+
k3 ? k3[field] : sub(scale(k2[field], 2), k1[field])
|
|
76
|
+
];
|
|
77
|
+
const [p0, p3] = ends("position");
|
|
78
|
+
const [q0, q3] = ends("target");
|
|
79
|
+
return {
|
|
80
|
+
position: catmull(p0, k1.position, k2.position, p3, t),
|
|
81
|
+
target: catmull(q0, k1.target, k2.target, q3, t),
|
|
82
|
+
fov: k1.fov + (k2.fov - k1.fov) * t
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function pathPoints(keys, perSegment = 24) {
|
|
86
|
+
const sorted = sortKeys(keys);
|
|
87
|
+
if (sorted.length < 2) return sorted.map((k) => evaluatePath(sorted, k.percent));
|
|
88
|
+
const out = [];
|
|
89
|
+
for (let i = 0; i < sorted.length - 1; i++) {
|
|
90
|
+
const a = sorted[i].percent;
|
|
91
|
+
const b = sorted[i + 1].percent;
|
|
92
|
+
for (let s = 0; s < perSegment; s++)
|
|
93
|
+
out.push(evaluatePath(sorted, a + (b - a) * s / perSegment));
|
|
94
|
+
}
|
|
95
|
+
out.push(evaluatePath(sorted, sorted[sorted.length - 1].percent));
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
function lookAtQuaternion(eye, target, up = [0, 1, 0]) {
|
|
99
|
+
let z = norm(sub(eye, target));
|
|
100
|
+
if (len(z) === 0) z = [0, 0, 1];
|
|
101
|
+
let x = norm(cross(up, z));
|
|
102
|
+
if (len(x) === 0) {
|
|
103
|
+
const nudged = Math.abs(up[2]) === 1 ? [z[0] + 1e-4, z[1], z[2]] : [z[0], z[1], z[2] + 1e-4];
|
|
104
|
+
x = norm(cross(up, norm(nudged)));
|
|
105
|
+
}
|
|
106
|
+
const y = cross(z, x);
|
|
107
|
+
const m00 = x[0], m01 = y[0], m02 = z[0];
|
|
108
|
+
const m10 = x[1], m11 = y[1], m12 = z[1];
|
|
109
|
+
const m20 = x[2], m21 = y[2], m22 = z[2];
|
|
110
|
+
const trace = m00 + m11 + m22;
|
|
111
|
+
if (trace > 0) {
|
|
112
|
+
const s2 = 0.5 / Math.sqrt(trace + 1);
|
|
113
|
+
return [(m21 - m12) * s2, (m02 - m20) * s2, (m10 - m01) * s2, 0.25 / s2];
|
|
114
|
+
}
|
|
115
|
+
if (m00 > m11 && m00 > m22) {
|
|
116
|
+
const s2 = 2 * Math.sqrt(1 + m00 - m11 - m22);
|
|
117
|
+
return [0.25 * s2, (m01 + m10) / s2, (m02 + m20) / s2, (m21 - m12) / s2];
|
|
118
|
+
}
|
|
119
|
+
if (m11 > m22) {
|
|
120
|
+
const s2 = 2 * Math.sqrt(1 + m11 - m00 - m22);
|
|
121
|
+
return [(m01 + m10) / s2, 0.25 * s2, (m12 + m21) / s2, (m02 - m20) / s2];
|
|
122
|
+
}
|
|
123
|
+
const s = 2 * Math.sqrt(1 + m22 - m00 - m11);
|
|
124
|
+
return [(m02 + m20) / s, (m12 + m21) / s, 0.25 * s, (m10 - m01) / s];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/cameraPath.tsx
|
|
128
|
+
function windowProgress() {
|
|
129
|
+
const el = document.scrollingElement ?? document.documentElement;
|
|
130
|
+
const max = el.scrollHeight - el.clientHeight;
|
|
131
|
+
return max > 0 ? Math.min(1, Math.max(0, el.scrollTop / max)) : 0;
|
|
132
|
+
}
|
|
133
|
+
function useCameraPath(path, { progress = windowProgress, enabled = true } = {}) {
|
|
134
|
+
const camera = useThree((s) => s.camera);
|
|
135
|
+
const source = useRef(null);
|
|
136
|
+
const keys = useRef([]);
|
|
137
|
+
if (source.current !== path) {
|
|
138
|
+
source.current = path;
|
|
139
|
+
keys.current = pathKeys(path);
|
|
140
|
+
}
|
|
141
|
+
useFrame(() => {
|
|
142
|
+
if (!enabled) return;
|
|
143
|
+
const pose = evaluatePath(keys.current, progress() * 100);
|
|
144
|
+
if (!pose) return;
|
|
145
|
+
camera.position.set(...pose.position);
|
|
146
|
+
camera.lookAt(...pose.target);
|
|
147
|
+
const persp = camera;
|
|
148
|
+
if (persp.isPerspectiveCamera && persp.fov !== pose.fov) {
|
|
149
|
+
persp.fov = pose.fov;
|
|
150
|
+
camera.updateProjectionMatrix();
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
function CameraPath({
|
|
155
|
+
path,
|
|
156
|
+
...options
|
|
157
|
+
}) {
|
|
158
|
+
useCameraPath(path, options);
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
export {
|
|
162
|
+
CameraPath,
|
|
163
|
+
evaluatePath,
|
|
164
|
+
lookAtQuaternion,
|
|
165
|
+
pathKeys,
|
|
166
|
+
pathPoints,
|
|
167
|
+
useCameraPath
|
|
168
|
+
};
|
package/dist/controls.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// src/controls.ts
|
|
2
|
+
var GLOBAL = "__DEPTHVIEW_CONTROLS__";
|
|
3
|
+
var typeOf = (value) => typeof value === "number" ? "number" : typeof value === "boolean" ? "boolean" : /^#[0-9a-fA-F]{6}$/.test(value) ? "color" : "text";
|
|
4
|
+
function controlRegistry() {
|
|
5
|
+
const host = globalThis;
|
|
6
|
+
const existing = host[GLOBAL];
|
|
7
|
+
if (existing?.version === 1) return existing;
|
|
8
|
+
const registry = {
|
|
9
|
+
version: 1,
|
|
10
|
+
groups: /* @__PURE__ */ new Map(),
|
|
11
|
+
listeners: /* @__PURE__ */ new Set(),
|
|
12
|
+
list() {
|
|
13
|
+
const out = [];
|
|
14
|
+
for (const group of registry.groups.values())
|
|
15
|
+
for (const info of group.info.values())
|
|
16
|
+
out.push({ ...info, value: group.values[info.key] });
|
|
17
|
+
return out;
|
|
18
|
+
},
|
|
19
|
+
set(groupName, key, value) {
|
|
20
|
+
const group = registry.groups.get(groupName);
|
|
21
|
+
const info = group?.info.get(key);
|
|
22
|
+
if (!group || !info) return false;
|
|
23
|
+
if (typeof value !== typeof info.initial) return false;
|
|
24
|
+
let next = value;
|
|
25
|
+
if (typeof next === "number") {
|
|
26
|
+
if (!Number.isFinite(next)) return false;
|
|
27
|
+
if (info.min !== void 0) next = Math.max(info.min, next);
|
|
28
|
+
if (info.max !== void 0) next = Math.min(info.max, next);
|
|
29
|
+
}
|
|
30
|
+
if (group.values[key] === next) return true;
|
|
31
|
+
group.values[key] = next;
|
|
32
|
+
for (const listener of group.listeners) listener(key);
|
|
33
|
+
for (const listener of registry.listeners) listener(groupName, key);
|
|
34
|
+
return true;
|
|
35
|
+
},
|
|
36
|
+
subscribe(listener) {
|
|
37
|
+
registry.listeners.add(listener);
|
|
38
|
+
return () => void registry.listeners.delete(listener);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
host[GLOBAL] = registry;
|
|
42
|
+
return registry;
|
|
43
|
+
}
|
|
44
|
+
function register(name, schema) {
|
|
45
|
+
const registry = controlRegistry();
|
|
46
|
+
let group = registry.groups.get(name);
|
|
47
|
+
if (!group) {
|
|
48
|
+
group = { values: {}, info: /* @__PURE__ */ new Map(), listeners: /* @__PURE__ */ new Set() };
|
|
49
|
+
registry.groups.set(name, group);
|
|
50
|
+
}
|
|
51
|
+
for (const [key, input] of Object.entries(schema)) {
|
|
52
|
+
const spec = typeof input === "object" && input !== null ? input : { value: input };
|
|
53
|
+
const previous = group.info.get(key);
|
|
54
|
+
const info = {
|
|
55
|
+
group: name,
|
|
56
|
+
key,
|
|
57
|
+
type: typeOf(spec.value),
|
|
58
|
+
value: spec.value,
|
|
59
|
+
initial: spec.value,
|
|
60
|
+
...spec.min !== void 0 ? { min: spec.min } : {},
|
|
61
|
+
...spec.max !== void 0 ? { max: spec.max } : {},
|
|
62
|
+
...spec.step !== void 0 ? { step: spec.step } : {},
|
|
63
|
+
...spec.label !== void 0 ? { label: spec.label } : {}
|
|
64
|
+
};
|
|
65
|
+
group.info.set(key, info);
|
|
66
|
+
const keep = previous !== void 0 && previous.initial === spec.value && typeof group.values[key] === typeof spec.value;
|
|
67
|
+
if (!keep) group.values[key] = spec.value;
|
|
68
|
+
}
|
|
69
|
+
return group;
|
|
70
|
+
}
|
|
71
|
+
function depthviewControls(group, schema, onChange) {
|
|
72
|
+
const g = register(group, schema);
|
|
73
|
+
const values = g.values;
|
|
74
|
+
if (onChange) g.listeners.add((key) => onChange(values, key));
|
|
75
|
+
return values;
|
|
76
|
+
}
|
|
77
|
+
function watchControls(group, listener) {
|
|
78
|
+
const g = controlRegistry().groups.get(group);
|
|
79
|
+
if (!g) return () => void 0;
|
|
80
|
+
g.listeners.add(listener);
|
|
81
|
+
return () => void g.listeners.delete(listener);
|
|
82
|
+
}
|
|
83
|
+
export {
|
|
84
|
+
controlRegistry,
|
|
85
|
+
depthviewControls,
|
|
86
|
+
watchControls
|
|
87
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/controls.prod.ts
|
|
2
|
+
function depthviewControls(_group, schema, onChange) {
|
|
3
|
+
const values = {};
|
|
4
|
+
for (const [key, input] of Object.entries(schema))
|
|
5
|
+
values[key] = typeof input === "object" && input !== null ? input.value : input;
|
|
6
|
+
void onChange;
|
|
7
|
+
return values;
|
|
8
|
+
}
|
|
9
|
+
function watchControls() {
|
|
10
|
+
return () => void 0;
|
|
11
|
+
}
|
|
12
|
+
export {
|
|
13
|
+
depthviewControls,
|
|
14
|
+
watchControls
|
|
15
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// src/controlsReact.ts
|
|
2
|
+
import { useEffect, useReducer, useRef } from "react";
|
|
3
|
+
|
|
4
|
+
// src/controls.ts
|
|
5
|
+
var GLOBAL = "__DEPTHVIEW_CONTROLS__";
|
|
6
|
+
var typeOf = (value) => typeof value === "number" ? "number" : typeof value === "boolean" ? "boolean" : /^#[0-9a-fA-F]{6}$/.test(value) ? "color" : "text";
|
|
7
|
+
function controlRegistry() {
|
|
8
|
+
const host = globalThis;
|
|
9
|
+
const existing = host[GLOBAL];
|
|
10
|
+
if (existing?.version === 1) return existing;
|
|
11
|
+
const registry = {
|
|
12
|
+
version: 1,
|
|
13
|
+
groups: /* @__PURE__ */ new Map(),
|
|
14
|
+
listeners: /* @__PURE__ */ new Set(),
|
|
15
|
+
list() {
|
|
16
|
+
const out = [];
|
|
17
|
+
for (const group of registry.groups.values())
|
|
18
|
+
for (const info of group.info.values())
|
|
19
|
+
out.push({ ...info, value: group.values[info.key] });
|
|
20
|
+
return out;
|
|
21
|
+
},
|
|
22
|
+
set(groupName, key, value) {
|
|
23
|
+
const group = registry.groups.get(groupName);
|
|
24
|
+
const info = group?.info.get(key);
|
|
25
|
+
if (!group || !info) return false;
|
|
26
|
+
if (typeof value !== typeof info.initial) return false;
|
|
27
|
+
let next = value;
|
|
28
|
+
if (typeof next === "number") {
|
|
29
|
+
if (!Number.isFinite(next)) return false;
|
|
30
|
+
if (info.min !== void 0) next = Math.max(info.min, next);
|
|
31
|
+
if (info.max !== void 0) next = Math.min(info.max, next);
|
|
32
|
+
}
|
|
33
|
+
if (group.values[key] === next) return true;
|
|
34
|
+
group.values[key] = next;
|
|
35
|
+
for (const listener of group.listeners) listener(key);
|
|
36
|
+
for (const listener of registry.listeners) listener(groupName, key);
|
|
37
|
+
return true;
|
|
38
|
+
},
|
|
39
|
+
subscribe(listener) {
|
|
40
|
+
registry.listeners.add(listener);
|
|
41
|
+
return () => void registry.listeners.delete(listener);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
host[GLOBAL] = registry;
|
|
45
|
+
return registry;
|
|
46
|
+
}
|
|
47
|
+
function register(name, schema) {
|
|
48
|
+
const registry = controlRegistry();
|
|
49
|
+
let group = registry.groups.get(name);
|
|
50
|
+
if (!group) {
|
|
51
|
+
group = { values: {}, info: /* @__PURE__ */ new Map(), listeners: /* @__PURE__ */ new Set() };
|
|
52
|
+
registry.groups.set(name, group);
|
|
53
|
+
}
|
|
54
|
+
for (const [key, input] of Object.entries(schema)) {
|
|
55
|
+
const spec = typeof input === "object" && input !== null ? input : { value: input };
|
|
56
|
+
const previous = group.info.get(key);
|
|
57
|
+
const info = {
|
|
58
|
+
group: name,
|
|
59
|
+
key,
|
|
60
|
+
type: typeOf(spec.value),
|
|
61
|
+
value: spec.value,
|
|
62
|
+
initial: spec.value,
|
|
63
|
+
...spec.min !== void 0 ? { min: spec.min } : {},
|
|
64
|
+
...spec.max !== void 0 ? { max: spec.max } : {},
|
|
65
|
+
...spec.step !== void 0 ? { step: spec.step } : {},
|
|
66
|
+
...spec.label !== void 0 ? { label: spec.label } : {}
|
|
67
|
+
};
|
|
68
|
+
group.info.set(key, info);
|
|
69
|
+
const keep = previous !== void 0 && previous.initial === spec.value && typeof group.values[key] === typeof spec.value;
|
|
70
|
+
if (!keep) group.values[key] = spec.value;
|
|
71
|
+
}
|
|
72
|
+
return group;
|
|
73
|
+
}
|
|
74
|
+
function depthviewControls(group, schema, onChange) {
|
|
75
|
+
const g = register(group, schema);
|
|
76
|
+
const values = g.values;
|
|
77
|
+
if (onChange) g.listeners.add((key) => onChange(values, key));
|
|
78
|
+
return values;
|
|
79
|
+
}
|
|
80
|
+
function watchControls(group, listener) {
|
|
81
|
+
const g = controlRegistry().groups.get(group);
|
|
82
|
+
if (!g) return () => void 0;
|
|
83
|
+
g.listeners.add(listener);
|
|
84
|
+
return () => void g.listeners.delete(listener);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/controlsReact.ts
|
|
88
|
+
function useDepthviewControls(group, schema) {
|
|
89
|
+
const [, rerender] = useReducer((n) => n + 1, 0);
|
|
90
|
+
const values = useRef(null);
|
|
91
|
+
values.current = depthviewControls(group, schema);
|
|
92
|
+
useEffect(() => watchControls(group, rerender), [group]);
|
|
93
|
+
return { ...values.current };
|
|
94
|
+
}
|
|
95
|
+
export {
|
|
96
|
+
controlRegistry,
|
|
97
|
+
depthviewControls,
|
|
98
|
+
useDepthviewControls
|
|
99
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/controlsReact.prod.ts
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
|
|
4
|
+
// src/controls.prod.ts
|
|
5
|
+
function depthviewControls(_group, schema, onChange) {
|
|
6
|
+
const values = {};
|
|
7
|
+
for (const [key, input] of Object.entries(schema))
|
|
8
|
+
values[key] = typeof input === "object" && input !== null ? input.value : input;
|
|
9
|
+
void onChange;
|
|
10
|
+
return values;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// src/controlsReact.prod.ts
|
|
14
|
+
function useDepthviewControls(group, schema) {
|
|
15
|
+
const key = JSON.stringify(schema);
|
|
16
|
+
return useMemo(() => depthviewControls(group, JSON.parse(key)), [group, key]);
|
|
17
|
+
}
|
|
18
|
+
export {
|
|
19
|
+
depthviewControls,
|
|
20
|
+
useDepthviewControls
|
|
21
|
+
};
|