@mark1russell7/room-view 0.2.0 → 0.2.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/README.md +31 -4
- package/build/{layers-bgJ3OTId.js → anchors-J6mcUeJn.js} +572 -90
- package/build/anchors-J6mcUeJn.js.map +1 -0
- package/build/index.d.ts +4 -4
- package/build/index.js +3 -3
- package/build/layers-CtgTEnmt.js +90 -0
- package/build/layers-CtgTEnmt.js.map +1 -0
- package/build/{layers-CKx0dcM0.d.ts → layers-D_HqUjYu.d.ts} +8 -3
- package/build/layers-D_HqUjYu.d.ts.map +1 -0
- package/build/loads-Bp8aH2HJ.d.ts +73 -0
- package/build/loads-Bp8aH2HJ.d.ts.map +1 -0
- package/build/marks.d.ts +1 -1
- package/build/marks.js +2 -1
- package/build/marks.js.map +1 -1
- package/build/{loads-CriZuwcu.d.ts → solids-BHLnvDVu.d.ts} +38 -72
- package/build/solids-BHLnvDVu.d.ts.map +1 -0
- package/build/three.d.ts +51 -17
- package/build/three.d.ts.map +1 -1
- package/build/three.js +288 -674
- package/build/three.js.map +1 -1
- package/package.json +2 -2
- package/build/joins-2p01wnr_.d.ts +0 -39
- package/build/joins-2p01wnr_.d.ts.map +0 -1
- package/build/layers-CKx0dcM0.d.ts.map +0 -1
- package/build/layers-bgJ3OTId.js.map +0 -1
- package/build/loads-CriZuwcu.d.ts.map +0 -1
- package/build/solids-BKb-izIP.js +0 -567
- package/build/solids-BKb-izIP.js.map +0 -1
package/build/three.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { D as wallFrames, O as wallHasOutline, S as solidCorners, _ as TO_Y_UP, d as recordName, f as solidRecord, h as OUTLET_PLATE, k as wallPoint, n as sceneSpace, v as boundsOfSolids, x as sceneSolids } from "./anchors-J6mcUeJn.js";
|
|
2
2
|
import { i as metersPer } from "./units-BZmvVyu4.js";
|
|
3
|
-
import {
|
|
4
|
-
import { pointInRing, transformPoint3 } from "@mark1russell7/layout-contract";
|
|
5
|
-
import { BoxGeometry,
|
|
6
|
-
import {
|
|
3
|
+
import { s as outletLevels } from "./loads-CtHa4EHo.js";
|
|
4
|
+
import { addPoints3, pointInRing, scalePoint3, transformPoint3 } from "@mark1russell7/layout-contract";
|
|
5
|
+
import { BoxGeometry, Color, EdgesGeometry, ExtrudeGeometry, Group, Mesh, MeshLambertMaterial, Shape, Vector2 } from "three";
|
|
6
|
+
import { fitView } from "@mark1russell7/visuals";
|
|
7
|
+
import { createMarkers } from "@mark1russell7/visuals/markers";
|
|
8
|
+
import { checkMarks, transformMarks } from "@mark1russell7/visuals/marks";
|
|
9
|
+
import { createStage, stackContents } from "@mark1russell7/visuals/three";
|
|
7
10
|
import { LineMaterial } from "three/addons/lines/LineMaterial.js";
|
|
8
11
|
import { LineSegments2 } from "three/addons/lines/LineSegments2.js";
|
|
9
12
|
import { LineSegmentsGeometry } from "three/addons/lines/LineSegmentsGeometry.js";
|
|
@@ -196,103 +199,6 @@ function hidingWalls(shades, camera, limit = HIDDEN_LIMIT) {
|
|
|
196
199
|
}
|
|
197
200
|
return hiding;
|
|
198
201
|
}
|
|
199
|
-
/** The width of the line around a sign, as a part of the radius of the disc. */
|
|
200
|
-
const HALO_WIDTH = .36;
|
|
201
|
-
/** The three.js objects of a marker and of a link are above the solids in the sequence of the drawing. */
|
|
202
|
-
const LINK_ORDER = 9;
|
|
203
|
-
const MARKER_ORDER = 10;
|
|
204
|
-
/**
|
|
205
|
-
* This function gives the factor between a sign and the line around it.
|
|
206
|
-
* The line has the same width around each shape, so a shape with a small circle in it gets a larger factor.
|
|
207
|
-
*/
|
|
208
|
-
function haloScale(shape) {
|
|
209
|
-
const { radius, sides } = SHAPE_OUTLINES[shape];
|
|
210
|
-
return 1 + HALO_WIDTH / (radius * Math.cos(Math.PI / sides));
|
|
211
|
-
}
|
|
212
|
-
/** This function makes the geometry of the outline of a shape, in the plane of x and y, with its center at the origin. */
|
|
213
|
-
function outlineGeometry(shape) {
|
|
214
|
-
const { radius, sides, start } = SHAPE_OUTLINES[shape];
|
|
215
|
-
return new CircleGeometry(radius, sides, start);
|
|
216
|
-
}
|
|
217
|
-
/** 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. */
|
|
218
|
-
function signGeometry(shape) {
|
|
219
|
-
const { radius, sides, start, hole } = SHAPE_OUTLINES[shape];
|
|
220
|
-
return hole > 0 ? new RingGeometry(hole, radius, sides, 1, start) : outlineGeometry(shape);
|
|
221
|
-
}
|
|
222
|
-
/** This function makes a material that the solids do not hide. */
|
|
223
|
-
function overlayMaterial() {
|
|
224
|
-
return new MeshBasicMaterial({
|
|
225
|
-
transparent: true,
|
|
226
|
-
depthTest: false,
|
|
227
|
-
depthWrite: false
|
|
228
|
-
});
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* This function makes the marker of an outlet at a point of the space of the viewer.
|
|
232
|
-
* The marker has no colors until `paintPart` gives them, and the viewer gives it its size and its turn for each picture.
|
|
233
|
-
*/
|
|
234
|
-
function makeMarker(at, level) {
|
|
235
|
-
const shape = levelShape(level);
|
|
236
|
-
const root = new Group();
|
|
237
|
-
root.position.set(at.x, at.y, at.z);
|
|
238
|
-
const back = new Mesh(outlineGeometry(shape), overlayMaterial());
|
|
239
|
-
back.scale.setScalar(haloScale(shape));
|
|
240
|
-
const front = new Mesh(signGeometry(shape), overlayMaterial());
|
|
241
|
-
back.renderOrder = MARKER_ORDER;
|
|
242
|
-
front.renderOrder = MARKER_ORDER;
|
|
243
|
-
root.add(back, front);
|
|
244
|
-
return {
|
|
245
|
-
root,
|
|
246
|
-
back,
|
|
247
|
-
front,
|
|
248
|
-
shape,
|
|
249
|
-
radius: 7,
|
|
250
|
-
hidden: false
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
/** 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. */
|
|
254
|
-
function makeLink(outlet, object, level, from, to) {
|
|
255
|
-
const geometry = new LineSegmentsGeometry().setPositions([
|
|
256
|
-
from.x,
|
|
257
|
-
from.y,
|
|
258
|
-
from.z,
|
|
259
|
-
to.x,
|
|
260
|
-
to.y,
|
|
261
|
-
to.z
|
|
262
|
-
]);
|
|
263
|
-
const material = new LineMaterial({
|
|
264
|
-
linewidth: 2,
|
|
265
|
-
dashed: true,
|
|
266
|
-
dashSize: .09,
|
|
267
|
-
gapSize: .06,
|
|
268
|
-
transparent: true,
|
|
269
|
-
depthTest: false,
|
|
270
|
-
depthWrite: false
|
|
271
|
-
});
|
|
272
|
-
const line = new LineSegments2(geometry, material);
|
|
273
|
-
line.computeLineDistances();
|
|
274
|
-
line.renderOrder = LINK_ORDER;
|
|
275
|
-
line.visible = false;
|
|
276
|
-
return {
|
|
277
|
-
outlet,
|
|
278
|
-
object,
|
|
279
|
-
level,
|
|
280
|
-
line
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
/** This function releases the memory of the graphics card that a marker uses. */
|
|
284
|
-
function disposeMarker(marker) {
|
|
285
|
-
marker.back.geometry.dispose();
|
|
286
|
-
marker.back.material.dispose();
|
|
287
|
-
marker.front.geometry.dispose();
|
|
288
|
-
marker.front.material.dispose();
|
|
289
|
-
marker.root.clear();
|
|
290
|
-
}
|
|
291
|
-
/** This function releases the memory of the graphics card that a link uses. */
|
|
292
|
-
function disposeLink(link) {
|
|
293
|
-
link.line.geometry.dispose();
|
|
294
|
-
link.line.material.dispose();
|
|
295
|
-
}
|
|
296
202
|
// src/viewer/view.ts
|
|
297
203
|
/** The view of a dollhouse: from the south-east, 38 degrees above the floor. */
|
|
298
204
|
const HOME_VIEW = {
|
|
@@ -320,91 +226,7 @@ function toRoom(point) {
|
|
|
320
226
|
z: point.y
|
|
321
227
|
};
|
|
322
228
|
}
|
|
323
|
-
/** This function gives the axes of the camera of a view. At a polar angle of 0, the top of the picture is north. */
|
|
324
|
-
function viewAxes(view) {
|
|
325
|
-
const sinPolar = Math.sin(view.polar);
|
|
326
|
-
const cosPolar = Math.cos(view.polar);
|
|
327
|
-
const sinAzimuth = Math.sin(view.azimuth);
|
|
328
|
-
const cosAzimuth = Math.cos(view.azimuth);
|
|
329
|
-
return {
|
|
330
|
-
back: {
|
|
331
|
-
x: sinPolar * sinAzimuth,
|
|
332
|
-
y: cosPolar,
|
|
333
|
-
z: sinPolar * cosAzimuth
|
|
334
|
-
},
|
|
335
|
-
right: {
|
|
336
|
-
x: cosAzimuth,
|
|
337
|
-
y: 0,
|
|
338
|
-
z: 0 - sinAzimuth
|
|
339
|
-
},
|
|
340
|
-
up: {
|
|
341
|
-
x: 0 - cosPolar * sinAzimuth,
|
|
342
|
-
y: sinPolar,
|
|
343
|
-
z: 0 - cosPolar * cosAzimuth
|
|
344
|
-
}
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
/** This function gives the dot product of the vector from `origin` to `point` with `axis`. */
|
|
348
|
-
function along(point, origin, axis) {
|
|
349
|
-
return (point.x - origin.x) * axis.x + (point.y - origin.y) * axis.y + (point.z - origin.z) * axis.z;
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* This function gives the smallest distance from `target` at which a camera shows each point.
|
|
353
|
-
*
|
|
354
|
-
* The camera looks at `target` along the direction of `view`. `fov` is the vertical angle of the picture in degrees,
|
|
355
|
-
* and `aspect` is the width of the picture divided by its height. The points and the target are in the space of the viewer.
|
|
356
|
-
* The function gives 0 for no points, and for an angle or an aspect that makes no picture.
|
|
357
|
-
*/
|
|
358
|
-
function fitDistance(points, target, view, fov, aspect) {
|
|
359
|
-
const tanUp = Math.tan(fov * Math.PI / 360);
|
|
360
|
-
const tanRight = tanUp * aspect;
|
|
361
|
-
if (!(tanUp > 0) || !(tanRight > 0) || !Number.isFinite(tanRight)) return 0;
|
|
362
|
-
const axes = viewAxes(view);
|
|
363
|
-
let distance = 0;
|
|
364
|
-
for (const point of points) {
|
|
365
|
-
const depth = along(point, target, axes.back);
|
|
366
|
-
const needed = Math.max(Math.abs(along(point, target, axes.right)) / tanRight, Math.abs(along(point, target, axes.up)) / tanUp);
|
|
367
|
-
distance = Math.max(distance, depth + needed);
|
|
368
|
-
}
|
|
369
|
-
return distance;
|
|
370
|
-
}
|
|
371
|
-
/** This function gives the position of a camera at a distance from `target`, in the direction of `view`. */
|
|
372
|
-
function cameraPosition(target, view, distance) {
|
|
373
|
-
const { back } = viewAxes(view);
|
|
374
|
-
return {
|
|
375
|
-
x: target.x + back.x * distance,
|
|
376
|
-
y: target.y + back.y * distance,
|
|
377
|
-
z: target.z + back.z * distance
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
/** 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. */
|
|
381
|
-
function viewOf(camera, target) {
|
|
382
|
-
const x = camera.x - target.x;
|
|
383
|
-
const y = camera.y - target.y;
|
|
384
|
-
const z = camera.z - target.z;
|
|
385
|
-
const distance = Math.hypot(x, y, z);
|
|
386
|
-
if (!(distance > 0)) return {
|
|
387
|
-
polar: 0,
|
|
388
|
-
azimuth: 0,
|
|
389
|
-
distance: 0
|
|
390
|
-
};
|
|
391
|
-
return {
|
|
392
|
-
polar: Math.acos(Math.min(Math.max(y / distance, -1), 1)),
|
|
393
|
-
azimuth: Math.atan2(x, z),
|
|
394
|
-
distance
|
|
395
|
-
};
|
|
396
|
-
}
|
|
397
|
-
/** This function gives the angle between two azimuths along the shortest way, from −π to π. */
|
|
398
|
-
function turnBetween(from, to) {
|
|
399
|
-
const full = 2 * Math.PI;
|
|
400
|
-
const turn = ((to - from) % full + full) % full;
|
|
401
|
-
return turn > Math.PI ? turn - full : turn;
|
|
402
|
-
}
|
|
403
229
|
// src/viewer/model.ts
|
|
404
|
-
/** This function gives the name of a record of a kind. */
|
|
405
|
-
function recordName(kind, source) {
|
|
406
|
-
return `${kind}:${source}`;
|
|
407
|
-
}
|
|
408
230
|
/** The width of the edges of each kind of solid, in CSS pixels. */
|
|
409
231
|
const EDGE_WIDTH = {
|
|
410
232
|
floor: 1.25,
|
|
@@ -439,7 +261,6 @@ function holders(scene) {
|
|
|
439
261
|
}
|
|
440
262
|
/**
|
|
441
263
|
* This function makes the three.js objects of a scene: one mesh with its edges for each solid.
|
|
442
|
-
* Each outlet has a marker, and each placed object with an outlet has a link to it.
|
|
443
264
|
* The parts have no colors until `paintPart` gives them. The caller disposes the model with `disposeModel`.
|
|
444
265
|
*/
|
|
445
266
|
function buildModel(scene, options = {}, loads = {}) {
|
|
@@ -450,8 +271,6 @@ function buildModel(scene, options = {}, loads = {}) {
|
|
|
450
271
|
const parentOf = new Map((scene.objects ?? []).map((object) => [object.id, object.parent]));
|
|
451
272
|
const holding = holders(scene);
|
|
452
273
|
const group = new Group();
|
|
453
|
-
const overlay = new Group();
|
|
454
|
-
const centers = /* @__PURE__ */ new Map();
|
|
455
274
|
group.quaternion.set(TO_Y_UP.rotation.x, TO_Y_UP.rotation.y, TO_Y_UP.rotation.z, TO_Y_UP.rotation.w);
|
|
456
275
|
const parts = solids.map((solid) => {
|
|
457
276
|
const { geometry, position, rotation } = solidGeometry(solid);
|
|
@@ -468,37 +287,19 @@ function buildModel(scene, options = {}, loads = {}) {
|
|
|
468
287
|
mesh.add(lines);
|
|
469
288
|
group.add(mesh);
|
|
470
289
|
const parent = solid.kind === "object" ? parentOf.get(solid.source) : void 0;
|
|
471
|
-
const record = recordName(solid.kind, solid.source);
|
|
472
|
-
const center = toViewer(centerOf(solid));
|
|
473
|
-
centers.set(record, center);
|
|
474
|
-
const level = solid.kind === "outlet" ? levels.get(solid.source) : void 0;
|
|
475
|
-
const marker = solid.kind === "outlet" ? makeMarker(center, level) : void 0;
|
|
476
|
-
if (marker !== void 0) overlay.add(marker.root);
|
|
477
290
|
return {
|
|
478
291
|
solid,
|
|
479
|
-
record,
|
|
292
|
+
record: solidRecord(solid.kind, solid.source),
|
|
480
293
|
wall: solid.kind === "wall" ? solid.source : solid.kind === "glass" ? wallOfOpening.get(solid.source) : solid.kind === "outlet" ? wallOfOutlet.get(solid.source) : void 0,
|
|
481
|
-
holder: parent === void 0 ? void 0 :
|
|
294
|
+
holder: parent === void 0 ? void 0 : solidRecord("object", parent),
|
|
482
295
|
holds: solid.kind === "object" && holding.has(solid.source),
|
|
483
|
-
level,
|
|
484
|
-
marker,
|
|
296
|
+
level: solid.kind === "outlet" ? levels.get(solid.source) : void 0,
|
|
485
297
|
mesh,
|
|
486
298
|
lines,
|
|
487
299
|
fade: 0
|
|
488
300
|
};
|
|
489
301
|
});
|
|
490
|
-
|
|
491
|
-
for (const object of scene.objects ?? []) {
|
|
492
|
-
if (object.outlet === void 0) continue;
|
|
493
|
-
const outlet = recordName("outlet", object.outlet);
|
|
494
|
-
const user = recordName("object", object.id);
|
|
495
|
-
const from = centers.get(outlet);
|
|
496
|
-
const to = centers.get(user);
|
|
497
|
-
if (from === void 0 || to === void 0) continue;
|
|
498
|
-
const link = makeLink(outlet, user, levels.get(object.outlet), from, to);
|
|
499
|
-
overlay.add(link.line);
|
|
500
|
-
links.push(link);
|
|
501
|
-
}
|
|
302
|
+
group.updateMatrixWorld(true);
|
|
502
303
|
const bounds = boundsOfSolids(solids);
|
|
503
304
|
const middle = bounds === void 0 ? {
|
|
504
305
|
x: 0,
|
|
@@ -511,33 +312,12 @@ function buildModel(scene, options = {}, loads = {}) {
|
|
|
511
312
|
};
|
|
512
313
|
return {
|
|
513
314
|
group,
|
|
514
|
-
overlay,
|
|
515
315
|
parts,
|
|
516
|
-
links,
|
|
517
316
|
corners: solids.flatMap((solid) => solidCorners(solid).map((corner) => toViewer(corner))),
|
|
518
317
|
center: toViewer(middle),
|
|
519
318
|
shades: wallShades(scene.room, bounds === void 0 ? void 0 : middle)
|
|
520
319
|
};
|
|
521
320
|
}
|
|
522
|
-
/** This function gives the center of a solid, in the frame of the room: the mean of its corners. */
|
|
523
|
-
function centerOf(solid) {
|
|
524
|
-
const corners = solidCorners(solid);
|
|
525
|
-
const sum = corners.reduce((total, corner) => ({
|
|
526
|
-
x: total.x + corner.x,
|
|
527
|
-
y: total.y + corner.y,
|
|
528
|
-
z: total.z + corner.z
|
|
529
|
-
}), {
|
|
530
|
-
x: 0,
|
|
531
|
-
y: 0,
|
|
532
|
-
z: 0
|
|
533
|
-
});
|
|
534
|
-
const count = Math.max(corners.length, 1);
|
|
535
|
-
return {
|
|
536
|
-
x: sum.x / count,
|
|
537
|
-
y: sum.y / count,
|
|
538
|
-
z: sum.z / count
|
|
539
|
-
};
|
|
540
|
-
}
|
|
541
321
|
/** This function sets the opacity of a material. A material that is not solid does not write its depth. */
|
|
542
322
|
function setOpacity(material, opacity) {
|
|
543
323
|
const clear = opacity < SOLID_OPACITY;
|
|
@@ -557,9 +337,9 @@ function baseColor(part, scene, theme) {
|
|
|
557
337
|
return (object === void 0 ? void 0 : theme.objects[object.kind]) ?? theme.object;
|
|
558
338
|
}
|
|
559
339
|
/**
|
|
560
|
-
* This function gives a part its colors: the colors of the theme, the fade of the part and its
|
|
561
|
-
* A
|
|
562
|
-
* An outlet has the color of the level of its circuit
|
|
340
|
+
* This function gives a part its colors: the colors of the theme, the fade of the part and its highlight.
|
|
341
|
+
* A part with a highlight has some of the color `accent` of the theme, and a selected part has wide edges.
|
|
342
|
+
* An outlet has the color of the level of its circuit.
|
|
563
343
|
*/
|
|
564
344
|
function paintPart(part, scene, theme, mark) {
|
|
565
345
|
const kind = part.solid.kind;
|
|
@@ -582,23 +362,6 @@ function paintPart(part, scene, theme, mark) {
|
|
|
582
362
|
}
|
|
583
363
|
setOpacity(fill, opacity);
|
|
584
364
|
setOpacity(ink, edge);
|
|
585
|
-
if (part.marker !== void 0) paintMarker(part.marker, color, theme, mark, still);
|
|
586
|
-
}
|
|
587
|
-
/** The part of a marker that is not clear while a solid hides its outlet. */
|
|
588
|
-
const HIDDEN_MARKER = .45;
|
|
589
|
-
/** 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. */
|
|
590
|
-
function paintMarker(marker, color, theme, mark, still) {
|
|
591
|
-
marker.front.material.color.set(color);
|
|
592
|
-
marker.back.material.color.set(mark === 0 ? theme.halo : theme.accent);
|
|
593
|
-
const opacity = mark === 0 ? (.35 + .65 * still) * (marker.hidden ? HIDDEN_MARKER : 1) : 1;
|
|
594
|
-
marker.front.material.opacity = opacity;
|
|
595
|
-
marker.back.material.opacity = opacity;
|
|
596
|
-
marker.radius = 7 * (mark === 2 ? 1.3 : mark === 1 ? 1.15 : 1);
|
|
597
|
-
}
|
|
598
|
-
/** This function gives a link the color of the level of its circuit, and shows it or hides it. */
|
|
599
|
-
function paintLink(link, theme, visible) {
|
|
600
|
-
link.line.material.color.set(link.level === void 0 ? theme.outlet : theme.levels[link.level]);
|
|
601
|
-
link.line.visible = visible;
|
|
602
365
|
}
|
|
603
366
|
/** This function releases the memory of the graphics card that a model uses. */
|
|
604
367
|
function disposeModel(model) {
|
|
@@ -607,461 +370,312 @@ function disposeModel(model) {
|
|
|
607
370
|
part.mesh.material.dispose();
|
|
608
371
|
part.lines.geometry.dispose();
|
|
609
372
|
part.lines.material.dispose();
|
|
610
|
-
if (part.marker !== void 0) disposeMarker(part.marker);
|
|
611
373
|
}
|
|
612
|
-
for (const link of model.links) disposeLink(link);
|
|
613
374
|
model.group.clear();
|
|
614
|
-
model.overlay.clear();
|
|
615
375
|
}
|
|
616
|
-
// src/viewer/
|
|
617
|
-
/** The vertical angle of the picture, in degrees. A small angle gives the look of a drawing. */
|
|
618
|
-
const FIELD_OF_VIEW = 32;
|
|
619
|
-
/** The space around the model in a view that shows all of it, as a factor of the distance. */
|
|
620
|
-
const MARGIN = 1.06;
|
|
621
|
-
/** The time of the move between two views, in milliseconds. */
|
|
622
|
-
const MOVE_TIME = 380;
|
|
376
|
+
// src/viewer/content.ts
|
|
623
377
|
/** The time in which a wall goes most of the way to clear or to solid, in seconds. */
|
|
624
378
|
const FADE_TIME = .11;
|
|
625
|
-
/** A pointer that moves less than this distance between down and up is a click, in CSS pixels. */
|
|
626
|
-
const CLICK_DISTANCE = 4;
|
|
627
|
-
/** The angle of one step of an arrow key, in radians. */
|
|
628
|
-
const KEY_TURN = Math.PI / 24;
|
|
629
|
-
/** The smallest polar angle. At 0, the azimuth of a camera has no meaning. */
|
|
630
|
-
const SMALLEST_POLAR = 5e-4;
|
|
631
|
-
/** The largest polar angle: the camera stays above the floor. */
|
|
632
|
-
const LARGEST_POLAR = Math.PI / 2 - .05;
|
|
633
|
-
/** A solid hides a marker when it is this distance or more before the outlet, in meters. */
|
|
634
|
-
const SIGHT_MARGIN = .06;
|
|
635
|
-
/** This function tells if the person asks for less motion. */
|
|
636
|
-
function lessMotion() {
|
|
637
|
-
return typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
638
|
-
}
|
|
639
379
|
/**
|
|
640
|
-
* This function makes
|
|
641
|
-
*
|
|
642
|
-
* A person turns the model with a drag, and moves nearer with the wheel after a click in the picture.
|
|
643
|
-
* The walls between the camera and the room become almost clear. A click selects the record of a solid.
|
|
644
|
-
* 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.
|
|
645
|
-
* A marked outlet or object shows a line from the outlet to each object that takes its power from it.
|
|
646
|
-
* With the keyboard, the arrow keys turn the model, `+` and `-` change the distance, `Home` shows the first view and `T` shows the plan.
|
|
380
|
+
* This function makes the content of a stage that shows the solids of a scene.
|
|
381
|
+
* The solids are the floors, the walls, the windows, the plates of the outlets and the placed objects.
|
|
647
382
|
*
|
|
648
|
-
* The
|
|
383
|
+
* The walls between the camera and the room become almost clear, and the pointer goes through them.
|
|
384
|
+
* An object in a clear box of objects gets the pick before the box. Each pick has its distance, so a mark in front of a solid gets the pick.
|
|
385
|
+
* The space of the content has the y axis up: the z axis of the scene becomes the y axis.
|
|
649
386
|
*/
|
|
650
|
-
function
|
|
651
|
-
const
|
|
652
|
-
const
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
stencil: false,
|
|
657
|
-
premultipliedAlpha: true,
|
|
658
|
-
powerPreference: "default"
|
|
659
|
-
});
|
|
660
|
-
if (context === null) return void 0;
|
|
661
|
-
let renderer;
|
|
662
|
-
try {
|
|
663
|
-
renderer = new WebGLRenderer({
|
|
664
|
-
canvas,
|
|
665
|
-
context,
|
|
666
|
-
alpha: true,
|
|
667
|
-
antialias: true
|
|
668
|
-
});
|
|
669
|
-
} catch {
|
|
670
|
-
return;
|
|
671
|
-
}
|
|
672
|
-
renderer.setClearColor(0, 0);
|
|
673
|
-
canvas.style.display = "block";
|
|
674
|
-
canvas.style.width = "100%";
|
|
675
|
-
canvas.style.height = "100%";
|
|
676
|
-
canvas.style.cursor = "grab";
|
|
677
|
-
canvas.tabIndex = 0;
|
|
678
|
-
canvas.setAttribute("role", "application");
|
|
679
|
-
canvas.setAttribute("aria-label", options.label ?? "3D model of the room");
|
|
680
|
-
container.append(canvas);
|
|
681
|
-
const world = new Scene();
|
|
682
|
-
const sky = new HemisphereLight("#ffffff", "#ffffff", 2.2);
|
|
683
|
-
const sun = new DirectionalLight("#ffffff", 1.5);
|
|
684
|
-
sun.position.set(-10, 24, 15);
|
|
685
|
-
world.add(sky, sun);
|
|
686
|
-
const camera = new PerspectiveCamera(FIELD_OF_VIEW, 1, .05, 1e3);
|
|
687
|
-
let scene = options.scene;
|
|
688
|
-
let theme = options.theme ?? LIGHT_THEME;
|
|
689
|
-
let model = buildModel(scene, options.solids, options.loads);
|
|
690
|
-
world.add(model.group, model.overlay);
|
|
691
|
-
let hovered;
|
|
692
|
-
let selected;
|
|
693
|
-
let marked = /* @__PURE__ */ new Set();
|
|
694
|
-
let hiding = /* @__PURE__ */ new Set();
|
|
695
|
-
let view = "home";
|
|
696
|
-
let move;
|
|
697
|
-
/** This value is true after a person moved the view. Then a new size of the container does not move the camera. */
|
|
698
|
-
let moved = false;
|
|
699
|
-
/** The height of the picture, in CSS pixels. */
|
|
700
|
-
let viewHeight = 1;
|
|
701
|
-
let frame = 0;
|
|
702
|
-
let lastTime = 0;
|
|
703
|
-
let disposed = false;
|
|
704
|
-
const invalidate = () => {
|
|
705
|
-
if (frame === 0 && !disposed) frame = requestAnimationFrame(tick);
|
|
706
|
-
};
|
|
707
|
-
const controls = new OrbitControls(camera, canvas);
|
|
708
|
-
controls.enableDamping = !lessMotion();
|
|
709
|
-
controls.dampingFactor = .14;
|
|
710
|
-
controls.maxPolarAngle = LARGEST_POLAR;
|
|
711
|
-
controls.rotateSpeed = .9;
|
|
712
|
-
controls.screenSpacePanning = true;
|
|
713
|
-
controls.enableZoom = false;
|
|
714
|
-
const markOfRecord = (record) => record === selected ? 2 : record === hovered || marked.has(record) ? 1 : 0;
|
|
715
|
-
const markOf = (part) => markOfRecord(part.record);
|
|
716
|
-
const paintAll = () => {
|
|
717
|
-
for (const part of model.parts) paintPart(part, scene, theme, markOf(part));
|
|
718
|
-
for (const link of model.links) paintLink(link, theme, markOfRecord(link.outlet) !== 0 || markOfRecord(link.object) !== 0);
|
|
719
|
-
invalidate();
|
|
720
|
-
};
|
|
721
|
-
const pickOf = (part) => ({
|
|
387
|
+
function createRoomContent(scene, solids = {}, loads = {}) {
|
|
388
|
+
const model = buildModel(scene, solids, loads);
|
|
389
|
+
const first = /* @__PURE__ */ new Map();
|
|
390
|
+
for (const part of model.parts) if (!first.has(part.record)) first.set(part.record, part);
|
|
391
|
+
const byMesh = new Map(model.parts.map((part) => [part.mesh, part]));
|
|
392
|
+
const pickOfPart = (part) => ({
|
|
722
393
|
solid: part.solid.id,
|
|
723
394
|
kind: part.solid.kind,
|
|
724
395
|
source: part.solid.source
|
|
725
396
|
});
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
}, distance);
|
|
733
|
-
controls.target.set(to.target.x, to.target.y, to.target.z);
|
|
734
|
-
camera.position.set(eye.x, eye.y, eye.z);
|
|
735
|
-
camera.lookAt(controls.target);
|
|
736
|
-
};
|
|
737
|
-
const placeNow = () => {
|
|
738
|
-
const target = {
|
|
739
|
-
x: controls.target.x,
|
|
740
|
-
y: controls.target.y,
|
|
741
|
-
z: controls.target.z
|
|
742
|
-
};
|
|
743
|
-
return {
|
|
744
|
-
target,
|
|
745
|
-
...viewOf(camera.position, target)
|
|
746
|
-
};
|
|
747
|
-
};
|
|
748
|
-
const fit = (name) => {
|
|
749
|
-
const angles = name === "top" ? TOP_VIEW : HOME_VIEW;
|
|
750
|
-
const distance = fitDistance(model.corners, model.center, angles, FIELD_OF_VIEW, camera.aspect) * MARGIN;
|
|
751
|
-
return {
|
|
752
|
-
target: model.center,
|
|
753
|
-
...angles,
|
|
754
|
-
distance: distance > 0 ? distance : 10
|
|
755
|
-
};
|
|
756
|
-
};
|
|
757
|
-
/** This function sets the nearest and the farthest distance of the camera from the size of the model. */
|
|
758
|
-
const setLimits = () => {
|
|
759
|
-
const far = Math.max(fit("home").distance, fit("top").distance);
|
|
760
|
-
controls.minDistance = .4;
|
|
761
|
-
controls.maxDistance = far * 3;
|
|
762
|
-
camera.far = far * 8 + 100;
|
|
763
|
-
camera.updateProjectionMatrix();
|
|
764
|
-
};
|
|
765
|
-
/**
|
|
766
|
-
* This function stops the turn that continues after a drag. The camera stays where it is.
|
|
767
|
-
* Without it, the camera continues to turn after a move to a view, and the view is not exact.
|
|
768
|
-
*/
|
|
769
|
-
const stopDrift = () => {
|
|
770
|
-
if (!controls.enableDamping) return;
|
|
771
|
-
const now = placeNow();
|
|
772
|
-
controls.enableDamping = false;
|
|
773
|
-
controls.update();
|
|
774
|
-
controls.enableDamping = true;
|
|
775
|
-
place(now);
|
|
397
|
+
/** The walls that hide the room from the camera of the last step. */
|
|
398
|
+
let hiding = /* @__PURE__ */ new Set();
|
|
399
|
+
let theme;
|
|
400
|
+
let highlightOf = () => 0;
|
|
401
|
+
const paintOne = (part) => {
|
|
402
|
+
if (theme !== void 0) paintPart(part, scene, theme, highlightOf(part.record));
|
|
776
403
|
};
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
404
|
+
return {
|
|
405
|
+
objects: [model.group],
|
|
406
|
+
extent: {
|
|
407
|
+
corners: model.corners,
|
|
408
|
+
center: model.center
|
|
409
|
+
},
|
|
410
|
+
solids: model.parts.map((part) => part.solid),
|
|
411
|
+
has: (record) => first.has(record),
|
|
412
|
+
pickOf(record) {
|
|
413
|
+
const part = first.get(record);
|
|
414
|
+
return part === void 0 ? void 0 : pickOfPart(part);
|
|
415
|
+
},
|
|
416
|
+
pick(ray) {
|
|
417
|
+
const open = model.parts.filter((part) => part.wall === void 0 || !hiding.has(part.wall)).map((part) => part.mesh);
|
|
418
|
+
const hits = ray.intersectObjects(open, false);
|
|
419
|
+
const nearest = hits[0];
|
|
420
|
+
const found = nearest === void 0 ? void 0 : byMesh.get(nearest.object);
|
|
421
|
+
if (nearest === void 0 || found === void 0) return void 0;
|
|
422
|
+
if (found.holds) for (const hit of hits) {
|
|
423
|
+
const part = byMesh.get(hit.object);
|
|
424
|
+
if (part !== void 0 && part.holder === found.record) return {
|
|
425
|
+
record: part.record,
|
|
426
|
+
value: pickOfPart(part),
|
|
427
|
+
distance: hit.distance
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
return {
|
|
431
|
+
record: found.record,
|
|
432
|
+
value: pickOfPart(found),
|
|
433
|
+
distance: nearest.distance
|
|
434
|
+
};
|
|
435
|
+
},
|
|
436
|
+
paint(nextTheme, highlight) {
|
|
437
|
+
theme = nextTheme;
|
|
438
|
+
highlightOf = highlight;
|
|
439
|
+
for (const part of model.parts) paintOne(part);
|
|
440
|
+
},
|
|
441
|
+
step(frame) {
|
|
442
|
+
hiding = hidingWalls(model.shades, toRoom(frame.camera.position));
|
|
443
|
+
const rate = frame.settle ? 1 : 1 - Math.exp(0 - frame.seconds / FADE_TIME);
|
|
444
|
+
let active = false;
|
|
445
|
+
for (const part of model.parts) {
|
|
446
|
+
if (part.wall === void 0) continue;
|
|
447
|
+
const goal = hiding.has(part.wall) ? 1 : 0;
|
|
448
|
+
if (part.fade === goal) continue;
|
|
449
|
+
const next = part.fade + (goal - part.fade) * rate;
|
|
450
|
+
part.fade = Math.abs(goal - next) < .01 ? goal : next;
|
|
451
|
+
paintOne(part);
|
|
452
|
+
active = active || part.fade !== goal;
|
|
453
|
+
}
|
|
454
|
+
return active;
|
|
455
|
+
},
|
|
456
|
+
dispose() {
|
|
457
|
+
disposeModel(model);
|
|
790
458
|
}
|
|
791
|
-
invalidate();
|
|
792
|
-
};
|
|
793
|
-
/** This function sets the size of the picture from the size of the container. It gives false for a container with no size. */
|
|
794
|
-
const setSize = () => {
|
|
795
|
-
const width = container.clientWidth;
|
|
796
|
-
const height = container.clientHeight;
|
|
797
|
-
if (width === 0 || height === 0) return false;
|
|
798
|
-
viewHeight = height;
|
|
799
|
-
renderer.setPixelRatio(Math.min(globalThis.devicePixelRatio, 2));
|
|
800
|
-
renderer.setSize(width, height, false);
|
|
801
|
-
camera.aspect = width / height;
|
|
802
|
-
camera.updateProjectionMatrix();
|
|
803
|
-
return true;
|
|
804
459
|
};
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
460
|
+
}
|
|
461
|
+
// src/viewer/outlets.ts
|
|
462
|
+
/** This function gives the center of a solid, in the space of the scene: the mean of its corners. */
|
|
463
|
+
function centerOf(solid) {
|
|
464
|
+
const corners = solidCorners(solid);
|
|
465
|
+
const sum = corners.reduce((total, corner) => addPoints3(total, corner), {
|
|
466
|
+
x: 0,
|
|
467
|
+
y: 0,
|
|
468
|
+
z: 0
|
|
469
|
+
});
|
|
470
|
+
return scalePoint3(sum, 1 / Math.max(corners.length, 1));
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* This function gives the marks of the outlets of a scene, in the space of the scene: meters, with the z axis up.
|
|
474
|
+
*
|
|
475
|
+
* - An outlet gives a point on the front of its plate. Its level is the level of its circuit, and `unknown` for no circuit of the room.
|
|
476
|
+
* - A placed object with an outlet gives a link from that outlet to the center of the object.
|
|
477
|
+
*
|
|
478
|
+
* The record name of a point is the name of the record of its outlet, so the sign and the plate are one record.
|
|
479
|
+
* The record name of a link is the name of the record of its object: that record has the outlet.
|
|
480
|
+
* `solids` are the solids of the scene. An outlet or an object with no solid gives no mark. The first record of an `id` gives the mark.
|
|
481
|
+
*/
|
|
482
|
+
function outletMarks(scene, solids, loads = {}) {
|
|
483
|
+
const space = sceneSpace(scene);
|
|
484
|
+
const levels = outletLevels(scene, loads);
|
|
485
|
+
/** The first solid of each outlet and of each placed object, by the name of its record. */
|
|
486
|
+
const made = /* @__PURE__ */ new Map();
|
|
487
|
+
for (const solid of solids) {
|
|
488
|
+
const name = recordName(solid.kind, solid.source);
|
|
489
|
+
if ((solid.kind === "outlet" || solid.kind === "object") && !made.has(name)) made.set(name, solid);
|
|
490
|
+
}
|
|
491
|
+
const circuits = new Map((scene.room.circuits ?? []).map((circuit) => [circuit.id, circuit.name ?? circuit.id]));
|
|
492
|
+
const marks = [];
|
|
493
|
+
const fronts = /* @__PURE__ */ new Map();
|
|
494
|
+
for (const outlet of scene.room.outlets ?? []) {
|
|
495
|
+
if (fronts.has(outlet.id) || !made.has(recordName("outlet", outlet.id))) continue;
|
|
496
|
+
const frame = space.frame({
|
|
497
|
+
on: "wall",
|
|
498
|
+
wall: outlet.wall,
|
|
499
|
+
face: outlet.face,
|
|
500
|
+
offset: outlet.offset,
|
|
501
|
+
height: outlet.height
|
|
821
502
|
});
|
|
822
|
-
if (
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
503
|
+
if (frame === void 0) continue;
|
|
504
|
+
const front = addPoints3(frame.at, scalePoint3(frame.out, OUTLET_PLATE.depth));
|
|
505
|
+
fronts.set(outlet.id, front);
|
|
506
|
+
const level = levels.get(outlet.id);
|
|
507
|
+
const circuit = level === void 0 || outlet.circuit === void 0 ? "" : `, circuit ${circuits.get(outlet.circuit) ?? outlet.circuit}`;
|
|
508
|
+
const point = {
|
|
509
|
+
type: "point",
|
|
510
|
+
record: recordName("outlet", outlet.id),
|
|
511
|
+
kind: "outlet",
|
|
512
|
+
level: level ?? "unknown",
|
|
513
|
+
label: `Outlet ${outlet.id}${circuit}`.replace(/\s+/g, " ").trim(),
|
|
514
|
+
frame: {
|
|
515
|
+
at: front,
|
|
516
|
+
out: frame.out
|
|
517
|
+
}
|
|
518
|
+
};
|
|
519
|
+
marks.push(point);
|
|
520
|
+
}
|
|
521
|
+
const linked = /* @__PURE__ */ new Set();
|
|
522
|
+
for (const object of scene.objects ?? []) {
|
|
523
|
+
const from = object.outlet === void 0 ? void 0 : fronts.get(object.outlet);
|
|
524
|
+
const solid = made.get(recordName("object", object.id));
|
|
525
|
+
if (object.outlet === void 0 || from === void 0 || solid === void 0 || linked.has(object.id)) continue;
|
|
526
|
+
linked.add(object.id);
|
|
527
|
+
const link = {
|
|
528
|
+
type: "link",
|
|
529
|
+
record: recordName("object", object.id),
|
|
530
|
+
kind: "feed",
|
|
531
|
+
level: levels.get(object.outlet) ?? "unknown",
|
|
532
|
+
label: `Outlet ${object.outlet} to ${object.name ?? object.id}`.replace(/\s+/g, " ").trim(),
|
|
533
|
+
from: {
|
|
534
|
+
record: recordName("outlet", object.outlet),
|
|
535
|
+
at: from
|
|
536
|
+
},
|
|
537
|
+
to: {
|
|
538
|
+
record: recordName("object", object.id),
|
|
539
|
+
at: centerOf(solid)
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
marks.push(link);
|
|
543
|
+
}
|
|
544
|
+
return marks;
|
|
545
|
+
}
|
|
546
|
+
// src/viewer/viewer.ts
|
|
547
|
+
/** This function gives the name of a record for the stage. */
|
|
548
|
+
function nameOf(record) {
|
|
549
|
+
return typeof record === "string" ? record : solidRecord(record.kind, record.source);
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* This function makes a function that gives the colors of the marks of a viewer theme.
|
|
553
|
+
* The marks paint each field again for a new theme object. So the function gives the same object while the viewer theme is the same.
|
|
554
|
+
* A theme with no colors of a field gives a field the color of its level.
|
|
555
|
+
*/
|
|
556
|
+
function marksThemes() {
|
|
557
|
+
let from;
|
|
558
|
+
let to;
|
|
559
|
+
return (theme) => {
|
|
560
|
+
if (to === void 0 || theme !== from) to = {
|
|
561
|
+
sky: theme.sky,
|
|
562
|
+
ground: theme.ground,
|
|
563
|
+
levels: theme.levels,
|
|
564
|
+
halo: theme.halo,
|
|
565
|
+
accent: theme.accent,
|
|
566
|
+
field: theme.field ?? [],
|
|
567
|
+
fieldOpacity: theme.fieldOpacity
|
|
568
|
+
};
|
|
569
|
+
from = theme;
|
|
570
|
+
return to;
|
|
843
571
|
};
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* This function makes the content of a viewer for a scene: the solids, and the marks above them.
|
|
575
|
+
*
|
|
576
|
+
* `marks` are in the space of the scene: meters, with the z axis up. With no list, the function makes the marks of the outlets of the scene.
|
|
577
|
+
* A mark with a fault is not in the content. The space of the content has the y axis up.
|
|
578
|
+
*/
|
|
579
|
+
function createViewerContent(scene, marks, options = {}) {
|
|
580
|
+
const room = createRoomContent(scene, options.solids, options.loads);
|
|
581
|
+
const given = marks ?? outletMarks(scene, room.solids, options.loads);
|
|
582
|
+
const faulty = new Set(checkMarks(given).map((fault) => fault.index));
|
|
583
|
+
const good = given.filter((_, index) => !faulty.has(index));
|
|
584
|
+
const onStage = transformMarks(good, TO_Y_UP);
|
|
585
|
+
/** The mark of the scene for each mark of the stage. */
|
|
586
|
+
const inScene = new Map(onStage.map((mark, index) => [mark, good[index] ?? mark]));
|
|
587
|
+
const sceneMark = (mark) => inScene.get(mark) ?? mark;
|
|
588
|
+
const markers = createMarkers(onStage, options.markers);
|
|
589
|
+
const themeOf = marksThemes();
|
|
590
|
+
const above = {
|
|
591
|
+
objects: markers.objects,
|
|
592
|
+
extent: markers.extent,
|
|
593
|
+
has: (record) => markers.has(record),
|
|
594
|
+
pick: (ray) => markers.pick(ray),
|
|
595
|
+
paint(theme, highlight) {
|
|
596
|
+
markers.paint(themeOf(theme), highlight);
|
|
597
|
+
},
|
|
598
|
+
step: (frame) => markers.step?.(frame) ?? false,
|
|
599
|
+
dispose() {
|
|
600
|
+
markers.dispose();
|
|
872
601
|
}
|
|
873
602
|
};
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
renderer.render(world, camera);
|
|
883
|
-
lastTime = active ? now : 0;
|
|
884
|
-
if (active) invalidate();
|
|
885
|
-
}
|
|
886
|
-
const raycaster = new Raycaster();
|
|
887
|
-
const pointer = new Vector2();
|
|
888
|
-
/**
|
|
889
|
-
* This function gives the part under a pointer. A wall that hides the room is clear, and the pointer goes through it.
|
|
890
|
-
* The marker of an outlet is above each solid in the picture, so the pointer selects a marker first.
|
|
891
|
-
*/
|
|
892
|
-
const partAt = (event) => {
|
|
893
|
-
const box = canvas.getBoundingClientRect();
|
|
894
|
-
if (box.width === 0 || box.height === 0) return void 0;
|
|
895
|
-
pointer.set((event.clientX - box.left) / box.width * 2 - 1, 1 - (event.clientY - box.top) / box.height * 2);
|
|
896
|
-
raycaster.setFromCamera(pointer, camera);
|
|
897
|
-
const bySign = /* @__PURE__ */ new Map();
|
|
898
|
-
for (const part of model.parts) if (part.marker !== void 0) bySign.set(part.marker.back, part);
|
|
899
|
-
const sign = raycaster.intersectObjects([...bySign.keys()], false)[0];
|
|
900
|
-
if (sign !== void 0) return bySign.get(sign.object);
|
|
901
|
-
const open = model.parts.filter((part) => part.wall === void 0 || !hiding.has(part.wall));
|
|
902
|
-
const byMesh = new Map(open.map((part) => [part.mesh, part]));
|
|
903
|
-
const hits = raycaster.intersectObjects(open.map((part) => part.mesh), false);
|
|
904
|
-
const first = hits[0] === void 0 ? void 0 : byMesh.get(hits[0].object);
|
|
905
|
-
if (first === void 0 || !first.holds) return first;
|
|
906
|
-
for (const hit of hits) {
|
|
907
|
-
const part = byMesh.get(hit.object);
|
|
908
|
-
if (part !== void 0 && part.holder === first.record) return part;
|
|
603
|
+
return {
|
|
604
|
+
content: stackContents([room, above]),
|
|
605
|
+
show(test) {
|
|
606
|
+
markers.show((mark) => test(sceneMark(mark)));
|
|
607
|
+
},
|
|
608
|
+
told(value) {
|
|
609
|
+
if (value === void 0) return [void 0, void 0];
|
|
610
|
+
return "type" in value ? [room.pickOf(value.record), sceneMark(value)] : [value, void 0];
|
|
909
611
|
}
|
|
910
|
-
return first;
|
|
911
612
|
};
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
id: event.pointerId,
|
|
931
|
-
x: event.clientX,
|
|
932
|
-
y: event.clientY
|
|
933
|
-
};
|
|
934
|
-
}, { signal });
|
|
935
|
-
canvas.addEventListener("pointermove", (event) => {
|
|
936
|
-
if (event.buttons === 0 && event.pointerType !== "touch") setHover(partAt(event));
|
|
937
|
-
}, { signal });
|
|
938
|
-
canvas.addEventListener("pointerup", (event) => {
|
|
939
|
-
const start = down;
|
|
940
|
-
down = void 0;
|
|
941
|
-
if (start === void 0 || start.id !== event.pointerId) return;
|
|
942
|
-
if (Math.hypot(event.clientX - start.x, event.clientY - start.y) > CLICK_DISTANCE) return;
|
|
943
|
-
const part = partAt(event);
|
|
944
|
-
selected = part?.record;
|
|
945
|
-
paintAll();
|
|
946
|
-
options.onSelect?.(part === void 0 ? void 0 : pickOf(part));
|
|
947
|
-
}, { signal });
|
|
948
|
-
canvas.addEventListener("pointercancel", () => {
|
|
949
|
-
down = void 0;
|
|
950
|
-
}, { signal });
|
|
951
|
-
canvas.addEventListener("pointerleave", (event) => {
|
|
952
|
-
setHover(void 0);
|
|
953
|
-
if (event.pointerType === "mouse") setInUse(false);
|
|
954
|
-
}, { signal });
|
|
955
|
-
canvas.addEventListener("blur", () => setInUse(false), { signal });
|
|
956
|
-
canvas.addEventListener("keydown", (event) => {
|
|
957
|
-
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
958
|
-
const now = placeNow();
|
|
959
|
-
let to;
|
|
960
|
-
if (event.key === "ArrowLeft") to = {
|
|
961
|
-
...now,
|
|
962
|
-
azimuth: now.azimuth - KEY_TURN
|
|
963
|
-
};
|
|
964
|
-
else if (event.key === "ArrowRight") to = {
|
|
965
|
-
...now,
|
|
966
|
-
azimuth: now.azimuth + KEY_TURN
|
|
967
|
-
};
|
|
968
|
-
else if (event.key === "ArrowUp") to = {
|
|
969
|
-
...now,
|
|
970
|
-
polar: now.polar - KEY_TURN
|
|
971
|
-
};
|
|
972
|
-
else if (event.key === "ArrowDown") to = {
|
|
973
|
-
...now,
|
|
974
|
-
polar: now.polar + KEY_TURN
|
|
975
|
-
};
|
|
976
|
-
else if (event.key === "+" || event.key === "=") to = {
|
|
977
|
-
...now,
|
|
978
|
-
distance: now.distance * .85
|
|
979
|
-
};
|
|
980
|
-
else if (event.key === "-" || event.key === "_") to = {
|
|
981
|
-
...now,
|
|
982
|
-
distance: now.distance / .85
|
|
983
|
-
};
|
|
984
|
-
else if (event.key === "Home" || event.key === "0") goTo("home", true);
|
|
985
|
-
else if (event.key === "t" || event.key === "T") goTo("top", true);
|
|
986
|
-
else return;
|
|
987
|
-
event.preventDefault();
|
|
988
|
-
setInUse(true);
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* 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.
|
|
616
|
+
*
|
|
617
|
+
* A person turns the model with a drag, and moves nearer with the wheel after a click in the picture.
|
|
618
|
+
* The walls between the camera and the room become almost clear. A click selects the record of a solid or of a mark.
|
|
619
|
+
* With the keyboard, the arrow keys turn the model, `+` and `-` change the distance, `Home` shows the first view and `T` shows the plan.
|
|
620
|
+
*
|
|
621
|
+
* The viewer draws the marks of the scene with `@mark1russell7/visuals/markers`. A point is a sign with the color and the shape of its level.
|
|
622
|
+
* With no list of marks, each outlet has a sign. An outlet or an object with a highlight then shows a line from the outlet to each object on it.
|
|
623
|
+
*
|
|
624
|
+
* The function gives `undefined` when the browser has no WebGL 2. It does not throw, and it writes no error to the console.
|
|
625
|
+
*/
|
|
626
|
+
function createRoomViewer(container, options) {
|
|
627
|
+
let accepted = () => true;
|
|
628
|
+
let current = createViewerContent(options.scene, options.marks, options);
|
|
629
|
+
/** This function gives a record of the stage to a function of the page. */
|
|
630
|
+
const tell = (to, value) => {
|
|
989
631
|
if (to === void 0) return;
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
invalidate();
|
|
994
|
-
}, { signal });
|
|
995
|
-
canvas.addEventListener("webglcontextlost", (event) => event.preventDefault(), { signal });
|
|
996
|
-
canvas.addEventListener("webglcontextrestored", invalidate, { signal });
|
|
997
|
-
controls.addEventListener("start", () => {
|
|
998
|
-
moved = true;
|
|
999
|
-
move = void 0;
|
|
1000
|
-
canvas.style.cursor = "grabbing";
|
|
1001
|
-
invalidate();
|
|
1002
|
-
});
|
|
1003
|
-
controls.addEventListener("end", () => {
|
|
1004
|
-
canvas.style.cursor = hovered === void 0 ? "grab" : "pointer";
|
|
1005
|
-
});
|
|
1006
|
-
controls.addEventListener("change", invalidate);
|
|
1007
|
-
const applyTheme = () => {
|
|
1008
|
-
sky.color.set(theme.sky);
|
|
1009
|
-
sky.groundColor.set(theme.ground);
|
|
1010
|
-
paintAll();
|
|
632
|
+
const [pick, mark] = current.told(value);
|
|
633
|
+
if (mark === void 0) to(pick);
|
|
634
|
+
else to(pick, mark);
|
|
1011
635
|
};
|
|
1012
|
-
const
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
636
|
+
const stage = createStage(container, {
|
|
637
|
+
content: current.content,
|
|
638
|
+
theme: options.theme ?? LIGHT_THEME,
|
|
639
|
+
label: options.label ?? "3D model of the room",
|
|
640
|
+
views: {
|
|
641
|
+
home: fitView(HOME_VIEW),
|
|
642
|
+
top: fitView(TOP_VIEW)
|
|
643
|
+
},
|
|
644
|
+
home: "home",
|
|
645
|
+
keys: { t: "top" },
|
|
646
|
+
onHover: (value) => tell(options.onHover, value),
|
|
647
|
+
onSelect: (value) => tell(options.onSelect, value)
|
|
1017
648
|
});
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
applyTheme();
|
|
1023
|
-
observer.observe(container);
|
|
649
|
+
if (stage === void 0) {
|
|
650
|
+
current.content.dispose();
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
1024
653
|
return {
|
|
1025
|
-
canvas,
|
|
1026
|
-
setScene(
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
model = buildModel(scene, options.solids, options.loads);
|
|
1031
|
-
world.add(model.group, model.overlay);
|
|
1032
|
-
hovered = void 0;
|
|
1033
|
-
if (selected !== void 0 && !model.parts.some((part) => part.record === selected)) selected = void 0;
|
|
1034
|
-
setLimits();
|
|
1035
|
-
if (!moved) goTo(view, false);
|
|
1036
|
-
stepFade(0);
|
|
1037
|
-
paintAll();
|
|
654
|
+
canvas: stage.canvas,
|
|
655
|
+
setScene(scene, marks) {
|
|
656
|
+
current = createViewerContent(scene, marks, options);
|
|
657
|
+
current.show(accepted);
|
|
658
|
+
stage.setContent(current.content);
|
|
1038
659
|
},
|
|
1039
|
-
setTheme(
|
|
1040
|
-
theme
|
|
1041
|
-
applyTheme();
|
|
660
|
+
setTheme(theme) {
|
|
661
|
+
stage.setTheme(theme);
|
|
1042
662
|
},
|
|
1043
|
-
setView(
|
|
1044
|
-
|
|
663
|
+
setView(view) {
|
|
664
|
+
stage.setView(view);
|
|
1045
665
|
},
|
|
1046
666
|
highlight(records) {
|
|
1047
|
-
|
|
1048
|
-
paintAll();
|
|
667
|
+
stage.highlight(records.map(nameOf));
|
|
1049
668
|
},
|
|
1050
669
|
select(record) {
|
|
1051
|
-
|
|
1052
|
-
|
|
670
|
+
stage.select(record === void 0 ? void 0 : nameOf(record));
|
|
671
|
+
},
|
|
672
|
+
showMarks(test) {
|
|
673
|
+
accepted = test;
|
|
674
|
+
current.show(test);
|
|
675
|
+
stage.refresh();
|
|
1053
676
|
},
|
|
1054
677
|
dispose() {
|
|
1055
|
-
|
|
1056
|
-
disposed = true;
|
|
1057
|
-
cancelAnimationFrame(frame);
|
|
1058
|
-
observer.disconnect();
|
|
1059
|
-
listeners.abort();
|
|
1060
|
-
controls.dispose();
|
|
1061
|
-
world.remove(model.group, model.overlay);
|
|
1062
|
-
disposeModel(model);
|
|
1063
|
-
renderer.dispose();
|
|
1064
|
-
canvas.remove();
|
|
678
|
+
stage.dispose();
|
|
1065
679
|
}
|
|
1066
680
|
};
|
|
1067
681
|
}
|