@joplo-app/plugin-3d 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/LICENSE +97 -0
- package/README.md +19 -2
- package/dist/guide.d.ts +3 -0
- package/dist/guide.js +96 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +39 -0
- package/dist/label.d.ts +2 -0
- package/dist/label.js +5 -0
- package/dist/local.d.ts +35 -0
- package/dist/local.js +199 -0
- package/dist/model.d.ts +5 -0
- package/dist/model.js +46 -0
- package/dist/request.d.ts +33 -0
- package/dist/request.js +33 -0
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.js +863 -0
- package/dist/version.json +3 -0
- package/package.json +45 -3
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,863 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @joplo-app/plugin-3d 运行时(浏览器端):用 three.js 画 <Scene3D>。
|
|
3
|
+
*
|
|
4
|
+
* - 子元素(Object / Target)是数据:形状、位置、尺寸、颜色,和随状态变化的 lit / open / 可见 / 禁用。
|
|
5
|
+
* 结构属性变了就重建这个物体;随状态变化的属性平滑过渡(frame.animate 为 false 时直接到终点)。
|
|
6
|
+
* - 每个可见子元素在 DOM 里有一个锚点(带 node.attrs),摆在它投影到画面上的包围盒处:
|
|
7
|
+
* Joplo 的光标、镜头、聚光、圈注、⌥+单击检查都按锚点定位。锚点不接收指针事件。
|
|
8
|
+
* - 观众拖动 = 旋转视角(只影响这位观众的画面);单击 = 射线命中的子元素交给 ctx.click,由引擎判断。
|
|
9
|
+
* - 颜色读主题变量(--jp-*),主题变化时重新上色;描边用墨色线条,和手绘 / 线框主题一致。
|
|
10
|
+
*/
|
|
11
|
+
import * as THREE from 'three';
|
|
12
|
+
import { loadModel, disposeModel, modelStructureKey } from "./model.js";
|
|
13
|
+
import { visibleLabel } from "./label.js";
|
|
14
|
+
import { defineRuntime } from '@joplo-app/kit/plugin';
|
|
15
|
+
const ROLES = ['ink', 'bg', 'surface', 'subtle', 'line', 'muted', 'accent', 'info', 'ok', 'warn', 'danger'];
|
|
16
|
+
const FALLBACK = { ink: '#1e1e1e', bg: '#fafaf7', surface: '#ffffff', subtle: '#f1f0ec', line: '#d8d6cf', muted: '#6b6b6b', accent: '#1971c2', info: '#4dabf7', ok: '#2f9e44', warn: '#f08c00', danger: '#e03131' };
|
|
17
|
+
/** 预设尺寸(米);base = 底部中心在 at(落地家具),center = 中心在 at(墙、窗、挂墙设备)。 */
|
|
18
|
+
const PRESET = {
|
|
19
|
+
box: { size: [1, 1, 1], anchor: 'base', color: 'surface' },
|
|
20
|
+
cylinder: { size: [1, 1, 1], anchor: 'base', color: 'surface' },
|
|
21
|
+
sphere: { size: [1, 1, 1], anchor: 'base', color: 'surface' },
|
|
22
|
+
cone: { size: [1, 1, 1], anchor: 'base', color: 'surface' },
|
|
23
|
+
floor: { size: [6, 0.1, 6], anchor: 'base', color: 'subtle' },
|
|
24
|
+
wall: { size: [6, 3, 0.1], anchor: 'center', color: 'surface' },
|
|
25
|
+
lamp: { size: [0.5, 1.7, 0.5], anchor: 'base', color: 'surface' },
|
|
26
|
+
door: { size: [0.9, 2.1, 0.08], anchor: 'base', color: 'surface' },
|
|
27
|
+
window: { size: [1.4, 1.1, 0.08], anchor: 'center', color: 'surface' },
|
|
28
|
+
sofa: { size: [2, 0.8, 0.9], anchor: 'base', color: 'subtle' },
|
|
29
|
+
table: { size: [1.2, 0.75, 0.8], anchor: 'base', color: 'surface' },
|
|
30
|
+
bed: { size: [1.6, 0.6, 2.1], anchor: 'base', color: 'surface' },
|
|
31
|
+
tv: { size: [1.4, 0.85, 0.08], anchor: 'center', color: 'ink' },
|
|
32
|
+
plant: { size: [0.5, 1, 0.5], anchor: 'base', color: 'ok' },
|
|
33
|
+
speaker: { size: [0.3, 0.5, 0.3], anchor: 'base', color: 'ink' },
|
|
34
|
+
ac: { size: [0.9, 0.3, 0.25], anchor: 'center', color: 'surface' },
|
|
35
|
+
};
|
|
36
|
+
const num = (v, d) => (typeof v === 'number' && Number.isFinite(v) ? v : d);
|
|
37
|
+
const vec = (v, d) => (Array.isArray(v) && v.length === 3 && v.every((x) => typeof x === 'number' && Number.isFinite(x)) ? v : d);
|
|
38
|
+
const DEG = Math.PI / 180;
|
|
39
|
+
const CAMERA_MS = 700;
|
|
40
|
+
const ease = (t) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2);
|
|
41
|
+
/** 主题颜色:读 CSS 变量,用 2D canvas 规范化(color-mix 等写法也能得到 rgb)。 */
|
|
42
|
+
function themePalette(ctx) {
|
|
43
|
+
const c2d = document.createElement('canvas').getContext('2d');
|
|
44
|
+
const out = {};
|
|
45
|
+
for (const r of ROLES) {
|
|
46
|
+
const raw = ctx.css(`--jp-${r}`) || FALLBACK[r];
|
|
47
|
+
let norm = FALLBACK[r];
|
|
48
|
+
if (c2d) {
|
|
49
|
+
c2d.fillStyle = FALLBACK[r];
|
|
50
|
+
c2d.fillStyle = raw;
|
|
51
|
+
norm = String(c2d.fillStyle);
|
|
52
|
+
}
|
|
53
|
+
const col = new THREE.Color();
|
|
54
|
+
try {
|
|
55
|
+
col.setStyle(norm.replace(/^rgba\(([^,]+),([^,]+),([^,]+),[^)]+\)$/, 'rgb($1,$2,$3)'));
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
col.set(FALLBACK[r]);
|
|
59
|
+
}
|
|
60
|
+
out[r] = col;
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
/** 灯的光晕:径向渐变的白色贴图,颜色由材质决定。 */
|
|
65
|
+
let haloTex = null;
|
|
66
|
+
function haloTexture() {
|
|
67
|
+
if (haloTex)
|
|
68
|
+
return haloTex;
|
|
69
|
+
const c = document.createElement('canvas');
|
|
70
|
+
c.width = c.height = 128;
|
|
71
|
+
const g = c.getContext('2d');
|
|
72
|
+
const grd = g.createRadialGradient(64, 64, 0, 64, 64, 64);
|
|
73
|
+
grd.addColorStop(0, 'rgba(255,255,255,0.9)');
|
|
74
|
+
grd.addColorStop(0.35, 'rgba(255,255,255,0.35)');
|
|
75
|
+
grd.addColorStop(1, 'rgba(255,255,255,0)');
|
|
76
|
+
g.fillStyle = grd;
|
|
77
|
+
g.fillRect(0, 0, 128, 128);
|
|
78
|
+
haloTex = new THREE.CanvasTexture(c);
|
|
79
|
+
return haloTex;
|
|
80
|
+
}
|
|
81
|
+
/** 卡通明暗的三级色阶。 */
|
|
82
|
+
function toonGradient() {
|
|
83
|
+
const data = new Uint8Array([150, 150, 150, 255, 210, 210, 210, 255, 255, 255, 255, 255]);
|
|
84
|
+
const t = new THREE.DataTexture(data, 3, 1, THREE.RGBAFormat);
|
|
85
|
+
t.minFilter = THREE.NearestFilter;
|
|
86
|
+
t.magFilter = THREE.NearestFilter;
|
|
87
|
+
t.needsUpdate = true;
|
|
88
|
+
return t;
|
|
89
|
+
}
|
|
90
|
+
class SceneView {
|
|
91
|
+
el;
|
|
92
|
+
ctx;
|
|
93
|
+
renderer;
|
|
94
|
+
scene = new THREE.Scene();
|
|
95
|
+
camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 400);
|
|
96
|
+
world = new THREE.Group();
|
|
97
|
+
overlay;
|
|
98
|
+
items = new Map();
|
|
99
|
+
gradient = toonGradient();
|
|
100
|
+
palette;
|
|
101
|
+
theme = '';
|
|
102
|
+
width = 1;
|
|
103
|
+
height = 1;
|
|
104
|
+
/** 视角:方位角、仰角(弧度)、注视点、半高(正交相机的取景)。 */
|
|
105
|
+
view = { az: 45 * DEG, el: 32 * DEG, target: new THREE.Vector3(), half: 4 };
|
|
106
|
+
tween = null;
|
|
107
|
+
focusKey = null;
|
|
108
|
+
/** 观众拖动过视角:画框尺寸变化时不再自动重新取景,直到下一次 focus 变化。 */
|
|
109
|
+
userMoved = false;
|
|
110
|
+
framedOnce = false;
|
|
111
|
+
resetKey;
|
|
112
|
+
frozen = null;
|
|
113
|
+
gestureFrom = null;
|
|
114
|
+
last = 0;
|
|
115
|
+
hover = null;
|
|
116
|
+
drag = null;
|
|
117
|
+
ro;
|
|
118
|
+
raycaster = new THREE.Raycaster();
|
|
119
|
+
cleanup = [];
|
|
120
|
+
constructor(el, ctx) {
|
|
121
|
+
this.el = el;
|
|
122
|
+
this.ctx = ctx;
|
|
123
|
+
this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, preserveDrawingBuffer: true });
|
|
124
|
+
this.renderer.setPixelRatio(Math.min(2, window.devicePixelRatio || 1));
|
|
125
|
+
this.renderer.setClearColor(0x000000, 0);
|
|
126
|
+
const canvas = this.renderer.domElement;
|
|
127
|
+
canvas.style.cssText = 'display:block;width:100%;height:100%;touch-action:none;cursor:grab';
|
|
128
|
+
canvas.setAttribute('aria-label', String(ctx.node.props.label ?? ctx.node.label ?? '3D scene'));
|
|
129
|
+
this.overlay = document.createElement('div');
|
|
130
|
+
this.overlay.style.cssText = 'position:absolute;inset:0;pointer-events:none;overflow:hidden';
|
|
131
|
+
el.style.position = 'relative';
|
|
132
|
+
el.style.overflow = 'hidden';
|
|
133
|
+
el.append(canvas, this.overlay);
|
|
134
|
+
this.palette = themePalette(ctx);
|
|
135
|
+
this.theme = ctx.frame.theme;
|
|
136
|
+
this.scene.add(this.world);
|
|
137
|
+
const hemi = new THREE.HemisphereLight(0xffffff, 0xbbbbbb, 2.2);
|
|
138
|
+
const sun = new THREE.DirectionalLight(0xffffff, 1.6);
|
|
139
|
+
sun.position.set(6, 10, 8);
|
|
140
|
+
this.scene.add(hemi, sun);
|
|
141
|
+
canvas.dataset.jgestureView = '';
|
|
142
|
+
canvas.tabIndex = 0;
|
|
143
|
+
canvas.setAttribute('aria-label', String(ctx.node.props.label ?? ctx.node.label));
|
|
144
|
+
this.bindPointer(canvas);
|
|
145
|
+
this.ro = new ResizeObserver(() => this.resize());
|
|
146
|
+
this.ro.observe(el);
|
|
147
|
+
this.update(ctx);
|
|
148
|
+
}
|
|
149
|
+
// ---------- 引擎每次渲染调用 ----------
|
|
150
|
+
update(ctx) {
|
|
151
|
+
if (this.resetKey !== ctx.frame.resetKey && this.frozen) {
|
|
152
|
+
this.view = this.frozen.view;
|
|
153
|
+
this.tween = this.frozen.tween;
|
|
154
|
+
this.focusKey = this.frozen.focusKey;
|
|
155
|
+
for (const [id, value] of this.frozen.values) {
|
|
156
|
+
const item = this.items.get(id);
|
|
157
|
+
if (item)
|
|
158
|
+
item.cur = value;
|
|
159
|
+
}
|
|
160
|
+
this.userMoved = false;
|
|
161
|
+
this.frozen = null;
|
|
162
|
+
}
|
|
163
|
+
if (ctx.frame.playground && !this.frozen)
|
|
164
|
+
this.freezeView();
|
|
165
|
+
this.resetKey = ctx.frame.resetKey;
|
|
166
|
+
this.ctx = ctx;
|
|
167
|
+
const p = ctx.node.props;
|
|
168
|
+
this.el.style.height = `${Math.max(160, num(p.height, 420))}px`;
|
|
169
|
+
if (ctx.frame.theme !== this.theme) {
|
|
170
|
+
this.theme = ctx.frame.theme;
|
|
171
|
+
this.palette = themePalette(ctx);
|
|
172
|
+
for (const it of this.items.values())
|
|
173
|
+
this.repaint(it);
|
|
174
|
+
}
|
|
175
|
+
this.sync(ctx.node.children);
|
|
176
|
+
this.resize(false);
|
|
177
|
+
// 镜头:focus 变化(或第一次)时重新取景;用户拖动过的视角由导演接管
|
|
178
|
+
const focus = typeof p.focus === 'string' && this.items.has(p.focus) ? p.focus : null;
|
|
179
|
+
const key = `${focus ?? '*'}|${String(p.view ?? 'iso')}|${String(p.azimuth ?? '')}|${String(p.elevation ?? '')}`;
|
|
180
|
+
if (!this.framedOnce || key !== this.focusKey) {
|
|
181
|
+
const to = this.frameFor(focus, String(p.view ?? 'iso'));
|
|
182
|
+
if (!this.framedOnce || !ctx.frame.animate || !ctx.frame.clock || !ctx.frame.clock.running && !ctx.frame.clock.paused || ctx.frame.playground) {
|
|
183
|
+
this.view = to;
|
|
184
|
+
this.tween = null;
|
|
185
|
+
}
|
|
186
|
+
else
|
|
187
|
+
this.tween = { from: { ...this.view, target: this.view.target.clone() }, to, t0: ctx.frame.clock?.timeMs ?? 0 };
|
|
188
|
+
this.focusKey = key;
|
|
189
|
+
this.framedOnce = true;
|
|
190
|
+
this.userMoved = false;
|
|
191
|
+
}
|
|
192
|
+
if (!ctx.frame.animate || !ctx.frame.clock || !ctx.frame.clock.running && !ctx.frame.clock.paused || ctx.frame.playground) {
|
|
193
|
+
for (const it of this.items.values())
|
|
194
|
+
it.cur = { ...it.goal };
|
|
195
|
+
this.tween = null;
|
|
196
|
+
}
|
|
197
|
+
const gesture = ctx.frame.gesture;
|
|
198
|
+
if (gesture?.action.type === 'plugin' && gesture.action.name === 'scene3d:rotate' && gesture.action.target === ctx.node.id) {
|
|
199
|
+
// Audience orbit owns the view until the reset epoch. Retain the original
|
|
200
|
+
// gesture anchor so reset/resume restores the same in-flight orientation.
|
|
201
|
+
if (!this.userMoved) {
|
|
202
|
+
if (!this.gestureFrom)
|
|
203
|
+
this.gestureFrom = { ...this.view, target: this.view.target.clone() };
|
|
204
|
+
const data = gesture.action.data;
|
|
205
|
+
this.tween = null;
|
|
206
|
+
this.view = { ...this.gestureFrom, az: this.gestureFrom.az + (num(data.azimuth, 45) * DEG - this.gestureFrom.az) * gesture.progress, el: this.gestureFrom.el + (num(data.elevation, 32) * DEG - this.gestureFrom.el) * gesture.progress };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else
|
|
210
|
+
this.gestureFrom = null;
|
|
211
|
+
this.tick(ctx.frame.clock?.timeMs ?? 0, false);
|
|
212
|
+
}
|
|
213
|
+
freezeView() {
|
|
214
|
+
if (!this.frozen)
|
|
215
|
+
this.frozen = { focusKey: this.focusKey, view: { ...this.view, target: this.view.target.clone() }, tween: this.tween, values: new Map([...this.items].map(([id, item]) => [id, { ...item.cur }])) };
|
|
216
|
+
}
|
|
217
|
+
destroy() {
|
|
218
|
+
this.ro.disconnect();
|
|
219
|
+
for (const f of this.cleanup)
|
|
220
|
+
f();
|
|
221
|
+
for (const it of this.items.values())
|
|
222
|
+
this.dispose(it);
|
|
223
|
+
this.gradient.dispose();
|
|
224
|
+
this.renderer.dispose();
|
|
225
|
+
this.renderer.forceContextLoss?.();
|
|
226
|
+
}
|
|
227
|
+
// ---------- 子元素 ----------
|
|
228
|
+
sync(children) {
|
|
229
|
+
const seen = new Set();
|
|
230
|
+
for (const n of children) {
|
|
231
|
+
if (n.name !== 'Object' && n.name !== 'Target' && n.name !== 'Model')
|
|
232
|
+
continue;
|
|
233
|
+
seen.add(n.id);
|
|
234
|
+
const shape = typeof n.props.shape === 'string' && PRESET[n.props.shape] ? n.props.shape : 'box';
|
|
235
|
+
const key = n.name === 'Model' ? modelStructureKey(n.props) : JSON.stringify([shape, n.props.at, n.props.size, n.props.rotate, n.props.color, n.props.outline]);
|
|
236
|
+
let it = this.items.get(n.id);
|
|
237
|
+
if (it && (it.key !== key || it.src !== n.props.src)) {
|
|
238
|
+
this.dispose(it);
|
|
239
|
+
this.items.delete(n.id);
|
|
240
|
+
it = undefined;
|
|
241
|
+
}
|
|
242
|
+
if (!it) {
|
|
243
|
+
it = this.build(n, shape, key);
|
|
244
|
+
this.items.set(n.id, it);
|
|
245
|
+
if (n.name === 'Model')
|
|
246
|
+
this.load(it);
|
|
247
|
+
// 新出现的物体:第一次就在终点(不从 0 长出来),之后的显示 / 隐藏才过渡
|
|
248
|
+
it.cur = { lit: n.props.lit ? 1 : 0, open: n.props.open ? 1 : 0, show: n.visible ? 1 : 0, fade: n.disabled ? 1 : 0 };
|
|
249
|
+
}
|
|
250
|
+
const colorChanged = it.node.props.color !== n.props.color;
|
|
251
|
+
it.node = n;
|
|
252
|
+
if (it.loaded && colorChanged) {
|
|
253
|
+
for (const paint of it.paints)
|
|
254
|
+
if (paint.originalColor)
|
|
255
|
+
paint.role = typeof n.props.color === 'string' ? n.props.color : 'original';
|
|
256
|
+
this.repaint(it);
|
|
257
|
+
}
|
|
258
|
+
it.goal = { lit: n.props.lit ? 1 : 0, open: n.props.open ? 1 : 0, show: n.visible ? 1 : 0, fade: n.disabled ? 1 : 0 };
|
|
259
|
+
// 锚点:引擎要求的属性原样放上
|
|
260
|
+
for (const a of [...it.anchor.attributes])
|
|
261
|
+
if (a.name.startsWith('data-') || a.name.startsWith('aria-'))
|
|
262
|
+
it.anchor.removeAttribute(a.name);
|
|
263
|
+
for (const [k, v] of Object.entries(n.attrs))
|
|
264
|
+
it.anchor.setAttribute(k, v);
|
|
265
|
+
const text = visibleLabel(n.props, n.interactive);
|
|
266
|
+
const wantLabel = text !== null;
|
|
267
|
+
if (wantLabel && !it.label) {
|
|
268
|
+
it.label = document.createElement('div');
|
|
269
|
+
it.label.style.cssText = 'position:absolute;transform:translate(-50%,-100%);white-space:nowrap;font:500 12px/1.4 var(--jp-font);color:var(--jp-ink);background:var(--jp-surface);border:1px solid var(--jp-line-strong);border-radius:4px;padding:1px 7px;transition:opacity .25s';
|
|
270
|
+
this.overlay.append(it.label);
|
|
271
|
+
}
|
|
272
|
+
else if (!wantLabel && it.label) {
|
|
273
|
+
it.label.remove();
|
|
274
|
+
it.label = null;
|
|
275
|
+
}
|
|
276
|
+
if (it.label)
|
|
277
|
+
it.label.textContent = text;
|
|
278
|
+
}
|
|
279
|
+
for (const [id, it] of this.items)
|
|
280
|
+
if (!seen.has(id)) {
|
|
281
|
+
this.dispose(it);
|
|
282
|
+
this.items.delete(id);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
mat(paints, role, glow, opts = {}) {
|
|
286
|
+
const m = new THREE.MeshToonMaterial({ gradientMap: this.gradient, ...opts });
|
|
287
|
+
paints.push({ mat: m, role, ...(glow ? { glow } : {}) });
|
|
288
|
+
return m;
|
|
289
|
+
}
|
|
290
|
+
color(role) {
|
|
291
|
+
if (ROLES.includes(role))
|
|
292
|
+
return this.palette[role].clone();
|
|
293
|
+
const c = new THREE.Color();
|
|
294
|
+
try {
|
|
295
|
+
c.setStyle(role);
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
c.copy(this.palette.surface);
|
|
299
|
+
}
|
|
300
|
+
return c;
|
|
301
|
+
}
|
|
302
|
+
build(n, shape, key) {
|
|
303
|
+
const pre = PRESET[shape];
|
|
304
|
+
const [w, h, d] = vec(n.props.size, pre.size);
|
|
305
|
+
const at = vec(n.props.at, [0, 0, 0]);
|
|
306
|
+
const tone = typeof n.props.color === 'string' && n.props.color ? n.props.color : pre.color;
|
|
307
|
+
const group = new THREE.Group();
|
|
308
|
+
group.position.set(at[0], at[1], at[2]);
|
|
309
|
+
group.rotation.y = num(n.props.rotate, 0) * DEG;
|
|
310
|
+
const paints = [];
|
|
311
|
+
const meshes = [];
|
|
312
|
+
let hinge = null;
|
|
313
|
+
let hingeSign = 1;
|
|
314
|
+
let light = null;
|
|
315
|
+
const y0 = pre.anchor === 'base' ? 0 : -h / 2;
|
|
316
|
+
const add = (geo, m, x, y, z, parent = group) => {
|
|
317
|
+
const mesh = new THREE.Mesh(geo, m);
|
|
318
|
+
mesh.position.set(x, y, z);
|
|
319
|
+
mesh.userData.itemId = n.id;
|
|
320
|
+
parent.add(mesh);
|
|
321
|
+
meshes.push(mesh);
|
|
322
|
+
return mesh;
|
|
323
|
+
};
|
|
324
|
+
const box = (bw, bh, bd) => new THREE.BoxGeometry(bw, bh, bd);
|
|
325
|
+
// 通用形状亮起时整体发光;预设物件只让专门的部件发光(灯罩、屏幕、指示灯)
|
|
326
|
+
const generic = shape === 'box' || shape === 'cylinder' || shape === 'sphere' || shape === 'cone';
|
|
327
|
+
const body = this.mat(paints, tone, generic ? 'warn' : undefined);
|
|
328
|
+
let halo = null;
|
|
329
|
+
switch (shape) {
|
|
330
|
+
case 'cylinder':
|
|
331
|
+
add(new THREE.CylinderGeometry(w / 2, w / 2, h, 32), body, 0, h / 2, 0).scale.set(1, 1, d / w);
|
|
332
|
+
break;
|
|
333
|
+
case 'sphere':
|
|
334
|
+
add(new THREE.SphereGeometry(0.5, 32, 16), body, 0, h / 2, 0).scale.set(w, h, d);
|
|
335
|
+
break;
|
|
336
|
+
case 'cone':
|
|
337
|
+
add(new THREE.ConeGeometry(w / 2, h, 32), body, 0, h / 2, 0).scale.set(1, 1, d / w);
|
|
338
|
+
break;
|
|
339
|
+
case 'floor':
|
|
340
|
+
add(box(w, h, d), body, 0, -h / 2, 0);
|
|
341
|
+
break;
|
|
342
|
+
case 'wall':
|
|
343
|
+
add(box(w, h, d), body, 0, 0, 0);
|
|
344
|
+
break;
|
|
345
|
+
case 'lamp': {
|
|
346
|
+
const dark = this.mat(paints, 'ink');
|
|
347
|
+
add(new THREE.CylinderGeometry(w * 0.36, w * 0.4, 0.05, 24), dark, 0, 0.025, 0);
|
|
348
|
+
add(new THREE.CylinderGeometry(0.022, 0.022, h * 0.8, 8), dark, 0, h * 0.4, 0);
|
|
349
|
+
const shade = this.mat(paints, tone, 'warn', { side: THREE.DoubleSide });
|
|
350
|
+
paints[paints.length - 1].tint = 'warn';
|
|
351
|
+
add(new THREE.CylinderGeometry(w * 0.26, w * 0.5, h * 0.22, 32, 1, true), shade, 0, h * 0.86, 0);
|
|
352
|
+
halo = new THREE.Sprite(new THREE.SpriteMaterial({ map: haloTexture(), transparent: true, depthWrite: false }));
|
|
353
|
+
halo.scale.setScalar(w * 3.2);
|
|
354
|
+
halo.position.set(0, h * 0.86, 0);
|
|
355
|
+
paints.push({ mat: halo.material, role: 'halo' });
|
|
356
|
+
group.add(halo);
|
|
357
|
+
light = new THREE.PointLight(0xffffff, 0, 7, 1.4);
|
|
358
|
+
light.position.set(0, h * 0.8, 0);
|
|
359
|
+
group.add(light);
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
case 'door': {
|
|
363
|
+
hinge = new THREE.Group();
|
|
364
|
+
hinge.position.set(-w / 2, 0, 0);
|
|
365
|
+
group.add(hinge);
|
|
366
|
+
add(box(w, h, d), body, w / 2, h / 2, 0, hinge);
|
|
367
|
+
add(new THREE.SphereGeometry(0.04, 12, 8), this.mat(paints, 'ink'), w - 0.1, h * 0.48, d / 2 + 0.03, hinge);
|
|
368
|
+
hingeSign = 1;
|
|
369
|
+
break;
|
|
370
|
+
}
|
|
371
|
+
case 'window': {
|
|
372
|
+
const fw = 0.06;
|
|
373
|
+
add(box(w, fw, d), body, 0, h / 2 - fw / 2, 0);
|
|
374
|
+
add(box(w, fw, d), body, 0, -h / 2 + fw / 2, 0);
|
|
375
|
+
add(box(fw, h, d), body, -w / 2 + fw / 2, 0, 0);
|
|
376
|
+
add(box(fw, h, d), body, w / 2 - fw / 2, 0, 0);
|
|
377
|
+
hinge = new THREE.Group();
|
|
378
|
+
hinge.position.set(-w / 2 + fw, 0, d / 2);
|
|
379
|
+
group.add(hinge);
|
|
380
|
+
const glass = this.mat(paints, 'info', undefined, { transparent: true, opacity: 0.35 });
|
|
381
|
+
add(box(w - fw * 2, h - fw * 2, 0.02), glass, (w - fw * 2) / 2, 0, 0, hinge);
|
|
382
|
+
hingeSign = 1;
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
case 'sofa': {
|
|
386
|
+
add(box(w, h * 0.5, d), body, 0, h * 0.25, 0);
|
|
387
|
+
add(box(w, h * 0.55, d * 0.22), body, 0, h * 0.5 + h * 0.275, -d / 2 + d * 0.11);
|
|
388
|
+
add(box(w * 0.1, h * 0.75, d), body, -w / 2 + w * 0.05, h * 0.375, 0);
|
|
389
|
+
add(box(w * 0.1, h * 0.75, d), body, w / 2 - w * 0.05, h * 0.375, 0);
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
case 'table': {
|
|
393
|
+
add(box(w, 0.05, d), body, 0, h - 0.025, 0);
|
|
394
|
+
const leg = this.mat(paints, 'ink');
|
|
395
|
+
for (const sx of [-1, 1])
|
|
396
|
+
for (const sz of [-1, 1])
|
|
397
|
+
add(new THREE.CylinderGeometry(0.025, 0.025, h - 0.05, 8), leg, sx * (w / 2 - 0.08), (h - 0.05) / 2, sz * (d / 2 - 0.08));
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
case 'bed': {
|
|
401
|
+
add(box(w, h * 0.55, d), body, 0, h * 0.275, 0);
|
|
402
|
+
add(box(w * 0.96, h * 0.32, d * 0.96), this.mat(paints, 'subtle', 'warn'), 0, h * 0.55 + h * 0.16, 0);
|
|
403
|
+
add(box(w * 0.7, h * 0.16, d * 0.16), body, 0, h * 0.95, -d / 2 + d * 0.14);
|
|
404
|
+
add(box(w, h * 1.6, 0.08), body, 0, h * 0.8, -d / 2 - 0.04);
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
case 'tv': {
|
|
408
|
+
add(box(w, h, d), body, 0, 0, 0);
|
|
409
|
+
add(new THREE.PlaneGeometry(w * 0.93, h * 0.88), this.mat(paints, 'line', 'info'), 0, 0, d / 2 + 0.002);
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
case 'plant': {
|
|
413
|
+
add(new THREE.CylinderGeometry(w * 0.36, w * 0.28, h * 0.35, 20), this.mat(paints, 'muted'), 0, h * 0.175, 0);
|
|
414
|
+
add(new THREE.SphereGeometry(w * 0.5, 16, 12), body, 0, h * 0.62, 0);
|
|
415
|
+
add(new THREE.SphereGeometry(w * 0.32, 14, 10), body, w * 0.18, h * 0.86, w * 0.05);
|
|
416
|
+
break;
|
|
417
|
+
}
|
|
418
|
+
case 'speaker': {
|
|
419
|
+
add(new THREE.CylinderGeometry(w / 2, w / 2, h, 28), body, 0, h / 2, 0);
|
|
420
|
+
add(new THREE.CylinderGeometry(w / 2 + 0.005, w / 2 + 0.005, 0.03, 28), this.mat(paints, 'line', 'info'), 0, h - 0.04, 0);
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
case 'ac': {
|
|
424
|
+
add(box(w, h, d), body, 0, 0, 0);
|
|
425
|
+
add(box(w * 0.8, 0.025, 0.01), this.mat(paints, 'line', 'info'), 0, -h / 2 + 0.06, d / 2 + 0.006);
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
default:
|
|
429
|
+
add(box(w, h, d), body, 0, y0 + h / 2, 0);
|
|
430
|
+
}
|
|
431
|
+
// 墨色描边
|
|
432
|
+
const edges = [];
|
|
433
|
+
for (const m of meshes) {
|
|
434
|
+
const lm = new THREE.LineBasicMaterial({ transparent: true });
|
|
435
|
+
paints.push({ mat: lm, role: 'ink' });
|
|
436
|
+
const e = new THREE.LineSegments(new THREE.EdgesGeometry(m.geometry, 28), lm);
|
|
437
|
+
e.position.copy(m.position);
|
|
438
|
+
e.rotation.copy(m.rotation);
|
|
439
|
+
e.scale.copy(m.scale);
|
|
440
|
+
m.parent.add(e);
|
|
441
|
+
edges.push(e);
|
|
442
|
+
}
|
|
443
|
+
this.world.add(group);
|
|
444
|
+
const anchor = document.createElement('div');
|
|
445
|
+
anchor.style.cssText = 'position:absolute;pointer-events:none';
|
|
446
|
+
this.overlay.append(anchor);
|
|
447
|
+
const it = { id: n.id, key, src: n.props.src, node: n, group, meshes, edges, paints, hinge, hingeSign, light, halo, cur: { lit: 0, open: 0, show: 1, fade: 0 }, goal: { lit: 0, open: 0, show: 1, fade: 0 }, anchor, label: null };
|
|
448
|
+
this.repaint(it);
|
|
449
|
+
return it;
|
|
450
|
+
}
|
|
451
|
+
repaint(it) {
|
|
452
|
+
for (const p of it.paints) {
|
|
453
|
+
if (p.role === 'halo')
|
|
454
|
+
p.mat.color.copy(this.palette.warn);
|
|
455
|
+
else if (p.mat instanceof THREE.LineBasicMaterial)
|
|
456
|
+
p.mat.color.copy(this.palette.ink);
|
|
457
|
+
else {
|
|
458
|
+
if (p.role !== 'original')
|
|
459
|
+
p.mat.color.copy(this.color(p.role));
|
|
460
|
+
else if (p.originalColor)
|
|
461
|
+
p.mat.color.copy(p.originalColor);
|
|
462
|
+
if (p.glow)
|
|
463
|
+
p.mat.emissive.copy(this.palette[p.glow]);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
if (it.light)
|
|
467
|
+
it.light.color.copy(this.palette.warn);
|
|
468
|
+
}
|
|
469
|
+
load(it) {
|
|
470
|
+
const release = this.ctx.hold();
|
|
471
|
+
let released = false;
|
|
472
|
+
it.release = () => { if (!released) {
|
|
473
|
+
released = true;
|
|
474
|
+
release();
|
|
475
|
+
} };
|
|
476
|
+
const status = document.createElement('div');
|
|
477
|
+
status.textContent = 'Loading 3D model…';
|
|
478
|
+
status.setAttribute('role', 'status');
|
|
479
|
+
status.style.cssText = 'position:absolute;left:12px;top:12px;padding:6px 10px;background:var(--jp-surface);color:var(--jp-ink);border:1px solid var(--jp-line);font:12px var(--jp-font)';
|
|
480
|
+
this.overlay.append(status);
|
|
481
|
+
it.status = status;
|
|
482
|
+
void loadModel(it.node.props.src).then(model => {
|
|
483
|
+
if (it.cancelled) {
|
|
484
|
+
disposeModel(model);
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
// Replace the visible loading box, retaining the stable item and its anchors.
|
|
488
|
+
for (const mesh of it.meshes) {
|
|
489
|
+
mesh.removeFromParent();
|
|
490
|
+
mesh.geometry.dispose();
|
|
491
|
+
}
|
|
492
|
+
for (const edge of it.edges) {
|
|
493
|
+
edge.removeFromParent();
|
|
494
|
+
edge.geometry.dispose();
|
|
495
|
+
}
|
|
496
|
+
for (const paint of it.paints)
|
|
497
|
+
paint.mat.dispose();
|
|
498
|
+
it.meshes = [];
|
|
499
|
+
it.edges = [];
|
|
500
|
+
it.paints = [];
|
|
501
|
+
const box = new THREE.Box3().setFromObject(model), dimensions = box.getSize(new THREE.Vector3()), center = box.getCenter(new THREE.Vector3());
|
|
502
|
+
const size = vec(it.node.props.size, [1, 1, 1]);
|
|
503
|
+
const scale = Math.min(...size.map((v, i) => dimensions.getComponent(i) > 1e-8 ? v / dimensions.getComponent(i) : Infinity));
|
|
504
|
+
if (!Number.isFinite(scale) || scale <= 0) {
|
|
505
|
+
disposeModel(model);
|
|
506
|
+
throw new Error('Empty model geometry');
|
|
507
|
+
}
|
|
508
|
+
model.position.sub(new THREE.Vector3(center.x, box.min.y, center.z));
|
|
509
|
+
const normalized = new THREE.Group();
|
|
510
|
+
normalized.add(model);
|
|
511
|
+
normalized.scale.setScalar(scale);
|
|
512
|
+
it.group.add(normalized);
|
|
513
|
+
it.loaded = normalized;
|
|
514
|
+
model.traverse(object => {
|
|
515
|
+
if (!(object instanceof THREE.Mesh))
|
|
516
|
+
return;
|
|
517
|
+
object.userData.itemId = it.id;
|
|
518
|
+
it.meshes.push(object);
|
|
519
|
+
for (const material of Array.isArray(object.material) ? object.material : [object.material]) {
|
|
520
|
+
if (material instanceof THREE.MeshStandardMaterial || material instanceof THREE.MeshToonMaterial)
|
|
521
|
+
it.paints.push({ mat: material, role: typeof it.node.props.color === 'string' ? it.node.props.color : 'original', glow: 'warn', originalColor: material.color.clone() });
|
|
522
|
+
}
|
|
523
|
+
if (it.node.props.outline !== false) {
|
|
524
|
+
const material = new THREE.LineBasicMaterial({ transparent: true });
|
|
525
|
+
const edges = new THREE.LineSegments(new THREE.EdgesGeometry(object.geometry, 28), material);
|
|
526
|
+
object.add(edges);
|
|
527
|
+
it.edges.push(edges);
|
|
528
|
+
it.paints.push({ mat: material, role: 'ink' });
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
status.remove();
|
|
532
|
+
it.status = undefined;
|
|
533
|
+
this.repaint(it);
|
|
534
|
+
if (!this.userMoved) {
|
|
535
|
+
this.view = this.frameFor(this.focusId(), String(this.ctx.node.props.view ?? 'iso'));
|
|
536
|
+
this.tween = null;
|
|
537
|
+
}
|
|
538
|
+
this.tick(this.ctx.frame.clock?.timeMs ?? 0, false);
|
|
539
|
+
}).catch(() => {
|
|
540
|
+
if (it.cancelled)
|
|
541
|
+
return;
|
|
542
|
+
status.textContent = `${it.node.label || it.id}: model unavailable. Restore a valid GLB or run joplo resources.`;
|
|
543
|
+
status.dataset.joploDiagnostic = 'MODEL_LOAD_FAILED';
|
|
544
|
+
status.setAttribute('role', 'alert');
|
|
545
|
+
}).finally(() => it.release?.());
|
|
546
|
+
}
|
|
547
|
+
dispose(it) {
|
|
548
|
+
it.cancelled = true;
|
|
549
|
+
it.release?.();
|
|
550
|
+
it.status?.remove();
|
|
551
|
+
if (it.loaded)
|
|
552
|
+
disposeModel(it.loaded);
|
|
553
|
+
this.world.remove(it.group);
|
|
554
|
+
for (const m of it.meshes)
|
|
555
|
+
m.geometry.dispose();
|
|
556
|
+
for (const e of it.edges)
|
|
557
|
+
e.geometry.dispose();
|
|
558
|
+
for (const p of it.paints)
|
|
559
|
+
p.mat.dispose();
|
|
560
|
+
it.anchor.remove();
|
|
561
|
+
it.label?.remove();
|
|
562
|
+
}
|
|
563
|
+
/** 每帧把过渡值落到场景上。 */
|
|
564
|
+
apply(it) {
|
|
565
|
+
const { lit, open, show, fade } = it.cur;
|
|
566
|
+
const hovered = this.hover === it.id && it.node.interactive && !it.node.disabled;
|
|
567
|
+
const selected = this.ctx.frame.selectedId === it.id;
|
|
568
|
+
for (const p of it.paints) {
|
|
569
|
+
if (p.role === 'halo') {
|
|
570
|
+
p.mat.opacity = lit * 0.75 * show;
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
if (p.mat instanceof THREE.LineBasicMaterial) {
|
|
574
|
+
p.mat.color.copy(hovered || selected ? this.palette.accent : this.palette.ink);
|
|
575
|
+
p.mat.opacity = (1 - fade * 0.6) * show;
|
|
576
|
+
}
|
|
577
|
+
else {
|
|
578
|
+
if (p.glow)
|
|
579
|
+
p.mat.emissiveIntensity = lit * (p.glow === 'warn' && it.light ? 0.6 : 0.85);
|
|
580
|
+
if (p.tint)
|
|
581
|
+
p.mat.color.copy(this.color(p.role)).lerp(this.palette[p.tint], lit * 0.7);
|
|
582
|
+
const baseOpacity = p.mat.userData.baseOpacity ?? (p.mat.userData.baseOpacity = p.mat.opacity);
|
|
583
|
+
const o = baseOpacity * (1 - fade * 0.55) * show;
|
|
584
|
+
p.mat.transparent = o < 0.999 || p.mat.userData.baseOpacity < 1;
|
|
585
|
+
p.mat.opacity = o;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
if (it.light)
|
|
589
|
+
it.light.intensity = lit * 5;
|
|
590
|
+
if (it.halo)
|
|
591
|
+
it.halo.visible = lit > 0.01 && show > 0.01;
|
|
592
|
+
if (it.hinge)
|
|
593
|
+
it.hinge.rotation.y = -it.hingeSign * open * (it.node.props.shape === 'door' ? 1.45 : 1.15);
|
|
594
|
+
it.group.visible = show > 0.01;
|
|
595
|
+
const s = 0.6 + 0.4 * show;
|
|
596
|
+
it.group.scale.setScalar(s);
|
|
597
|
+
}
|
|
598
|
+
// ---------- 镜头 ----------
|
|
599
|
+
boxOf(ids) {
|
|
600
|
+
const b = new THREE.Box3();
|
|
601
|
+
for (const it of this.items.values()) {
|
|
602
|
+
if (ids && !ids.includes(it.id))
|
|
603
|
+
continue;
|
|
604
|
+
if (!ids && it.goal.show < 0.5)
|
|
605
|
+
continue;
|
|
606
|
+
const s = it.group.scale.x;
|
|
607
|
+
it.group.scale.setScalar(1);
|
|
608
|
+
it.group.updateWorldMatrix(true, true);
|
|
609
|
+
for (const m of it.meshes)
|
|
610
|
+
b.expandByObject(m);
|
|
611
|
+
it.group.scale.setScalar(s);
|
|
612
|
+
}
|
|
613
|
+
if (b.isEmpty())
|
|
614
|
+
b.set(new THREE.Vector3(-2, 0, -2), new THREE.Vector3(2, 2, 2));
|
|
615
|
+
return b;
|
|
616
|
+
}
|
|
617
|
+
frameFor(focus, viewName) {
|
|
618
|
+
const angles = viewName === 'front' ? { az: 0, el: 12 * DEG } : viewName === 'top' ? { az: 0, el: 88 * DEG } : { az: 45 * DEG, el: 32 * DEG };
|
|
619
|
+
const b = this.boxOf(focus ? [focus] : null);
|
|
620
|
+
const sphere = b.getBoundingSphere(new THREE.Sphere());
|
|
621
|
+
const aspect = this.width / Math.max(1, this.height);
|
|
622
|
+
const r = Math.max(sphere.radius, focus ? 0.9 : 1.5);
|
|
623
|
+
const half = (focus ? 1.9 : 1.04) * Math.max(r, r / Math.max(0.2, aspect)) * (focus ? 1 : 0.92);
|
|
624
|
+
const p = this.ctx.node.props;
|
|
625
|
+
return { ...angles, az: typeof p.azimuth === 'number' ? p.azimuth * DEG : angles.az, el: typeof p.elevation === 'number' ? p.elevation * DEG : angles.el, target: sphere.center.clone(), half };
|
|
626
|
+
}
|
|
627
|
+
placeCamera() {
|
|
628
|
+
const { az, el, target, half } = this.view;
|
|
629
|
+
const dir = new THREE.Vector3(Math.cos(el) * Math.sin(az), Math.sin(el), Math.cos(el) * Math.cos(az));
|
|
630
|
+
this.camera.position.copy(target).addScaledVector(dir, 80);
|
|
631
|
+
this.camera.up.set(0, 1, 0);
|
|
632
|
+
if (el > 80 * DEG)
|
|
633
|
+
this.camera.up.set(-Math.sin(az), 0, -Math.cos(az));
|
|
634
|
+
this.camera.lookAt(target);
|
|
635
|
+
const aspect = this.width / Math.max(1, this.height);
|
|
636
|
+
this.camera.left = -half * aspect;
|
|
637
|
+
this.camera.right = half * aspect;
|
|
638
|
+
this.camera.top = half;
|
|
639
|
+
this.camera.bottom = -half;
|
|
640
|
+
this.camera.updateProjectionMatrix();
|
|
641
|
+
}
|
|
642
|
+
focusId() {
|
|
643
|
+
const f = this.ctx.node.props.focus;
|
|
644
|
+
return typeof f === 'string' && this.items.has(f) ? f : null;
|
|
645
|
+
}
|
|
646
|
+
reportOrbit() {
|
|
647
|
+
const azimuth = ((this.view.az / DEG + 180) % 360 + 360) % 360 - 180;
|
|
648
|
+
this.ctx.act?.(this.ctx.node.id, { type: 'plugin', name: 'scene3d:rotate', data: { azimuth, elevation: this.view.el / DEG } });
|
|
649
|
+
}
|
|
650
|
+
// ---------- 帧 ----------
|
|
651
|
+
resize(render = true) {
|
|
652
|
+
const w = Math.max(1, this.el.clientWidth);
|
|
653
|
+
const h = Math.max(1, this.el.clientHeight);
|
|
654
|
+
if (w === this.width && h === this.height)
|
|
655
|
+
return;
|
|
656
|
+
this.width = w;
|
|
657
|
+
this.height = h;
|
|
658
|
+
this.renderer.setSize(w, h, false);
|
|
659
|
+
if (this.framedOnce && !this.userMoved && !this.tween)
|
|
660
|
+
this.view = this.frameFor(this.focusId(), String(this.ctx.node.props.view ?? 'iso'));
|
|
661
|
+
if (render)
|
|
662
|
+
this.tick(this.ctx.frame.clock?.timeMs ?? 0, true);
|
|
663
|
+
}
|
|
664
|
+
tick(now, immediate) {
|
|
665
|
+
const dt = immediate || !this.last ? 0 : Math.min(0.1, (now - this.last) / 1000);
|
|
666
|
+
this.last = now;
|
|
667
|
+
let busy = false;
|
|
668
|
+
// 过渡:指数逼近;frame.animate 为 false 时 update 已经直接到终点
|
|
669
|
+
const k = 1 - Math.exp(-dt * 9);
|
|
670
|
+
for (const it of this.items.values()) {
|
|
671
|
+
for (const f of ['lit', 'open', 'show', 'fade']) {
|
|
672
|
+
const d = it.goal[f] - it.cur[f];
|
|
673
|
+
if (Math.abs(d) < 0.002)
|
|
674
|
+
it.cur[f] = it.goal[f];
|
|
675
|
+
else {
|
|
676
|
+
it.cur[f] += d * k;
|
|
677
|
+
busy = true;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
this.apply(it);
|
|
681
|
+
}
|
|
682
|
+
if (this.tween) {
|
|
683
|
+
const t = Math.min(1, (now - this.tween.t0) / CAMERA_MS);
|
|
684
|
+
const e = ease(t);
|
|
685
|
+
const { from, to } = this.tween;
|
|
686
|
+
let daz = to.az - from.az;
|
|
687
|
+
daz = ((daz + Math.PI) % (2 * Math.PI) + 2 * Math.PI) % (2 * Math.PI) - Math.PI;
|
|
688
|
+
this.view = { az: from.az + daz * e, el: from.el + (to.el - from.el) * e, target: from.target.clone().lerp(to.target, e), half: from.half + (to.half - from.half) * e };
|
|
689
|
+
if (t >= 1)
|
|
690
|
+
this.tween = null;
|
|
691
|
+
else
|
|
692
|
+
busy = true;
|
|
693
|
+
}
|
|
694
|
+
this.placeCamera();
|
|
695
|
+
this.renderer.render(this.scene, this.camera);
|
|
696
|
+
this.placeOverlay();
|
|
697
|
+
if (busy && (!this.ctx.frame.animate || !this.ctx.frame.clock)) {
|
|
698
|
+
for (const it of this.items.values()) {
|
|
699
|
+
it.cur = { ...it.goal };
|
|
700
|
+
this.apply(it);
|
|
701
|
+
}
|
|
702
|
+
this.renderer.render(this.scene, this.camera);
|
|
703
|
+
this.placeOverlay();
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
/** 锚点与名牌:物体包围盒投影到画面上的位置(元素坐标,未缩放)。 */
|
|
707
|
+
placeOverlay() {
|
|
708
|
+
const v = new THREE.Vector3();
|
|
709
|
+
const b = new THREE.Box3();
|
|
710
|
+
for (const it of this.items.values()) {
|
|
711
|
+
const visible = it.node.visible && it.cur.show > 0.5;
|
|
712
|
+
it.anchor.style.display = visible ? '' : 'none';
|
|
713
|
+
if (it.label)
|
|
714
|
+
it.label.style.opacity = visible ? '1' : '0';
|
|
715
|
+
if (!visible)
|
|
716
|
+
continue;
|
|
717
|
+
b.makeEmpty();
|
|
718
|
+
for (const m of it.meshes)
|
|
719
|
+
b.expandByObject(m);
|
|
720
|
+
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
|
|
721
|
+
for (let i = 0; i < 8; i++) {
|
|
722
|
+
v.set(i & 1 ? b.max.x : b.min.x, i & 2 ? b.max.y : b.min.y, i & 4 ? b.max.z : b.min.z).project(this.camera);
|
|
723
|
+
const px = (v.x * 0.5 + 0.5) * this.width;
|
|
724
|
+
const py = (-v.y * 0.5 + 0.5) * this.height;
|
|
725
|
+
x0 = Math.min(x0, px);
|
|
726
|
+
y0 = Math.min(y0, py);
|
|
727
|
+
x1 = Math.max(x1, px);
|
|
728
|
+
y1 = Math.max(y1, py);
|
|
729
|
+
}
|
|
730
|
+
// 夹到画面里,至少 8px,便于光标与提示对准
|
|
731
|
+
const cx0 = Math.max(0, Math.min(this.width - 8, x0));
|
|
732
|
+
const cy0 = Math.max(0, Math.min(this.height - 8, y0));
|
|
733
|
+
const cx1 = Math.max(cx0 + 8, Math.min(this.width, x1));
|
|
734
|
+
const cy1 = Math.max(cy0 + 8, Math.min(this.height, y1));
|
|
735
|
+
Object.assign(it.anchor.style, { left: `${cx0}px`, top: `${cy0}px`, width: `${cx1 - cx0}px`, height: `${cy1 - cy0}px` });
|
|
736
|
+
if (it.label) {
|
|
737
|
+
it.label.style.left = `${(cx0 + cx1) / 2}px`;
|
|
738
|
+
it.label.style.top = `${Math.max(18, cy0 - 4)}px`;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
// ---------- 指针 ----------
|
|
743
|
+
pick(clientX, clientY) {
|
|
744
|
+
const r = this.renderer.domElement.getBoundingClientRect();
|
|
745
|
+
if (!r.width || !r.height)
|
|
746
|
+
return null;
|
|
747
|
+
const ndc = new THREE.Vector2(((clientX - r.left) / r.width) * 2 - 1, -((clientY - r.top) / r.height) * 2 + 1);
|
|
748
|
+
this.raycaster.setFromCamera(ndc, this.camera);
|
|
749
|
+
const meshes = [...this.items.values()].filter((it) => it.node.visible && it.cur.show > 0.5).flatMap((it) => it.meshes);
|
|
750
|
+
const hits = this.raycaster.intersectObjects(meshes, false);
|
|
751
|
+
// 优先命中可操作的元素(灯在墙前面时点到墙也算点灯附近的墙,不抢灯)
|
|
752
|
+
const ids = hits.map((h) => h.object.userData.itemId);
|
|
753
|
+
const first = ids.find((id) => this.items.get(id)?.node.interactive) ?? ids.find((id) => { const s = this.items.get(id)?.node.props.shape; return s !== 'floor' && s !== 'wall'; }) ?? null;
|
|
754
|
+
return first ? this.items.get(first) ?? null : null;
|
|
755
|
+
}
|
|
756
|
+
bindPointer(canvas) {
|
|
757
|
+
const on = (type, fn, opts) => {
|
|
758
|
+
canvas.addEventListener(type, fn, opts);
|
|
759
|
+
this.cleanup.push(() => canvas.removeEventListener(type, fn, opts));
|
|
760
|
+
};
|
|
761
|
+
const scale = () => {
|
|
762
|
+
const r = canvas.getBoundingClientRect();
|
|
763
|
+
return r.width / Math.max(1, canvas.clientWidth) || 1;
|
|
764
|
+
};
|
|
765
|
+
const key = (e) => {
|
|
766
|
+
if (!['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown'].includes(e.key))
|
|
767
|
+
return;
|
|
768
|
+
e.preventDefault();
|
|
769
|
+
e.stopPropagation();
|
|
770
|
+
this.freezeView();
|
|
771
|
+
this.userMoved = true;
|
|
772
|
+
this.tween = null;
|
|
773
|
+
const step = (e.shiftKey ? 1 : 15) * DEG;
|
|
774
|
+
this.view.az += e.key === 'ArrowLeft' ? -step : e.key === 'ArrowRight' ? step : 0;
|
|
775
|
+
this.view.el = Math.max(6 * DEG, Math.min(86 * DEG, this.view.el + (e.key === 'ArrowUp' ? step : e.key === 'ArrowDown' ? -step : 0)));
|
|
776
|
+
this.tick(this.ctx.frame.clock?.timeMs ?? 0, true);
|
|
777
|
+
this.reportOrbit();
|
|
778
|
+
};
|
|
779
|
+
canvas.addEventListener('keydown', key);
|
|
780
|
+
this.cleanup.push(() => canvas.removeEventListener('keydown', key));
|
|
781
|
+
on('pointerdown', (e) => {
|
|
782
|
+
if (e.button !== 0)
|
|
783
|
+
return;
|
|
784
|
+
e.stopPropagation(); // 不让舞台把拖动当作平移
|
|
785
|
+
const hit = this.pick(e.clientX, e.clientY);
|
|
786
|
+
// Host-owned anchor metadata is the same ownership decision used by DOM gestures.
|
|
787
|
+
const target = hit?.anchor.hasAttribute('data-jdraggable') ? hit.id : undefined;
|
|
788
|
+
this.ctx.hover?.(null);
|
|
789
|
+
this.drag = { x: e.clientX, y: e.clientY, az: this.view.az, el: this.view.el, moved: false, id: e.pointerId, target };
|
|
790
|
+
});
|
|
791
|
+
on('pointermove', (e) => {
|
|
792
|
+
const d = this.drag;
|
|
793
|
+
if (d && e.buttons & 1) {
|
|
794
|
+
e.stopPropagation();
|
|
795
|
+
const s = scale();
|
|
796
|
+
const dx = (e.clientX - d.x) / s;
|
|
797
|
+
const dy = (e.clientY - d.y) / s;
|
|
798
|
+
if (!d.moved && Math.hypot(dx, dy) < 4)
|
|
799
|
+
return;
|
|
800
|
+
if (!d.moved) {
|
|
801
|
+
d.moved = true;
|
|
802
|
+
canvas.setPointerCapture?.(d.id);
|
|
803
|
+
canvas.style.cursor = 'grabbing';
|
|
804
|
+
}
|
|
805
|
+
if (d.target)
|
|
806
|
+
return;
|
|
807
|
+
this.freezeView();
|
|
808
|
+
this.tween = null;
|
|
809
|
+
this.userMoved = true;
|
|
810
|
+
this.view = { ...this.view, az: d.az - dx * 0.008, el: Math.max(6 * DEG, Math.min(86 * DEG, d.el + dy * 0.006)) };
|
|
811
|
+
this.tick(this.ctx.frame.clock?.timeMs ?? 0, true);
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
const hit = this.pick(e.clientX, e.clientY);
|
|
815
|
+
const id = hit?.id ?? null;
|
|
816
|
+
this.ctx.hover?.(e.pointerType === 'touch' ? null : id);
|
|
817
|
+
if (id !== this.hover) {
|
|
818
|
+
this.hover = id;
|
|
819
|
+
this.tick(this.ctx.frame.clock?.timeMs ?? 0, true);
|
|
820
|
+
}
|
|
821
|
+
canvas.style.cursor = this.ctx.frame.mode === 'review' ? 'help' : hit?.node.interactive ? 'pointer' : 'grab';
|
|
822
|
+
});
|
|
823
|
+
on('pointerleave', () => {
|
|
824
|
+
this.ctx.hover?.(null);
|
|
825
|
+
if (this.hover) {
|
|
826
|
+
this.hover = null;
|
|
827
|
+
this.tick(this.ctx.frame.clock?.timeMs ?? 0, true);
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
on('pointerup', (e) => {
|
|
831
|
+
const d = this.drag;
|
|
832
|
+
this.drag = null;
|
|
833
|
+
canvas.style.cursor = 'grab';
|
|
834
|
+
if (!d)
|
|
835
|
+
return;
|
|
836
|
+
e.stopPropagation();
|
|
837
|
+
if (d.moved) {
|
|
838
|
+
if (d.target) {
|
|
839
|
+
const stage = this.el.closest('[data-joplo-stage]');
|
|
840
|
+
const box = stage?.getBoundingClientRect();
|
|
841
|
+
const scale = box && stage ? box.width / stage.offsetWidth || 1 : 1;
|
|
842
|
+
const point = { x: (e.clientX - (box?.left ?? 0)) / scale, y: (e.clientY - (box?.top ?? 0)) / scale };
|
|
843
|
+
const destination = this.pick(e.clientX, e.clientY);
|
|
844
|
+
this.ctx.act?.(d.target, { type: 'drag', to: destination && destination.id !== d.target ? destination.id : point }, { type: 'drag', target: d.target, to: point });
|
|
845
|
+
}
|
|
846
|
+
else
|
|
847
|
+
this.reportOrbit();
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
850
|
+
const hit = this.pick(e.clientX, e.clientY);
|
|
851
|
+
if (hit)
|
|
852
|
+
this.ctx.click(hit.id, { clientX: e.clientX, clientY: e.clientY, altKey: e.altKey });
|
|
853
|
+
});
|
|
854
|
+
// 原生 click 不交给舞台(上面已经按命中的元素转交引擎)
|
|
855
|
+
on('click', (e) => e.stopPropagation());
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
export default defineRuntime({
|
|
859
|
+
id: 'scene3d',
|
|
860
|
+
components: {
|
|
861
|
+
Scene: { mount: (el, ctx) => new SceneView(el, ctx) },
|
|
862
|
+
},
|
|
863
|
+
});
|