@tonybfox/threejs-tools 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/asset-loader/index.cjs +3 -417
- package/dist/asset-loader/index.mjs +1 -6
- package/dist/camera/index.cjs +1 -393
- package/dist/camera/index.mjs +1 -6
- package/dist/chunk-4O4ENFL7.mjs +83 -0
- package/dist/chunk-55YVGK52.mjs +1 -0
- package/dist/chunk-B75TYEOO.mjs +44 -0
- package/dist/chunk-CLSRN5D2.mjs +1 -0
- package/dist/chunk-EQRUOKFV.mjs +1 -0
- package/dist/chunk-JRJBW66X.mjs +1 -0
- package/dist/chunk-OJFYE56U.mjs +1 -0
- package/dist/chunk-WZ4X7GQ2.mjs +1 -0
- package/dist/chunk-Z5VL3O6M.mjs +43 -0
- package/dist/compass/index.cjs +3 -304
- package/dist/compass/index.mjs +1 -6
- package/dist/grid/index.cjs +3 -159
- package/dist/grid/index.mjs +1 -6
- package/dist/index.cjs +7 -5406
- package/dist/index.mjs +1 -384
- package/dist/measurements/index.cjs +1 -1197
- package/dist/measurements/index.mjs +1 -8
- package/dist/sunlight/index.cjs +1 -440
- package/dist/sunlight/index.d.mts +19 -0
- package/dist/sunlight/index.d.ts +19 -0
- package/dist/sunlight/index.mjs +1 -6
- package/dist/terrain/index.cjs +1 -422
- package/dist/terrain/index.mjs +1 -6
- package/dist/transform-controls/index.cjs +1 -1586
- package/dist/transform-controls/index.mjs +1 -12
- package/dist/view-helper/index.cjs +1 -435
- package/dist/view-helper/index.mjs +1 -6
- package/package.json +1 -1
- package/dist/chunk-2CDI7ORN.mjs +0 -163
- package/dist/chunk-FBTT6MU6.mjs +0 -386
- package/dist/chunk-IAZH4OHC.mjs +0 -399
- package/dist/chunk-LUE7VHLK.mjs +0 -422
- package/dist/chunk-OZKJ3GAD.mjs +0 -1160
- package/dist/chunk-W4DAAAW6.mjs +0 -404
- package/dist/chunk-XA7OKYSM.mjs +0 -357
- package/dist/chunk-YMMLYGHV.mjs +0 -1578
- package/dist/chunk-ZNGFST7K.mjs +0 -348
|
@@ -1,1197 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// packages/measurements/src/index.ts
|
|
31
|
-
var src_exports = {};
|
|
32
|
-
__export(src_exports, {
|
|
33
|
-
MeasurementTool: () => MeasurementTool,
|
|
34
|
-
SnapMode: () => SnapMode
|
|
35
|
-
});
|
|
36
|
-
module.exports = __toCommonJS(src_exports);
|
|
37
|
-
|
|
38
|
-
// packages/measurements/src/MeasurementTool.ts
|
|
39
|
-
var THREE = __toESM(require("three"));
|
|
40
|
-
var import_CSS2DRenderer = require("three/examples/jsm/renderers/CSS2DRenderer");
|
|
41
|
-
|
|
42
|
-
// packages/measurements/src/MeasurementTypes.ts
|
|
43
|
-
var SnapMode = /* @__PURE__ */ ((SnapMode2) => {
|
|
44
|
-
SnapMode2["VERTEX"] = "vertex";
|
|
45
|
-
SnapMode2["FACE"] = "face";
|
|
46
|
-
SnapMode2["EDGE"] = "edge";
|
|
47
|
-
SnapMode2["DISABLED"] = "disabled";
|
|
48
|
-
return SnapMode2;
|
|
49
|
-
})(SnapMode || {});
|
|
50
|
-
|
|
51
|
-
// packages/measurements/src/MeasurementTool.ts
|
|
52
|
-
var MeasurementTool = class extends THREE.EventDispatcher {
|
|
53
|
-
constructor(scene, camera, options = {}) {
|
|
54
|
-
super();
|
|
55
|
-
this.measurements = [];
|
|
56
|
-
this.raycaster = new THREE.Raycaster();
|
|
57
|
-
// Interactive mode properties
|
|
58
|
-
this.isInteractive = false;
|
|
59
|
-
this.domElement = null;
|
|
60
|
-
this.controls = null;
|
|
61
|
-
this.defaultTargets = [];
|
|
62
|
-
this.activeTargets = [];
|
|
63
|
-
this.currentMeasurement = null;
|
|
64
|
-
this.activeInteractionOptions = null;
|
|
65
|
-
this.pendingMeasurementOptions = null;
|
|
66
|
-
this.previewLine = null;
|
|
67
|
-
this.previewLabel = null;
|
|
68
|
-
this.snapMarker = null;
|
|
69
|
-
this.originalCursor = "";
|
|
70
|
-
this.cursorHidden = false;
|
|
71
|
-
// Configuration defaults
|
|
72
|
-
this.defaultOptions = {
|
|
73
|
-
lineColor: 16711680,
|
|
74
|
-
labelColor: "#ffffff",
|
|
75
|
-
lineWidth: 2,
|
|
76
|
-
fontSize: 16,
|
|
77
|
-
fontFamily: "Arial, sans-serif",
|
|
78
|
-
snapMode: "vertex" /* VERTEX */,
|
|
79
|
-
snapEnabled: true,
|
|
80
|
-
snapDistance: 0.05,
|
|
81
|
-
targets: [],
|
|
82
|
-
isDynamic: false
|
|
83
|
-
};
|
|
84
|
-
this.previewColor = 65535;
|
|
85
|
-
this.markerColor = 65280;
|
|
86
|
-
this.markerSize = 0.08;
|
|
87
|
-
this.markerVisible = true;
|
|
88
|
-
// Edit mode properties
|
|
89
|
-
this.isEditMode = false;
|
|
90
|
-
this.editingMeasurement = null;
|
|
91
|
-
this.editingPoint = null;
|
|
92
|
-
this.startEditSprite = null;
|
|
93
|
-
this.endEditSprite = null;
|
|
94
|
-
this.editSpriteMaterial = null;
|
|
95
|
-
this.isDragging = false;
|
|
96
|
-
// Private methods
|
|
97
|
-
this.onMouseClick = (event) => {
|
|
98
|
-
if (!this.isInteractive) return;
|
|
99
|
-
const snapResult = this.getSnapResult(event);
|
|
100
|
-
if (!snapResult) return;
|
|
101
|
-
if (!this.currentMeasurement) {
|
|
102
|
-
this.startMeasurement(snapResult);
|
|
103
|
-
} else {
|
|
104
|
-
this.completeMeasurement(snapResult);
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
this.onMouseMove = (event) => {
|
|
108
|
-
if (!this.isInteractive) return;
|
|
109
|
-
const snapResult = this.getSnapResult(event);
|
|
110
|
-
if (!snapResult) {
|
|
111
|
-
this.hideSnapMarker();
|
|
112
|
-
this.showCursor();
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
if (!this.currentMeasurement) {
|
|
116
|
-
this.updateSnapMarker(snapResult.point, true);
|
|
117
|
-
this.hideCursor();
|
|
118
|
-
} else {
|
|
119
|
-
this.updateSnapMarker(snapResult.point, true);
|
|
120
|
-
this.hideCursor();
|
|
121
|
-
this.updatePreview(snapResult.point);
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
this.onKeyDown = (event) => {
|
|
125
|
-
if (!this.isInteractive) return;
|
|
126
|
-
if (event.key === "Escape") {
|
|
127
|
-
this.cancelCurrentMeasurement();
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
this.onEditMouseDown = (event) => {
|
|
131
|
-
if (!this.isEditMode || !this.domElement) return;
|
|
132
|
-
const mouse = new THREE.Vector2();
|
|
133
|
-
const rect = this.domElement.getBoundingClientRect();
|
|
134
|
-
mouse.x = (event.clientX - rect.left) / rect.width * 2 - 1;
|
|
135
|
-
mouse.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
|
|
136
|
-
this.raycaster.setFromCamera(mouse, this.camera);
|
|
137
|
-
const sprites = [this.startEditSprite, this.endEditSprite].filter(
|
|
138
|
-
(s) => s !== null
|
|
139
|
-
);
|
|
140
|
-
const spriteIntersects = this.raycaster.intersectObjects(sprites);
|
|
141
|
-
if (spriteIntersects.length > 0) {
|
|
142
|
-
const sprite = spriteIntersects[0].object;
|
|
143
|
-
this.editingPoint = sprite.userData.editPoint;
|
|
144
|
-
this.isDragging = true;
|
|
145
|
-
this.disableControls();
|
|
146
|
-
if (this.editingPoint === "start" && this.startEditSprite) {
|
|
147
|
-
this.startEditSprite.visible = false;
|
|
148
|
-
} else if (this.editingPoint === "end" && this.endEditSprite) {
|
|
149
|
-
this.endEditSprite.visible = false;
|
|
150
|
-
}
|
|
151
|
-
this.createSnapMarker();
|
|
152
|
-
if (this.snapMarker) {
|
|
153
|
-
this.snapMarker.position.copy(sprite.position);
|
|
154
|
-
this.snapMarker.visible = true;
|
|
155
|
-
}
|
|
156
|
-
this.hideCursor();
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
this.onEditMouseMove = (event) => {
|
|
160
|
-
if (!this.isEditMode || !this.isDragging || !this.editingMeasurement) return;
|
|
161
|
-
const snapResult = this.getSnapResult(event);
|
|
162
|
-
if (!snapResult) return;
|
|
163
|
-
if (this.snapMarker) {
|
|
164
|
-
this.snapMarker.position.copy(snapResult.point);
|
|
165
|
-
this.snapMarker.visible = this.markerVisible;
|
|
166
|
-
}
|
|
167
|
-
if (this.editingPoint === "start") {
|
|
168
|
-
this.updateMeasurementPreview(
|
|
169
|
-
snapResult.point,
|
|
170
|
-
this.editingMeasurement.end.position
|
|
171
|
-
);
|
|
172
|
-
} else if (this.editingPoint === "end") {
|
|
173
|
-
this.updateMeasurementPreview(
|
|
174
|
-
this.editingMeasurement.start.position,
|
|
175
|
-
snapResult.point
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
this.onEditMouseUp = (event) => {
|
|
180
|
-
if (!this.isEditMode || !this.isDragging || !this.editingMeasurement || !this.editingPoint)
|
|
181
|
-
return;
|
|
182
|
-
const snapResult = this.getSnapResult(event);
|
|
183
|
-
if (!snapResult) {
|
|
184
|
-
this.cancelEdit();
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
const point = this.editingPoint === "start" ? this.editingMeasurement.start : this.editingMeasurement.end;
|
|
188
|
-
point.position.copy(snapResult.point);
|
|
189
|
-
const measurementOptions = this.editingMeasurement.options;
|
|
190
|
-
if (measurementOptions.isDynamic && snapResult.object) {
|
|
191
|
-
const localPos = snapResult.object.worldToLocal(snapResult.point.clone());
|
|
192
|
-
point.anchor = {
|
|
193
|
-
object: snapResult.object,
|
|
194
|
-
localPosition: localPos
|
|
195
|
-
};
|
|
196
|
-
} else {
|
|
197
|
-
point.anchor = void 0;
|
|
198
|
-
}
|
|
199
|
-
const newDistance = this.editingMeasurement.start.position.distanceTo(
|
|
200
|
-
this.editingMeasurement.end.position
|
|
201
|
-
);
|
|
202
|
-
this.editingMeasurement.distance = newDistance;
|
|
203
|
-
const positions = [
|
|
204
|
-
this.editingMeasurement.start.position,
|
|
205
|
-
this.editingMeasurement.end.position
|
|
206
|
-
];
|
|
207
|
-
this.editingMeasurement.line.geometry.setFromPoints(positions);
|
|
208
|
-
this.editingMeasurement.line.geometry.attributes.position.needsUpdate = true;
|
|
209
|
-
const midpoint = this.editingMeasurement.start.position.clone().add(this.editingMeasurement.end.position).multiplyScalar(0.5);
|
|
210
|
-
this.editingMeasurement.label.position.copy(midpoint);
|
|
211
|
-
this.updateLabelText(this.editingMeasurement.label.element, newDistance);
|
|
212
|
-
if (this.startEditSprite) {
|
|
213
|
-
this.startEditSprite.position.copy(this.editingMeasurement.start.position);
|
|
214
|
-
this.startEditSprite.visible = true;
|
|
215
|
-
}
|
|
216
|
-
if (this.endEditSprite) {
|
|
217
|
-
this.endEditSprite.position.copy(this.editingMeasurement.end.position);
|
|
218
|
-
this.endEditSprite.visible = true;
|
|
219
|
-
}
|
|
220
|
-
this.removeSnapMarker();
|
|
221
|
-
this.showCursor();
|
|
222
|
-
this.enableControls();
|
|
223
|
-
this.dispatchEvent({
|
|
224
|
-
type: "measurementUpdated",
|
|
225
|
-
measurement: this.editingMeasurement
|
|
226
|
-
});
|
|
227
|
-
this.isDragging = false;
|
|
228
|
-
this.editingPoint = null;
|
|
229
|
-
};
|
|
230
|
-
this.scene = scene;
|
|
231
|
-
this.camera = camera;
|
|
232
|
-
const { domElement, controls } = options;
|
|
233
|
-
if (domElement) {
|
|
234
|
-
this.domElement = domElement;
|
|
235
|
-
}
|
|
236
|
-
if (controls) {
|
|
237
|
-
this.controls = controls;
|
|
238
|
-
}
|
|
239
|
-
this.previewMaterial = new THREE.LineDashedMaterial({
|
|
240
|
-
color: this.previewColor,
|
|
241
|
-
linewidth: this.defaultOptions.lineWidth,
|
|
242
|
-
dashSize: 0.1,
|
|
243
|
-
gapSize: 0.05
|
|
244
|
-
});
|
|
245
|
-
this.markerMaterial = new THREE.SpriteMaterial({
|
|
246
|
-
map: this.createCrosshairTexture(),
|
|
247
|
-
color: this.markerColor,
|
|
248
|
-
transparent: true,
|
|
249
|
-
opacity: 0.8,
|
|
250
|
-
sizeAttenuation: false,
|
|
251
|
-
// Keep constant size regardless of distance
|
|
252
|
-
depthTest: false
|
|
253
|
-
// Always render in front of other objects
|
|
254
|
-
});
|
|
255
|
-
this.editSpriteMaterial = new THREE.SpriteMaterial({
|
|
256
|
-
map: this.createDotTexture(),
|
|
257
|
-
color: 16755200,
|
|
258
|
-
// Orange color for edit points
|
|
259
|
-
transparent: true,
|
|
260
|
-
opacity: 0.9,
|
|
261
|
-
sizeAttenuation: false,
|
|
262
|
-
depthTest: false
|
|
263
|
-
});
|
|
264
|
-
this.raycaster.params.Line.threshold = 0.01;
|
|
265
|
-
this.raycaster.params.Points.threshold = 0.01;
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
* Create a crosshair texture for the snap marker sprite
|
|
269
|
-
*/
|
|
270
|
-
createCrosshairTexture() {
|
|
271
|
-
const size = 64;
|
|
272
|
-
const canvas = document.createElement("canvas");
|
|
273
|
-
canvas.width = size;
|
|
274
|
-
canvas.height = size;
|
|
275
|
-
const context = canvas.getContext("2d");
|
|
276
|
-
const centerX = size / 2;
|
|
277
|
-
const centerY = size / 2;
|
|
278
|
-
const lineLength = 20;
|
|
279
|
-
const gap = 6;
|
|
280
|
-
context.clearRect(0, 0, size, size);
|
|
281
|
-
context.strokeStyle = "#ffffff";
|
|
282
|
-
context.lineWidth = 3;
|
|
283
|
-
context.lineCap = "round";
|
|
284
|
-
context.beginPath();
|
|
285
|
-
context.moveTo(centerX - lineLength, centerY);
|
|
286
|
-
context.lineTo(centerX - gap, centerY);
|
|
287
|
-
context.moveTo(centerX + gap, centerY);
|
|
288
|
-
context.lineTo(centerX + lineLength, centerY);
|
|
289
|
-
context.moveTo(centerX, centerY - lineLength);
|
|
290
|
-
context.lineTo(centerX, centerY - gap);
|
|
291
|
-
context.moveTo(centerX, centerY + gap);
|
|
292
|
-
context.lineTo(centerX, centerY + lineLength);
|
|
293
|
-
context.stroke();
|
|
294
|
-
context.strokeStyle = "#000000";
|
|
295
|
-
context.lineWidth = 5;
|
|
296
|
-
context.globalCompositeOperation = "destination-over";
|
|
297
|
-
context.beginPath();
|
|
298
|
-
context.moveTo(centerX - lineLength, centerY);
|
|
299
|
-
context.lineTo(centerX - gap, centerY);
|
|
300
|
-
context.moveTo(centerX + gap, centerY);
|
|
301
|
-
context.lineTo(centerX + lineLength, centerY);
|
|
302
|
-
context.moveTo(centerX, centerY - lineLength);
|
|
303
|
-
context.lineTo(centerX, centerY - gap);
|
|
304
|
-
context.moveTo(centerX, centerY + gap);
|
|
305
|
-
context.lineTo(centerX, centerY + lineLength);
|
|
306
|
-
context.stroke();
|
|
307
|
-
const texture = new THREE.CanvasTexture(canvas);
|
|
308
|
-
texture.needsUpdate = true;
|
|
309
|
-
return texture;
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* Create a dot texture for edit point sprites
|
|
313
|
-
*/
|
|
314
|
-
createDotTexture() {
|
|
315
|
-
const size = 64;
|
|
316
|
-
const canvas = document.createElement("canvas");
|
|
317
|
-
canvas.width = size;
|
|
318
|
-
canvas.height = size;
|
|
319
|
-
const context = canvas.getContext("2d");
|
|
320
|
-
const centerX = size / 2;
|
|
321
|
-
const centerY = size / 2;
|
|
322
|
-
const radius = 12;
|
|
323
|
-
context.clearRect(0, 0, size, size);
|
|
324
|
-
context.fillStyle = "#ffffff";
|
|
325
|
-
context.beginPath();
|
|
326
|
-
context.arc(centerX, centerY, radius, 0, Math.PI * 2);
|
|
327
|
-
context.fill();
|
|
328
|
-
context.strokeStyle = "#000000";
|
|
329
|
-
context.lineWidth = 3;
|
|
330
|
-
context.beginPath();
|
|
331
|
-
context.arc(centerX, centerY, radius, 0, Math.PI * 2);
|
|
332
|
-
context.stroke();
|
|
333
|
-
const texture = new THREE.CanvasTexture(canvas);
|
|
334
|
-
texture.needsUpdate = true;
|
|
335
|
-
return texture;
|
|
336
|
-
}
|
|
337
|
-
/**
|
|
338
|
-
* Create a measurement point that optionally tracks a scene object.
|
|
339
|
-
*/
|
|
340
|
-
createMeasurementPoint(worldPosition, object, localPosition) {
|
|
341
|
-
if (!object) {
|
|
342
|
-
return { position: worldPosition.clone() };
|
|
343
|
-
}
|
|
344
|
-
const anchorLocal = localPosition?.clone() ?? object.worldToLocal(worldPosition.clone());
|
|
345
|
-
const resolvedWorld = object.localToWorld(anchorLocal.clone());
|
|
346
|
-
return {
|
|
347
|
-
position: resolvedWorld,
|
|
348
|
-
anchor: {
|
|
349
|
-
object,
|
|
350
|
-
localPosition: anchorLocal
|
|
351
|
-
}
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* Update a measurement point's world position from its anchor (if dynamic)
|
|
356
|
-
*/
|
|
357
|
-
updateMeasurementPoint(point) {
|
|
358
|
-
if (!point.anchor) {
|
|
359
|
-
return false;
|
|
360
|
-
}
|
|
361
|
-
const newWorldPosition = point.anchor.localPosition.clone();
|
|
362
|
-
point.anchor.object.localToWorld(newWorldPosition);
|
|
363
|
-
if (!point.position.equals(newWorldPosition)) {
|
|
364
|
-
point.position.copy(newWorldPosition);
|
|
365
|
-
return true;
|
|
366
|
-
}
|
|
367
|
-
return false;
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Add a measurement between two world positions with optional attachments.
|
|
371
|
-
*
|
|
372
|
-
* @param start - Starting world position
|
|
373
|
-
* @param end - Ending world position
|
|
374
|
-
* @param options - Optional configuration for the measurement
|
|
375
|
-
*/
|
|
376
|
-
addMeasurement(start, end, options = {}) {
|
|
377
|
-
const hasAnchors = Boolean(options.startObject || options.endObject);
|
|
378
|
-
const resolvedOptions = this.resolveMeasurementOptions(
|
|
379
|
-
options,
|
|
380
|
-
options.isDynamic ?? hasAnchors
|
|
381
|
-
);
|
|
382
|
-
const startPoint = this.createMeasurementPoint(
|
|
383
|
-
start,
|
|
384
|
-
options.startObject,
|
|
385
|
-
options.startLocalPosition
|
|
386
|
-
);
|
|
387
|
-
const endPoint = this.createMeasurementPoint(
|
|
388
|
-
end,
|
|
389
|
-
options.endObject,
|
|
390
|
-
options.endLocalPosition
|
|
391
|
-
);
|
|
392
|
-
return this.addMeasurementFromPoints(startPoint, endPoint, resolvedOptions, {
|
|
393
|
-
id: options.id
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
resolveMeasurementOptions(overrides = {}, inferredDynamic) {
|
|
397
|
-
let targetCandidates;
|
|
398
|
-
if (overrides.targets && overrides.targets.length > 0) {
|
|
399
|
-
targetCandidates = overrides.targets;
|
|
400
|
-
} else if (this.defaultOptions.targets.length > 0) {
|
|
401
|
-
targetCandidates = this.defaultOptions.targets;
|
|
402
|
-
} else if (this.defaultTargets.length > 0) {
|
|
403
|
-
targetCandidates = this.defaultTargets;
|
|
404
|
-
}
|
|
405
|
-
const targets = targetCandidates && targetCandidates.length > 0 ? targetCandidates : this.getAllMeshes();
|
|
406
|
-
const isDynamic = overrides.isDynamic ?? inferredDynamic ?? this.defaultOptions.isDynamic;
|
|
407
|
-
return {
|
|
408
|
-
lineColor: overrides.lineColor ?? this.defaultOptions.lineColor,
|
|
409
|
-
labelColor: overrides.labelColor ?? this.defaultOptions.labelColor,
|
|
410
|
-
lineWidth: overrides.lineWidth ?? this.defaultOptions.lineWidth,
|
|
411
|
-
fontSize: overrides.fontSize ?? this.defaultOptions.fontSize,
|
|
412
|
-
fontFamily: overrides.fontFamily ?? this.defaultOptions.fontFamily,
|
|
413
|
-
snapMode: overrides.snapMode ?? this.defaultOptions.snapMode,
|
|
414
|
-
snapEnabled: overrides.snapEnabled ?? this.defaultOptions.snapEnabled,
|
|
415
|
-
snapDistance: overrides.snapDistance ?? this.defaultOptions.snapDistance,
|
|
416
|
-
targets,
|
|
417
|
-
isDynamic
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
getActiveMeasurementOptions() {
|
|
421
|
-
if (this.isEditMode && this.editingMeasurement) {
|
|
422
|
-
return this.editingMeasurement.options;
|
|
423
|
-
}
|
|
424
|
-
return this.activeInteractionOptions;
|
|
425
|
-
}
|
|
426
|
-
/**
|
|
427
|
-
* @deprecated Use addMeasurement(obj1, obj2, { startLocalPos, endLocalPos }) instead
|
|
428
|
-
* Add a dynamic measurement between two objects
|
|
429
|
-
*/
|
|
430
|
-
addDynamicMeasurement(startObject, endObject, startLocalPos = new THREE.Vector3(), endLocalPos = new THREE.Vector3()) {
|
|
431
|
-
const startWorld = startObject.localToWorld(startLocalPos.clone());
|
|
432
|
-
const endWorld = endObject.localToWorld(endLocalPos.clone());
|
|
433
|
-
return this.addMeasurement(startWorld, endWorld, {
|
|
434
|
-
startObject,
|
|
435
|
-
endObject,
|
|
436
|
-
startLocalPosition: startLocalPos,
|
|
437
|
-
endLocalPosition: endLocalPos
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* @deprecated Use addMeasurement(staticPos, targetObject, { endLocalPos }) instead
|
|
442
|
-
* Add a measurement from a static point to a dynamic object
|
|
443
|
-
*/
|
|
444
|
-
addMeasurementToObject(staticPos, targetObject, objectLocalPos = new THREE.Vector3()) {
|
|
445
|
-
const endWorld = targetObject.localToWorld(objectLocalPos.clone());
|
|
446
|
-
return this.addMeasurement(staticPos, endWorld, {
|
|
447
|
-
endObject: targetObject,
|
|
448
|
-
endLocalPosition: objectLocalPos
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Core method to add a measurement from two measurement points
|
|
453
|
-
*/
|
|
454
|
-
addMeasurementFromPoints(start, end, options, context) {
|
|
455
|
-
const id = context?.id || this.generateId();
|
|
456
|
-
const distance = start.position.distanceTo(end.position);
|
|
457
|
-
const geometry = new THREE.BufferGeometry().setFromPoints([
|
|
458
|
-
start.position,
|
|
459
|
-
end.position
|
|
460
|
-
]);
|
|
461
|
-
const line = new THREE.Line(
|
|
462
|
-
geometry,
|
|
463
|
-
new THREE.LineBasicMaterial({
|
|
464
|
-
color: options.lineColor,
|
|
465
|
-
linewidth: options.lineWidth
|
|
466
|
-
})
|
|
467
|
-
);
|
|
468
|
-
const label = this.createLabel(distance, options);
|
|
469
|
-
const midpoint = start.position.clone().add(end.position).multiplyScalar(0.5);
|
|
470
|
-
label.position.copy(midpoint);
|
|
471
|
-
const measurement = {
|
|
472
|
-
id,
|
|
473
|
-
start,
|
|
474
|
-
end,
|
|
475
|
-
line,
|
|
476
|
-
label,
|
|
477
|
-
distance,
|
|
478
|
-
options: {
|
|
479
|
-
...options,
|
|
480
|
-
targets: [...options.targets]
|
|
481
|
-
}
|
|
482
|
-
};
|
|
483
|
-
this.scene.add(line);
|
|
484
|
-
this.scene.add(label);
|
|
485
|
-
this.measurements.push(measurement);
|
|
486
|
-
this.dispatchEvent({
|
|
487
|
-
type: "measurementCreated",
|
|
488
|
-
measurement
|
|
489
|
-
});
|
|
490
|
-
return measurement;
|
|
491
|
-
}
|
|
492
|
-
/**
|
|
493
|
-
* Update all dynamic measurements in real-time
|
|
494
|
-
* Call this in your animation loop to keep dynamic measurements up-to-date
|
|
495
|
-
*/
|
|
496
|
-
updateDynamicMeasurements() {
|
|
497
|
-
let updated = false;
|
|
498
|
-
for (const measurement of this.measurements) {
|
|
499
|
-
if (!measurement.options.isDynamic) continue;
|
|
500
|
-
let needsUpdate = false;
|
|
501
|
-
if (this.updateMeasurementPoint(measurement.start)) {
|
|
502
|
-
needsUpdate = true;
|
|
503
|
-
}
|
|
504
|
-
if (this.updateMeasurementPoint(measurement.end)) {
|
|
505
|
-
needsUpdate = true;
|
|
506
|
-
}
|
|
507
|
-
if (needsUpdate) {
|
|
508
|
-
const newDistance = measurement.start.position.distanceTo(
|
|
509
|
-
measurement.end.position
|
|
510
|
-
);
|
|
511
|
-
measurement.distance = newDistance;
|
|
512
|
-
const positions = [measurement.start.position, measurement.end.position];
|
|
513
|
-
measurement.line.geometry.setFromPoints(positions);
|
|
514
|
-
measurement.line.geometry.attributes.position.needsUpdate = true;
|
|
515
|
-
const midpoint = measurement.start.position.clone().add(measurement.end.position).multiplyScalar(0.5);
|
|
516
|
-
measurement.label.position.copy(midpoint);
|
|
517
|
-
this.updateLabelText(measurement.label.element, newDistance);
|
|
518
|
-
updated = true;
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
return updated;
|
|
522
|
-
}
|
|
523
|
-
/**
|
|
524
|
-
* Set whether interactive measurements should be dynamic or static
|
|
525
|
-
*/
|
|
526
|
-
setDynamicMode(enabled) {
|
|
527
|
-
this.setDefaultMeasurementOptions({ isDynamic: enabled });
|
|
528
|
-
}
|
|
529
|
-
/**
|
|
530
|
-
* Get the current dynamic mode state
|
|
531
|
-
*/
|
|
532
|
-
getDynamicMode() {
|
|
533
|
-
return this.defaultOptions.isDynamic;
|
|
534
|
-
}
|
|
535
|
-
/**
|
|
536
|
-
* Enter edit mode for a specific measurement
|
|
537
|
-
* Shows edit sprites at the measurement endpoints
|
|
538
|
-
* @param measurementIdOrIndex - The measurement ID or index
|
|
539
|
-
* @param targets - Optional target objects for snapping during edit
|
|
540
|
-
*/
|
|
541
|
-
enterEditMode(measurementIdOrIndex, targets) {
|
|
542
|
-
this.exitEditMode();
|
|
543
|
-
let measurement;
|
|
544
|
-
if (typeof measurementIdOrIndex === "string") {
|
|
545
|
-
measurement = this.measurements.find((m) => m.id === measurementIdOrIndex);
|
|
546
|
-
} else {
|
|
547
|
-
measurement = this.measurements[measurementIdOrIndex];
|
|
548
|
-
}
|
|
549
|
-
if (!measurement) {
|
|
550
|
-
console.warn("Measurement not found:", measurementIdOrIndex);
|
|
551
|
-
return;
|
|
552
|
-
}
|
|
553
|
-
if (!this.domElement) {
|
|
554
|
-
console.warn(
|
|
555
|
-
"DOM element not set. Call setDomElement() or enableInteraction() first."
|
|
556
|
-
);
|
|
557
|
-
return;
|
|
558
|
-
}
|
|
559
|
-
this.isEditMode = true;
|
|
560
|
-
this.editingMeasurement = measurement;
|
|
561
|
-
const resolvedTargets = targets && targets.length > 0 ? targets : measurement.options.targets.length > 0 ? measurement.options.targets : this.getAllMeshes();
|
|
562
|
-
this.activeTargets = resolvedTargets;
|
|
563
|
-
if (targets && targets.length > 0) {
|
|
564
|
-
measurement.options.targets = [...targets];
|
|
565
|
-
}
|
|
566
|
-
this.createEditSprites();
|
|
567
|
-
this.domElement.addEventListener("mousedown", this.onEditMouseDown);
|
|
568
|
-
this.domElement.addEventListener("mousemove", this.onEditMouseMove);
|
|
569
|
-
this.domElement.addEventListener("mouseup", this.onEditMouseUp);
|
|
570
|
-
this.domElement.style.cursor = "pointer";
|
|
571
|
-
this.dispatchEvent({
|
|
572
|
-
type: "editModeEntered",
|
|
573
|
-
measurement
|
|
574
|
-
});
|
|
575
|
-
}
|
|
576
|
-
/**
|
|
577
|
-
* Exit edit mode
|
|
578
|
-
*/
|
|
579
|
-
exitEditMode() {
|
|
580
|
-
if (!this.isEditMode) return;
|
|
581
|
-
const measurement = this.editingMeasurement;
|
|
582
|
-
this.removeEditSprites();
|
|
583
|
-
if (this.domElement) {
|
|
584
|
-
this.domElement.removeEventListener("mousedown", this.onEditMouseDown);
|
|
585
|
-
this.domElement.removeEventListener("mousemove", this.onEditMouseMove);
|
|
586
|
-
this.domElement.removeEventListener("mouseup", this.onEditMouseUp);
|
|
587
|
-
this.domElement.style.cursor = this.isInteractive ? "crosshair" : "default";
|
|
588
|
-
}
|
|
589
|
-
this.isEditMode = false;
|
|
590
|
-
this.editingMeasurement = null;
|
|
591
|
-
this.editingPoint = null;
|
|
592
|
-
this.isDragging = false;
|
|
593
|
-
this.activeTargets = [];
|
|
594
|
-
if (measurement) {
|
|
595
|
-
this.dispatchEvent({
|
|
596
|
-
type: "editModeExited",
|
|
597
|
-
measurement
|
|
598
|
-
});
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
/**
|
|
602
|
-
* Set the DOM element for interactions (both measurement and edit mode)
|
|
603
|
-
*/
|
|
604
|
-
setDomElement(domElement) {
|
|
605
|
-
this.domElement = domElement;
|
|
606
|
-
}
|
|
607
|
-
/**
|
|
608
|
-
* Set the camera controls to disable during edit dragging
|
|
609
|
-
*/
|
|
610
|
-
setControls(controls) {
|
|
611
|
-
this.controls = controls;
|
|
612
|
-
}
|
|
613
|
-
/**
|
|
614
|
-
* Set target objects for snapping (used in both interactive mode and edit mode)
|
|
615
|
-
*/
|
|
616
|
-
setTargetObjects(targets) {
|
|
617
|
-
this.defaultTargets = targets.length > 0 ? targets : this.getAllMeshes();
|
|
618
|
-
this.defaultOptions.targets = [...this.defaultTargets];
|
|
619
|
-
}
|
|
620
|
-
/**
|
|
621
|
-
* Update default measurement options used when none are provided explicitly.
|
|
622
|
-
*/
|
|
623
|
-
setDefaultMeasurementOptions(options) {
|
|
624
|
-
if (options.lineColor !== void 0) {
|
|
625
|
-
this.defaultOptions.lineColor = options.lineColor;
|
|
626
|
-
}
|
|
627
|
-
if (options.labelColor !== void 0) {
|
|
628
|
-
this.defaultOptions.labelColor = options.labelColor;
|
|
629
|
-
}
|
|
630
|
-
if (options.lineWidth !== void 0) {
|
|
631
|
-
this.defaultOptions.lineWidth = options.lineWidth;
|
|
632
|
-
}
|
|
633
|
-
if (options.fontSize !== void 0) {
|
|
634
|
-
this.defaultOptions.fontSize = options.fontSize;
|
|
635
|
-
}
|
|
636
|
-
if (options.fontFamily !== void 0) {
|
|
637
|
-
this.defaultOptions.fontFamily = options.fontFamily;
|
|
638
|
-
}
|
|
639
|
-
if (options.snapMode !== void 0) {
|
|
640
|
-
this.defaultOptions.snapMode = options.snapMode;
|
|
641
|
-
}
|
|
642
|
-
if (options.snapEnabled !== void 0) {
|
|
643
|
-
this.defaultOptions.snapEnabled = options.snapEnabled;
|
|
644
|
-
}
|
|
645
|
-
if (options.snapDistance !== void 0) {
|
|
646
|
-
this.defaultOptions.snapDistance = options.snapDistance;
|
|
647
|
-
}
|
|
648
|
-
if (options.targets !== void 0) {
|
|
649
|
-
this.defaultTargets = options.targets;
|
|
650
|
-
this.defaultOptions.targets = [...options.targets];
|
|
651
|
-
if (this.activeInteractionOptions) {
|
|
652
|
-
this.activeInteractionOptions.targets = [...options.targets];
|
|
653
|
-
this.activeTargets = options.targets;
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
if (options.isDynamic !== void 0) {
|
|
657
|
-
this.defaultOptions.isDynamic = options.isDynamic;
|
|
658
|
-
if (this.activeInteractionOptions) {
|
|
659
|
-
this.activeInteractionOptions.isDynamic = options.isDynamic;
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
if (this.activeInteractionOptions) {
|
|
663
|
-
if (options.lineColor !== void 0) {
|
|
664
|
-
this.activeInteractionOptions.lineColor = options.lineColor;
|
|
665
|
-
}
|
|
666
|
-
if (options.labelColor !== void 0) {
|
|
667
|
-
this.activeInteractionOptions.labelColor = options.labelColor;
|
|
668
|
-
}
|
|
669
|
-
if (options.lineWidth !== void 0) {
|
|
670
|
-
this.activeInteractionOptions.lineWidth = options.lineWidth;
|
|
671
|
-
}
|
|
672
|
-
if (options.fontSize !== void 0) {
|
|
673
|
-
this.activeInteractionOptions.fontSize = options.fontSize;
|
|
674
|
-
}
|
|
675
|
-
if (options.fontFamily !== void 0) {
|
|
676
|
-
this.activeInteractionOptions.fontFamily = options.fontFamily;
|
|
677
|
-
}
|
|
678
|
-
if (options.snapMode !== void 0) {
|
|
679
|
-
this.activeInteractionOptions.snapMode = options.snapMode;
|
|
680
|
-
}
|
|
681
|
-
if (options.snapEnabled !== void 0) {
|
|
682
|
-
this.activeInteractionOptions.snapEnabled = options.snapEnabled;
|
|
683
|
-
}
|
|
684
|
-
if (options.snapDistance !== void 0) {
|
|
685
|
-
this.activeInteractionOptions.snapDistance = options.snapDistance;
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
/**
|
|
690
|
-
* Disable camera controls (used during edit dragging)
|
|
691
|
-
*/
|
|
692
|
-
disableControls() {
|
|
693
|
-
if (this.controls) {
|
|
694
|
-
this.controls.enabled = false;
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
/**
|
|
698
|
-
* Enable camera controls (used after edit dragging)
|
|
699
|
-
*/
|
|
700
|
-
enableControls() {
|
|
701
|
-
if (this.controls) {
|
|
702
|
-
this.controls.enabled = true;
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
/**
|
|
706
|
-
* Enable interactive measurement mode
|
|
707
|
-
*/
|
|
708
|
-
enableInteraction(options = {}) {
|
|
709
|
-
if (this.isInteractive) {
|
|
710
|
-
this.disableInteraction();
|
|
711
|
-
}
|
|
712
|
-
const resolvedOptions = this.resolveMeasurementOptions(options);
|
|
713
|
-
this.activeInteractionOptions = {
|
|
714
|
-
...resolvedOptions,
|
|
715
|
-
targets: [...resolvedOptions.targets]
|
|
716
|
-
};
|
|
717
|
-
this.activeTargets = this.activeInteractionOptions.targets.length > 0 ? this.activeInteractionOptions.targets : this.getAllMeshes();
|
|
718
|
-
this.isInteractive = true;
|
|
719
|
-
if (this.domElement) {
|
|
720
|
-
this.domElement.addEventListener("click", this.onMouseClick);
|
|
721
|
-
this.domElement.addEventListener("mousemove", this.onMouseMove);
|
|
722
|
-
this.domElement.addEventListener("keydown", this.onKeyDown);
|
|
723
|
-
this.domElement.style.cursor = "crosshair";
|
|
724
|
-
}
|
|
725
|
-
this.createSnapMarker();
|
|
726
|
-
this.dispatchEvent({ type: "started" });
|
|
727
|
-
}
|
|
728
|
-
/**
|
|
729
|
-
* Disable interactive measurement mode
|
|
730
|
-
*/
|
|
731
|
-
disableInteraction() {
|
|
732
|
-
if (!this.isInteractive || !this.domElement) return;
|
|
733
|
-
this.exitEditMode();
|
|
734
|
-
this.domElement.removeEventListener("click", this.onMouseClick);
|
|
735
|
-
this.domElement.removeEventListener("mousemove", this.onMouseMove);
|
|
736
|
-
this.domElement.removeEventListener("keydown", this.onKeyDown);
|
|
737
|
-
this.showCursor();
|
|
738
|
-
this.domElement.style.cursor = "default";
|
|
739
|
-
this.cancelCurrentMeasurement();
|
|
740
|
-
this.removeSnapMarker();
|
|
741
|
-
this.isInteractive = false;
|
|
742
|
-
this.activeInteractionOptions = null;
|
|
743
|
-
this.activeTargets = [];
|
|
744
|
-
this.dispatchEvent({ type: "ended" });
|
|
745
|
-
}
|
|
746
|
-
/**
|
|
747
|
-
* Remove the last measurement (undo)
|
|
748
|
-
*/
|
|
749
|
-
undoLast() {
|
|
750
|
-
if (this.measurements.length === 0) return;
|
|
751
|
-
const lastMeasurement = this.measurements.pop();
|
|
752
|
-
this.removeMeasurementFromScene(lastMeasurement);
|
|
753
|
-
this.dispatchEvent({
|
|
754
|
-
type: "measurementRemoved",
|
|
755
|
-
measurement: lastMeasurement
|
|
756
|
-
});
|
|
757
|
-
}
|
|
758
|
-
/**
|
|
759
|
-
* Remove a specific measurement
|
|
760
|
-
*/
|
|
761
|
-
removeMeasurement(measurement) {
|
|
762
|
-
const index = this.measurements.indexOf(measurement);
|
|
763
|
-
if (index === -1) return;
|
|
764
|
-
this.measurements.splice(index, 1);
|
|
765
|
-
this.removeMeasurementFromScene(measurement);
|
|
766
|
-
this.dispatchEvent({
|
|
767
|
-
type: "measurementRemoved",
|
|
768
|
-
measurement
|
|
769
|
-
});
|
|
770
|
-
}
|
|
771
|
-
/**
|
|
772
|
-
* Clear all measurements
|
|
773
|
-
*/
|
|
774
|
-
clearAll() {
|
|
775
|
-
const count = this.measurements.length;
|
|
776
|
-
this.measurements.forEach((measurement) => {
|
|
777
|
-
this.removeMeasurementFromScene(measurement);
|
|
778
|
-
});
|
|
779
|
-
this.measurements = [];
|
|
780
|
-
this.dispatchEvent({
|
|
781
|
-
type: "measurementsCleared",
|
|
782
|
-
count
|
|
783
|
-
});
|
|
784
|
-
}
|
|
785
|
-
/**
|
|
786
|
-
* Get all measurements
|
|
787
|
-
*/
|
|
788
|
-
getMeasurements() {
|
|
789
|
-
return [...this.measurements];
|
|
790
|
-
}
|
|
791
|
-
/**
|
|
792
|
-
* Convert a MeasurementPoint to serializable data
|
|
793
|
-
*/
|
|
794
|
-
serializeMeasurementPoint(point) {
|
|
795
|
-
return {
|
|
796
|
-
position: point.position.toArray(),
|
|
797
|
-
anchorObjectId: point.anchor?.object.uuid,
|
|
798
|
-
anchorLocalPosition: point.anchor ? point.anchor.localPosition.toArray() : void 0
|
|
799
|
-
};
|
|
800
|
-
}
|
|
801
|
-
/**
|
|
802
|
-
* Serialize measurements to JSON-compatible format
|
|
803
|
-
* Note: Dynamic measurements will lose their object references and become static when deserialized
|
|
804
|
-
*/
|
|
805
|
-
serialize() {
|
|
806
|
-
return this.measurements.map((measurement) => ({
|
|
807
|
-
id: measurement.id,
|
|
808
|
-
start: this.serializeMeasurementPoint(measurement.start),
|
|
809
|
-
end: this.serializeMeasurementPoint(measurement.end),
|
|
810
|
-
distance: measurement.distance,
|
|
811
|
-
options: {
|
|
812
|
-
snapMode: measurement.options.snapMode,
|
|
813
|
-
snapEnabled: measurement.options.snapEnabled,
|
|
814
|
-
snapDistance: measurement.options.snapDistance,
|
|
815
|
-
lineColor: measurement.options.lineColor,
|
|
816
|
-
labelColor: measurement.options.labelColor,
|
|
817
|
-
lineWidth: measurement.options.lineWidth,
|
|
818
|
-
fontSize: measurement.options.fontSize,
|
|
819
|
-
fontFamily: measurement.options.fontFamily,
|
|
820
|
-
isDynamic: measurement.options.isDynamic,
|
|
821
|
-
targetObjectIds: measurement.options.targets.map((obj) => obj.uuid)
|
|
822
|
-
}
|
|
823
|
-
}));
|
|
824
|
-
}
|
|
825
|
-
/**
|
|
826
|
-
* Deserialize measurements from JSON data
|
|
827
|
-
* Note: Dynamic measurements become static since object references are lost
|
|
828
|
-
*/
|
|
829
|
-
deserialize(data) {
|
|
830
|
-
this.clearAll();
|
|
831
|
-
data.forEach((item) => {
|
|
832
|
-
const start = new THREE.Vector3().fromArray(item.start.position);
|
|
833
|
-
const end = new THREE.Vector3().fromArray(item.end.position);
|
|
834
|
-
const startObject = item.start.anchorObjectId ? this.scene.getObjectByProperty(
|
|
835
|
-
"uuid",
|
|
836
|
-
item.start.anchorObjectId
|
|
837
|
-
) : null;
|
|
838
|
-
const endObject = item.end.anchorObjectId ? this.scene.getObjectByProperty(
|
|
839
|
-
"uuid",
|
|
840
|
-
item.end.anchorObjectId
|
|
841
|
-
) : null;
|
|
842
|
-
const restoredTargets = item.options.targetObjectIds && item.options.targetObjectIds.length > 0 ? item.options.targetObjectIds.map((uuid) => this.scene.getObjectByProperty("uuid", uuid)).filter((obj) => obj !== void 0) : void 0;
|
|
843
|
-
this.addMeasurement(start, end, {
|
|
844
|
-
id: item.id,
|
|
845
|
-
targets: restoredTargets && restoredTargets.length > 0 ? restoredTargets : void 0,
|
|
846
|
-
snapMode: item.options.snapMode,
|
|
847
|
-
snapEnabled: item.options.snapEnabled,
|
|
848
|
-
snapDistance: item.options.snapDistance,
|
|
849
|
-
lineColor: item.options.lineColor,
|
|
850
|
-
labelColor: item.options.labelColor,
|
|
851
|
-
lineWidth: item.options.lineWidth,
|
|
852
|
-
fontSize: item.options.fontSize,
|
|
853
|
-
fontFamily: item.options.fontFamily,
|
|
854
|
-
isDynamic: item.options.isDynamic,
|
|
855
|
-
startObject: startObject || void 0,
|
|
856
|
-
startLocalPosition: item.start.anchorLocalPosition ? new THREE.Vector3().fromArray(item.start.anchorLocalPosition) : void 0,
|
|
857
|
-
endObject: endObject || void 0,
|
|
858
|
-
endLocalPosition: item.end.anchorLocalPosition ? new THREE.Vector3().fromArray(item.end.anchorLocalPosition) : void 0
|
|
859
|
-
});
|
|
860
|
-
});
|
|
861
|
-
}
|
|
862
|
-
/**
|
|
863
|
-
* Dispose of all resources
|
|
864
|
-
*/
|
|
865
|
-
dispose() {
|
|
866
|
-
this.exitEditMode();
|
|
867
|
-
this.disableInteraction();
|
|
868
|
-
this.clearAll();
|
|
869
|
-
this.previewMaterial.dispose();
|
|
870
|
-
if (this.markerMaterial.map) {
|
|
871
|
-
this.markerMaterial.map.dispose();
|
|
872
|
-
}
|
|
873
|
-
this.markerMaterial.dispose();
|
|
874
|
-
if (this.editSpriteMaterial) {
|
|
875
|
-
if (this.editSpriteMaterial.map) {
|
|
876
|
-
this.editSpriteMaterial.map.dispose();
|
|
877
|
-
}
|
|
878
|
-
this.editSpriteMaterial.dispose();
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
|
-
hideCursor() {
|
|
882
|
-
if (!this.domElement || this.cursorHidden) return;
|
|
883
|
-
this.originalCursor = this.domElement.style.cursor;
|
|
884
|
-
this.domElement.style.cursor = "none";
|
|
885
|
-
this.cursorHidden = true;
|
|
886
|
-
}
|
|
887
|
-
showCursor() {
|
|
888
|
-
if (!this.domElement || !this.cursorHidden) return;
|
|
889
|
-
this.domElement.style.cursor = this.originalCursor || "crosshair";
|
|
890
|
-
this.cursorHidden = false;
|
|
891
|
-
}
|
|
892
|
-
createSnapMarker() {
|
|
893
|
-
if (!this.markerVisible) return;
|
|
894
|
-
if (this.snapMarker) {
|
|
895
|
-
this.scene.remove(this.snapMarker);
|
|
896
|
-
}
|
|
897
|
-
this.snapMarker = new THREE.Sprite(this.markerMaterial);
|
|
898
|
-
this.snapMarker.scale.setScalar(this.markerSize);
|
|
899
|
-
this.snapMarker.visible = false;
|
|
900
|
-
this.snapMarker.renderOrder = 999;
|
|
901
|
-
this.snapMarker.material.depthTest = false;
|
|
902
|
-
this.scene.add(this.snapMarker);
|
|
903
|
-
}
|
|
904
|
-
updateSnapMarker(point, visible = true) {
|
|
905
|
-
if (!this.snapMarker || !this.markerVisible) return;
|
|
906
|
-
this.snapMarker.position.copy(point);
|
|
907
|
-
this.snapMarker.visible = visible;
|
|
908
|
-
}
|
|
909
|
-
hideSnapMarker() {
|
|
910
|
-
if (this.snapMarker) {
|
|
911
|
-
this.snapMarker.visible = false;
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
removeSnapMarker() {
|
|
915
|
-
if (this.snapMarker) {
|
|
916
|
-
this.scene.remove(this.snapMarker);
|
|
917
|
-
this.snapMarker = null;
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
startMeasurement(snapResult) {
|
|
921
|
-
const id = this.generateId();
|
|
922
|
-
const point = snapResult.point;
|
|
923
|
-
const baseOptions = this.activeInteractionOptions ?? this.defaultOptions;
|
|
924
|
-
const measurementOptions = {
|
|
925
|
-
...baseOptions,
|
|
926
|
-
targets: [...baseOptions.targets]
|
|
927
|
-
};
|
|
928
|
-
this.pendingMeasurementOptions = measurementOptions;
|
|
929
|
-
this.hideSnapMarker();
|
|
930
|
-
const geometry = new THREE.BufferGeometry().setFromPoints([point, point]);
|
|
931
|
-
this.previewLine = new THREE.Line(geometry, this.previewMaterial);
|
|
932
|
-
this.previewLine.computeLineDistances();
|
|
933
|
-
this.scene.add(this.previewLine);
|
|
934
|
-
this.previewLabel = this.createLabel(0, measurementOptions);
|
|
935
|
-
this.previewLabel.position.copy(point);
|
|
936
|
-
this.scene.add(this.previewLabel);
|
|
937
|
-
const startPoint = measurementOptions.isDynamic && snapResult.object ? this.createMeasurementPoint(point, snapResult.object) : this.createMeasurementPoint(point);
|
|
938
|
-
this.currentMeasurement = {
|
|
939
|
-
id,
|
|
940
|
-
start: startPoint
|
|
941
|
-
};
|
|
942
|
-
}
|
|
943
|
-
updatePreview(point) {
|
|
944
|
-
if (!this.currentMeasurement || !this.previewLine || !this.previewLabel)
|
|
945
|
-
return;
|
|
946
|
-
const start = this.currentMeasurement.start;
|
|
947
|
-
const distance = start.position.distanceTo(point);
|
|
948
|
-
const geometry = new THREE.BufferGeometry().setFromPoints([
|
|
949
|
-
start.position,
|
|
950
|
-
point
|
|
951
|
-
]);
|
|
952
|
-
this.previewLine.geometry.dispose();
|
|
953
|
-
this.previewLine.geometry = geometry;
|
|
954
|
-
this.previewLine.computeLineDistances();
|
|
955
|
-
const midpoint = start.position.clone().add(point).multiplyScalar(0.5);
|
|
956
|
-
this.previewLabel.position.copy(midpoint);
|
|
957
|
-
this.updateLabelText(this.previewLabel.element, distance);
|
|
958
|
-
this.dispatchEvent({
|
|
959
|
-
type: "previewUpdated",
|
|
960
|
-
start: start.position,
|
|
961
|
-
current: point,
|
|
962
|
-
distance
|
|
963
|
-
});
|
|
964
|
-
}
|
|
965
|
-
completeMeasurement(snapResult) {
|
|
966
|
-
if (!this.currentMeasurement) return;
|
|
967
|
-
const start = this.currentMeasurement.start;
|
|
968
|
-
const point = snapResult.point;
|
|
969
|
-
const options = this.pendingMeasurementOptions ?? this.activeInteractionOptions ?? this.defaultOptions;
|
|
970
|
-
this.disableInteraction();
|
|
971
|
-
this.cleanupPreview();
|
|
972
|
-
const resolvedOptions = {
|
|
973
|
-
...options,
|
|
974
|
-
targets: [...options.targets]
|
|
975
|
-
};
|
|
976
|
-
const endPoint = resolvedOptions.isDynamic && snapResult.object ? this.createMeasurementPoint(point, snapResult.object) : this.createMeasurementPoint(point);
|
|
977
|
-
this.addMeasurementFromPoints(start, endPoint, resolvedOptions);
|
|
978
|
-
this.currentMeasurement = null;
|
|
979
|
-
this.pendingMeasurementOptions = null;
|
|
980
|
-
this.createSnapMarker();
|
|
981
|
-
}
|
|
982
|
-
cancelCurrentMeasurement() {
|
|
983
|
-
this.cleanupPreview();
|
|
984
|
-
this.currentMeasurement = null;
|
|
985
|
-
this.pendingMeasurementOptions = null;
|
|
986
|
-
this.createSnapMarker();
|
|
987
|
-
}
|
|
988
|
-
cleanupPreview() {
|
|
989
|
-
if (this.previewLine) {
|
|
990
|
-
this.scene.remove(this.previewLine);
|
|
991
|
-
this.previewLine.geometry.dispose();
|
|
992
|
-
this.previewLine = null;
|
|
993
|
-
}
|
|
994
|
-
if (this.previewLabel) {
|
|
995
|
-
this.scene.remove(this.previewLabel);
|
|
996
|
-
if (this.previewLabel.element.parentNode) {
|
|
997
|
-
this.previewLabel.element.parentNode.removeChild(
|
|
998
|
-
this.previewLabel.element
|
|
999
|
-
);
|
|
1000
|
-
}
|
|
1001
|
-
this.previewLabel = null;
|
|
1002
|
-
}
|
|
1003
|
-
}
|
|
1004
|
-
getSnapResult(event) {
|
|
1005
|
-
const mouse = new THREE.Vector2();
|
|
1006
|
-
const rect = this.domElement.getBoundingClientRect();
|
|
1007
|
-
mouse.x = (event.clientX - rect.left) / rect.width * 2 - 1;
|
|
1008
|
-
mouse.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
|
|
1009
|
-
const options = this.getActiveMeasurementOptions() ?? this.defaultOptions;
|
|
1010
|
-
const targets = this.activeTargets.length > 0 ? this.activeTargets : options.targets.length > 0 ? options.targets : this.getAllMeshes();
|
|
1011
|
-
this.raycaster.setFromCamera(mouse, this.camera);
|
|
1012
|
-
const intersects = this.raycaster.intersectObjects(targets, true);
|
|
1013
|
-
if (intersects.length === 0) return null;
|
|
1014
|
-
const intersection = intersects[0];
|
|
1015
|
-
let snapPoint = intersection.point.clone();
|
|
1016
|
-
let snapped = false;
|
|
1017
|
-
let snapMode = "disabled" /* DISABLED */;
|
|
1018
|
-
if (options.snapEnabled) {
|
|
1019
|
-
const snapResult = this.performSnapping(intersection, options);
|
|
1020
|
-
snapPoint = snapResult.point;
|
|
1021
|
-
snapped = snapResult.snapped;
|
|
1022
|
-
snapMode = snapResult.snapMode;
|
|
1023
|
-
}
|
|
1024
|
-
return {
|
|
1025
|
-
point: snapPoint,
|
|
1026
|
-
originalPoint: intersection.point,
|
|
1027
|
-
snapped,
|
|
1028
|
-
snapMode,
|
|
1029
|
-
object: intersection.object
|
|
1030
|
-
};
|
|
1031
|
-
}
|
|
1032
|
-
performSnapping(intersection, options) {
|
|
1033
|
-
const originalPoint = intersection.point;
|
|
1034
|
-
let snapPoint = originalPoint.clone();
|
|
1035
|
-
let snapped = false;
|
|
1036
|
-
let snapMode = "disabled" /* DISABLED */;
|
|
1037
|
-
if (options.snapMode === "vertex" /* VERTEX */) {
|
|
1038
|
-
const vertexSnap = this.snapToVertex(intersection, options.snapDistance);
|
|
1039
|
-
if (vertexSnap) {
|
|
1040
|
-
snapPoint = vertexSnap;
|
|
1041
|
-
snapped = true;
|
|
1042
|
-
snapMode = "vertex" /* VERTEX */;
|
|
1043
|
-
}
|
|
1044
|
-
} else if (options.snapMode === "face" /* FACE */) {
|
|
1045
|
-
snapped = true;
|
|
1046
|
-
snapMode = "face" /* FACE */;
|
|
1047
|
-
}
|
|
1048
|
-
return {
|
|
1049
|
-
point: snapPoint,
|
|
1050
|
-
originalPoint,
|
|
1051
|
-
snapped,
|
|
1052
|
-
snapMode,
|
|
1053
|
-
object: intersection.object
|
|
1054
|
-
};
|
|
1055
|
-
}
|
|
1056
|
-
snapToVertex(intersection, snapDistance) {
|
|
1057
|
-
const geometry = intersection.object.geometry;
|
|
1058
|
-
if (!geometry.attributes.position) return null;
|
|
1059
|
-
const positions = geometry.attributes.position;
|
|
1060
|
-
const worldMatrix = intersection.object.matrixWorld;
|
|
1061
|
-
const closestVertex = new THREE.Vector3();
|
|
1062
|
-
let minDistance = Infinity;
|
|
1063
|
-
let found = false;
|
|
1064
|
-
for (let i = 0; i < positions.count; i++) {
|
|
1065
|
-
const vertex = new THREE.Vector3();
|
|
1066
|
-
vertex.fromBufferAttribute(positions, i);
|
|
1067
|
-
vertex.applyMatrix4(worldMatrix);
|
|
1068
|
-
const distance = vertex.distanceTo(intersection.point);
|
|
1069
|
-
if (distance < snapDistance && distance < minDistance) {
|
|
1070
|
-
minDistance = distance;
|
|
1071
|
-
closestVertex.copy(vertex);
|
|
1072
|
-
found = true;
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
return found ? closestVertex : null;
|
|
1076
|
-
}
|
|
1077
|
-
createLabel(distance, options) {
|
|
1078
|
-
const labelDiv = document.createElement("div");
|
|
1079
|
-
labelDiv.className = "measurement-label";
|
|
1080
|
-
Object.assign(labelDiv.style, {
|
|
1081
|
-
color: options.labelColor,
|
|
1082
|
-
fontSize: `${options.fontSize}px`,
|
|
1083
|
-
fontFamily: options.fontFamily,
|
|
1084
|
-
fontWeight: "bold",
|
|
1085
|
-
background: "rgba(0, 0, 0, 0.9)",
|
|
1086
|
-
padding: "8px 12px",
|
|
1087
|
-
borderRadius: "8px",
|
|
1088
|
-
border: "2px solid rgba(255, 255, 255, 0.3)",
|
|
1089
|
-
whiteSpace: "nowrap",
|
|
1090
|
-
userSelect: "none",
|
|
1091
|
-
pointerEvents: "auto",
|
|
1092
|
-
// Enable pointer events for double-click
|
|
1093
|
-
boxShadow: "0 2px 8px rgba(0, 0, 0, 0.5)",
|
|
1094
|
-
textShadow: "1px 1px 2px rgba(0, 0, 0, 0.8)",
|
|
1095
|
-
zIndex: "1000",
|
|
1096
|
-
cursor: "pointer"
|
|
1097
|
-
});
|
|
1098
|
-
this.updateLabelText(labelDiv, distance);
|
|
1099
|
-
const css2dObject = new import_CSS2DRenderer.CSS2DObject(labelDiv);
|
|
1100
|
-
labelDiv.addEventListener("dblclick", (event) => {
|
|
1101
|
-
event.stopPropagation();
|
|
1102
|
-
const measurement = this.measurements.find((m) => m.label === css2dObject);
|
|
1103
|
-
if (measurement) {
|
|
1104
|
-
this.enterEditMode(measurement.id);
|
|
1105
|
-
}
|
|
1106
|
-
});
|
|
1107
|
-
return css2dObject;
|
|
1108
|
-
}
|
|
1109
|
-
updateLabelText(element, distance) {
|
|
1110
|
-
const text = `${distance.toFixed(2)}m`;
|
|
1111
|
-
element.textContent = text;
|
|
1112
|
-
}
|
|
1113
|
-
removeMeasurementFromScene(measurement) {
|
|
1114
|
-
this.scene.remove(measurement.line);
|
|
1115
|
-
this.scene.remove(measurement.label);
|
|
1116
|
-
measurement.line.geometry.dispose();
|
|
1117
|
-
if (measurement.line.material instanceof THREE.Material) {
|
|
1118
|
-
measurement.line.material.dispose();
|
|
1119
|
-
}
|
|
1120
|
-
if (measurement.label.element.parentNode) {
|
|
1121
|
-
measurement.label.element.parentNode.removeChild(
|
|
1122
|
-
measurement.label.element
|
|
1123
|
-
);
|
|
1124
|
-
}
|
|
1125
|
-
}
|
|
1126
|
-
getAllMeshes() {
|
|
1127
|
-
const meshes = [];
|
|
1128
|
-
this.scene.traverse((object) => {
|
|
1129
|
-
if (object instanceof THREE.Mesh) {
|
|
1130
|
-
meshes.push(object);
|
|
1131
|
-
}
|
|
1132
|
-
});
|
|
1133
|
-
return meshes;
|
|
1134
|
-
}
|
|
1135
|
-
generateId() {
|
|
1136
|
-
return `measurement_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
1137
|
-
}
|
|
1138
|
-
// Edit mode helper methods
|
|
1139
|
-
createEditSprites() {
|
|
1140
|
-
if (!this.editingMeasurement || !this.editSpriteMaterial) return;
|
|
1141
|
-
const measurement = this.editingMeasurement;
|
|
1142
|
-
this.startEditSprite = new THREE.Sprite(this.editSpriteMaterial.clone());
|
|
1143
|
-
this.startEditSprite.position.copy(measurement.start.position);
|
|
1144
|
-
this.startEditSprite.scale.set(this.markerSize, this.markerSize, 1);
|
|
1145
|
-
this.startEditSprite.userData.editPoint = "start";
|
|
1146
|
-
this.scene.add(this.startEditSprite);
|
|
1147
|
-
this.endEditSprite = new THREE.Sprite(this.editSpriteMaterial.clone());
|
|
1148
|
-
this.endEditSprite.position.copy(measurement.end.position);
|
|
1149
|
-
this.endEditSprite.scale.set(this.markerSize, this.markerSize, 1);
|
|
1150
|
-
this.endEditSprite.userData.editPoint = "end";
|
|
1151
|
-
this.scene.add(this.endEditSprite);
|
|
1152
|
-
}
|
|
1153
|
-
removeEditSprites() {
|
|
1154
|
-
if (this.startEditSprite) {
|
|
1155
|
-
this.scene.remove(this.startEditSprite);
|
|
1156
|
-
if (this.startEditSprite.material instanceof THREE.Material) {
|
|
1157
|
-
this.startEditSprite.material.dispose();
|
|
1158
|
-
}
|
|
1159
|
-
this.startEditSprite = null;
|
|
1160
|
-
}
|
|
1161
|
-
if (this.endEditSprite) {
|
|
1162
|
-
this.scene.remove(this.endEditSprite);
|
|
1163
|
-
if (this.endEditSprite.material instanceof THREE.Material) {
|
|
1164
|
-
this.endEditSprite.material.dispose();
|
|
1165
|
-
}
|
|
1166
|
-
this.endEditSprite = null;
|
|
1167
|
-
}
|
|
1168
|
-
}
|
|
1169
|
-
cancelEdit() {
|
|
1170
|
-
this.isDragging = false;
|
|
1171
|
-
this.editingPoint = null;
|
|
1172
|
-
this.enableControls();
|
|
1173
|
-
if (this.startEditSprite) {
|
|
1174
|
-
this.startEditSprite.visible = true;
|
|
1175
|
-
}
|
|
1176
|
-
if (this.endEditSprite) {
|
|
1177
|
-
this.endEditSprite.visible = true;
|
|
1178
|
-
}
|
|
1179
|
-
this.removeSnapMarker();
|
|
1180
|
-
this.showCursor();
|
|
1181
|
-
}
|
|
1182
|
-
updateMeasurementPreview(startPos, endPos) {
|
|
1183
|
-
if (!this.editingMeasurement) return;
|
|
1184
|
-
const distance = startPos.distanceTo(endPos);
|
|
1185
|
-
const positions = [startPos, endPos];
|
|
1186
|
-
this.editingMeasurement.line.geometry.setFromPoints(positions);
|
|
1187
|
-
this.editingMeasurement.line.geometry.attributes.position.needsUpdate = true;
|
|
1188
|
-
const midpoint = startPos.clone().add(endPos).multiplyScalar(0.5);
|
|
1189
|
-
this.editingMeasurement.label.position.copy(midpoint);
|
|
1190
|
-
this.updateLabelText(this.editingMeasurement.label.element, distance);
|
|
1191
|
-
}
|
|
1192
|
-
};
|
|
1193
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
1194
|
-
0 && (module.exports = {
|
|
1195
|
-
MeasurementTool,
|
|
1196
|
-
SnapMode
|
|
1197
|
-
});
|
|
1
|
+
"use strict";var b=Object.create;var m=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var S=Object.getPrototypeOf,O=Object.prototype.hasOwnProperty;var y=(d,c)=>{for(var e in c)m(d,e,{get:c[e],enumerable:!0})},f=(d,c,e,t)=>{if(c&&typeof c=="object"||typeof c=="function")for(let i of T(c))!O.call(d,i)&&i!==e&&m(d,i,{get:()=>c[i],enumerable:!(t=g(c,i))||t.enumerable});return d};var D=(d,c,e)=>(e=d!=null?b(S(d)):{},f(c||!d||!d.__esModule?m(e,"default",{value:d,enumerable:!0}):e,d)),R=d=>f(m({},"__esModule",{value:!0}),d);var H={};y(H,{MeasurementTool:()=>M,SnapMode:()=>E});module.exports=R(H);var a=D(require("three")),v=require("three/examples/jsm/renderers/CSS2DRenderer");var E=(i=>(i.VERTEX="vertex",i.FACE="face",i.EDGE="edge",i.DISABLED="disabled",i))(E||{});var M=class extends a.EventDispatcher{constructor(e,t,i={}){super();this.measurements=[];this.raycaster=new a.Raycaster;this.isInteractive=!1;this.domElement=null;this.controls=null;this.defaultTargets=[];this.activeTargets=[];this.currentMeasurement=null;this.activeInteractionOptions=null;this.pendingMeasurementOptions=null;this.previewLine=null;this.previewLabel=null;this.snapMarker=null;this.originalCursor="";this.cursorHidden=!1;this.defaultOptions={lineColor:16711680,labelColor:"#ffffff",lineWidth:2,fontSize:16,fontFamily:"Arial, sans-serif",snapMode:"vertex",snapEnabled:!0,snapDistance:.05,targets:[],isDynamic:!1};this.previewColor=65535;this.markerColor=65280;this.markerSize=.08;this.markerVisible=!0;this.isEditMode=!1;this.editingMeasurement=null;this.editingPoint=null;this.startEditSprite=null;this.endEditSprite=null;this.editSpriteMaterial=null;this.isDragging=!1;this.onMouseClick=e=>{if(!this.isInteractive)return;let t=this.getSnapResult(e);t&&(this.currentMeasurement?this.completeMeasurement(t):this.startMeasurement(t))};this.onMouseMove=e=>{if(!this.isInteractive)return;let t=this.getSnapResult(e);if(!t){this.hideSnapMarker(),this.showCursor();return}this.currentMeasurement?(this.updateSnapMarker(t.point,!0),this.hideCursor(),this.updatePreview(t.point)):(this.updateSnapMarker(t.point,!0),this.hideCursor())};this.onKeyDown=e=>{this.isInteractive&&e.key==="Escape"&&this.cancelCurrentMeasurement()};this.onEditMouseDown=e=>{if(!this.isEditMode||!this.domElement)return;let t=new a.Vector2,i=this.domElement.getBoundingClientRect();t.x=(e.clientX-i.left)/i.width*2-1,t.y=-((e.clientY-i.top)/i.height)*2+1,this.raycaster.setFromCamera(t,this.camera);let n=[this.startEditSprite,this.endEditSprite].filter(r=>r!==null),s=this.raycaster.intersectObjects(n);if(s.length>0){let r=s[0].object;this.editingPoint=r.userData.editPoint,this.isDragging=!0,this.disableControls(),this.editingPoint==="start"&&this.startEditSprite?this.startEditSprite.visible=!1:this.editingPoint==="end"&&this.endEditSprite&&(this.endEditSprite.visible=!1),this.createSnapMarker(),this.snapMarker&&(this.snapMarker.position.copy(r.position),this.snapMarker.visible=!0),this.hideCursor()}};this.onEditMouseMove=e=>{if(!this.isEditMode||!this.isDragging||!this.editingMeasurement)return;let t=this.getSnapResult(e);t&&(this.snapMarker&&(this.snapMarker.position.copy(t.point),this.snapMarker.visible=this.markerVisible),this.editingPoint==="start"?this.updateMeasurementPreview(t.point,this.editingMeasurement.end.position):this.editingPoint==="end"&&this.updateMeasurementPreview(this.editingMeasurement.start.position,t.point))};this.onEditMouseUp=e=>{if(!this.isEditMode||!this.isDragging||!this.editingMeasurement||!this.editingPoint)return;let t=this.getSnapResult(e);if(!t){this.cancelEdit();return}let i=this.editingPoint==="start"?this.editingMeasurement.start:this.editingMeasurement.end;if(i.position.copy(t.point),this.editingMeasurement.options.isDynamic&&t.object){let l=t.object.worldToLocal(t.point.clone());i.anchor={object:t.object,localPosition:l}}else i.anchor=void 0;let s=this.editingMeasurement.start.position.distanceTo(this.editingMeasurement.end.position);this.editingMeasurement.distance=s;let r=[this.editingMeasurement.start.position,this.editingMeasurement.end.position];this.editingMeasurement.line.geometry.setFromPoints(r),this.editingMeasurement.line.geometry.attributes.position.needsUpdate=!0;let o=this.editingMeasurement.start.position.clone().add(this.editingMeasurement.end.position).multiplyScalar(.5);this.editingMeasurement.label.position.copy(o),this.updateLabelText(this.editingMeasurement.label.element,s),this.startEditSprite&&(this.startEditSprite.position.copy(this.editingMeasurement.start.position),this.startEditSprite.visible=!0),this.endEditSprite&&(this.endEditSprite.position.copy(this.editingMeasurement.end.position),this.endEditSprite.visible=!0),this.removeSnapMarker(),this.showCursor(),this.enableControls(),this.dispatchEvent({type:"measurementUpdated",measurement:this.editingMeasurement}),this.isDragging=!1,this.editingPoint=null};this.scene=e,this.camera=t;let{domElement:n,controls:s}=i;n&&(this.domElement=n),s&&(this.controls=s),this.previewMaterial=new a.LineDashedMaterial({color:this.previewColor,linewidth:this.defaultOptions.lineWidth,dashSize:.1,gapSize:.05}),this.markerMaterial=new a.SpriteMaterial({map:this.createCrosshairTexture(),color:this.markerColor,transparent:!0,opacity:.8,sizeAttenuation:!1,depthTest:!1}),this.editSpriteMaterial=new a.SpriteMaterial({map:this.createDotTexture(),color:16755200,transparent:!0,opacity:.9,sizeAttenuation:!1,depthTest:!1}),this.raycaster.params.Line.threshold=.01,this.raycaster.params.Points.threshold=.01}createCrosshairTexture(){let t=document.createElement("canvas");t.width=64,t.height=64;let i=t.getContext("2d"),n=64/2,s=64/2,r=20,o=6;i.clearRect(0,0,64,64),i.strokeStyle="#ffffff",i.lineWidth=3,i.lineCap="round",i.beginPath(),i.moveTo(n-r,s),i.lineTo(n-o,s),i.moveTo(n+o,s),i.lineTo(n+r,s),i.moveTo(n,s-r),i.lineTo(n,s-o),i.moveTo(n,s+o),i.lineTo(n,s+r),i.stroke(),i.strokeStyle="#000000",i.lineWidth=5,i.globalCompositeOperation="destination-over",i.beginPath(),i.moveTo(n-r,s),i.lineTo(n-o,s),i.moveTo(n+o,s),i.lineTo(n+r,s),i.moveTo(n,s-r),i.lineTo(n,s-o),i.moveTo(n,s+o),i.lineTo(n,s+r),i.stroke();let l=new a.CanvasTexture(t);return l.needsUpdate=!0,l}createDotTexture(){let t=document.createElement("canvas");t.width=64,t.height=64;let i=t.getContext("2d"),n=64/2,s=64/2,r=12;i.clearRect(0,0,64,64),i.fillStyle="#ffffff",i.beginPath(),i.arc(n,s,r,0,Math.PI*2),i.fill(),i.strokeStyle="#000000",i.lineWidth=3,i.beginPath(),i.arc(n,s,r,0,Math.PI*2),i.stroke();let o=new a.CanvasTexture(t);return o.needsUpdate=!0,o}createMeasurementPoint(e,t,i){if(!t)return{position:e.clone()};let n=i?.clone()??t.worldToLocal(e.clone());return{position:t.localToWorld(n.clone()),anchor:{object:t,localPosition:n}}}updateMeasurementPoint(e){if(!e.anchor)return!1;let t=e.anchor.localPosition.clone();return e.anchor.object.localToWorld(t),e.position.equals(t)?!1:(e.position.copy(t),!0)}addMeasurement(e,t,i={}){let n=!!(i.startObject||i.endObject),s=this.resolveMeasurementOptions(i,i.isDynamic??n),r=this.createMeasurementPoint(e,i.startObject,i.startLocalPosition),o=this.createMeasurementPoint(t,i.endObject,i.endLocalPosition);return this.addMeasurementFromPoints(r,o,s,{id:i.id})}resolveMeasurementOptions(e={},t){let i;e.targets&&e.targets.length>0?i=e.targets:this.defaultOptions.targets.length>0?i=this.defaultOptions.targets:this.defaultTargets.length>0&&(i=this.defaultTargets);let n=i&&i.length>0?i:this.getAllMeshes(),s=e.isDynamic??t??this.defaultOptions.isDynamic;return{lineColor:e.lineColor??this.defaultOptions.lineColor,labelColor:e.labelColor??this.defaultOptions.labelColor,lineWidth:e.lineWidth??this.defaultOptions.lineWidth,fontSize:e.fontSize??this.defaultOptions.fontSize,fontFamily:e.fontFamily??this.defaultOptions.fontFamily,snapMode:e.snapMode??this.defaultOptions.snapMode,snapEnabled:e.snapEnabled??this.defaultOptions.snapEnabled,snapDistance:e.snapDistance??this.defaultOptions.snapDistance,targets:n,isDynamic:s}}getActiveMeasurementOptions(){return this.isEditMode&&this.editingMeasurement?this.editingMeasurement.options:this.activeInteractionOptions}addDynamicMeasurement(e,t,i=new a.Vector3,n=new a.Vector3){let s=e.localToWorld(i.clone()),r=t.localToWorld(n.clone());return this.addMeasurement(s,r,{startObject:e,endObject:t,startLocalPosition:i,endLocalPosition:n})}addMeasurementToObject(e,t,i=new a.Vector3){let n=t.localToWorld(i.clone());return this.addMeasurement(e,n,{endObject:t,endLocalPosition:i})}addMeasurementFromPoints(e,t,i,n){let s=n?.id||this.generateId(),r=e.position.distanceTo(t.position),o=new a.BufferGeometry().setFromPoints([e.position,t.position]),l=new a.Line(o,new a.LineBasicMaterial({color:i.lineColor,linewidth:i.lineWidth})),u=this.createLabel(r,i),h=e.position.clone().add(t.position).multiplyScalar(.5);u.position.copy(h);let p={id:s,start:e,end:t,line:l,label:u,distance:r,options:{...i,targets:[...i.targets]}};return this.scene.add(l),this.scene.add(u),this.measurements.push(p),this.dispatchEvent({type:"measurementCreated",measurement:p}),p}updateDynamicMeasurements(){let e=!1;for(let t of this.measurements){if(!t.options.isDynamic)continue;let i=!1;if(this.updateMeasurementPoint(t.start)&&(i=!0),this.updateMeasurementPoint(t.end)&&(i=!0),i){let n=t.start.position.distanceTo(t.end.position);t.distance=n;let s=[t.start.position,t.end.position];t.line.geometry.setFromPoints(s),t.line.geometry.attributes.position.needsUpdate=!0;let r=t.start.position.clone().add(t.end.position).multiplyScalar(.5);t.label.position.copy(r),this.updateLabelText(t.label.element,n),e=!0}}return e}setDynamicMode(e){this.setDefaultMeasurementOptions({isDynamic:e})}getDynamicMode(){return this.defaultOptions.isDynamic}enterEditMode(e,t){this.exitEditMode();let i;if(typeof e=="string"?i=this.measurements.find(s=>s.id===e):i=this.measurements[e],!i){console.warn("Measurement not found:",e);return}if(!this.domElement){console.warn("DOM element not set. Call setDomElement() or enableInteraction() first.");return}this.isEditMode=!0,this.editingMeasurement=i;let n=t&&t.length>0?t:i.options.targets.length>0?i.options.targets:this.getAllMeshes();this.activeTargets=n,t&&t.length>0&&(i.options.targets=[...t]),this.createEditSprites(),this.domElement.addEventListener("mousedown",this.onEditMouseDown),this.domElement.addEventListener("mousemove",this.onEditMouseMove),this.domElement.addEventListener("mouseup",this.onEditMouseUp),this.domElement.style.cursor="pointer",this.dispatchEvent({type:"editModeEntered",measurement:i})}exitEditMode(){if(!this.isEditMode)return;let e=this.editingMeasurement;this.removeEditSprites(),this.domElement&&(this.domElement.removeEventListener("mousedown",this.onEditMouseDown),this.domElement.removeEventListener("mousemove",this.onEditMouseMove),this.domElement.removeEventListener("mouseup",this.onEditMouseUp),this.domElement.style.cursor=this.isInteractive?"crosshair":"default"),this.isEditMode=!1,this.editingMeasurement=null,this.editingPoint=null,this.isDragging=!1,this.activeTargets=[],e&&this.dispatchEvent({type:"editModeExited",measurement:e})}setDomElement(e){this.domElement=e}setControls(e){this.controls=e}setTargetObjects(e){this.defaultTargets=e.length>0?e:this.getAllMeshes(),this.defaultOptions.targets=[...this.defaultTargets]}setDefaultMeasurementOptions(e){e.lineColor!==void 0&&(this.defaultOptions.lineColor=e.lineColor),e.labelColor!==void 0&&(this.defaultOptions.labelColor=e.labelColor),e.lineWidth!==void 0&&(this.defaultOptions.lineWidth=e.lineWidth),e.fontSize!==void 0&&(this.defaultOptions.fontSize=e.fontSize),e.fontFamily!==void 0&&(this.defaultOptions.fontFamily=e.fontFamily),e.snapMode!==void 0&&(this.defaultOptions.snapMode=e.snapMode),e.snapEnabled!==void 0&&(this.defaultOptions.snapEnabled=e.snapEnabled),e.snapDistance!==void 0&&(this.defaultOptions.snapDistance=e.snapDistance),e.targets!==void 0&&(this.defaultTargets=e.targets,this.defaultOptions.targets=[...e.targets],this.activeInteractionOptions&&(this.activeInteractionOptions.targets=[...e.targets],this.activeTargets=e.targets)),e.isDynamic!==void 0&&(this.defaultOptions.isDynamic=e.isDynamic,this.activeInteractionOptions&&(this.activeInteractionOptions.isDynamic=e.isDynamic)),this.activeInteractionOptions&&(e.lineColor!==void 0&&(this.activeInteractionOptions.lineColor=e.lineColor),e.labelColor!==void 0&&(this.activeInteractionOptions.labelColor=e.labelColor),e.lineWidth!==void 0&&(this.activeInteractionOptions.lineWidth=e.lineWidth),e.fontSize!==void 0&&(this.activeInteractionOptions.fontSize=e.fontSize),e.fontFamily!==void 0&&(this.activeInteractionOptions.fontFamily=e.fontFamily),e.snapMode!==void 0&&(this.activeInteractionOptions.snapMode=e.snapMode),e.snapEnabled!==void 0&&(this.activeInteractionOptions.snapEnabled=e.snapEnabled),e.snapDistance!==void 0&&(this.activeInteractionOptions.snapDistance=e.snapDistance))}disableControls(){this.controls&&(this.controls.enabled=!1)}enableControls(){this.controls&&(this.controls.enabled=!0)}enableInteraction(e={}){this.isInteractive&&this.disableInteraction();let t=this.resolveMeasurementOptions(e);this.activeInteractionOptions={...t,targets:[...t.targets]},this.activeTargets=this.activeInteractionOptions.targets.length>0?this.activeInteractionOptions.targets:this.getAllMeshes(),this.isInteractive=!0,this.domElement&&(this.domElement.addEventListener("click",this.onMouseClick),this.domElement.addEventListener("mousemove",this.onMouseMove),this.domElement.addEventListener("keydown",this.onKeyDown),this.domElement.style.cursor="crosshair"),this.createSnapMarker(),this.dispatchEvent({type:"started"})}disableInteraction(){!this.isInteractive||!this.domElement||(this.exitEditMode(),this.domElement.removeEventListener("click",this.onMouseClick),this.domElement.removeEventListener("mousemove",this.onMouseMove),this.domElement.removeEventListener("keydown",this.onKeyDown),this.showCursor(),this.domElement.style.cursor="default",this.cancelCurrentMeasurement(),this.removeSnapMarker(),this.isInteractive=!1,this.activeInteractionOptions=null,this.activeTargets=[],this.dispatchEvent({type:"ended"}))}undoLast(){if(this.measurements.length===0)return;let e=this.measurements.pop();this.removeMeasurementFromScene(e),this.dispatchEvent({type:"measurementRemoved",measurement:e})}removeMeasurement(e){let t=this.measurements.indexOf(e);t!==-1&&(this.measurements.splice(t,1),this.removeMeasurementFromScene(e),this.dispatchEvent({type:"measurementRemoved",measurement:e}))}clearAll(){let e=this.measurements.length;this.measurements.forEach(t=>{this.removeMeasurementFromScene(t)}),this.measurements=[],this.dispatchEvent({type:"measurementsCleared",count:e})}getMeasurements(){return[...this.measurements]}serializeMeasurementPoint(e){return{position:e.position.toArray(),anchorObjectId:e.anchor?.object.uuid,anchorLocalPosition:e.anchor?e.anchor.localPosition.toArray():void 0}}serialize(){return this.measurements.map(e=>({id:e.id,start:this.serializeMeasurementPoint(e.start),end:this.serializeMeasurementPoint(e.end),distance:e.distance,options:{snapMode:e.options.snapMode,snapEnabled:e.options.snapEnabled,snapDistance:e.options.snapDistance,lineColor:e.options.lineColor,labelColor:e.options.labelColor,lineWidth:e.options.lineWidth,fontSize:e.options.fontSize,fontFamily:e.options.fontFamily,isDynamic:e.options.isDynamic,targetObjectIds:e.options.targets.map(t=>t.uuid)}}))}deserialize(e){this.clearAll(),e.forEach(t=>{let i=new a.Vector3().fromArray(t.start.position),n=new a.Vector3().fromArray(t.end.position),s=t.start.anchorObjectId?this.scene.getObjectByProperty("uuid",t.start.anchorObjectId):null,r=t.end.anchorObjectId?this.scene.getObjectByProperty("uuid",t.end.anchorObjectId):null,o=t.options.targetObjectIds&&t.options.targetObjectIds.length>0?t.options.targetObjectIds.map(l=>this.scene.getObjectByProperty("uuid",l)).filter(l=>l!==void 0):void 0;this.addMeasurement(i,n,{id:t.id,targets:o&&o.length>0?o:void 0,snapMode:t.options.snapMode,snapEnabled:t.options.snapEnabled,snapDistance:t.options.snapDistance,lineColor:t.options.lineColor,labelColor:t.options.labelColor,lineWidth:t.options.lineWidth,fontSize:t.options.fontSize,fontFamily:t.options.fontFamily,isDynamic:t.options.isDynamic,startObject:s||void 0,startLocalPosition:t.start.anchorLocalPosition?new a.Vector3().fromArray(t.start.anchorLocalPosition):void 0,endObject:r||void 0,endLocalPosition:t.end.anchorLocalPosition?new a.Vector3().fromArray(t.end.anchorLocalPosition):void 0})})}dispose(){this.exitEditMode(),this.disableInteraction(),this.clearAll(),this.previewMaterial.dispose(),this.markerMaterial.map&&this.markerMaterial.map.dispose(),this.markerMaterial.dispose(),this.editSpriteMaterial&&(this.editSpriteMaterial.map&&this.editSpriteMaterial.map.dispose(),this.editSpriteMaterial.dispose())}hideCursor(){!this.domElement||this.cursorHidden||(this.originalCursor=this.domElement.style.cursor,this.domElement.style.cursor="none",this.cursorHidden=!0)}showCursor(){!this.domElement||!this.cursorHidden||(this.domElement.style.cursor=this.originalCursor||"crosshair",this.cursorHidden=!1)}createSnapMarker(){this.markerVisible&&(this.snapMarker&&this.scene.remove(this.snapMarker),this.snapMarker=new a.Sprite(this.markerMaterial),this.snapMarker.scale.setScalar(this.markerSize),this.snapMarker.visible=!1,this.snapMarker.renderOrder=999,this.snapMarker.material.depthTest=!1,this.scene.add(this.snapMarker))}updateSnapMarker(e,t=!0){!this.snapMarker||!this.markerVisible||(this.snapMarker.position.copy(e),this.snapMarker.visible=t)}hideSnapMarker(){this.snapMarker&&(this.snapMarker.visible=!1)}removeSnapMarker(){this.snapMarker&&(this.scene.remove(this.snapMarker),this.snapMarker=null)}startMeasurement(e){let t=this.generateId(),i=e.point,n=this.activeInteractionOptions??this.defaultOptions,s={...n,targets:[...n.targets]};this.pendingMeasurementOptions=s,this.hideSnapMarker();let r=new a.BufferGeometry().setFromPoints([i,i]);this.previewLine=new a.Line(r,this.previewMaterial),this.previewLine.computeLineDistances(),this.scene.add(this.previewLine),this.previewLabel=this.createLabel(0,s),this.previewLabel.position.copy(i),this.scene.add(this.previewLabel);let o=s.isDynamic&&e.object?this.createMeasurementPoint(i,e.object):this.createMeasurementPoint(i);this.currentMeasurement={id:t,start:o}}updatePreview(e){if(!this.currentMeasurement||!this.previewLine||!this.previewLabel)return;let t=this.currentMeasurement.start,i=t.position.distanceTo(e),n=new a.BufferGeometry().setFromPoints([t.position,e]);this.previewLine.geometry.dispose(),this.previewLine.geometry=n,this.previewLine.computeLineDistances();let s=t.position.clone().add(e).multiplyScalar(.5);this.previewLabel.position.copy(s),this.updateLabelText(this.previewLabel.element,i),this.dispatchEvent({type:"previewUpdated",start:t.position,current:e,distance:i})}completeMeasurement(e){if(!this.currentMeasurement)return;let t=this.currentMeasurement.start,i=e.point,n=this.pendingMeasurementOptions??this.activeInteractionOptions??this.defaultOptions;this.disableInteraction(),this.cleanupPreview();let s={...n,targets:[...n.targets]},r=s.isDynamic&&e.object?this.createMeasurementPoint(i,e.object):this.createMeasurementPoint(i);this.addMeasurementFromPoints(t,r,s),this.currentMeasurement=null,this.pendingMeasurementOptions=null,this.createSnapMarker()}cancelCurrentMeasurement(){this.cleanupPreview(),this.currentMeasurement=null,this.pendingMeasurementOptions=null,this.createSnapMarker()}cleanupPreview(){this.previewLine&&(this.scene.remove(this.previewLine),this.previewLine.geometry.dispose(),this.previewLine=null),this.previewLabel&&(this.scene.remove(this.previewLabel),this.previewLabel.element.parentNode&&this.previewLabel.element.parentNode.removeChild(this.previewLabel.element),this.previewLabel=null)}getSnapResult(e){let t=new a.Vector2,i=this.domElement.getBoundingClientRect();t.x=(e.clientX-i.left)/i.width*2-1,t.y=-((e.clientY-i.top)/i.height)*2+1;let n=this.getActiveMeasurementOptions()??this.defaultOptions,s=this.activeTargets.length>0?this.activeTargets:n.targets.length>0?n.targets:this.getAllMeshes();this.raycaster.setFromCamera(t,this.camera);let r=this.raycaster.intersectObjects(s,!0);if(r.length===0)return null;let o=r[0],l=o.point.clone(),u=!1,h="disabled";if(n.snapEnabled){let p=this.performSnapping(o,n);l=p.point,u=p.snapped,h=p.snapMode}return{point:l,originalPoint:o.point,snapped:u,snapMode:h,object:o.object}}performSnapping(e,t){let i=e.point,n=i.clone(),s=!1,r="disabled";if(t.snapMode==="vertex"){let o=this.snapToVertex(e,t.snapDistance);o&&(n=o,s=!0,r="vertex")}else t.snapMode==="face"&&(s=!0,r="face");return{point:n,originalPoint:i,snapped:s,snapMode:r,object:e.object}}snapToVertex(e,t){let i=e.object.geometry;if(!i.attributes.position)return null;let n=i.attributes.position,s=e.object.matrixWorld,r=new a.Vector3,o=1/0,l=!1;for(let u=0;u<n.count;u++){let h=new a.Vector3;h.fromBufferAttribute(n,u),h.applyMatrix4(s);let p=h.distanceTo(e.point);p<t&&p<o&&(o=p,r.copy(h),l=!0)}return l?r:null}createLabel(e,t){let i=document.createElement("div");i.className="measurement-label",Object.assign(i.style,{color:t.labelColor,fontSize:`${t.fontSize}px`,fontFamily:t.fontFamily,fontWeight:"bold",background:"rgba(0, 0, 0, 0.9)",padding:"8px 12px",borderRadius:"8px",border:"2px solid rgba(255, 255, 255, 0.3)",whiteSpace:"nowrap",userSelect:"none",pointerEvents:"auto",boxShadow:"0 2px 8px rgba(0, 0, 0, 0.5)",textShadow:"1px 1px 2px rgba(0, 0, 0, 0.8)",zIndex:"1000",cursor:"pointer"}),this.updateLabelText(i,e);let n=new v.CSS2DObject(i);return i.addEventListener("dblclick",s=>{s.stopPropagation();let r=this.measurements.find(o=>o.label===n);r&&this.enterEditMode(r.id)}),n}updateLabelText(e,t){let i=`${t.toFixed(2)}m`;e.textContent=i}removeMeasurementFromScene(e){this.scene.remove(e.line),this.scene.remove(e.label),e.line.geometry.dispose(),e.line.material instanceof a.Material&&e.line.material.dispose(),e.label.element.parentNode&&e.label.element.parentNode.removeChild(e.label.element)}getAllMeshes(){let e=[];return this.scene.traverse(t=>{t instanceof a.Mesh&&e.push(t)}),e}generateId(){return`measurement_${Date.now()}_${Math.random().toString(36).substr(2,9)}`}createEditSprites(){if(!this.editingMeasurement||!this.editSpriteMaterial)return;let e=this.editingMeasurement;this.startEditSprite=new a.Sprite(this.editSpriteMaterial.clone()),this.startEditSprite.position.copy(e.start.position),this.startEditSprite.scale.set(this.markerSize,this.markerSize,1),this.startEditSprite.userData.editPoint="start",this.scene.add(this.startEditSprite),this.endEditSprite=new a.Sprite(this.editSpriteMaterial.clone()),this.endEditSprite.position.copy(e.end.position),this.endEditSprite.scale.set(this.markerSize,this.markerSize,1),this.endEditSprite.userData.editPoint="end",this.scene.add(this.endEditSprite)}removeEditSprites(){this.startEditSprite&&(this.scene.remove(this.startEditSprite),this.startEditSprite.material instanceof a.Material&&this.startEditSprite.material.dispose(),this.startEditSprite=null),this.endEditSprite&&(this.scene.remove(this.endEditSprite),this.endEditSprite.material instanceof a.Material&&this.endEditSprite.material.dispose(),this.endEditSprite=null)}cancelEdit(){this.isDragging=!1,this.editingPoint=null,this.enableControls(),this.startEditSprite&&(this.startEditSprite.visible=!0),this.endEditSprite&&(this.endEditSprite.visible=!0),this.removeSnapMarker(),this.showCursor()}updateMeasurementPreview(e,t){if(!this.editingMeasurement)return;let i=e.distanceTo(t),n=[e,t];this.editingMeasurement.line.geometry.setFromPoints(n),this.editingMeasurement.line.geometry.attributes.position.needsUpdate=!0;let s=e.clone().add(t).multiplyScalar(.5);this.editingMeasurement.label.position.copy(s),this.updateLabelText(this.editingMeasurement.label.element,i)}};0&&(module.exports={MeasurementTool,SnapMode});
|