depthview-r3f-bridge 0.1.0 → 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/dist/path.js ADDED
@@ -0,0 +1,181 @@
1
+ // src/path.ts
2
+ var add = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];
3
+ var sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
4
+ var scale = (a, s) => [a[0] * s, a[1] * s, a[2] * s];
5
+ var len = (a) => Math.hypot(a[0], a[1], a[2]);
6
+ var norm = (a) => {
7
+ const l = len(a);
8
+ return l > 1e-12 ? scale(a, 1 / l) : [0, 0, 0];
9
+ };
10
+ var cross = (a, b) => [
11
+ a[1] * b[2] - a[2] * b[1],
12
+ a[2] * b[0] - a[0] * b[2],
13
+ a[0] * b[1] - a[1] * b[0]
14
+ ];
15
+ function pathKeys(input) {
16
+ const list = Array.isArray(input) ? input : input && typeof input === "object" && Array.isArray(input.keys) ? input.keys : [];
17
+ const vec = (v) => Array.isArray(v) && v.length === 3 && v.every((n) => typeof n === "number" && Number.isFinite(n));
18
+ const out = [];
19
+ for (const item of list) {
20
+ const k = item;
21
+ if (!k || typeof k.percent !== "number" || !vec(k.position) || !vec(k.target)) continue;
22
+ out.push({
23
+ percent: k.percent,
24
+ position: [...k.position],
25
+ target: [...k.target],
26
+ fov: typeof k.fov === "number" && Number.isFinite(k.fov) ? k.fov : 50
27
+ });
28
+ }
29
+ return out;
30
+ }
31
+ function sortKeys(keys) {
32
+ const sorted = [...keys].sort((a, b) => a.percent - b.percent);
33
+ return sorted.filter((k, i) => i === 0 || k.percent - sorted[i - 1].percent > 1e-6);
34
+ }
35
+ 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;
36
+ function samePose(a, b) {
37
+ return sameVec(a.position, b.position) && sameVec(a.target, b.target) && Math.abs(a.fov - b.fov) < 1e-6;
38
+ }
39
+ function catmull(p0, p1, p2, p3, t) {
40
+ const t2 = t * t;
41
+ const t3 = t2 * t;
42
+ const out = [0, 0, 0];
43
+ for (let i = 0; i < 3; i++) {
44
+ 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);
45
+ }
46
+ return out;
47
+ }
48
+ function evaluatePath(input, percent) {
49
+ const keys = sortKeys(input);
50
+ if (!keys.length) return null;
51
+ const first = keys[0];
52
+ const last = keys[keys.length - 1];
53
+ const pick = (k) => ({
54
+ position: [...k.position],
55
+ target: [...k.target],
56
+ fov: k.fov
57
+ });
58
+ if (keys.length === 1 || percent <= first.percent) return pick(first);
59
+ if (percent >= last.percent) return pick(last);
60
+ let i = 0;
61
+ while (i < keys.length - 2 && percent > keys[i + 1].percent) i++;
62
+ const k1 = keys[i];
63
+ const k2 = keys[i + 1];
64
+ if (samePose(k1, k2)) return pick(k1);
65
+ const before = keys[i - 1];
66
+ const after = keys[i + 2];
67
+ const k0 = before && !samePose(before, k1) ? before : void 0;
68
+ const k3 = after && !samePose(after, k2) ? after : void 0;
69
+ const t = (percent - k1.percent) / (k2.percent - k1.percent);
70
+ const ends = (field) => [
71
+ k0 ? k0[field] : sub(scale(k1[field], 2), k2[field]),
72
+ k3 ? k3[field] : sub(scale(k2[field], 2), k1[field])
73
+ ];
74
+ const [p0, p3] = ends("position");
75
+ const [q0, q3] = ends("target");
76
+ return {
77
+ position: catmull(p0, k1.position, k2.position, p3, t),
78
+ target: catmull(q0, k1.target, k2.target, q3, t),
79
+ fov: k1.fov + (k2.fov - k1.fov) * t
80
+ };
81
+ }
82
+ function pathPoints(keys, perSegment = 24) {
83
+ const sorted = sortKeys(keys);
84
+ if (sorted.length < 2) return sorted.map((k) => evaluatePath(sorted, k.percent));
85
+ const out = [];
86
+ for (let i = 0; i < sorted.length - 1; i++) {
87
+ const a = sorted[i].percent;
88
+ const b = sorted[i + 1].percent;
89
+ for (let s = 0; s < perSegment; s++)
90
+ out.push(evaluatePath(sorted, a + (b - a) * s / perSegment));
91
+ }
92
+ out.push(evaluatePath(sorted, sorted[sorted.length - 1].percent));
93
+ return out;
94
+ }
95
+ function lookAtQuaternion(eye, target, up = [0, 1, 0]) {
96
+ let z = norm(sub(eye, target));
97
+ if (len(z) === 0) z = [0, 0, 1];
98
+ let x = norm(cross(up, z));
99
+ if (len(x) === 0) {
100
+ const nudged = Math.abs(up[2]) === 1 ? [z[0] + 1e-4, z[1], z[2]] : [z[0], z[1], z[2] + 1e-4];
101
+ x = norm(cross(up, norm(nudged)));
102
+ }
103
+ const y = cross(z, x);
104
+ const m00 = x[0], m01 = y[0], m02 = z[0];
105
+ const m10 = x[1], m11 = y[1], m12 = z[1];
106
+ const m20 = x[2], m21 = y[2], m22 = z[2];
107
+ const trace = m00 + m11 + m22;
108
+ if (trace > 0) {
109
+ const s2 = 0.5 / Math.sqrt(trace + 1);
110
+ return [(m21 - m12) * s2, (m02 - m20) * s2, (m10 - m01) * s2, 0.25 / s2];
111
+ }
112
+ if (m00 > m11 && m00 > m22) {
113
+ const s2 = 2 * Math.sqrt(1 + m00 - m11 - m22);
114
+ return [0.25 * s2, (m01 + m10) / s2, (m02 + m20) / s2, (m21 - m12) / s2];
115
+ }
116
+ if (m11 > m22) {
117
+ const s2 = 2 * Math.sqrt(1 + m11 - m00 - m22);
118
+ return [(m01 + m10) / s2, 0.25 * s2, (m12 + m21) / s2, (m02 - m20) / s2];
119
+ }
120
+ const s = 2 * Math.sqrt(1 + m22 - m00 - m11);
121
+ return [(m02 + m20) / s, (m12 + m21) / s, 0.25 * s, (m10 - m01) / s];
122
+ }
123
+ function forwardOf(q) {
124
+ const [x, y, z, w] = q;
125
+ return [-(2 * (x * z + w * y)), -(2 * (y * z - w * x)), -(1 - 2 * (x * x + y * y))];
126
+ }
127
+ function toSegment(p, a, b) {
128
+ const ab = sub(b, a);
129
+ const l2 = ab[0] ** 2 + ab[1] ** 2 + ab[2] ** 2;
130
+ if (l2 === 0) return len(sub(p, a));
131
+ const ap = sub(p, a);
132
+ const t = Math.max(0, Math.min(1, (ap[0] * ab[0] + ap[1] * ab[1] + ap[2] * ab[2]) / l2));
133
+ return len(sub(p, add(a, scale(ab, t))));
134
+ }
135
+ function keysFromSamples(samples, lookDistance, maxKeys = 10) {
136
+ if (!samples.length) return [];
137
+ const chosen = /* @__PURE__ */ new Set([0, samples.length - 1]);
138
+ for (let i = 1; i < samples.length; i++)
139
+ if ((samples[i].section ?? null) !== (samples[i - 1].section ?? null)) chosen.add(i);
140
+ const extent = Math.max(1e-9, ...samples.map((s) => len(sub(s.position, samples[0].position))));
141
+ while (chosen.size < Math.min(maxKeys, samples.length)) {
142
+ const ordered = [...chosen].sort((a, b) => a - b);
143
+ let best = -1;
144
+ let bestDistance = extent * 0.01;
145
+ for (let k = 0; k < ordered.length - 1; k++) {
146
+ const a = ordered[k];
147
+ const b = ordered[k + 1];
148
+ for (let i = a + 1; i < b; i++) {
149
+ const d = toSegment(samples[i].position, samples[a].position, samples[b].position);
150
+ if (d > bestDistance) {
151
+ bestDistance = d;
152
+ best = i;
153
+ }
154
+ }
155
+ }
156
+ if (best < 0) break;
157
+ chosen.add(best);
158
+ }
159
+ const reach = lookDistance > 0 ? lookDistance : 1;
160
+ const r = (n) => Math.round(n * 1e3) / 1e3;
161
+ const rv = (v) => [r(v[0]), r(v[1]), r(v[2])];
162
+ return [...chosen].sort((a, b) => a - b).slice(0, Math.max(2, maxKeys)).map((i) => {
163
+ const s = samples[i];
164
+ return {
165
+ percent: r(s.percent),
166
+ position: rv(s.position),
167
+ target: rv(add(s.position, scale(forwardOf(s.quaternion), reach))),
168
+ fov: r(s.fov)
169
+ };
170
+ });
171
+ }
172
+ export {
173
+ evaluatePath,
174
+ forwardOf,
175
+ keysFromSamples,
176
+ lookAtQuaternion,
177
+ pathKeys,
178
+ pathPoints,
179
+ samePose,
180
+ sortKeys
181
+ };
@@ -1,6 +1,6 @@
1
1
  import { type CameraPathFile, type PathKey } from './path';
2
2
  export type { CameraPathFile, PathKey, PathPose, Vec3, Quat } from './path';
3
- export { evaluatePath, lookAtQuaternion, pathPoints } from './path';
3
+ export { evaluatePath, lookAtQuaternion, pathKeys, pathPoints } from './path';
4
4
  export interface CameraPathOptions {
5
5
  /**
6
6
  * Scroll progress from 0 (top) to 1 (bottom). Default: the window's scroll position.
@@ -10,6 +10,10 @@ export interface CameraPathOptions {
10
10
  /** Off: the camera is left alone (for example while another camera move plays). */
11
11
  enabled?: boolean;
12
12
  }
13
+ /** A path as written by hand, or a camera-path.json as imported (typed loosely by TypeScript). */
14
+ export type CameraPathInput = CameraPathFile | PathKey[] | {
15
+ keys: unknown[];
16
+ };
13
17
  /**
14
18
  * Moves the default camera along a Depthview camera path as the page scrolls. Place it inside
15
19
  * <Canvas>, in place of your own scroll camera code:
@@ -19,8 +23,8 @@ export interface CameraPathOptions {
19
23
  *
20
24
  * Unlike <DepthviewBridge />, this is meant for production: it is plain three.js maths.
21
25
  */
22
- export declare function useCameraPath(path: CameraPathFile | PathKey[], { progress, enabled }?: CameraPathOptions): void;
26
+ export declare function useCameraPath(path: CameraPathInput, { progress, enabled }?: CameraPathOptions): void;
23
27
  /** Component form of useCameraPath. */
24
28
  export declare function CameraPath({ path, ...options }: CameraPathOptions & {
25
- path: CameraPathFile | PathKey[];
29
+ path: CameraPathInput;
26
30
  }): null;
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Site controls: values of your scene that you want to tune from the Depthview inspector (or
3
+ * let your AI agent tune through MCP), declared where they are used:
4
+ *
5
+ * const coin = depthviewControls('Coin', {
6
+ * roughness: { value: 0.32, min: 0, max: 1 },
7
+ * spin: true,
8
+ * tint: '#c9ccd1',
9
+ * });
10
+ * material.roughness = coin.roughness; // read it each frame, or pass onChange
11
+ *
12
+ * Depthview shows them as a panel named after the group. Changing one updates the page at
13
+ * once; "Save to code" writes the new value back into this call. Without the inspector the
14
+ * values are simply the ones written here. A production build gets a stub that returns them
15
+ * and nothing more (package.json "exports" picks it), so no interface ships with the site.
16
+ */
17
+ export type ControlValue = number | boolean | string;
18
+ export type ControlInput = ControlValue | {
19
+ value: ControlValue;
20
+ min?: number;
21
+ max?: number;
22
+ step?: number;
23
+ label?: string;
24
+ };
25
+ export type ControlSchema = Record<string, ControlInput>;
26
+ export type ControlValues<S extends ControlSchema> = {
27
+ [K in keyof S]: S[K] extends {
28
+ value: infer V;
29
+ } ? Widen<V> : Widen<S[K]>;
30
+ };
31
+ type Widen<V> = V extends number ? number : V extends boolean ? boolean : V extends string ? string : V;
32
+ export interface ControlInfo {
33
+ group: string;
34
+ key: string;
35
+ type: 'number' | 'boolean' | 'color' | 'text';
36
+ value: ControlValue;
37
+ /** The value written in the code. */
38
+ initial: ControlValue;
39
+ min?: number;
40
+ max?: number;
41
+ step?: number;
42
+ label?: string;
43
+ }
44
+ interface Group {
45
+ values: Record<string, ControlValue>;
46
+ info: Map<string, ControlInfo>;
47
+ listeners: Set<(key: string) => void>;
48
+ }
49
+ export interface ControlRegistry {
50
+ version: 1;
51
+ list(): ControlInfo[];
52
+ /** Sets one value. False when there is no such control or the value has the wrong type. */
53
+ set(group: string, key: string, value: ControlValue): boolean;
54
+ /** Told about every change, from the inspector or from code. */
55
+ subscribe(listener: (group: string, key: string) => void): () => void;
56
+ groups: Map<string, Group>;
57
+ listeners: Set<(group: string, key: string) => void>;
58
+ }
59
+ /** One registry per page, shared by every copy of this module. */
60
+ export declare function controlRegistry(): ControlRegistry;
61
+ /**
62
+ * Declares a group of controls and returns their live values: the same object every time,
63
+ * updated in place, so `controls.roughness` read in a render loop is always current.
64
+ * `onChange` is called with the key that changed.
65
+ */
66
+ export declare function depthviewControls<S extends ControlSchema>(group: string, schema: S, onChange?: (values: ControlValues<S>, key: keyof S) => void): ControlValues<S>;
67
+ /** Subscribes to one group. Returns how to stop. Used by the React hook. */
68
+ export declare function watchControls(group: string, listener: (key: string) => void): () => void;
69
+ export {};
@@ -0,0 +1,8 @@
1
+ import type { ControlSchema, ControlValues } from './controls';
2
+ export type { ControlInfo, ControlInput, ControlSchema, ControlValue, ControlValues, } from './controls';
3
+ /**
4
+ * Production form of the site controls: the values written in the code, nothing else. No
5
+ * registry, no global, nothing for an inspector to talk to.
6
+ */
7
+ export declare function depthviewControls<S extends ControlSchema>(_group: string, schema: S, onChange?: (values: ControlValues<S>, key: keyof S) => void): ControlValues<S>;
8
+ export declare function watchControls(): () => void;
@@ -0,0 +1,14 @@
1
+ import { type ControlSchema, type ControlValues } from './controls';
2
+ export { depthviewControls, controlRegistry } from './controls';
3
+ export type { ControlInfo, ControlInput, ControlSchema, ControlValue, ControlValues, } from './controls';
4
+ /**
5
+ * React form of depthviewControls: the component renders again when a value is changed from
6
+ * the Depthview inspector.
7
+ *
8
+ * const { roughness, bevel } = useDepthviewControls('Coin', {
9
+ * roughness: { value: 0.32, min: 0, max: 1 },
10
+ * bevel: { value: 0.02, min: 0, max: 0.1 },
11
+ * });
12
+ * <meshStandardMaterial roughness={roughness} />
13
+ */
14
+ export declare function useDepthviewControls<S extends ControlSchema>(group: string, schema: S): ControlValues<S>;
@@ -0,0 +1,5 @@
1
+ import type { ControlSchema, ControlValues } from './controls';
2
+ export { depthviewControls } from './controls.prod';
3
+ export type { ControlInfo, ControlInput, ControlSchema, ControlValue, ControlValues, } from './controls';
4
+ /** Production form of the hook: the values written in the code. */
5
+ export declare function useDepthviewControls<S extends ControlSchema>(group: string, schema: S): ControlValues<S>;