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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # depthview-r3f-bridge
2
2
 
3
- Development-only bridge between a React Three Fiber project and the Depthview browser extension.
3
+ Bridge between a three.js / React Three Fiber project and the Depthview browser extension.
4
4
 
5
5
  - **Source locations.** A build plugin adds `userData.__depthview = { file, line, column, component }`
6
6
  to every JSX three element (`<mesh>`, `<directionalLight>`, `<meshStandardMaterial>`, drei cameras…),
@@ -8,8 +8,17 @@ Development-only bridge between a React Three Fiber project and the Depthview br
8
8
  - **Exact scene + scroll access.** `<DepthviewBridge />` hands the R3F store to the extension and,
9
9
  if you pass a `timeline`, lets it sample the camera path exactly instead of recording a scroll.
10
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.
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`.
13
22
 
14
23
  ## Vite
15
24
 
@@ -21,6 +30,10 @@ import react from '@vitejs/plugin-react';
21
30
  export default defineConfig({ plugins: [depthviewVite(), react()] });
22
31
  ```
23
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
+
24
37
  ## Next.js (Babel)
25
38
 
26
39
  ```js
@@ -58,9 +71,93 @@ const scroll = useScroll();
58
71
  useCameraPath(path, { progress: () => scroll.offset });
59
72
  ```
60
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
+
61
77
  `<CameraPath path={path} />` is the same as a component. `evaluatePath(keys, percent)` returns
62
78
  the pose at a scroll position if you drive the camera yourself.
63
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
+
64
161
  ## Bridge component
65
162
 
66
163
  ```tsx
@@ -15,10 +15,30 @@ var cross = (a, b) => [
15
15
  a[2] * b[0] - a[0] * b[2],
16
16
  a[0] * b[1] - a[1] * b[0]
17
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
+ }
18
34
  function sortKeys(keys) {
19
35
  const sorted = [...keys].sort((a, b) => a.percent - b.percent);
20
36
  return sorted.filter((k, i) => i === 0 || k.percent - sorted[i - 1].percent > 1e-6);
21
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
+ }
22
42
  function catmull(p0, p1, p2, p3, t) {
23
43
  const t2 = t * t;
24
44
  const t3 = t2 * t;
@@ -44,8 +64,11 @@ function evaluatePath(input, percent) {
44
64
  while (i < keys.length - 2 && percent > keys[i + 1].percent) i++;
45
65
  const k1 = keys[i];
46
66
  const k2 = keys[i + 1];
47
- const k0 = keys[i - 1];
48
- const k3 = keys[i + 2];
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;
49
72
  const t = (percent - k1.percent) / (k2.percent - k1.percent);
50
73
  const ends = (field) => [
51
74
  k0 ? k0[field] : sub(scale(k1[field], 2), k2[field]),
@@ -107,11 +130,14 @@ function windowProgress() {
107
130
  const max = el.scrollHeight - el.clientHeight;
108
131
  return max > 0 ? Math.min(1, Math.max(0, el.scrollTop / max)) : 0;
109
132
  }
110
- var keysOf = (path) => Array.isArray(path) ? path : path.keys;
111
133
  function useCameraPath(path, { progress = windowProgress, enabled = true } = {}) {
112
134
  const camera = useThree((s) => s.camera);
113
- const keys = useRef(keysOf(path));
114
- keys.current = keysOf(path);
135
+ const source = useRef(null);
136
+ const keys = useRef([]);
137
+ if (source.current !== path) {
138
+ source.current = path;
139
+ keys.current = pathKeys(path);
140
+ }
115
141
  useFrame(() => {
116
142
  if (!enabled) return;
117
143
  const pose = evaluatePath(keys.current, progress() * 100);
@@ -136,6 +162,7 @@ export {
136
162
  CameraPath,
137
163
  evaluatePath,
138
164
  lookAtQuaternion,
165
+ pathKeys,
139
166
  pathPoints,
140
167
  useCameraPath
141
168
  };
@@ -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
+ };