depthview-r3f-bridge 0.0.0-stage → 0.1.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 +74 -2
- package/dist/babel.cjs +123 -0
- package/dist/cameraPath.js +141 -0
- package/dist/index.dev.js +41 -0
- package/dist/index.prod.js +7 -0
- package/dist/types/babel.d.ts +8 -0
- package/dist/types/cameraPath.d.ts +26 -0
- package/dist/types/index.dev.d.ts +10 -0
- package/dist/types/index.prod.d.ts +4 -0
- package/dist/types/path.d.ts +56 -0
- package/dist/types/plugin.d.ts +21 -0
- package/dist/types/types.d.ts +15 -0
- package/dist/types/vite.d.ts +12 -0
- package/dist/vite.js +128 -0
- package/package.json +68 -4
package/README.md
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
-
#
|
|
1
|
+
# depthview-r3f-bridge
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Development-only bridge between a 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
|
+
Nothing ships to production: the Vite plugin only applies to the dev server, the Babel plugin does
|
|
12
|
+
nothing unless `NODE_ENV === 'development'`, and the package's production entry is an empty stub.
|
|
13
|
+
|
|
14
|
+
## Vite
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
// vite.config.ts
|
|
18
|
+
import { depthviewVite } from 'depthview-r3f-bridge/vite';
|
|
19
|
+
import react from '@vitejs/plugin-react';
|
|
20
|
+
|
|
21
|
+
export default defineConfig({ plugins: [depthviewVite(), react()] });
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Next.js (Babel)
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
// babel.config.js
|
|
28
|
+
module.exports = { presets: ['next/babel'], plugins: ['depthview-r3f-bridge/babel'] };
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Camera path
|
|
32
|
+
|
|
33
|
+
Depthview can edit a site's scroll camera path and save it as `camera-path.json`. This hook
|
|
34
|
+
drives the camera from that file. Unlike the rest of this package, it is meant for production:
|
|
35
|
+
it is plain three.js maths with no inspector code.
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { useCameraPath } from 'depthview-r3f-bridge/camera-path';
|
|
39
|
+
import path from './camera-path.json';
|
|
40
|
+
|
|
41
|
+
function ScrollCamera() {
|
|
42
|
+
useCameraPath(path); // window scroll by default
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
<Canvas>
|
|
47
|
+
<Scene />
|
|
48
|
+
<ScrollCamera />
|
|
49
|
+
</Canvas>;
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Each key is a scroll `percent` (0 top, 100 bottom) with a camera `position`, a look-at `target`
|
|
53
|
+
and a `fov`; between keys the camera follows a smooth curve through them. Pass `progress` for
|
|
54
|
+
another scroll source, for example drei's `ScrollControls`:
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
const scroll = useScroll();
|
|
58
|
+
useCameraPath(path, { progress: () => scroll.offset });
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
`<CameraPath path={path} />` is the same as a component. `evaluatePath(keys, percent)` returns
|
|
62
|
+
the pose at a scroll position if you drive the camera yourself.
|
|
63
|
+
|
|
64
|
+
## Bridge component
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
import { DepthviewBridge } from 'depthview-r3f-bridge';
|
|
68
|
+
|
|
69
|
+
<Canvas>
|
|
70
|
+
<Scene />
|
|
71
|
+
<DepthviewBridge timeline={{ getProgress: () => progress, setProgress: (p) => (progress = p) }} />
|
|
72
|
+
</Canvas>;
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`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,141 @@
|
|
|
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 sortKeys(keys) {
|
|
19
|
+
const sorted = [...keys].sort((a, b) => a.percent - b.percent);
|
|
20
|
+
return sorted.filter((k, i) => i === 0 || k.percent - sorted[i - 1].percent > 1e-6);
|
|
21
|
+
}
|
|
22
|
+
function catmull(p0, p1, p2, p3, t) {
|
|
23
|
+
const t2 = t * t;
|
|
24
|
+
const t3 = t2 * t;
|
|
25
|
+
const out = [0, 0, 0];
|
|
26
|
+
for (let i = 0; i < 3; i++) {
|
|
27
|
+
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);
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
function evaluatePath(input, percent) {
|
|
32
|
+
const keys = sortKeys(input);
|
|
33
|
+
if (!keys.length) return null;
|
|
34
|
+
const first = keys[0];
|
|
35
|
+
const last = keys[keys.length - 1];
|
|
36
|
+
const pick = (k) => ({
|
|
37
|
+
position: [...k.position],
|
|
38
|
+
target: [...k.target],
|
|
39
|
+
fov: k.fov
|
|
40
|
+
});
|
|
41
|
+
if (keys.length === 1 || percent <= first.percent) return pick(first);
|
|
42
|
+
if (percent >= last.percent) return pick(last);
|
|
43
|
+
let i = 0;
|
|
44
|
+
while (i < keys.length - 2 && percent > keys[i + 1].percent) i++;
|
|
45
|
+
const k1 = keys[i];
|
|
46
|
+
const k2 = keys[i + 1];
|
|
47
|
+
const k0 = keys[i - 1];
|
|
48
|
+
const k3 = keys[i + 2];
|
|
49
|
+
const t = (percent - k1.percent) / (k2.percent - k1.percent);
|
|
50
|
+
const ends = (field) => [
|
|
51
|
+
k0 ? k0[field] : sub(scale(k1[field], 2), k2[field]),
|
|
52
|
+
k3 ? k3[field] : sub(scale(k2[field], 2), k1[field])
|
|
53
|
+
];
|
|
54
|
+
const [p0, p3] = ends("position");
|
|
55
|
+
const [q0, q3] = ends("target");
|
|
56
|
+
return {
|
|
57
|
+
position: catmull(p0, k1.position, k2.position, p3, t),
|
|
58
|
+
target: catmull(q0, k1.target, k2.target, q3, t),
|
|
59
|
+
fov: k1.fov + (k2.fov - k1.fov) * t
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function pathPoints(keys, perSegment = 24) {
|
|
63
|
+
const sorted = sortKeys(keys);
|
|
64
|
+
if (sorted.length < 2) return sorted.map((k) => evaluatePath(sorted, k.percent));
|
|
65
|
+
const out = [];
|
|
66
|
+
for (let i = 0; i < sorted.length - 1; i++) {
|
|
67
|
+
const a = sorted[i].percent;
|
|
68
|
+
const b = sorted[i + 1].percent;
|
|
69
|
+
for (let s = 0; s < perSegment; s++)
|
|
70
|
+
out.push(evaluatePath(sorted, a + (b - a) * s / perSegment));
|
|
71
|
+
}
|
|
72
|
+
out.push(evaluatePath(sorted, sorted[sorted.length - 1].percent));
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
function lookAtQuaternion(eye, target, up = [0, 1, 0]) {
|
|
76
|
+
let z = norm(sub(eye, target));
|
|
77
|
+
if (len(z) === 0) z = [0, 0, 1];
|
|
78
|
+
let x = norm(cross(up, z));
|
|
79
|
+
if (len(x) === 0) {
|
|
80
|
+
const nudged = Math.abs(up[2]) === 1 ? [z[0] + 1e-4, z[1], z[2]] : [z[0], z[1], z[2] + 1e-4];
|
|
81
|
+
x = norm(cross(up, norm(nudged)));
|
|
82
|
+
}
|
|
83
|
+
const y = cross(z, x);
|
|
84
|
+
const m00 = x[0], m01 = y[0], m02 = z[0];
|
|
85
|
+
const m10 = x[1], m11 = y[1], m12 = z[1];
|
|
86
|
+
const m20 = x[2], m21 = y[2], m22 = z[2];
|
|
87
|
+
const trace = m00 + m11 + m22;
|
|
88
|
+
if (trace > 0) {
|
|
89
|
+
const s2 = 0.5 / Math.sqrt(trace + 1);
|
|
90
|
+
return [(m21 - m12) * s2, (m02 - m20) * s2, (m10 - m01) * s2, 0.25 / s2];
|
|
91
|
+
}
|
|
92
|
+
if (m00 > m11 && m00 > m22) {
|
|
93
|
+
const s2 = 2 * Math.sqrt(1 + m00 - m11 - m22);
|
|
94
|
+
return [0.25 * s2, (m01 + m10) / s2, (m02 + m20) / s2, (m21 - m12) / s2];
|
|
95
|
+
}
|
|
96
|
+
if (m11 > m22) {
|
|
97
|
+
const s2 = 2 * Math.sqrt(1 + m11 - m00 - m22);
|
|
98
|
+
return [(m01 + m10) / s2, 0.25 * s2, (m12 + m21) / s2, (m02 - m20) / s2];
|
|
99
|
+
}
|
|
100
|
+
const s = 2 * Math.sqrt(1 + m22 - m00 - m11);
|
|
101
|
+
return [(m02 + m20) / s, (m12 + m21) / s, 0.25 * s, (m10 - m01) / s];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/cameraPath.tsx
|
|
105
|
+
function windowProgress() {
|
|
106
|
+
const el = document.scrollingElement ?? document.documentElement;
|
|
107
|
+
const max = el.scrollHeight - el.clientHeight;
|
|
108
|
+
return max > 0 ? Math.min(1, Math.max(0, el.scrollTop / max)) : 0;
|
|
109
|
+
}
|
|
110
|
+
var keysOf = (path) => Array.isArray(path) ? path : path.keys;
|
|
111
|
+
function useCameraPath(path, { progress = windowProgress, enabled = true } = {}) {
|
|
112
|
+
const camera = useThree((s) => s.camera);
|
|
113
|
+
const keys = useRef(keysOf(path));
|
|
114
|
+
keys.current = keysOf(path);
|
|
115
|
+
useFrame(() => {
|
|
116
|
+
if (!enabled) return;
|
|
117
|
+
const pose = evaluatePath(keys.current, progress() * 100);
|
|
118
|
+
if (!pose) return;
|
|
119
|
+
camera.position.set(...pose.position);
|
|
120
|
+
camera.lookAt(...pose.target);
|
|
121
|
+
const persp = camera;
|
|
122
|
+
if (persp.isPerspectiveCamera && persp.fov !== pose.fov) {
|
|
123
|
+
persp.fov = pose.fov;
|
|
124
|
+
camera.updateProjectionMatrix();
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
function CameraPath({
|
|
129
|
+
path,
|
|
130
|
+
...options
|
|
131
|
+
}) {
|
|
132
|
+
useCameraPath(path, options);
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
export {
|
|
136
|
+
CameraPath,
|
|
137
|
+
evaluatePath,
|
|
138
|
+
lookAtQuaternion,
|
|
139
|
+
pathPoints,
|
|
140
|
+
useCameraPath
|
|
141
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// src/index.dev.tsx
|
|
2
|
+
import { useThree } from "@react-three/fiber";
|
|
3
|
+
import { useEffect } from "react";
|
|
4
|
+
var GLOBAL = "__DEPTHVIEW_BRIDGE__";
|
|
5
|
+
function DepthviewBridge({ timeline }) {
|
|
6
|
+
const get = useThree((state) => state.get);
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
const api = {
|
|
9
|
+
version: 1,
|
|
10
|
+
getState: () => {
|
|
11
|
+
const s = get();
|
|
12
|
+
return { scene: s.scene, camera: s.camera, gl: s.gl };
|
|
13
|
+
},
|
|
14
|
+
// Runs one R3F frame synchronously (useFrame callbacks) without advancing the clock.
|
|
15
|
+
advance: () => {
|
|
16
|
+
const s = get();
|
|
17
|
+
s.advance(s.clock.elapsedTime, false);
|
|
18
|
+
},
|
|
19
|
+
timeline: timeline ?? null
|
|
20
|
+
};
|
|
21
|
+
const w = window;
|
|
22
|
+
w[GLOBAL] = api;
|
|
23
|
+
window.postMessage(
|
|
24
|
+
{
|
|
25
|
+
source: "depthview",
|
|
26
|
+
v: 1,
|
|
27
|
+
id: `bridge-${Date.now().toString(36)}`,
|
|
28
|
+
type: "bridge:ready",
|
|
29
|
+
payload: { version: 1, timeline: !!timeline }
|
|
30
|
+
},
|
|
31
|
+
window.location.origin === "null" ? "*" : window.location.origin
|
|
32
|
+
);
|
|
33
|
+
return () => {
|
|
34
|
+
if (w[GLOBAL] === api) delete w[GLOBAL];
|
|
35
|
+
};
|
|
36
|
+
}, [get, timeline]);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
DepthviewBridge
|
|
41
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Babel plugin entry, for Next.js and other Babel-based setups:
|
|
3
|
+
*
|
|
4
|
+
* // babel.config.js
|
|
5
|
+
* module.exports = { presets: ['next/babel'], plugins: ['depthview-r3f-bridge/babel'] };
|
|
6
|
+
*/
|
|
7
|
+
export { default } from './plugin';
|
|
8
|
+
export type { DepthviewPluginOptions } from './plugin';
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type CameraPathFile, type PathKey } from './path';
|
|
2
|
+
export type { CameraPathFile, PathKey, PathPose, Vec3, Quat } from './path';
|
|
3
|
+
export { evaluatePath, lookAtQuaternion, pathPoints } from './path';
|
|
4
|
+
export interface CameraPathOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Scroll progress from 0 (top) to 1 (bottom). Default: the window's scroll position.
|
|
7
|
+
* With drei's ScrollControls pass `() => scroll.offset`; with GSAP, the tween's progress.
|
|
8
|
+
*/
|
|
9
|
+
progress?: () => number;
|
|
10
|
+
/** Off: the camera is left alone (for example while another camera move plays). */
|
|
11
|
+
enabled?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Moves the default camera along a Depthview camera path as the page scrolls. Place it inside
|
|
15
|
+
* <Canvas>, in place of your own scroll camera code:
|
|
16
|
+
*
|
|
17
|
+
* import path from './camera-path.json';
|
|
18
|
+
* useCameraPath(path);
|
|
19
|
+
*
|
|
20
|
+
* Unlike <DepthviewBridge />, this is meant for production: it is plain three.js maths.
|
|
21
|
+
*/
|
|
22
|
+
export declare function useCameraPath(path: CameraPathFile | PathKey[], { progress, enabled }?: CameraPathOptions): void;
|
|
23
|
+
/** Component form of useCameraPath. */
|
|
24
|
+
export declare function CameraPath({ path, ...options }: CameraPathOptions & {
|
|
25
|
+
path: CameraPathFile | PathKey[];
|
|
26
|
+
}): null;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DepthviewBridgeProps } from './types';
|
|
2
|
+
export type { DepthviewBridgeProps, DepthviewTimeline } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Place inside <Canvas>. Development builds only: the production entry of this package
|
|
5
|
+
* exports a component that renders nothing and contains none of this code.
|
|
6
|
+
*
|
|
7
|
+
* Exposes the R3F store (scene, camera, renderer) and the optional scroll timeline to the
|
|
8
|
+
* Depthview extension, and announces itself on the page's MAIN-world message channel.
|
|
9
|
+
*/
|
|
10
|
+
export declare function DepthviewBridge({ timeline }: DepthviewBridgeProps): null;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { DepthviewBridgeProps } from './types';
|
|
2
|
+
export type { DepthviewBridgeProps, DepthviewTimeline } from './types';
|
|
3
|
+
/** Production stub: renders nothing and carries no bridge code. */
|
|
4
|
+
export declare function DepthviewBridge(_props: DepthviewBridgeProps): null;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A scroll-driven camera path: a few key poses along the page, joined by smooth curves.
|
|
3
|
+
* Pure maths with no three.js import, shared by the Depthview extension (to draw, edit and
|
|
4
|
+
* preview the path) and by `useCameraPath` (to drive the camera in the developer's site).
|
|
5
|
+
*/
|
|
6
|
+
export type Vec3 = [number, number, number];
|
|
7
|
+
export type Quat = [number, number, number, number];
|
|
8
|
+
export interface PathKey {
|
|
9
|
+
/** Where along the page this pose is reached, 0 (top) to 100 (bottom). */
|
|
10
|
+
percent: number;
|
|
11
|
+
/** Camera position, world space. */
|
|
12
|
+
position: Vec3;
|
|
13
|
+
/** The point the camera looks at, world space. */
|
|
14
|
+
target: Vec3;
|
|
15
|
+
/** Vertical field of view in degrees. */
|
|
16
|
+
fov: number;
|
|
17
|
+
}
|
|
18
|
+
/** What Depthview writes to `camera-path.json`. */
|
|
19
|
+
export interface CameraPathFile {
|
|
20
|
+
version: 1;
|
|
21
|
+
keys: PathKey[];
|
|
22
|
+
}
|
|
23
|
+
export interface PathPose {
|
|
24
|
+
position: Vec3;
|
|
25
|
+
target: Vec3;
|
|
26
|
+
fov: number;
|
|
27
|
+
}
|
|
28
|
+
/** Keys in page order, without duplicates of the same percent. */
|
|
29
|
+
export declare function sortKeys(keys: PathKey[]): PathKey[];
|
|
30
|
+
/**
|
|
31
|
+
* The camera pose at a scroll position. The curve passes through every key; before the first
|
|
32
|
+
* key and after the last one the camera holds that key's pose.
|
|
33
|
+
*/
|
|
34
|
+
export declare function evaluatePath(input: PathKey[], percent: number): PathPose | null;
|
|
35
|
+
/** Points along the whole path, for drawing it. */
|
|
36
|
+
export declare function pathPoints(keys: PathKey[], perSegment?: number): PathPose[];
|
|
37
|
+
/**
|
|
38
|
+
* Rotation of a camera at `eye` looking at `target` (a three.js camera looks down its -Z),
|
|
39
|
+
* the same as `camera.lookAt(target)` with `up` as the up vector.
|
|
40
|
+
*/
|
|
41
|
+
export declare function lookAtQuaternion(eye: Vec3, target: Vec3, up?: Vec3): Quat;
|
|
42
|
+
/** The direction a camera with this rotation looks in (its -Z axis). */
|
|
43
|
+
export declare function forwardOf(q: Quat): Vec3;
|
|
44
|
+
export interface RecordedSample {
|
|
45
|
+
percent: number;
|
|
46
|
+
position: Vec3;
|
|
47
|
+
quaternion: Quat;
|
|
48
|
+
fov: number;
|
|
49
|
+
section?: string | null;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Turns a recorded scroll path (many samples) into a few editable keys: the two ends, every
|
|
53
|
+
* section change, and the samples where the path bends most, up to `maxKeys`. Each key looks
|
|
54
|
+
* `lookDistance` ahead along the recorded view direction.
|
|
55
|
+
*/
|
|
56
|
+
export declare function keysFromSamples(samples: RecordedSample[], lookDistance: number, maxKeys?: number): PathKey[];
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { PluginObj, PluginPass, types as T } from '@babel/core';
|
|
2
|
+
/** Key under `object.userData` where the source location is stored. */
|
|
3
|
+
export declare const SOURCE_KEY = "__depthview";
|
|
4
|
+
export interface DepthviewPluginOptions {
|
|
5
|
+
/** Defaults to `process.env.NODE_ENV === 'development'`. The plugin does nothing otherwise. */
|
|
6
|
+
enabled?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare const isThreeTag: (tag: string) => boolean;
|
|
9
|
+
interface State extends PluginPass {
|
|
10
|
+
dreiNames?: Set<string>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Babel plugin: adds `userData-__depthview={{ file, line, column, component }}` to every JSX
|
|
14
|
+
* three element so the Depthview inspector can map a live object back to its source.
|
|
15
|
+
* `line` is 1-based and `column` 0-based, pointing at the `<` of the element.
|
|
16
|
+
* Development only: with NODE_ENV=production the plugin changes nothing.
|
|
17
|
+
*/
|
|
18
|
+
export default function depthviewBabelPlugin(api: {
|
|
19
|
+
types: typeof T;
|
|
20
|
+
}, options?: DepthviewPluginOptions): PluginObj<State>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional scroll timeline. When provided, Depthview samples the camera path exactly by
|
|
3
|
+
* stepping `setProgress` instead of scrolling the page and recording.
|
|
4
|
+
*/
|
|
5
|
+
export interface DepthviewTimeline {
|
|
6
|
+
/** Current progress, 0..1. */
|
|
7
|
+
getProgress(): number;
|
|
8
|
+
/** Jump the scroll-linked animation to `progress` (0..1). Must not scroll the page. */
|
|
9
|
+
setProgress(progress: number): void;
|
|
10
|
+
/** Optional label of the page section shown at `progress`. */
|
|
11
|
+
getSection?(progress: number): string | null;
|
|
12
|
+
}
|
|
13
|
+
export interface DepthviewBridgeProps {
|
|
14
|
+
timeline?: DepthviewTimeline;
|
|
15
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
export interface DepthviewViteOptions {
|
|
3
|
+
/** Files to transform. Defaults to .jsx and .tsx outside node_modules. */
|
|
4
|
+
include?: RegExp;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Vite plugin: adds source-location metadata to JSX three elements.
|
|
8
|
+
* `apply: 'serve'` means it only exists in the dev server; `vite build` never loads it.
|
|
9
|
+
* It runs before the React plugin so line/column numbers match the file on disk.
|
|
10
|
+
*/
|
|
11
|
+
export declare function depthviewVite(options?: DepthviewViteOptions): Plugin;
|
|
12
|
+
export default depthviewVite;
|
package/dist/vite.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// src/vite.ts
|
|
2
|
+
import { transformAsync } from "@babel/core";
|
|
3
|
+
|
|
4
|
+
// src/plugin.ts
|
|
5
|
+
var SOURCE_KEY = "__depthview";
|
|
6
|
+
var ATTR = `userData-${SOURCE_KEY}`;
|
|
7
|
+
var OBJECT_TAGS = /* @__PURE__ */ new Set([
|
|
8
|
+
"mesh",
|
|
9
|
+
"group",
|
|
10
|
+
"points",
|
|
11
|
+
"sprite",
|
|
12
|
+
"scene",
|
|
13
|
+
"bone",
|
|
14
|
+
"lOD",
|
|
15
|
+
"object3D",
|
|
16
|
+
"line",
|
|
17
|
+
"lineLoop",
|
|
18
|
+
"lineSegments",
|
|
19
|
+
"instancedMesh",
|
|
20
|
+
"skinnedMesh",
|
|
21
|
+
"batchedMesh"
|
|
22
|
+
]);
|
|
23
|
+
var OBJECT_SUFFIX = /(Light|Camera|Material|Helper)$/;
|
|
24
|
+
var DREI_COMPONENTS = /* @__PURE__ */ new Set([
|
|
25
|
+
"PerspectiveCamera",
|
|
26
|
+
"OrthographicCamera",
|
|
27
|
+
"Box",
|
|
28
|
+
"Sphere",
|
|
29
|
+
"Plane",
|
|
30
|
+
"Circle",
|
|
31
|
+
"Cone",
|
|
32
|
+
"Cylinder",
|
|
33
|
+
"Torus",
|
|
34
|
+
"TorusKnot",
|
|
35
|
+
"Ring",
|
|
36
|
+
"RoundedBox",
|
|
37
|
+
"Icosahedron",
|
|
38
|
+
"Octahedron",
|
|
39
|
+
"Dodecahedron",
|
|
40
|
+
"Tetrahedron",
|
|
41
|
+
"Capsule"
|
|
42
|
+
]);
|
|
43
|
+
var isThreeTag = (tag) => OBJECT_TAGS.has(tag) || OBJECT_SUFFIX.test(tag);
|
|
44
|
+
function componentName(path) {
|
|
45
|
+
for (let p = path.parentPath; p; p = p.parentPath) {
|
|
46
|
+
let name;
|
|
47
|
+
if (p.isFunctionDeclaration() || p.isClassDeclaration()) name = p.node.id?.name;
|
|
48
|
+
else if (p.isArrowFunctionExpression() || p.isFunctionExpression() || p.isClassExpression()) {
|
|
49
|
+
const parent = p.parentPath;
|
|
50
|
+
if (parent?.isVariableDeclarator() && parent.node.id.type === "Identifier")
|
|
51
|
+
name = parent.node.id.name;
|
|
52
|
+
else if (parent?.isCallExpression()) {
|
|
53
|
+
const outer = parent.parentPath;
|
|
54
|
+
if (outer?.isVariableDeclarator() && outer.node.id.type === "Identifier")
|
|
55
|
+
name = outer.node.id.name;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (name && /^[A-Z]/.test(name)) return name;
|
|
59
|
+
}
|
|
60
|
+
return "";
|
|
61
|
+
}
|
|
62
|
+
function depthviewBabelPlugin(api, options = {}) {
|
|
63
|
+
const t = api.types;
|
|
64
|
+
const enabled = options.enabled ?? process.env.NODE_ENV === "development";
|
|
65
|
+
return {
|
|
66
|
+
name: "depthview-source-locations",
|
|
67
|
+
visitor: {
|
|
68
|
+
Program(path, state) {
|
|
69
|
+
if (!enabled) return;
|
|
70
|
+
const names = /* @__PURE__ */ new Set();
|
|
71
|
+
for (const node of path.node.body) {
|
|
72
|
+
if (node.type !== "ImportDeclaration" || node.source.value !== "@react-three/drei")
|
|
73
|
+
continue;
|
|
74
|
+
for (const spec of node.specifiers) {
|
|
75
|
+
if (spec.type !== "ImportSpecifier") continue;
|
|
76
|
+
const imported = spec.imported.type === "Identifier" ? spec.imported.name : spec.imported.value;
|
|
77
|
+
if (DREI_COMPONENTS.has(imported)) names.add(spec.local.name);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
state.dreiNames = names;
|
|
81
|
+
},
|
|
82
|
+
JSXOpeningElement(path, state) {
|
|
83
|
+
if (!enabled) return;
|
|
84
|
+
const { node } = path;
|
|
85
|
+
if (node.name.type !== "JSXIdentifier" || !node.loc) return;
|
|
86
|
+
const tag = node.name.name;
|
|
87
|
+
if (!(isThreeTag(tag) || state.dreiNames?.has(tag))) return;
|
|
88
|
+
if (node.attributes.some((a) => a.type === "JSXAttribute" && a.name.name === ATTR)) return;
|
|
89
|
+
const meta = t.objectExpression([
|
|
90
|
+
t.objectProperty(t.identifier("file"), t.stringLiteral(state.filename ?? "unknown")),
|
|
91
|
+
t.objectProperty(t.identifier("line"), t.numericLiteral(node.loc.start.line)),
|
|
92
|
+
t.objectProperty(t.identifier("column"), t.numericLiteral(node.loc.start.column)),
|
|
93
|
+
t.objectProperty(t.identifier("component"), t.stringLiteral(componentName(path)))
|
|
94
|
+
]);
|
|
95
|
+
node.attributes.push(t.jsxAttribute(t.jsxIdentifier(ATTR), t.jsxExpressionContainer(meta)));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/vite.ts
|
|
102
|
+
function depthviewVite(options = {}) {
|
|
103
|
+
const include = options.include ?? /\.[jt]sx$/;
|
|
104
|
+
return {
|
|
105
|
+
name: "depthview:source-locations",
|
|
106
|
+
enforce: "pre",
|
|
107
|
+
apply: "serve",
|
|
108
|
+
async transform(code, id) {
|
|
109
|
+
const file = id.split("?")[0];
|
|
110
|
+
if (!include.test(file) || file.includes("/node_modules/") || !code.includes("<"))
|
|
111
|
+
return null;
|
|
112
|
+
const result = await transformAsync(code, {
|
|
113
|
+
filename: file,
|
|
114
|
+
babelrc: false,
|
|
115
|
+
configFile: false,
|
|
116
|
+
sourceMaps: true,
|
|
117
|
+
parserOpts: { plugins: file.endsWith(".tsx") ? ["jsx", "typescript"] : ["jsx"] },
|
|
118
|
+
plugins: [[depthviewBabelPlugin, { enabled: true }]]
|
|
119
|
+
});
|
|
120
|
+
return result?.code ? { code: result.code, map: result.map } : null;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
var vite_default = depthviewVite;
|
|
125
|
+
export {
|
|
126
|
+
vite_default as default,
|
|
127
|
+
depthviewVite
|
|
128
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "depthview-r3f-bridge",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Development-only bridge between a React Three Fiber project and the Depthview inspector: source locations for JSX three elements and exact scene/scroll access.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/himavamsi12/depthview.git",
|
|
9
|
+
"directory": "packages/r3f-bridge"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/himavamsi12/depthview/tree/main/packages/r3f-bridge#readme",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/types/index.dev.d.ts",
|
|
21
|
+
"development": "./dist/index.dev.js",
|
|
22
|
+
"default": "./dist/index.prod.js"
|
|
23
|
+
},
|
|
24
|
+
"./camera-path": {
|
|
25
|
+
"types": "./dist/types/cameraPath.d.ts",
|
|
26
|
+
"default": "./dist/cameraPath.js"
|
|
27
|
+
},
|
|
28
|
+
"./vite": {
|
|
29
|
+
"types": "./dist/types/vite.d.ts",
|
|
30
|
+
"default": "./dist/vite.js"
|
|
31
|
+
},
|
|
32
|
+
"./babel": {
|
|
33
|
+
"types": "./dist/types/babel.d.ts",
|
|
34
|
+
"require": "./dist/babel.cjs",
|
|
35
|
+
"default": "./dist/babel.cjs"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "node build.mjs && tsc -p tsconfig.build.json",
|
|
40
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
41
|
+
"test": "vitest run"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@react-three/fiber": ">=8",
|
|
45
|
+
"react": ">=18",
|
|
46
|
+
"vite": ">=5",
|
|
47
|
+
"three": ">=0.150"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"vite": {
|
|
51
|
+
"optional": true
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@babel/core": "^7.28.0"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@react-three/fiber": "^8.18.0",
|
|
59
|
+
"@types/babel__core": "^7.20.5",
|
|
60
|
+
"@types/node": "^24.0.0",
|
|
61
|
+
"@types/react": "^18.3.12",
|
|
62
|
+
"@types/three": "^0.187.0",
|
|
63
|
+
"esbuild": "^0.25.0",
|
|
64
|
+
"react": "^18.3.1",
|
|
65
|
+
"three": "^0.187.0",
|
|
66
|
+
"typescript": "~5.9.3",
|
|
67
|
+
"vite": "^7.3.8",
|
|
68
|
+
"vitest": "^3.2.7"
|
|
69
|
+
}
|
|
70
|
+
}
|