@needle-tools/engine 2.56.2-pre → 2.57.0-pre

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/needle-engine.d.ts +66 -158
  3. package/dist/needle-engine.js +279 -3420
  4. package/dist/needle-engine.js.map +4 -4
  5. package/dist/needle-engine.min.js +18 -18
  6. package/dist/needle-engine.min.js.map +4 -4
  7. package/dist/needle-engine.tsbuildinfo +1 -1
  8. package/lib/engine/api.d.ts +2 -1
  9. package/lib/engine/api.js +2 -1
  10. package/lib/engine/api.js.map +1 -1
  11. package/lib/engine/codegen/register_types.js +0 -6
  12. package/lib/engine/codegen/register_types.js.map +1 -1
  13. package/lib/engine/engine.d.ts +1 -28
  14. package/lib/engine/engine.js.map +1 -1
  15. package/lib/engine/engine_scenetools.d.ts +1 -0
  16. package/lib/engine/engine_scenetools.js +8 -6
  17. package/lib/engine/engine_scenetools.js.map +1 -1
  18. package/lib/engine/engine_setup.d.ts +4 -2
  19. package/lib/engine/engine_setup.js +25 -14
  20. package/lib/engine/engine_setup.js.map +1 -1
  21. package/lib/engine/engine_three_utils.js.map +1 -1
  22. package/lib/engine/engine_utils_screenshot.d.ts +5 -0
  23. package/lib/engine/engine_utils_screenshot.js +32 -0
  24. package/lib/engine/engine_utils_screenshot.js.map +1 -0
  25. package/lib/engine-components/Skybox.js +3 -1
  26. package/lib/engine-components/Skybox.js.map +1 -1
  27. package/lib/engine-components/WebXR.js +2 -2
  28. package/lib/engine-components/WebXR.js.map +1 -1
  29. package/lib/tsconfig.tsbuildinfo +1 -1
  30. package/package.json +1 -2
  31. package/src/engine/api.ts +3 -2
  32. package/src/engine/codegen/register_types.js +2 -8
  33. package/src/engine/engine.ts +1 -1
  34. package/src/engine/engine_scenetools.ts +9 -6
  35. package/src/engine/engine_setup.ts +27 -16
  36. package/src/engine/engine_three_utils.ts +4 -4
  37. package/src/engine/engine_utils_screenshot.ts +41 -0
  38. package/src/engine-components/Skybox.ts +3 -1
  39. package/src/engine-components/TestRunner.ts +1 -1
  40. package/src/engine-components/WebXR.ts +2 -2
  41. package/lib/engine-components-experimental/annotation/LineDrawer.d.ts +0 -18
  42. package/lib/engine-components-experimental/annotation/LineDrawer.js +0 -175
  43. package/lib/engine-components-experimental/annotation/LineDrawer.js.map +0 -1
  44. package/lib/engine-components-experimental/annotation/LinesManager.d.ts +0 -54
  45. package/lib/engine-components-experimental/annotation/LinesManager.js +0 -155
  46. package/lib/engine-components-experimental/annotation/LinesManager.js.map +0 -1
  47. package/src/engine-components-experimental/annotation/LineDrawer.ts +0 -194
  48. package/src/engine-components-experimental/annotation/LinesManager.ts +0 -218
@@ -1,155 +0,0 @@
1
- import { Behaviour, GameObject } from "../../engine-components/Component";
2
- import * as THREE from 'three';
3
- import { MeshLine, MeshLineMaterial } from 'three.meshline';
4
- export class LineInstanceHandler {
5
- id = 0;
6
- points = [];
7
- line = new MeshLine();
8
- material;
9
- mesh;
10
- constructor(owner, options) {
11
- if (options) {
12
- this.material = options.material;
13
- }
14
- if (!this.material)
15
- this.material = this.defaultLineMaterial;
16
- if (options) {
17
- const opts = options.options;
18
- if (opts) {
19
- Object.assign(this.material, opts);
20
- }
21
- }
22
- this.mesh = new THREE.Mesh(this.line, this.material);
23
- owner.add(this.mesh);
24
- }
25
- static wp = new THREE.Vector3();
26
- appendPoint(vec) {
27
- let localPoint = LineInstanceHandler.wp;
28
- localPoint.set(vec.x, vec.y, vec.z);
29
- const parent = this.mesh?.parent;
30
- if (parent) {
31
- localPoint = parent.worldToLocal(localPoint);
32
- vec.x = localPoint.x;
33
- vec.y = localPoint.y;
34
- vec.z = localPoint.z;
35
- }
36
- this.points.push(vec.x, vec.y, vec.z);
37
- this.line.setPoints(this.points);
38
- return vec;
39
- }
40
- defaultLineMaterial = new MeshLineMaterial({ color: 0x999999, lineWidth: 0.01 });
41
- }
42
- export class LinesManager extends Behaviour {
43
- startLine(parent, options) {
44
- const id = Math.random() * Number.MAX_SAFE_INTEGER;
45
- return this.internalStartLine(parent, id, true, options);
46
- }
47
- updateLine(handle, args) {
48
- const line = this.inFlight[handle.id];
49
- if (!line)
50
- return;
51
- if (args.point) {
52
- args.point = line.appendPoint(args.point);
53
- }
54
- const buf = this.buffer[handle.id];
55
- if (buf) {
56
- if (args.point)
57
- buf.push(args.point.x, args.point.y, args.point.z);
58
- if (buf.length > 5) {
59
- this.sendLineUpdate(line, false, undefined, buf);
60
- buf.length = 0;
61
- }
62
- }
63
- }
64
- endLine(handle, send = true) {
65
- const line = this.inFlight[handle.id];
66
- if (!line)
67
- return undefined;
68
- this.finished.push(line);
69
- delete this.inFlight[handle.id];
70
- if (send)
71
- this.sendLineUpdate(line, true, 0);
72
- const buf = this.buffer[handle.id];
73
- if (buf) {
74
- delete this.buffer[handle.id];
75
- const arr = buf;
76
- arr.length = 0;
77
- this.freeBuffer.push(arr);
78
- }
79
- return line;
80
- }
81
- getLine(handle) {
82
- return this.inFlight[handle.id];
83
- }
84
- finished = [];
85
- inFlight = [];
86
- buffer = {};
87
- freeBuffer = new Array();
88
- awake() {
89
- this.context.connection.beginListen("line-start", (i) => {
90
- this.onEnsureLine(i.id, i.parentGuid);
91
- });
92
- this.context.connection.beginListen("line-update", (evt) => {
93
- let line = this.onEnsureLine(evt.guid, evt.parentGuid);
94
- if (line && evt.points) {
95
- if (evt.startIndex <= 0) {
96
- line.points = evt.points;
97
- }
98
- else {
99
- if (evt.startIndex >= line.points.length) {
100
- line.points.push(...evt.points);
101
- }
102
- }
103
- line.line.setPoints(line.points);
104
- line.material.lineWidth = evt.width;
105
- line.material.color.fromArray(evt.color);
106
- if (evt.finished === true) {
107
- this.endLine({ id: line.id }, false);
108
- }
109
- }
110
- });
111
- }
112
- onEnsureLine(lineId, parentGuid) {
113
- if (this.inFlight[lineId])
114
- return this.inFlight[lineId];
115
- let obj = GameObject.findByGuid(parentGuid, this.context.scene);
116
- if (!obj) {
117
- return null;
118
- }
119
- this.internalStartLine(obj, lineId, false);
120
- return this.inFlight[lineId];
121
- }
122
- internalStartLine(parent, id, send = true, options) {
123
- const line = new LineInstanceHandler(parent ?? this.context.scene, options);
124
- line.id = id;
125
- this.inFlight[id] = line;
126
- if (send)
127
- this.sendLineStart(line);
128
- let buf;
129
- if (this.freeBuffer.length <= 0)
130
- buf = new Array();
131
- else
132
- buf = this.freeBuffer.pop();
133
- this.buffer[id] = buf;
134
- return { id: id };
135
- }
136
- sendLineStart(instance) {
137
- const parent = instance.mesh.parent;
138
- this.context.connection.send("line-start", { id: instance.id, parentGuid: parent ? parent["guid"] : undefined });
139
- }
140
- sendLineUpdate(instance, finished, startIndex, points) {
141
- if (instance) {
142
- const model = {
143
- parentGuid: instance.mesh.parent["guid"],
144
- guid: instance.id,
145
- points: points ? [...points] : instance.points,
146
- width: instance.material.lineWidth,
147
- color: instance.material.color.toArray(),
148
- startIndex: startIndex !== undefined ? startIndex : instance.points.length,
149
- finished: finished,
150
- };
151
- this.context.connection.send("line-update", model);
152
- }
153
- }
154
- }
155
- //# sourceMappingURL=LinesManager.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LinesManager.js","sourceRoot":"","sources":["../../../../engine-components-experimental/annotation/LinesManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAC;AAC1E,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAkB5D,MAAM,OAAO,mBAAmB;IAC5B,EAAE,GAAW,CAAC,CAAC;IACf,MAAM,GAAe,EAAE,CAAC;IACxB,IAAI,GAAa,IAAI,QAAQ,EAAE,CAAC;IAChC,QAAQ,CAAoB;IAC5B,IAAI,CAAc;IAElB,YAAY,KAAqB,EAAE,OAAqB;QACpD,IAAI,OAAO,EAAE;YACT,IAAI,CAAC,QAAQ,GAAG,OAAO,CAA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,EAAE,QAAQ,CAAC,EAAE;gBACjB,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM;gBAC9C,KAAK,EAAE,QAAQ,CAAC,QAAS,CAAC,SAAS;gBACnC,KAAK,EAAE,QAAQ,CAAC,QAAS,CAAC,KAAK,CAAC,OAAO,EAAE;gBACzC,UAAU,EAAE,UAAU,KAAK,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM;gBAC1E,QAAQ,EAAE,QAAQ;aACrB,CAAC;YACF,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,aAAa,EAAE,KAAK,CAAC,CAAA;SACrD;IACL,CAAC;CACJ"}
@@ -1,194 +0,0 @@
1
- import { Behaviour, GameObject } from "../../engine-components/Component";
2
- import { OrbitControls } from "../../engine-components/OrbitControls";
3
- import * as THREE from 'three';
4
- import { Color, Ray, Raycaster, Vector3 } from "three";
5
- import { RaycastOptions } from "../../engine/engine_physics";
6
- import { LineHandle, LinesManager } from "./LinesManager";
7
- import { Mathf } from "../../engine/engine_math";
8
- import { getWorldPosition } from "../../engine/engine_three_utils";
9
- import { MeshLine, MeshLineMaterial } from 'three.meshline';
10
- import { KeyCode } from "../../engine/engine_input";
11
- import { PlayerColor } from "../../engine-components/PlayerColor";
12
- import { ControllerEvents, WebXRController } from "../../engine-components/WebXRController";
13
-
14
- class LineState {
15
- isDrawing: boolean;
16
- lastHit: Vector3;
17
- currentHandle: LineHandle | null;
18
- maxDistance: number;
19
- prevDistance: number;
20
- lastParent: THREE.Object3D | null;
21
-
22
- constructor() {
23
- this.isDrawing = false;
24
- this.lastHit = new Vector3();
25
- this.currentHandle = null;
26
- this.maxDistance = 0;
27
- this.prevDistance = 0;
28
- this.lastParent = null;
29
- }
30
- }
31
-
32
- export class LinesDrawer extends Behaviour {
33
-
34
- lines!: LinesManager;
35
- colliders?: THREE.Object3D[];
36
- alignToSurface: boolean = true;
37
- addToPaintedObject: boolean = true;
38
-
39
- private orbit?: OrbitControls;
40
-
41
- start() {
42
- if (!this.lines) {
43
- this.lines = GameObject.getComponent(this.gameObject, LinesManager)!;
44
- if (!this.lines)
45
- this.lines = GameObject.addNewComponent(this.gameObject, LinesManager);
46
- }
47
- this.orbit = GameObject.findObjectOfType(OrbitControls, this.context) ?? undefined;
48
- this._states["mouse"] = new LineState();
49
-
50
-
51
- const xrControllerSelected: { [key: string]: boolean } = {};
52
-
53
- WebXRController.addEventListener(ControllerEvents.SelectStart, (ctrl, _) => {
54
- xrControllerSelected[ctrl.controller.uuid] = true;
55
- });
56
- WebXRController.addEventListener(ControllerEvents.Update, (ctrl, _) => {
57
- if (xrControllerSelected[ctrl.controller.uuid] === true) {
58
- const ray = ctrl.getRay();
59
- this.updateLine(ctrl.controller.uuid, ray, true, false, false);
60
- }
61
- });
62
- WebXRController.addEventListener(ControllerEvents.SelectEnd, (ctrl, _) => {
63
- xrControllerSelected[ctrl.controller.uuid] = false;
64
- const ray = ctrl.getRay();
65
- this.updateLine(ctrl.controller.uuid, ray, true, true, false);
66
- });
67
- }
68
-
69
- private _states: { [id: string]: LineState } = {};
70
-
71
- update() {
72
- if (this.orbit && this._states["mouse"]) {
73
- if (this.orbit) this.orbit.enabled = !this._states["mouse"].isDrawing;
74
- }
75
- if (!this.context.mainCamera) return;
76
-
77
- const multi = this.context.input.getPointerPressedCount() > 1;
78
- const sp = this.context.input.getPointerPositionRC(0);
79
- if (!sp) return;
80
- LinesDrawer._raycaster.setFromCamera(sp, this.context.mainCamera);
81
- const ray = LinesDrawer._raycaster.ray;
82
- this.updateLine("mouse", ray,
83
- this.context.input.getPointerPressed(0),
84
- this.context.input.getPointerUp(0), multi || this.context.input.isKeyPressed(KeyCode.ALT)
85
- );
86
- }
87
-
88
- private updateLine(id: string, ray: THREE.Ray, active: boolean, finish: boolean, cancel: boolean = false): LineState {
89
- let state = this._states[id];
90
- if (!state) {
91
- this._states[id] = new LineState();
92
- state = this._states[id];
93
- }
94
-
95
- if (finish) {
96
- state.isDrawing = false;
97
- if (state.currentHandle) {
98
- // this.sendLineUpdate();
99
- this.lines.endLine(state.currentHandle);
100
- state.currentHandle = null;
101
- }
102
- }
103
- else if (active) {
104
- if (cancel) {
105
- return state;
106
- }
107
- const hit = this.getHit(ray);
108
- let pt: THREE.Vector3 | null = null;
109
- let prev = state.prevDistance;
110
- if (hit) {
111
- if (!state.currentHandle) {
112
- state.maxDistance = hit.distance;
113
- }
114
- pt = hit.point;
115
- if (hit.face)
116
- pt.add(hit.face.normal.multiplyScalar(0.01));
117
- state.prevDistance = hit.distance;
118
- }
119
- else if (state.maxDistance > 0) {
120
- let dist = state.maxDistance;
121
- // if we start drawing in thin air:
122
- if (!state.currentHandle && state.lastHit) {
123
- const wp = getWorldPosition(this.context.mainCamera!);
124
- dist = state.lastHit.distanceTo(wp);
125
- }
126
- pt = ray.origin.add(ray.direction.multiplyScalar(state.maxDistance));
127
- state.prevDistance = state.maxDistance;
128
- }
129
-
130
- if (pt) {
131
- if (!state.currentHandle) {
132
- let parent = state.lastParent ?? this.gameObject as THREE.Object3D;
133
- if (this.addToPaintedObject && hit) parent = hit.object;
134
- state.lastParent = parent;
135
- state.currentHandle = this.lines.startLine(parent, { material: this.createRandomMaterial() });
136
- }
137
-
138
- if (this.alignToSurface) {
139
- if (state.prevDistance > state.maxDistance || Math.abs(prev - state.prevDistance) > 0.2) {
140
- const newDistance = state.maxDistance;
141
- // if (state.maxDistance === 0) state.maxDistance = newDistance;
142
- // const camPos = getWorldPosition(this.context.mainCamera);
143
- pt = ray.origin.add(ray.direction.multiplyScalar(newDistance));
144
- // pt = camPos.add(dir.multiplyScalar(newDistance));
145
- state.prevDistance = newDistance;
146
- }
147
- }
148
- if (state.lastHit && state.lastHit.distanceTo(pt) < state.prevDistance * .01) {
149
- return state;
150
- }
151
- this.lines.updateLine(state.currentHandle, { point: pt });
152
- state.lastHit.copy(pt);
153
- }
154
-
155
- state.isDrawing = state.currentHandle !== null;
156
- }
157
- return state;
158
- }
159
-
160
- private _raycastOptions = new RaycastOptions();
161
- private static _raycaster: Raycaster = new Raycaster();
162
-
163
- private getHit(ray: THREE.Ray): THREE.Intersection | null {
164
- if (!this.colliders || this.colliders.length === 0) {
165
- this.colliders = [this.gameObject];
166
- }
167
- this._raycastOptions.targets = this.colliders;
168
- this._raycastOptions.ray = ray;
169
- const hits = this.context.physics.raycast(this._raycastOptions);
170
- if (hits.length > 0) {
171
- for (const hit of hits) {
172
- if (!GameObject.isActiveInHierarchy(hit.object)) {
173
- continue;
174
- }
175
- return hit;
176
- }
177
- }
178
- return null;
179
- }
180
-
181
- private createRandomMaterial() {
182
- let col: Color;
183
- if (this.context.connection.connectionId)
184
- col = PlayerColor.colorFromHashCode(PlayerColor.hashCode(this.context.connection.connectionId));
185
- else
186
- col = new THREE.Color("hsl(" + (Math.random() * 100).toFixed(0) + ", 80%, 30%)");
187
- // const col = new THREE.Color("hsl(0, 100%, 50%)");
188
-
189
- return new MeshLineMaterial({
190
- color: col,
191
- lineWidth: Mathf.lerp(0.005, 0.01, Math.random()),
192
- });
193
- }
194
- }
@@ -1,218 +0,0 @@
1
- import { Behaviour, GameObject } from "../../engine-components/Component";
2
- import * as THREE from 'three';
3
- import { Color, Material } from "three";
4
- import { MeshLine, MeshLineMaterial } from 'three.meshline';
5
-
6
-
7
-
8
- export declare type LineOptions = {
9
- material?: Material;
10
- options?: LineMaterialOptions;
11
- }
12
-
13
- export declare type LineMaterialOptions = {
14
- map?: THREE.Texture;
15
- useMap?: number;
16
- alphaMap?: THREE.Texture;
17
- color?: Color;
18
- opacity?: number;
19
- lineWidth?: number;
20
- }
21
-
22
- export class LineInstanceHandler {
23
- id: number = 0;
24
- points: Array<any> = [];
25
- line: MeshLine = new MeshLine();
26
- material?: MeshLineMaterial;
27
- mesh?: THREE.Mesh;
28
-
29
- constructor(owner: THREE.Object3D, options?: LineOptions) {
30
- if (options) {
31
- this.material = options.material;
32
- }
33
-
34
- if (!this.material)
35
- this.material = this.defaultLineMaterial;
36
- if (options) {
37
- const opts = options.options;
38
- if (opts) {
39
- Object.assign(this.material, opts);
40
- }
41
- }
42
- this.mesh = new THREE.Mesh(this.line, this.material);
43
- owner.add(this.mesh);
44
- }
45
-
46
- private static wp: THREE.Vector3 = new THREE.Vector3();
47
-
48
- appendPoint(vec: Vec3): Vec3 {
49
- let localPoint = LineInstanceHandler.wp;
50
- localPoint.set(vec.x, vec.y, vec.z);
51
- const parent = this.mesh?.parent;
52
- if (parent) {
53
- localPoint = parent.worldToLocal(localPoint);
54
- vec.x = localPoint.x;
55
- vec.y = localPoint.y;
56
- vec.z = localPoint.z;
57
- }
58
-
59
- this.points.push(vec.x, vec.y, vec.z);
60
- this.line.setPoints(this.points);
61
- return vec;
62
- }
63
-
64
- private defaultLineMaterial = new MeshLineMaterial({ color: 0x999999, lineWidth: 0.01 });
65
- }
66
-
67
- declare type Vec3 = {
68
- x: number;
69
- y: number;
70
- z: number;
71
- }
72
-
73
- declare type StartLineModel = {
74
- id: number;
75
- parentGuid: string;
76
- }
77
-
78
- declare type LineModel = {
79
- parentGuid: string;
80
- guid: number; // it must be named guid
81
- points: Array<Vec3>;
82
- width: number;
83
- color: Vec3;
84
- startIndex: number;
85
- finished: boolean;
86
- }
87
-
88
- export declare type LineHandle = {
89
- id: number;
90
- }
91
-
92
- declare type UpdateLineArgs = {
93
- point: Vec3;
94
- }
95
-
96
-
97
- export class LinesManager extends Behaviour {
98
-
99
- public startLine(parent?: THREE.Object3D, options?: LineOptions): LineHandle {
100
- const id = Math.random() * Number.MAX_SAFE_INTEGER;
101
- return this.internalStartLine(parent, id, true, options);
102
- }
103
-
104
- public updateLine(handle: LineHandle, args: UpdateLineArgs) {
105
- const line = this.inFlight[handle.id];
106
- if (!line) return;
107
- if (args.point) {
108
- args.point = line.appendPoint(args.point);
109
- }
110
- const buf = this.buffer[handle.id];
111
- if (buf) {
112
- if (args.point)
113
- buf.push(args.point.x, args.point.y, args.point.z);
114
- if (buf.length > 5) {
115
- this.sendLineUpdate(line, false, undefined, buf);
116
- buf.length = 0;
117
- }
118
- }
119
- }
120
-
121
- public endLine(handle: LineHandle, send: boolean = true): LineInstanceHandler | undefined {
122
- const line = this.inFlight[handle.id];
123
- if (!line) return undefined;
124
- this.finished.push(line);
125
- delete this.inFlight[handle.id];
126
-
127
- if (send)
128
- this.sendLineUpdate(line, true, 0);
129
-
130
- const buf = this.buffer[handle.id];
131
- if (buf) {
132
- delete this.buffer[handle.id];
133
- const arr = buf;
134
- arr.length = 0;
135
- this.freeBuffer.push(arr);
136
- }
137
-
138
- return line;
139
- }
140
-
141
- public getLine(handle: LineHandle): LineInstanceHandler | undefined {
142
- return this.inFlight[handle.id];
143
- }
144
-
145
- private finished: Array<LineInstanceHandler> = [];
146
- private inFlight: { [key: number]: LineInstanceHandler } = [];
147
- private buffer: { [key: number]: any[] } = {};
148
- private freeBuffer = new Array<Vec3[]>();
149
-
150
- awake(): void {
151
-
152
- this.context.connection.beginListen("line-start", (i: StartLineModel) => {
153
- this.onEnsureLine(i.id, i.parentGuid);
154
- });
155
- this.context.connection.beginListen("line-update", (evt: LineModel) => {
156
- let line = this.onEnsureLine(evt.guid, evt.parentGuid);
157
- if (line && evt.points) {
158
- if (evt.startIndex <= 0) {
159
- line.points = evt.points;
160
- }
161
- else {
162
- if (evt.startIndex >= line.points.length) {
163
- line.points.push(...evt.points);
164
- }
165
- }
166
- line.line.setPoints(line.points);
167
- line.material!.lineWidth = evt.width;
168
- line.material!.color.fromArray(evt.color);
169
- if (evt.finished === true) {
170
- this.endLine({ id: line.id }, false);
171
- }
172
- }
173
- });
174
- }
175
-
176
- private onEnsureLine(lineId: number, parentGuid: string): LineInstanceHandler | null {
177
- if (this.inFlight[lineId]) return this.inFlight[lineId];
178
- let obj = GameObject.findByGuid(parentGuid, this.context.scene);
179
- if (!obj) {
180
- return null;
181
- }
182
- this.internalStartLine(obj as THREE.Object3D, lineId, false);
183
- return this.inFlight[lineId];
184
- }
185
- private internalStartLine(parent: THREE.Object3D | null | undefined, id: number, send: boolean = true, options?: LineOptions): LineHandle {
186
- const line = new LineInstanceHandler(parent ?? this.context.scene, options);
187
- line.id = id;
188
- this.inFlight[id] = line;
189
- if (send)
190
- this.sendLineStart(line);
191
-
192
- let buf;
193
- if (this.freeBuffer.length <= 0) buf = new Array<Vec3>();
194
- else buf = this.freeBuffer.pop();
195
- this.buffer[id] = buf;
196
- return { id: id };
197
- }
198
-
199
- private sendLineStart(instance: LineInstanceHandler) {
200
- const parent = instance.mesh!.parent;
201
- this.context.connection.send("line-start", { id: instance.id, parentGuid: parent ? parent["guid"] : undefined })
202
- }
203
-
204
- private sendLineUpdate(instance: LineInstanceHandler, finished: boolean, startIndex?: number, points?: Array<any>) {
205
- if (instance) {
206
- const model: LineModel = {
207
- parentGuid: instance.mesh!.parent!["guid"],
208
- guid: instance.id,
209
- points: points ? [...points] : instance.points,
210
- width: instance.material!.lineWidth,
211
- color: instance.material!.color.toArray(),
212
- startIndex: startIndex !== undefined ? startIndex : instance.points.length,
213
- finished: finished,
214
- };
215
- this.context.connection.send("line-update", model)
216
- }
217
- }
218
- }