@needle-tools/engine 2.56.2-pre → 2.58.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 (73) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/needle-engine.d.ts +99 -160
  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 +2 -6
  12. package/lib/engine/codegen/register_types.js.map +1 -1
  13. package/lib/engine/engine.d.ts +2 -28
  14. package/lib/engine/engine.js +1 -0
  15. package/lib/engine/engine.js.map +1 -1
  16. package/lib/engine/engine_hot_reload.d.ts +3 -0
  17. package/lib/engine/engine_hot_reload.js +168 -0
  18. package/lib/engine/engine_hot_reload.js.map +1 -0
  19. package/lib/engine/engine_input.js +1 -1
  20. package/lib/engine/engine_scenetools.d.ts +1 -0
  21. package/lib/engine/engine_scenetools.js +8 -6
  22. package/lib/engine/engine_scenetools.js.map +1 -1
  23. package/lib/engine/engine_setup.d.ts +4 -2
  24. package/lib/engine/engine_setup.js +66 -50
  25. package/lib/engine/engine_setup.js.map +1 -1
  26. package/lib/engine/engine_three_utils.js.map +1 -1
  27. package/lib/engine/engine_utils_screenshot.d.ts +5 -0
  28. package/lib/engine/engine_utils_screenshot.js +32 -0
  29. package/lib/engine/engine_utils_screenshot.js.map +1 -0
  30. package/lib/engine-components/Fog.d.ts +20 -0
  31. package/lib/engine-components/Fog.js +61 -0
  32. package/lib/engine-components/Fog.js.map +1 -0
  33. package/lib/engine-components/Skybox.js +3 -1
  34. package/lib/engine-components/Skybox.js.map +1 -1
  35. package/lib/engine-components/WebXR.js +2 -2
  36. package/lib/engine-components/WebXR.js.map +1 -1
  37. package/lib/engine-components/codegen/components.d.ts +1 -0
  38. package/lib/engine-components/codegen/components.js +1 -0
  39. package/lib/engine-components/codegen/components.js.map +1 -1
  40. package/lib/engine-components/ui/EventSystem.d.ts +1 -1
  41. package/lib/engine-components/ui/EventSystem.js +25 -10
  42. package/lib/engine-components/ui/EventSystem.js.map +1 -1
  43. package/lib/engine-components/ui/PointerEvents.d.ts +3 -1
  44. package/lib/engine-components/ui/PointerEvents.js +1 -0
  45. package/lib/engine-components/ui/PointerEvents.js.map +1 -1
  46. package/lib/engine-components/ui/Raycaster.js.map +1 -1
  47. package/lib/tsconfig.tsbuildinfo +1 -1
  48. package/package.json +1 -2
  49. package/src/engine/api.ts +3 -2
  50. package/src/engine/codegen/register_types.js +4 -8
  51. package/src/engine/engine.ts +3 -1
  52. package/src/engine/engine_hot_reload.ts +186 -0
  53. package/src/engine/engine_input.ts +1 -1
  54. package/src/engine/engine_scenetools.ts +9 -6
  55. package/src/engine/engine_setup.ts +66 -52
  56. package/src/engine/engine_three_utils.ts +4 -4
  57. package/src/engine/engine_utils_screenshot.ts +41 -0
  58. package/src/engine-components/Fog.ts +60 -0
  59. package/src/engine-components/Skybox.ts +3 -1
  60. package/src/engine-components/TestRunner.ts +1 -1
  61. package/src/engine-components/WebXR.ts +2 -2
  62. package/src/engine-components/codegen/components.ts +1 -0
  63. package/src/engine-components/ui/EventSystem.ts +30 -15
  64. package/src/engine-components/ui/PointerEvents.ts +7 -4
  65. package/src/engine-components/ui/Raycaster.ts +6 -3
  66. package/lib/engine-components-experimental/annotation/LineDrawer.d.ts +0 -18
  67. package/lib/engine-components-experimental/annotation/LineDrawer.js +0 -175
  68. package/lib/engine-components-experimental/annotation/LineDrawer.js.map +0 -1
  69. package/lib/engine-components-experimental/annotation/LinesManager.d.ts +0 -54
  70. package/lib/engine-components-experimental/annotation/LinesManager.js +0 -155
  71. package/lib/engine-components-experimental/annotation/LinesManager.js.map +0 -1
  72. package/src/engine-components-experimental/annotation/LineDrawer.ts +0 -194
  73. package/src/engine-components-experimental/annotation/LinesManager.ts +0 -218
@@ -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
- }