@mark1russell7/room-view 0.0.0-stage → 0.1.1
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 +21 -0
- package/README.md +297 -2
- package/build/index.d.ts +83 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +391 -0
- package/build/index.js.map +1 -0
- package/build/level-84nrw7oL.d.ts +344 -0
- package/build/level-84nrw7oL.d.ts.map +1 -0
- package/build/level-xTO7P1Fn.js +757 -0
- package/build/level-xTO7P1Fn.js.map +1 -0
- package/build/loads-Br73kIsQ.js +667 -0
- package/build/loads-Br73kIsQ.js.map +1 -0
- package/build/loads-H-VFEx6b.d.ts +223 -0
- package/build/loads-H-VFEx6b.d.ts.map +1 -0
- package/build/room-BUlXejkm.d.ts +149 -0
- package/build/room-BUlXejkm.d.ts.map +1 -0
- package/build/sample.d.ts +28 -0
- package/build/sample.d.ts.map +1 -0
- package/build/sample.js +863 -0
- package/build/sample.js.map +1 -0
- package/build/schema.d.ts +24 -0
- package/build/schema.d.ts.map +1 -0
- package/build/schema.js +167 -0
- package/build/schema.js.map +1 -0
- package/build/three.d.ts +117 -0
- package/build/three.d.ts.map +1 -0
- package/build/three.js +1069 -0
- package/build/three.js.map +1 -0
- package/build/units-BZmvVyu4.js +30 -0
- package/build/units-BZmvVyu4.js.map +1 -0
- package/package.json +89 -3
package/build/three.js
ADDED
|
@@ -0,0 +1,1069 @@
|
|
|
1
|
+
import { C as wallFrames, T as wallPoint, _ as sceneSolids, i as SHAPE_OUTLINES, m as boundsOfSolids, o as levelShape, p as TO_Y_UP, s as outletLevels, v as solidCorners, w as wallHasOutline } from "./loads-Br73kIsQ.js";
|
|
2
|
+
import { i as metersPer } from "./units-BZmvVyu4.js";
|
|
3
|
+
import { pointInRing, transformPoint3 } from "@mark1russell7/layout-contract";
|
|
4
|
+
import { BoxGeometry, CircleGeometry, Color, DirectionalLight, EdgesGeometry, ExtrudeGeometry, Group, HemisphereLight, Mesh, MeshBasicMaterial, MeshLambertMaterial, PerspectiveCamera, Raycaster, RingGeometry, Scene, Shape, Vector2, Vector3, WebGLRenderer } from "three";
|
|
5
|
+
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
|
|
6
|
+
import { LineMaterial } from "three/addons/lines/LineMaterial.js";
|
|
7
|
+
import { LineSegments2 } from "three/addons/lines/LineSegments2.js";
|
|
8
|
+
import { LineSegmentsGeometry } from "three/addons/lines/LineSegmentsGeometry.js";
|
|
9
|
+
// src/viewer/geometry.ts
|
|
10
|
+
/** The rotation that changes no direction. */
|
|
11
|
+
const NO_ROTATION = {
|
|
12
|
+
x: 0,
|
|
13
|
+
y: 0,
|
|
14
|
+
z: 0,
|
|
15
|
+
w: 1
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* This function gives the three.js geometry of a solid, with its place in the room.
|
|
19
|
+
*
|
|
20
|
+
* A mesh with this geometry, this position and this rotation is in the space of the room: the z axis points up.
|
|
21
|
+
* Put the mesh in a group with the rotation of `TO_Y_UP`. Then three.js shows it with the y axis up.
|
|
22
|
+
*/
|
|
23
|
+
function solidGeometry(solid) {
|
|
24
|
+
const shape = solid.shape;
|
|
25
|
+
if (shape.kind === "box") {
|
|
26
|
+
const { box, transform } = shape;
|
|
27
|
+
const center = {
|
|
28
|
+
x: box.x + box.w / 2,
|
|
29
|
+
y: box.y + box.h / 2,
|
|
30
|
+
z: box.z + box.d / 2
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
geometry: new BoxGeometry(box.w, box.h, box.d),
|
|
34
|
+
position: transformPoint3(transform, center),
|
|
35
|
+
rotation: transform.rotation
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
let minX = 1e400;
|
|
39
|
+
let maxX = -1e400;
|
|
40
|
+
let minY = 1e400;
|
|
41
|
+
let maxY = -1e400;
|
|
42
|
+
for (const [x, y] of shape.ring) {
|
|
43
|
+
minX = Math.min(minX, x);
|
|
44
|
+
maxX = Math.max(maxX, x);
|
|
45
|
+
minY = Math.min(minY, y);
|
|
46
|
+
maxY = Math.max(maxY, y);
|
|
47
|
+
}
|
|
48
|
+
const centerX = (minX + maxX) / 2;
|
|
49
|
+
const centerY = (minY + maxY) / 2;
|
|
50
|
+
const outline = new Shape(shape.ring.map(([x, y]) => new Vector2(x - centerX, y - centerY)));
|
|
51
|
+
const geometry = new ExtrudeGeometry(outline, {
|
|
52
|
+
depth: shape.d,
|
|
53
|
+
bevelEnabled: false,
|
|
54
|
+
steps: 1,
|
|
55
|
+
curveSegments: 1
|
|
56
|
+
});
|
|
57
|
+
geometry.translate(0, 0, 0 - shape.d / 2);
|
|
58
|
+
return {
|
|
59
|
+
geometry,
|
|
60
|
+
position: {
|
|
61
|
+
x: centerX,
|
|
62
|
+
y: centerY,
|
|
63
|
+
z: shape.z + shape.d / 2
|
|
64
|
+
},
|
|
65
|
+
rotation: NO_ROTATION
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
// src/viewer/theme.ts
|
|
69
|
+
/** The colors of a model of white card with ink edges, for a light page. */
|
|
70
|
+
const LIGHT_THEME = {
|
|
71
|
+
edge: "#22364a",
|
|
72
|
+
accent: "#f2b705",
|
|
73
|
+
floor: "#dfe5ea",
|
|
74
|
+
wall: "#fcfcfb",
|
|
75
|
+
glass: "#8fc4e8",
|
|
76
|
+
glassOpacity: .35,
|
|
77
|
+
outlet: "#55677a",
|
|
78
|
+
levels: {
|
|
79
|
+
unknown: "#55677a",
|
|
80
|
+
ok: "#1f5fa8",
|
|
81
|
+
near: "#d9662f",
|
|
82
|
+
over: "#c62f2f"
|
|
83
|
+
},
|
|
84
|
+
halo: "#ffffff",
|
|
85
|
+
object: "#b8c0c8",
|
|
86
|
+
objects: {
|
|
87
|
+
desk: "#c9b596",
|
|
88
|
+
sofa: "#8fa3b5",
|
|
89
|
+
rack: "#3d4954",
|
|
90
|
+
computer: "#5b6d7e",
|
|
91
|
+
display: "#2c3640"
|
|
92
|
+
},
|
|
93
|
+
sky: "#ffffff",
|
|
94
|
+
ground: "#b9c3cd"
|
|
95
|
+
};
|
|
96
|
+
/** The colors of a blueprint, for a dark page. */
|
|
97
|
+
const DARK_THEME = {
|
|
98
|
+
edge: "#cfe6ff",
|
|
99
|
+
accent: "#ffd24a",
|
|
100
|
+
floor: "#17395a",
|
|
101
|
+
wall: "#1f4a73",
|
|
102
|
+
glass: "#9fd0ff",
|
|
103
|
+
glassOpacity: .25,
|
|
104
|
+
outlet: "#a9bfd4",
|
|
105
|
+
levels: {
|
|
106
|
+
unknown: "#a9bfd4",
|
|
107
|
+
ok: "#6fb3f2",
|
|
108
|
+
near: "#f5a962",
|
|
109
|
+
over: "#f0625f"
|
|
110
|
+
},
|
|
111
|
+
halo: "#0e2740",
|
|
112
|
+
object: "#2a5580",
|
|
113
|
+
objects: {
|
|
114
|
+
desk: "#2a5a8a",
|
|
115
|
+
sofa: "#28527c",
|
|
116
|
+
rack: "#16324e",
|
|
117
|
+
computer: "#3a74ad",
|
|
118
|
+
display: "#122a42"
|
|
119
|
+
},
|
|
120
|
+
sky: "#ffffff",
|
|
121
|
+
ground: "#7f9bb8"
|
|
122
|
+
};
|
|
123
|
+
// src/viewer/fade.ts
|
|
124
|
+
/** The distance from a face of a wall to the points that find the floor, in meters. */
|
|
125
|
+
const PROBE = .05;
|
|
126
|
+
/** A wall hides the room when it hides a strip of the floor that is deeper than this, in meters. */
|
|
127
|
+
const HIDDEN_LIMIT = .3;
|
|
128
|
+
/** This function gives the distance of a point of the plan across a wall. It is positive to the left of the line of the wall. */
|
|
129
|
+
function across(frame, point) {
|
|
130
|
+
return (point.x - frame.start.x) * frame.left.x + (point.y - frame.start.y) * frame.left.y;
|
|
131
|
+
}
|
|
132
|
+
/** This function tells if a floor is next to one face of a wall. `offset` is the distance across of points near that face. */
|
|
133
|
+
function hasFloor(frame, offset, floors) {
|
|
134
|
+
return [
|
|
135
|
+
.25,
|
|
136
|
+
.5,
|
|
137
|
+
.75
|
|
138
|
+
].some((part) => {
|
|
139
|
+
const point = wallPoint(frame, frame.length * part, offset);
|
|
140
|
+
return floors.some((ring) => pointInRing(point.x, point.y, ring));
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* This function gives each wall that the model makes, with the side that looks at the room.
|
|
145
|
+
*
|
|
146
|
+
* A side looks at the room when a floor is next to it. For a wall with no floor on each side, the side is the side of `center`,
|
|
147
|
+
* a point of the plan in meters. Without a center, such a wall has the value `both`.
|
|
148
|
+
*/
|
|
149
|
+
function wallShades(room, center) {
|
|
150
|
+
const scale = metersPer(room.unit);
|
|
151
|
+
const floors = (room.floors ?? []).map((floor) => floor.ring.map(([x, y]) => [x * scale, y * scale]));
|
|
152
|
+
return wallFrames(room).filter((frame) => wallHasOutline(frame)).map((frame) => {
|
|
153
|
+
const left = hasFloor(frame, frame.leftOffset + PROBE, floors);
|
|
154
|
+
if (left !== hasFloor(frame, frame.rightOffset - PROBE, floors)) return {
|
|
155
|
+
frame,
|
|
156
|
+
inner: left ? "left" : "right"
|
|
157
|
+
};
|
|
158
|
+
if (left || center === void 0) return {
|
|
159
|
+
frame,
|
|
160
|
+
inner: "both"
|
|
161
|
+
};
|
|
162
|
+
const side = across(frame, center);
|
|
163
|
+
return {
|
|
164
|
+
frame,
|
|
165
|
+
inner: side > frame.leftOffset ? "left" : side < frame.rightOffset ? "right" : "both"
|
|
166
|
+
};
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* This function gives the depth of the strip of the floor that a wall hides from a camera, in meters.
|
|
171
|
+
*
|
|
172
|
+
* `camera` is a point of the room in meters, with the z axis up. A camera above the wall, between its two faces, hides no strip.
|
|
173
|
+
* A camera that is not above the top of the wall hides all of the floor behind the wall, and the depth is `Infinity`.
|
|
174
|
+
*/
|
|
175
|
+
function hiddenDepth(frame, camera) {
|
|
176
|
+
const side = across(frame, camera);
|
|
177
|
+
const gap = side > frame.leftOffset ? side - frame.leftOffset : side < frame.rightOffset ? frame.rightOffset - side : 0;
|
|
178
|
+
if (!(gap > 0)) return 0;
|
|
179
|
+
return camera.z > frame.height ? frame.height * gap / (camera.z - frame.height) : 1e400;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* This function gives the `id` of each wall that hides the room from a camera. A viewer makes these walls almost clear.
|
|
183
|
+
*
|
|
184
|
+
* A wall with one side to the room hides the room when the camera is on its other side.
|
|
185
|
+
* A wall between two parts of a room hides the part behind it from each side.
|
|
186
|
+
* `limit` is the depth of the hidden strip of the floor, in meters, above which a wall hides the room.
|
|
187
|
+
*/
|
|
188
|
+
function hidingWalls(shades, camera, limit = HIDDEN_LIMIT) {
|
|
189
|
+
const hiding = /* @__PURE__ */ new Set();
|
|
190
|
+
for (const { frame, inner } of shades) {
|
|
191
|
+
const side = across(frame, camera);
|
|
192
|
+
const cameraSide = side > frame.leftOffset ? "left" : side < frame.rightOffset ? "right" : void 0;
|
|
193
|
+
if (cameraSide === void 0 || cameraSide === inner) continue;
|
|
194
|
+
if (hiddenDepth(frame, camera) > limit) hiding.add(frame.id);
|
|
195
|
+
}
|
|
196
|
+
return hiding;
|
|
197
|
+
}
|
|
198
|
+
/** The width of the line around a sign, as a part of the radius of the disc. */
|
|
199
|
+
const HALO_WIDTH = .36;
|
|
200
|
+
/** The three.js objects of a marker and of a link are above the solids in the sequence of the drawing. */
|
|
201
|
+
const LINK_ORDER = 9;
|
|
202
|
+
const MARKER_ORDER = 10;
|
|
203
|
+
/**
|
|
204
|
+
* This function gives the factor between a sign and the line around it.
|
|
205
|
+
* The line has the same width around each shape, so a shape with a small circle in it gets a larger factor.
|
|
206
|
+
*/
|
|
207
|
+
function haloScale(shape) {
|
|
208
|
+
const { radius, sides } = SHAPE_OUTLINES[shape];
|
|
209
|
+
return 1 + HALO_WIDTH / (radius * Math.cos(Math.PI / sides));
|
|
210
|
+
}
|
|
211
|
+
/** This function makes the geometry of the outline of a shape, in the plane of x and y, with its center at the origin. */
|
|
212
|
+
function outlineGeometry(shape) {
|
|
213
|
+
const { radius, sides, start } = SHAPE_OUTLINES[shape];
|
|
214
|
+
return new CircleGeometry(radius, sides, start);
|
|
215
|
+
}
|
|
216
|
+
/** This function makes the geometry of the sign of a shape. A sign with a hole is a ring, and each other sign is its outline. */
|
|
217
|
+
function signGeometry(shape) {
|
|
218
|
+
const { radius, sides, start, hole } = SHAPE_OUTLINES[shape];
|
|
219
|
+
return hole > 0 ? new RingGeometry(hole, radius, sides, 1, start) : outlineGeometry(shape);
|
|
220
|
+
}
|
|
221
|
+
/** This function makes a material that the solids do not hide. */
|
|
222
|
+
function overlayMaterial() {
|
|
223
|
+
return new MeshBasicMaterial({
|
|
224
|
+
transparent: true,
|
|
225
|
+
depthTest: false,
|
|
226
|
+
depthWrite: false
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* This function makes the marker of an outlet at a point of the space of the viewer.
|
|
231
|
+
* The marker has no colors until `paintPart` gives them, and the viewer gives it its size and its turn for each picture.
|
|
232
|
+
*/
|
|
233
|
+
function makeMarker(at, level) {
|
|
234
|
+
const shape = levelShape(level);
|
|
235
|
+
const root = new Group();
|
|
236
|
+
root.position.set(at.x, at.y, at.z);
|
|
237
|
+
const back = new Mesh(outlineGeometry(shape), overlayMaterial());
|
|
238
|
+
back.scale.setScalar(haloScale(shape));
|
|
239
|
+
const front = new Mesh(signGeometry(shape), overlayMaterial());
|
|
240
|
+
back.renderOrder = MARKER_ORDER;
|
|
241
|
+
front.renderOrder = MARKER_ORDER;
|
|
242
|
+
root.add(back, front);
|
|
243
|
+
return {
|
|
244
|
+
root,
|
|
245
|
+
back,
|
|
246
|
+
front,
|
|
247
|
+
shape,
|
|
248
|
+
radius: 7,
|
|
249
|
+
hidden: false
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
/** This function makes the line of a link between two points of the space of the viewer. The line shows only when the viewer says so. */
|
|
253
|
+
function makeLink(outlet, object, level, from, to) {
|
|
254
|
+
const geometry = new LineSegmentsGeometry().setPositions([
|
|
255
|
+
from.x,
|
|
256
|
+
from.y,
|
|
257
|
+
from.z,
|
|
258
|
+
to.x,
|
|
259
|
+
to.y,
|
|
260
|
+
to.z
|
|
261
|
+
]);
|
|
262
|
+
const material = new LineMaterial({
|
|
263
|
+
linewidth: 2,
|
|
264
|
+
dashed: true,
|
|
265
|
+
dashSize: .09,
|
|
266
|
+
gapSize: .06,
|
|
267
|
+
transparent: true,
|
|
268
|
+
depthTest: false,
|
|
269
|
+
depthWrite: false
|
|
270
|
+
});
|
|
271
|
+
const line = new LineSegments2(geometry, material);
|
|
272
|
+
line.computeLineDistances();
|
|
273
|
+
line.renderOrder = LINK_ORDER;
|
|
274
|
+
line.visible = false;
|
|
275
|
+
return {
|
|
276
|
+
outlet,
|
|
277
|
+
object,
|
|
278
|
+
level,
|
|
279
|
+
line
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
/** This function releases the memory of the graphics card that a marker uses. */
|
|
283
|
+
function disposeMarker(marker) {
|
|
284
|
+
marker.back.geometry.dispose();
|
|
285
|
+
marker.back.material.dispose();
|
|
286
|
+
marker.front.geometry.dispose();
|
|
287
|
+
marker.front.material.dispose();
|
|
288
|
+
marker.root.clear();
|
|
289
|
+
}
|
|
290
|
+
/** This function releases the memory of the graphics card that a link uses. */
|
|
291
|
+
function disposeLink(link) {
|
|
292
|
+
link.line.geometry.dispose();
|
|
293
|
+
link.line.material.dispose();
|
|
294
|
+
}
|
|
295
|
+
// src/viewer/view.ts
|
|
296
|
+
/** The view of a dollhouse: from the south-east, 38 degrees above the floor. */
|
|
297
|
+
const HOME_VIEW = {
|
|
298
|
+
polar: 52 * Math.PI / 180,
|
|
299
|
+
azimuth: 35 * Math.PI / 180
|
|
300
|
+
};
|
|
301
|
+
/** The view of a plan: from above, with north at the top. */
|
|
302
|
+
const TOP_VIEW = {
|
|
303
|
+
polar: 0,
|
|
304
|
+
azimuth: 0
|
|
305
|
+
};
|
|
306
|
+
/** This function gives a point of the room in the space of the viewer: the z axis of the room becomes the y axis. */
|
|
307
|
+
function toViewer(point) {
|
|
308
|
+
return {
|
|
309
|
+
x: point.x,
|
|
310
|
+
y: point.z,
|
|
311
|
+
z: 0 - point.y
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
/** This function gives a point of the viewer in the space of the room: the y axis of the viewer becomes the z axis. */
|
|
315
|
+
function toRoom(point) {
|
|
316
|
+
return {
|
|
317
|
+
x: point.x,
|
|
318
|
+
y: 0 - point.z,
|
|
319
|
+
z: point.y
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
/** This function gives the axes of the camera of a view. At a polar angle of 0, the top of the picture is north. */
|
|
323
|
+
function viewAxes(view) {
|
|
324
|
+
const sinPolar = Math.sin(view.polar);
|
|
325
|
+
const cosPolar = Math.cos(view.polar);
|
|
326
|
+
const sinAzimuth = Math.sin(view.azimuth);
|
|
327
|
+
const cosAzimuth = Math.cos(view.azimuth);
|
|
328
|
+
return {
|
|
329
|
+
back: {
|
|
330
|
+
x: sinPolar * sinAzimuth,
|
|
331
|
+
y: cosPolar,
|
|
332
|
+
z: sinPolar * cosAzimuth
|
|
333
|
+
},
|
|
334
|
+
right: {
|
|
335
|
+
x: cosAzimuth,
|
|
336
|
+
y: 0,
|
|
337
|
+
z: 0 - sinAzimuth
|
|
338
|
+
},
|
|
339
|
+
up: {
|
|
340
|
+
x: 0 - cosPolar * sinAzimuth,
|
|
341
|
+
y: sinPolar,
|
|
342
|
+
z: 0 - cosPolar * cosAzimuth
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
/** This function gives the dot product of the vector from `origin` to `point` with `axis`. */
|
|
347
|
+
function along(point, origin, axis) {
|
|
348
|
+
return (point.x - origin.x) * axis.x + (point.y - origin.y) * axis.y + (point.z - origin.z) * axis.z;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* This function gives the smallest distance from `target` at which a camera shows each point.
|
|
352
|
+
*
|
|
353
|
+
* The camera looks at `target` along the direction of `view`. `fov` is the vertical angle of the picture in degrees,
|
|
354
|
+
* and `aspect` is the width of the picture divided by its height. The points and the target are in the space of the viewer.
|
|
355
|
+
* The function gives 0 for no points, and for an angle or an aspect that makes no picture.
|
|
356
|
+
*/
|
|
357
|
+
function fitDistance(points, target, view, fov, aspect) {
|
|
358
|
+
const tanUp = Math.tan(fov * Math.PI / 360);
|
|
359
|
+
const tanRight = tanUp * aspect;
|
|
360
|
+
if (!(tanUp > 0) || !(tanRight > 0) || !Number.isFinite(tanRight)) return 0;
|
|
361
|
+
const axes = viewAxes(view);
|
|
362
|
+
let distance = 0;
|
|
363
|
+
for (const point of points) {
|
|
364
|
+
const depth = along(point, target, axes.back);
|
|
365
|
+
const needed = Math.max(Math.abs(along(point, target, axes.right)) / tanRight, Math.abs(along(point, target, axes.up)) / tanUp);
|
|
366
|
+
distance = Math.max(distance, depth + needed);
|
|
367
|
+
}
|
|
368
|
+
return distance;
|
|
369
|
+
}
|
|
370
|
+
/** This function gives the position of a camera at a distance from `target`, in the direction of `view`. */
|
|
371
|
+
function cameraPosition(target, view, distance) {
|
|
372
|
+
const { back } = viewAxes(view);
|
|
373
|
+
return {
|
|
374
|
+
x: target.x + back.x * distance,
|
|
375
|
+
y: target.y + back.y * distance,
|
|
376
|
+
z: target.z + back.z * distance
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
/** This function gives the angles of the view from `target` to a camera, and the distance between them. A camera at the target gives the angles 0. */
|
|
380
|
+
function viewOf(camera, target) {
|
|
381
|
+
const x = camera.x - target.x;
|
|
382
|
+
const y = camera.y - target.y;
|
|
383
|
+
const z = camera.z - target.z;
|
|
384
|
+
const distance = Math.hypot(x, y, z);
|
|
385
|
+
if (!(distance > 0)) return {
|
|
386
|
+
polar: 0,
|
|
387
|
+
azimuth: 0,
|
|
388
|
+
distance: 0
|
|
389
|
+
};
|
|
390
|
+
return {
|
|
391
|
+
polar: Math.acos(Math.min(Math.max(y / distance, -1), 1)),
|
|
392
|
+
azimuth: Math.atan2(x, z),
|
|
393
|
+
distance
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
/** This function gives the angle between two azimuths along the shortest way, from −π to π. */
|
|
397
|
+
function turnBetween(from, to) {
|
|
398
|
+
const full = 2 * Math.PI;
|
|
399
|
+
const turn = ((to - from) % full + full) % full;
|
|
400
|
+
return turn > Math.PI ? turn - full : turn;
|
|
401
|
+
}
|
|
402
|
+
// src/viewer/model.ts
|
|
403
|
+
/** This function gives the name of a record of a kind. */
|
|
404
|
+
function recordName(kind, source) {
|
|
405
|
+
return `${kind}:${source}`;
|
|
406
|
+
}
|
|
407
|
+
/** The width of the edges of each kind of solid, in CSS pixels. */
|
|
408
|
+
const EDGE_WIDTH = {
|
|
409
|
+
floor: 1.25,
|
|
410
|
+
wall: 1.25,
|
|
411
|
+
glass: 1,
|
|
412
|
+
outlet: 1,
|
|
413
|
+
object: 1.25
|
|
414
|
+
};
|
|
415
|
+
/** The part of a clear box of objects that is not clear. */
|
|
416
|
+
const HOLDER_OPACITY = .14;
|
|
417
|
+
/** A part with an opacity above this value writes its depth, as a solid part does. */
|
|
418
|
+
const SOLID_OPACITY = .999;
|
|
419
|
+
/** This function gives the `id` of each placed object that has an object in its box. */
|
|
420
|
+
function holders(scene) {
|
|
421
|
+
const scale = metersPer(scene.room.unit);
|
|
422
|
+
const objects = new Map((scene.objects ?? []).map((object) => [object.id, object]));
|
|
423
|
+
const found = /* @__PURE__ */ new Set();
|
|
424
|
+
for (const object of scene.objects ?? []) {
|
|
425
|
+
const parent = object.parent === void 0 ? void 0 : objects.get(object.parent);
|
|
426
|
+
if (parent === void 0) continue;
|
|
427
|
+
const x = object.at[0] * scale;
|
|
428
|
+
const y = object.at[1] * scale;
|
|
429
|
+
const z = ((object.lift ?? 0) + object.size[2] / 2) * scale;
|
|
430
|
+
const [w, h, d] = [
|
|
431
|
+
parent.size[0] * scale,
|
|
432
|
+
parent.size[1] * scale,
|
|
433
|
+
parent.size[2] * scale
|
|
434
|
+
];
|
|
435
|
+
if (Math.abs(x) < w / 2 && Math.abs(y) < h / 2 && z > 0 && z < d) found.add(parent.id);
|
|
436
|
+
}
|
|
437
|
+
return found;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* This function makes the three.js objects of a scene: one mesh with its edges for each solid.
|
|
441
|
+
* Each outlet has a marker, and each placed object with an outlet has a link to it.
|
|
442
|
+
* The parts have no colors until `paintPart` gives them. The caller disposes the model with `disposeModel`.
|
|
443
|
+
*/
|
|
444
|
+
function buildModel(scene, options = {}, loads = {}) {
|
|
445
|
+
const solids = sceneSolids(scene, options);
|
|
446
|
+
const levels = outletLevels(scene, loads);
|
|
447
|
+
const wallOfOpening = new Map((scene.room.openings ?? []).map((opening) => [opening.id, opening.wall]));
|
|
448
|
+
const wallOfOutlet = new Map((scene.room.outlets ?? []).map((outlet) => [outlet.id, outlet.wall]));
|
|
449
|
+
const parentOf = new Map((scene.objects ?? []).map((object) => [object.id, object.parent]));
|
|
450
|
+
const holding = holders(scene);
|
|
451
|
+
const group = new Group();
|
|
452
|
+
const overlay = new Group();
|
|
453
|
+
const centers = /* @__PURE__ */ new Map();
|
|
454
|
+
group.quaternion.set(TO_Y_UP.rotation.x, TO_Y_UP.rotation.y, TO_Y_UP.rotation.z, TO_Y_UP.rotation.w);
|
|
455
|
+
const parts = solids.map((solid) => {
|
|
456
|
+
const { geometry, position, rotation } = solidGeometry(solid);
|
|
457
|
+
const mesh = new Mesh(geometry, new MeshLambertMaterial({
|
|
458
|
+
polygonOffset: true,
|
|
459
|
+
polygonOffsetFactor: 1,
|
|
460
|
+
polygonOffsetUnits: 1
|
|
461
|
+
}));
|
|
462
|
+
mesh.position.set(position.x, position.y, position.z);
|
|
463
|
+
mesh.quaternion.set(rotation.x, rotation.y, rotation.z, rotation.w);
|
|
464
|
+
const edges = new EdgesGeometry(geometry, 30);
|
|
465
|
+
const lines = new LineSegments2(new LineSegmentsGeometry().fromEdgesGeometry(edges), new LineMaterial({ linewidth: EDGE_WIDTH[solid.kind] }));
|
|
466
|
+
edges.dispose();
|
|
467
|
+
mesh.add(lines);
|
|
468
|
+
group.add(mesh);
|
|
469
|
+
const parent = solid.kind === "object" ? parentOf.get(solid.source) : void 0;
|
|
470
|
+
const record = recordName(solid.kind, solid.source);
|
|
471
|
+
const center = toViewer(centerOf(solid));
|
|
472
|
+
centers.set(record, center);
|
|
473
|
+
const level = solid.kind === "outlet" ? levels.get(solid.source) : void 0;
|
|
474
|
+
const marker = solid.kind === "outlet" ? makeMarker(center, level) : void 0;
|
|
475
|
+
if (marker !== void 0) overlay.add(marker.root);
|
|
476
|
+
return {
|
|
477
|
+
solid,
|
|
478
|
+
record,
|
|
479
|
+
wall: solid.kind === "wall" ? solid.source : solid.kind === "glass" ? wallOfOpening.get(solid.source) : solid.kind === "outlet" ? wallOfOutlet.get(solid.source) : void 0,
|
|
480
|
+
holder: parent === void 0 ? void 0 : recordName("object", parent),
|
|
481
|
+
holds: solid.kind === "object" && holding.has(solid.source),
|
|
482
|
+
level,
|
|
483
|
+
marker,
|
|
484
|
+
mesh,
|
|
485
|
+
lines,
|
|
486
|
+
fade: 0
|
|
487
|
+
};
|
|
488
|
+
});
|
|
489
|
+
const links = [];
|
|
490
|
+
for (const object of scene.objects ?? []) {
|
|
491
|
+
if (object.outlet === void 0) continue;
|
|
492
|
+
const outlet = recordName("outlet", object.outlet);
|
|
493
|
+
const user = recordName("object", object.id);
|
|
494
|
+
const from = centers.get(outlet);
|
|
495
|
+
const to = centers.get(user);
|
|
496
|
+
if (from === void 0 || to === void 0) continue;
|
|
497
|
+
const link = makeLink(outlet, user, levels.get(object.outlet), from, to);
|
|
498
|
+
overlay.add(link.line);
|
|
499
|
+
links.push(link);
|
|
500
|
+
}
|
|
501
|
+
const bounds = boundsOfSolids(solids);
|
|
502
|
+
const middle = bounds === void 0 ? {
|
|
503
|
+
x: 0,
|
|
504
|
+
y: 0,
|
|
505
|
+
z: 0
|
|
506
|
+
} : {
|
|
507
|
+
x: bounds.x + bounds.w / 2,
|
|
508
|
+
y: bounds.y + bounds.h / 2,
|
|
509
|
+
z: bounds.z + bounds.d / 2
|
|
510
|
+
};
|
|
511
|
+
return {
|
|
512
|
+
group,
|
|
513
|
+
overlay,
|
|
514
|
+
parts,
|
|
515
|
+
links,
|
|
516
|
+
corners: solids.flatMap((solid) => solidCorners(solid).map((corner) => toViewer(corner))),
|
|
517
|
+
center: toViewer(middle),
|
|
518
|
+
shades: wallShades(scene.room, bounds === void 0 ? void 0 : middle)
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
/** This function gives the center of a solid, in the frame of the room: the mean of its corners. */
|
|
522
|
+
function centerOf(solid) {
|
|
523
|
+
const corners = solidCorners(solid);
|
|
524
|
+
const sum = corners.reduce((total, corner) => ({
|
|
525
|
+
x: total.x + corner.x,
|
|
526
|
+
y: total.y + corner.y,
|
|
527
|
+
z: total.z + corner.z
|
|
528
|
+
}), {
|
|
529
|
+
x: 0,
|
|
530
|
+
y: 0,
|
|
531
|
+
z: 0
|
|
532
|
+
});
|
|
533
|
+
const count = Math.max(corners.length, 1);
|
|
534
|
+
return {
|
|
535
|
+
x: sum.x / count,
|
|
536
|
+
y: sum.y / count,
|
|
537
|
+
z: sum.z / count
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
/** This function sets the opacity of a material. A material that is not solid does not write its depth. */
|
|
541
|
+
function setOpacity(material, opacity) {
|
|
542
|
+
const clear = opacity < SOLID_OPACITY;
|
|
543
|
+
if (material.transparent !== clear) {
|
|
544
|
+
material.transparent = clear;
|
|
545
|
+
material.depthWrite = !clear;
|
|
546
|
+
material.needsUpdate = true;
|
|
547
|
+
}
|
|
548
|
+
material.opacity = opacity;
|
|
549
|
+
}
|
|
550
|
+
/** This function gives the color of a part in a theme, before a mark. */
|
|
551
|
+
function baseColor(part, scene, theme) {
|
|
552
|
+
const kind = part.solid.kind;
|
|
553
|
+
if (kind === "outlet") return part.level === void 0 ? theme.outlet : theme.levels[part.level];
|
|
554
|
+
if (kind !== "object") return theme[kind];
|
|
555
|
+
const object = (scene.objects ?? []).find((candidate) => candidate.id === part.solid.source);
|
|
556
|
+
return (object === void 0 ? void 0 : theme.objects[object.kind]) ?? theme.object;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* This function gives a part its colors: the colors of the theme, the fade of the part and its mark.
|
|
560
|
+
* A marked part has some of the color `accent` of the theme, and a selected part has wide edges.
|
|
561
|
+
* An outlet has the color of the level of its circuit, and its marker has the same color.
|
|
562
|
+
*/
|
|
563
|
+
function paintPart(part, scene, theme, mark) {
|
|
564
|
+
const kind = part.solid.kind;
|
|
565
|
+
const fill = part.mesh.material;
|
|
566
|
+
const ink = part.lines.material;
|
|
567
|
+
const color = baseColor(part, scene, theme);
|
|
568
|
+
fill.color.set(color);
|
|
569
|
+
if (mark !== 0) fill.color.lerp(new Color(theme.accent), mark === 2 ? .55 : .3);
|
|
570
|
+
ink.color.set(theme.edge);
|
|
571
|
+
ink.linewidth = EDGE_WIDTH[kind] * (mark === 2 ? 2 : 1);
|
|
572
|
+
const still = 1 - part.fade;
|
|
573
|
+
let opacity = kind === "glass" ? theme.glassOpacity : part.holds ? mark === 0 ? HOLDER_OPACITY : .45 : 1;
|
|
574
|
+
let edge = 1;
|
|
575
|
+
if (kind === "wall") {
|
|
576
|
+
opacity *= .08 + .92 * still;
|
|
577
|
+
edge = .3 + .7 * still;
|
|
578
|
+
} else if (kind === "glass" || kind === "outlet") {
|
|
579
|
+
opacity *= .35 + .65 * still;
|
|
580
|
+
edge = .35 + .65 * still;
|
|
581
|
+
}
|
|
582
|
+
setOpacity(fill, opacity);
|
|
583
|
+
setOpacity(ink, edge);
|
|
584
|
+
if (part.marker !== void 0) paintMarker(part.marker, color, theme, mark, still);
|
|
585
|
+
}
|
|
586
|
+
/** The part of a marker that is not clear while a solid hides its outlet. */
|
|
587
|
+
const HIDDEN_MARKER = .45;
|
|
588
|
+
/** This function gives a marker its colors and its size. `still` is 1 for a marker on a solid wall and 0 on a clear wall. */
|
|
589
|
+
function paintMarker(marker, color, theme, mark, still) {
|
|
590
|
+
marker.front.material.color.set(color);
|
|
591
|
+
marker.back.material.color.set(mark === 0 ? theme.halo : theme.accent);
|
|
592
|
+
const opacity = mark === 0 ? (.35 + .65 * still) * (marker.hidden ? HIDDEN_MARKER : 1) : 1;
|
|
593
|
+
marker.front.material.opacity = opacity;
|
|
594
|
+
marker.back.material.opacity = opacity;
|
|
595
|
+
marker.radius = 7 * (mark === 2 ? 1.3 : mark === 1 ? 1.15 : 1);
|
|
596
|
+
}
|
|
597
|
+
/** This function gives a link the color of the level of its circuit, and shows it or hides it. */
|
|
598
|
+
function paintLink(link, theme, visible) {
|
|
599
|
+
link.line.material.color.set(link.level === void 0 ? theme.outlet : theme.levels[link.level]);
|
|
600
|
+
link.line.visible = visible;
|
|
601
|
+
}
|
|
602
|
+
/** This function releases the memory of the graphics card that a model uses. */
|
|
603
|
+
function disposeModel(model) {
|
|
604
|
+
for (const part of model.parts) {
|
|
605
|
+
part.mesh.geometry.dispose();
|
|
606
|
+
part.mesh.material.dispose();
|
|
607
|
+
part.lines.geometry.dispose();
|
|
608
|
+
part.lines.material.dispose();
|
|
609
|
+
if (part.marker !== void 0) disposeMarker(part.marker);
|
|
610
|
+
}
|
|
611
|
+
for (const link of model.links) disposeLink(link);
|
|
612
|
+
model.group.clear();
|
|
613
|
+
model.overlay.clear();
|
|
614
|
+
}
|
|
615
|
+
// src/viewer/viewer.ts
|
|
616
|
+
/** The vertical angle of the picture, in degrees. A small angle gives the look of a drawing. */
|
|
617
|
+
const FIELD_OF_VIEW = 32;
|
|
618
|
+
/** The space around the model in a view that shows all of it, as a factor of the distance. */
|
|
619
|
+
const MARGIN = 1.06;
|
|
620
|
+
/** The time of the move between two views, in milliseconds. */
|
|
621
|
+
const MOVE_TIME = 380;
|
|
622
|
+
/** The time in which a wall goes most of the way to clear or to solid, in seconds. */
|
|
623
|
+
const FADE_TIME = .11;
|
|
624
|
+
/** A pointer that moves less than this distance between down and up is a click, in CSS pixels. */
|
|
625
|
+
const CLICK_DISTANCE = 4;
|
|
626
|
+
/** The angle of one step of an arrow key, in radians. */
|
|
627
|
+
const KEY_TURN = Math.PI / 24;
|
|
628
|
+
/** The smallest polar angle. At 0, the azimuth of a camera has no meaning. */
|
|
629
|
+
const SMALLEST_POLAR = 5e-4;
|
|
630
|
+
/** The largest polar angle: the camera stays above the floor. */
|
|
631
|
+
const LARGEST_POLAR = Math.PI / 2 - .05;
|
|
632
|
+
/** A solid hides a marker when it is this distance or more before the outlet, in meters. */
|
|
633
|
+
const SIGHT_MARGIN = .06;
|
|
634
|
+
/** This function tells if the person asks for less motion. */
|
|
635
|
+
function lessMotion() {
|
|
636
|
+
return typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* This function makes a viewer of a scene in a container of a page. The viewer fills the container, so give the container a size.
|
|
640
|
+
*
|
|
641
|
+
* A person turns the model with a drag, and moves nearer with the wheel after a click in the picture.
|
|
642
|
+
* The walls between the camera and the room become almost clear. A click selects the record of a solid.
|
|
643
|
+
* Each outlet has a marker with the color and the shape of the level of its circuit. A marker keeps its size in the picture.
|
|
644
|
+
* A marked outlet or object shows a line from the outlet to each object that takes its power from it.
|
|
645
|
+
* With the keyboard, the arrow keys turn the model, `+` and `-` change the distance, `Home` shows the first view and `T` shows the plan.
|
|
646
|
+
*
|
|
647
|
+
* The function gives `undefined` when the browser has no WebGL 2. It does not throw, and it writes no error to the console.
|
|
648
|
+
*/
|
|
649
|
+
function createRoomViewer(container, options) {
|
|
650
|
+
const canvas = container.ownerDocument.createElement("canvas");
|
|
651
|
+
const context = canvas.getContext("webgl2", {
|
|
652
|
+
alpha: true,
|
|
653
|
+
antialias: true,
|
|
654
|
+
depth: true,
|
|
655
|
+
stencil: false,
|
|
656
|
+
premultipliedAlpha: true,
|
|
657
|
+
powerPreference: "default"
|
|
658
|
+
});
|
|
659
|
+
if (context === null) return void 0;
|
|
660
|
+
let renderer;
|
|
661
|
+
try {
|
|
662
|
+
renderer = new WebGLRenderer({
|
|
663
|
+
canvas,
|
|
664
|
+
context,
|
|
665
|
+
alpha: true,
|
|
666
|
+
antialias: true
|
|
667
|
+
});
|
|
668
|
+
} catch {
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
renderer.setClearColor(0, 0);
|
|
672
|
+
canvas.style.display = "block";
|
|
673
|
+
canvas.style.width = "100%";
|
|
674
|
+
canvas.style.height = "100%";
|
|
675
|
+
canvas.style.cursor = "grab";
|
|
676
|
+
canvas.tabIndex = 0;
|
|
677
|
+
canvas.setAttribute("role", "application");
|
|
678
|
+
canvas.setAttribute("aria-label", options.label ?? "3D model of the room");
|
|
679
|
+
container.append(canvas);
|
|
680
|
+
const world = new Scene();
|
|
681
|
+
const sky = new HemisphereLight("#ffffff", "#ffffff", 2.2);
|
|
682
|
+
const sun = new DirectionalLight("#ffffff", 1.5);
|
|
683
|
+
sun.position.set(-10, 24, 15);
|
|
684
|
+
world.add(sky, sun);
|
|
685
|
+
const camera = new PerspectiveCamera(FIELD_OF_VIEW, 1, .05, 1e3);
|
|
686
|
+
let scene = options.scene;
|
|
687
|
+
let theme = options.theme ?? LIGHT_THEME;
|
|
688
|
+
let model = buildModel(scene, options.solids, options.loads);
|
|
689
|
+
world.add(model.group, model.overlay);
|
|
690
|
+
let hovered;
|
|
691
|
+
let selected;
|
|
692
|
+
let marked = /* @__PURE__ */ new Set();
|
|
693
|
+
let hiding = /* @__PURE__ */ new Set();
|
|
694
|
+
let view = "home";
|
|
695
|
+
let move;
|
|
696
|
+
/** This value is true after a person moved the view. Then a new size of the container does not move the camera. */
|
|
697
|
+
let moved = false;
|
|
698
|
+
/** The height of the picture, in CSS pixels. */
|
|
699
|
+
let viewHeight = 1;
|
|
700
|
+
let frame = 0;
|
|
701
|
+
let lastTime = 0;
|
|
702
|
+
let disposed = false;
|
|
703
|
+
const invalidate = () => {
|
|
704
|
+
if (frame === 0 && !disposed) frame = requestAnimationFrame(tick);
|
|
705
|
+
};
|
|
706
|
+
const controls = new OrbitControls(camera, canvas);
|
|
707
|
+
controls.enableDamping = !lessMotion();
|
|
708
|
+
controls.dampingFactor = .14;
|
|
709
|
+
controls.maxPolarAngle = LARGEST_POLAR;
|
|
710
|
+
controls.rotateSpeed = .9;
|
|
711
|
+
controls.screenSpacePanning = true;
|
|
712
|
+
controls.enableZoom = false;
|
|
713
|
+
const markOfRecord = (record) => record === selected ? 2 : record === hovered || marked.has(record) ? 1 : 0;
|
|
714
|
+
const markOf = (part) => markOfRecord(part.record);
|
|
715
|
+
const paintAll = () => {
|
|
716
|
+
for (const part of model.parts) paintPart(part, scene, theme, markOf(part));
|
|
717
|
+
for (const link of model.links) paintLink(link, theme, markOfRecord(link.outlet) !== 0 || markOfRecord(link.object) !== 0);
|
|
718
|
+
invalidate();
|
|
719
|
+
};
|
|
720
|
+
const pickOf = (part) => ({
|
|
721
|
+
solid: part.solid.id,
|
|
722
|
+
kind: part.solid.kind,
|
|
723
|
+
source: part.solid.source
|
|
724
|
+
});
|
|
725
|
+
const place = (to) => {
|
|
726
|
+
const polar = Math.min(Math.max(to.polar, SMALLEST_POLAR), LARGEST_POLAR);
|
|
727
|
+
const distance = Math.min(Math.max(to.distance, controls.minDistance), controls.maxDistance);
|
|
728
|
+
const eye = cameraPosition(to.target, {
|
|
729
|
+
polar,
|
|
730
|
+
azimuth: to.azimuth
|
|
731
|
+
}, distance);
|
|
732
|
+
controls.target.set(to.target.x, to.target.y, to.target.z);
|
|
733
|
+
camera.position.set(eye.x, eye.y, eye.z);
|
|
734
|
+
camera.lookAt(controls.target);
|
|
735
|
+
};
|
|
736
|
+
const placeNow = () => {
|
|
737
|
+
const target = {
|
|
738
|
+
x: controls.target.x,
|
|
739
|
+
y: controls.target.y,
|
|
740
|
+
z: controls.target.z
|
|
741
|
+
};
|
|
742
|
+
return {
|
|
743
|
+
target,
|
|
744
|
+
...viewOf(camera.position, target)
|
|
745
|
+
};
|
|
746
|
+
};
|
|
747
|
+
const fit = (name) => {
|
|
748
|
+
const angles = name === "top" ? TOP_VIEW : HOME_VIEW;
|
|
749
|
+
const distance = fitDistance(model.corners, model.center, angles, FIELD_OF_VIEW, camera.aspect) * MARGIN;
|
|
750
|
+
return {
|
|
751
|
+
target: model.center,
|
|
752
|
+
...angles,
|
|
753
|
+
distance: distance > 0 ? distance : 10
|
|
754
|
+
};
|
|
755
|
+
};
|
|
756
|
+
/** This function sets the nearest and the farthest distance of the camera from the size of the model. */
|
|
757
|
+
const setLimits = () => {
|
|
758
|
+
const far = Math.max(fit("home").distance, fit("top").distance);
|
|
759
|
+
controls.minDistance = .4;
|
|
760
|
+
controls.maxDistance = far * 3;
|
|
761
|
+
camera.far = far * 8 + 100;
|
|
762
|
+
camera.updateProjectionMatrix();
|
|
763
|
+
};
|
|
764
|
+
/**
|
|
765
|
+
* This function stops the turn that continues after a drag. The camera stays where it is.
|
|
766
|
+
* Without it, the camera continues to turn after a move to a view, and the view is not exact.
|
|
767
|
+
*/
|
|
768
|
+
const stopDrift = () => {
|
|
769
|
+
if (!controls.enableDamping) return;
|
|
770
|
+
const now = placeNow();
|
|
771
|
+
controls.enableDamping = false;
|
|
772
|
+
controls.update();
|
|
773
|
+
controls.enableDamping = true;
|
|
774
|
+
place(now);
|
|
775
|
+
};
|
|
776
|
+
const goTo = (name, animate) => {
|
|
777
|
+
view = name;
|
|
778
|
+
moved = false;
|
|
779
|
+
stopDrift();
|
|
780
|
+
const to = fit(name);
|
|
781
|
+
if (animate && !lessMotion()) move = {
|
|
782
|
+
from: placeNow(),
|
|
783
|
+
to,
|
|
784
|
+
start: performance.now()
|
|
785
|
+
};
|
|
786
|
+
else {
|
|
787
|
+
move = void 0;
|
|
788
|
+
place(to);
|
|
789
|
+
}
|
|
790
|
+
invalidate();
|
|
791
|
+
};
|
|
792
|
+
/** This function sets the size of the picture from the size of the container. It gives false for a container with no size. */
|
|
793
|
+
const setSize = () => {
|
|
794
|
+
const width = container.clientWidth;
|
|
795
|
+
const height = container.clientHeight;
|
|
796
|
+
if (width === 0 || height === 0) return false;
|
|
797
|
+
viewHeight = height;
|
|
798
|
+
renderer.setPixelRatio(Math.min(globalThis.devicePixelRatio, 2));
|
|
799
|
+
renderer.setSize(width, height, false);
|
|
800
|
+
camera.aspect = width / height;
|
|
801
|
+
camera.updateProjectionMatrix();
|
|
802
|
+
return true;
|
|
803
|
+
};
|
|
804
|
+
/** This function moves the camera one step of a move between two views. It gives true while the move continues. */
|
|
805
|
+
const stepMove = (now) => {
|
|
806
|
+
if (move === void 0) return false;
|
|
807
|
+
const { from, to } = move;
|
|
808
|
+
const part = Math.min(Math.max((now - move.start) / MOVE_TIME, 0), 1);
|
|
809
|
+
const eased = part < .5 ? 4 * part * part * part : 1 - (-2 * part + 2) ** 3 / 2;
|
|
810
|
+
const between = (a, b) => a + (b - a) * eased;
|
|
811
|
+
place({
|
|
812
|
+
target: {
|
|
813
|
+
x: between(from.target.x, to.target.x),
|
|
814
|
+
y: between(from.target.y, to.target.y),
|
|
815
|
+
z: between(from.target.z, to.target.z)
|
|
816
|
+
},
|
|
817
|
+
polar: between(from.polar, to.polar),
|
|
818
|
+
azimuth: from.azimuth + turnBetween(from.azimuth, to.azimuth) * eased,
|
|
819
|
+
distance: between(from.distance, to.distance)
|
|
820
|
+
});
|
|
821
|
+
if (part >= 1) move = void 0;
|
|
822
|
+
return true;
|
|
823
|
+
};
|
|
824
|
+
/**
|
|
825
|
+
* This function moves the fade of each wall one step to its goal: clear for a wall that hides the room, solid for the others.
|
|
826
|
+
* `seconds` is the time since the last picture, and 0 sets each goal immediately. It gives true while a wall changes.
|
|
827
|
+
*/
|
|
828
|
+
const stepFade = (seconds) => {
|
|
829
|
+
hiding = hidingWalls(model.shades, toRoom(camera.position));
|
|
830
|
+
const rate = seconds > 0 ? 1 - Math.exp(0 - seconds / FADE_TIME) : 1;
|
|
831
|
+
let active = false;
|
|
832
|
+
for (const part of model.parts) {
|
|
833
|
+
if (part.wall === void 0) continue;
|
|
834
|
+
const goal = hiding.has(part.wall) ? 1 : 0;
|
|
835
|
+
if (part.fade === goal) continue;
|
|
836
|
+
const next = part.fade + (goal - part.fade) * rate;
|
|
837
|
+
part.fade = Math.abs(goal - next) < .01 ? goal : next;
|
|
838
|
+
paintPart(part, scene, theme, markOf(part));
|
|
839
|
+
active = active || part.fade !== goal;
|
|
840
|
+
}
|
|
841
|
+
return active;
|
|
842
|
+
};
|
|
843
|
+
const sight = new Raycaster();
|
|
844
|
+
const forward = new Vector3();
|
|
845
|
+
const offset = new Vector3();
|
|
846
|
+
/**
|
|
847
|
+
* This function turns each marker to the camera and gives it its size in the picture.
|
|
848
|
+
* It also finds each marker with a solid between the camera and its outlet: such a marker is less clear.
|
|
849
|
+
*/
|
|
850
|
+
const stepOverlay = () => {
|
|
851
|
+
const perPixel = 2 * Math.tan(FIELD_OF_VIEW * Math.PI / 360) / viewHeight;
|
|
852
|
+
camera.getWorldDirection(forward);
|
|
853
|
+
model.group.updateMatrixWorld();
|
|
854
|
+
const blockers = model.parts.filter((part) => {
|
|
855
|
+
const kind = part.solid.kind;
|
|
856
|
+
return kind === "floor" || kind === "wall" && !hiding.has(part.solid.source) || kind === "object" && !part.holds;
|
|
857
|
+
}).map((part) => part.mesh);
|
|
858
|
+
for (const part of model.parts) {
|
|
859
|
+
const marker = part.marker;
|
|
860
|
+
if (marker === void 0) continue;
|
|
861
|
+
offset.subVectors(marker.root.position, camera.position);
|
|
862
|
+
const distance = offset.length();
|
|
863
|
+
marker.root.quaternion.copy(camera.quaternion);
|
|
864
|
+
marker.root.scale.setScalar(Math.max(offset.dot(forward), .01) * perPixel * marker.radius);
|
|
865
|
+
sight.set(camera.position, offset.divideScalar(Math.max(distance, 1e-9)));
|
|
866
|
+
sight.far = distance - SIGHT_MARGIN;
|
|
867
|
+
const hidden = sight.far > 0 && sight.intersectObjects(blockers, false).length > 0;
|
|
868
|
+
if (hidden === marker.hidden) continue;
|
|
869
|
+
marker.hidden = hidden;
|
|
870
|
+
paintPart(part, scene, theme, markOf(part));
|
|
871
|
+
}
|
|
872
|
+
};
|
|
873
|
+
function tick(now) {
|
|
874
|
+
frame = 0;
|
|
875
|
+
if (disposed) return;
|
|
876
|
+
const seconds = lastTime === 0 ? 1 / 60 : Math.min((now - lastTime) / 1e3, .1);
|
|
877
|
+
let active = stepMove(now);
|
|
878
|
+
active = controls.update() || active;
|
|
879
|
+
active = stepFade(lessMotion() ? 0 : seconds) || active;
|
|
880
|
+
stepOverlay();
|
|
881
|
+
renderer.render(world, camera);
|
|
882
|
+
lastTime = active ? now : 0;
|
|
883
|
+
if (active) invalidate();
|
|
884
|
+
}
|
|
885
|
+
const raycaster = new Raycaster();
|
|
886
|
+
const pointer = new Vector2();
|
|
887
|
+
/**
|
|
888
|
+
* This function gives the part under a pointer. A wall that hides the room is clear, and the pointer goes through it.
|
|
889
|
+
* The marker of an outlet is above each solid in the picture, so the pointer selects a marker first.
|
|
890
|
+
*/
|
|
891
|
+
const partAt = (event) => {
|
|
892
|
+
const box = canvas.getBoundingClientRect();
|
|
893
|
+
if (box.width === 0 || box.height === 0) return void 0;
|
|
894
|
+
pointer.set((event.clientX - box.left) / box.width * 2 - 1, 1 - (event.clientY - box.top) / box.height * 2);
|
|
895
|
+
raycaster.setFromCamera(pointer, camera);
|
|
896
|
+
const bySign = /* @__PURE__ */ new Map();
|
|
897
|
+
for (const part of model.parts) if (part.marker !== void 0) bySign.set(part.marker.back, part);
|
|
898
|
+
const sign = raycaster.intersectObjects([...bySign.keys()], false)[0];
|
|
899
|
+
if (sign !== void 0) return bySign.get(sign.object);
|
|
900
|
+
const open = model.parts.filter((part) => part.wall === void 0 || !hiding.has(part.wall));
|
|
901
|
+
const byMesh = new Map(open.map((part) => [part.mesh, part]));
|
|
902
|
+
const hits = raycaster.intersectObjects(open.map((part) => part.mesh), false);
|
|
903
|
+
const first = hits[0] === void 0 ? void 0 : byMesh.get(hits[0].object);
|
|
904
|
+
if (first === void 0 || !first.holds) return first;
|
|
905
|
+
for (const hit of hits) {
|
|
906
|
+
const part = byMesh.get(hit.object);
|
|
907
|
+
if (part !== void 0 && part.holder === first.record) return part;
|
|
908
|
+
}
|
|
909
|
+
return first;
|
|
910
|
+
};
|
|
911
|
+
const setHover = (part) => {
|
|
912
|
+
const record = part?.record;
|
|
913
|
+
if (record === hovered) return;
|
|
914
|
+
hovered = record;
|
|
915
|
+
canvas.style.cursor = record === void 0 ? "grab" : "pointer";
|
|
916
|
+
paintAll();
|
|
917
|
+
options.onHover?.(part === void 0 ? void 0 : pickOf(part));
|
|
918
|
+
};
|
|
919
|
+
const listeners = new AbortController();
|
|
920
|
+
const signal = listeners.signal;
|
|
921
|
+
let down;
|
|
922
|
+
const setInUse = (inUse) => {
|
|
923
|
+
controls.enableZoom = inUse;
|
|
924
|
+
};
|
|
925
|
+
canvas.addEventListener("pointerdown", (event) => {
|
|
926
|
+
setInUse(true);
|
|
927
|
+
canvas.focus({ preventScroll: true });
|
|
928
|
+
down = {
|
|
929
|
+
id: event.pointerId,
|
|
930
|
+
x: event.clientX,
|
|
931
|
+
y: event.clientY
|
|
932
|
+
};
|
|
933
|
+
}, { signal });
|
|
934
|
+
canvas.addEventListener("pointermove", (event) => {
|
|
935
|
+
if (event.buttons === 0 && event.pointerType !== "touch") setHover(partAt(event));
|
|
936
|
+
}, { signal });
|
|
937
|
+
canvas.addEventListener("pointerup", (event) => {
|
|
938
|
+
const start = down;
|
|
939
|
+
down = void 0;
|
|
940
|
+
if (start === void 0 || start.id !== event.pointerId) return;
|
|
941
|
+
if (Math.hypot(event.clientX - start.x, event.clientY - start.y) > CLICK_DISTANCE) return;
|
|
942
|
+
const part = partAt(event);
|
|
943
|
+
selected = part?.record;
|
|
944
|
+
paintAll();
|
|
945
|
+
options.onSelect?.(part === void 0 ? void 0 : pickOf(part));
|
|
946
|
+
}, { signal });
|
|
947
|
+
canvas.addEventListener("pointercancel", () => {
|
|
948
|
+
down = void 0;
|
|
949
|
+
}, { signal });
|
|
950
|
+
canvas.addEventListener("pointerleave", (event) => {
|
|
951
|
+
setHover(void 0);
|
|
952
|
+
if (event.pointerType === "mouse") setInUse(false);
|
|
953
|
+
}, { signal });
|
|
954
|
+
canvas.addEventListener("blur", () => setInUse(false), { signal });
|
|
955
|
+
canvas.addEventListener("keydown", (event) => {
|
|
956
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
957
|
+
const now = placeNow();
|
|
958
|
+
let to;
|
|
959
|
+
if (event.key === "ArrowLeft") to = {
|
|
960
|
+
...now,
|
|
961
|
+
azimuth: now.azimuth - KEY_TURN
|
|
962
|
+
};
|
|
963
|
+
else if (event.key === "ArrowRight") to = {
|
|
964
|
+
...now,
|
|
965
|
+
azimuth: now.azimuth + KEY_TURN
|
|
966
|
+
};
|
|
967
|
+
else if (event.key === "ArrowUp") to = {
|
|
968
|
+
...now,
|
|
969
|
+
polar: now.polar - KEY_TURN
|
|
970
|
+
};
|
|
971
|
+
else if (event.key === "ArrowDown") to = {
|
|
972
|
+
...now,
|
|
973
|
+
polar: now.polar + KEY_TURN
|
|
974
|
+
};
|
|
975
|
+
else if (event.key === "+" || event.key === "=") to = {
|
|
976
|
+
...now,
|
|
977
|
+
distance: now.distance * .85
|
|
978
|
+
};
|
|
979
|
+
else if (event.key === "-" || event.key === "_") to = {
|
|
980
|
+
...now,
|
|
981
|
+
distance: now.distance / .85
|
|
982
|
+
};
|
|
983
|
+
else if (event.key === "Home" || event.key === "0") goTo("home", true);
|
|
984
|
+
else if (event.key === "t" || event.key === "T") goTo("top", true);
|
|
985
|
+
else return;
|
|
986
|
+
event.preventDefault();
|
|
987
|
+
setInUse(true);
|
|
988
|
+
if (to === void 0) return;
|
|
989
|
+
moved = true;
|
|
990
|
+
move = void 0;
|
|
991
|
+
place(to);
|
|
992
|
+
invalidate();
|
|
993
|
+
}, { signal });
|
|
994
|
+
canvas.addEventListener("webglcontextlost", (event) => event.preventDefault(), { signal });
|
|
995
|
+
canvas.addEventListener("webglcontextrestored", invalidate, { signal });
|
|
996
|
+
controls.addEventListener("start", () => {
|
|
997
|
+
moved = true;
|
|
998
|
+
move = void 0;
|
|
999
|
+
canvas.style.cursor = "grabbing";
|
|
1000
|
+
invalidate();
|
|
1001
|
+
});
|
|
1002
|
+
controls.addEventListener("end", () => {
|
|
1003
|
+
canvas.style.cursor = hovered === void 0 ? "grab" : "pointer";
|
|
1004
|
+
});
|
|
1005
|
+
controls.addEventListener("change", invalidate);
|
|
1006
|
+
const applyTheme = () => {
|
|
1007
|
+
sky.color.set(theme.sky);
|
|
1008
|
+
sky.groundColor.set(theme.ground);
|
|
1009
|
+
paintAll();
|
|
1010
|
+
};
|
|
1011
|
+
const observer = new ResizeObserver(() => {
|
|
1012
|
+
if (disposed || !setSize()) return;
|
|
1013
|
+
setLimits();
|
|
1014
|
+
if (!moved) goTo(view, false);
|
|
1015
|
+
invalidate();
|
|
1016
|
+
});
|
|
1017
|
+
setSize();
|
|
1018
|
+
setLimits();
|
|
1019
|
+
goTo("home", false);
|
|
1020
|
+
stepFade(0);
|
|
1021
|
+
applyTheme();
|
|
1022
|
+
observer.observe(container);
|
|
1023
|
+
return {
|
|
1024
|
+
canvas,
|
|
1025
|
+
setScene(next) {
|
|
1026
|
+
world.remove(model.group, model.overlay);
|
|
1027
|
+
disposeModel(model);
|
|
1028
|
+
scene = next;
|
|
1029
|
+
model = buildModel(scene, options.solids, options.loads);
|
|
1030
|
+
world.add(model.group, model.overlay);
|
|
1031
|
+
hovered = void 0;
|
|
1032
|
+
if (selected !== void 0 && !model.parts.some((part) => part.record === selected)) selected = void 0;
|
|
1033
|
+
setLimits();
|
|
1034
|
+
if (!moved) goTo(view, false);
|
|
1035
|
+
stepFade(0);
|
|
1036
|
+
paintAll();
|
|
1037
|
+
},
|
|
1038
|
+
setTheme(next) {
|
|
1039
|
+
theme = next;
|
|
1040
|
+
applyTheme();
|
|
1041
|
+
},
|
|
1042
|
+
setView(name) {
|
|
1043
|
+
goTo(name, true);
|
|
1044
|
+
},
|
|
1045
|
+
highlight(records) {
|
|
1046
|
+
marked = new Set(records.map((record) => recordName(record.kind, record.source)));
|
|
1047
|
+
paintAll();
|
|
1048
|
+
},
|
|
1049
|
+
select(record) {
|
|
1050
|
+
selected = record === void 0 ? void 0 : recordName(record.kind, record.source);
|
|
1051
|
+
paintAll();
|
|
1052
|
+
},
|
|
1053
|
+
dispose() {
|
|
1054
|
+
if (disposed) return;
|
|
1055
|
+
disposed = true;
|
|
1056
|
+
cancelAnimationFrame(frame);
|
|
1057
|
+
observer.disconnect();
|
|
1058
|
+
listeners.abort();
|
|
1059
|
+
controls.dispose();
|
|
1060
|
+
world.remove(model.group, model.overlay);
|
|
1061
|
+
disposeModel(model);
|
|
1062
|
+
renderer.dispose();
|
|
1063
|
+
canvas.remove();
|
|
1064
|
+
}
|
|
1065
|
+
};
|
|
1066
|
+
}
|
|
1067
|
+
export { DARK_THEME, LIGHT_THEME, createRoomViewer, solidGeometry };
|
|
1068
|
+
|
|
1069
|
+
//# sourceMappingURL=three.js.map
|