@shopware-ag/dive 1.19.1-beta.0 → 1.19.1-beta.2

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 (188) hide show
  1. package/build/dive.cjs +362 -199
  2. package/build/dive.cjs.map +1 -1
  3. package/build/dive.js +330 -167
  4. package/build/dive.js.map +1 -1
  5. package/build/dive.mjs +10803 -8714
  6. package/build/dive.mjs.map +1 -1
  7. package/build/src/converter/Converter.d.ts +3 -12
  8. package/build/src/dive.d.ts +1 -0
  9. package/build/src/exporter/Exporter.d.ts +20 -2
  10. package/build/src/loader/Loader.d.ts +0 -4
  11. package/build/src/types/error/index.d.ts +12 -0
  12. package/build/src/types/file/FileTypes.d.ts +17 -0
  13. package/build/src/types/file/index.d.ts +1 -0
  14. package/build/src/types/index.d.ts +5 -8
  15. package/package.json +2 -3
  16. package/build/src/types/ExporterOptions.d.ts +0 -15
  17. package/build/src/types/FileTypes.d.ts +0 -27
  18. package/src/__test__/DIVE.test.ts +0 -274
  19. package/src/animation/AnimationSystem.ts +0 -34
  20. package/src/animation/__test__/AnimationSystem.test.ts +0 -40
  21. package/src/ar/AR.ts +0 -101
  22. package/src/ar/__test__/AR.test.ts +0 -276
  23. package/src/ar/arquicklook/ARQuickLook.ts +0 -53
  24. package/src/ar/arquicklook/__test__/ARQuickLook.test.ts +0 -144
  25. package/src/ar/sceneviewer/SceneViewer.ts +0 -119
  26. package/src/ar/sceneviewer/__test__/SceneViewer.test.ts +0 -190
  27. package/src/ar/webxr/WebXR.ts +0 -177
  28. package/src/ar/webxr/controller/WebXRController.ts +0 -340
  29. package/src/ar/webxr/crosshair/WebXRCrosshair.ts +0 -35
  30. package/src/ar/webxr/origin/WebXROrigin.ts +0 -192
  31. package/src/ar/webxr/overlay/Overlay.ts +0 -50
  32. package/src/ar/webxr/raycaster/WebXRRaycaster.ts +0 -131
  33. package/src/ar/webxr/raycaster/ar/WebXRRaycasterAR.ts +0 -102
  34. package/src/ar/webxr/raycaster/three/WebXRRaycasterTHREE.ts +0 -49
  35. package/src/ar/webxr/touchscreencontrols/WebXRTouchscreenControls.ts +0 -356
  36. package/src/axiscamera/AxisCamera.ts +0 -108
  37. package/src/axiscamera/__test__/AxisCamera.test.ts +0 -150
  38. package/src/camera/PerspectiveCamera.ts +0 -63
  39. package/src/camera/__test__/PerspectiveCamera.test.ts +0 -36
  40. package/src/com/Communication.ts +0 -806
  41. package/src/com/__test__/Communication.test.ts +0 -1030
  42. package/src/com/actions/camera/computeencompassingview.ts +0 -10
  43. package/src/com/actions/camera/getcameratransform.ts +0 -10
  44. package/src/com/actions/camera/movecamera.ts +0 -18
  45. package/src/com/actions/camera/resetcamera.ts +0 -5
  46. package/src/com/actions/camera/setcameralayer.ts +0 -5
  47. package/src/com/actions/camera/setcameratransform.ts +0 -10
  48. package/src/com/actions/camera/zoomcamera.ts +0 -5
  49. package/src/com/actions/index.ts +0 -61
  50. package/src/com/actions/media/generatemedia.ts +0 -18
  51. package/src/com/actions/object/addobject.ts +0 -7
  52. package/src/com/actions/object/deleteobject.ts +0 -7
  53. package/src/com/actions/object/deselectobject.ts +0 -7
  54. package/src/com/actions/object/getallobjects.ts +0 -7
  55. package/src/com/actions/object/getobjects.ts +0 -7
  56. package/src/com/actions/object/model/dropit.ts +0 -5
  57. package/src/com/actions/object/model/modelloaded.ts +0 -5
  58. package/src/com/actions/object/model/placeonfloor.ts +0 -5
  59. package/src/com/actions/object/selectobject.ts +0 -7
  60. package/src/com/actions/object/setparent.ts +0 -10
  61. package/src/com/actions/object/updateobject.ts +0 -7
  62. package/src/com/actions/renderer/startrender.ts +0 -5
  63. package/src/com/actions/scene/exportscene.ts +0 -7
  64. package/src/com/actions/scene/getallscenedata.ts +0 -7
  65. package/src/com/actions/scene/launchar.ts +0 -7
  66. package/src/com/actions/scene/setbackground.ts +0 -5
  67. package/src/com/actions/scene/updatescene.ts +0 -11
  68. package/src/com/actions/toolbox/select/setgizmomode.ts +0 -5
  69. package/src/com/actions/toolbox/transform/setgizmoscalelinked.ts +0 -5
  70. package/src/com/actions/toolbox/transform/setgizmovisible.ts +0 -5
  71. package/src/com/actions/toolbox/usetool.ts +0 -7
  72. package/src/com/types/COMBaseEntity.ts +0 -9
  73. package/src/com/types/COMEntity.ts +0 -7
  74. package/src/com/types/COMEntityType.ts +0 -1
  75. package/src/com/types/COMGeometry.ts +0 -8
  76. package/src/com/types/COMGeometryType.ts +0 -9
  77. package/src/com/types/COMGroup.ts +0 -9
  78. package/src/com/types/COMLight.ts +0 -11
  79. package/src/com/types/COMMaterial.ts +0 -12
  80. package/src/com/types/COMModel.ts +0 -12
  81. package/src/com/types/COMPov.ts +0 -8
  82. package/src/com/types/COMPrimitive.ts +0 -12
  83. package/src/com/types/index.ts +0 -23
  84. package/src/constant/AxisHelperColors.ts +0 -7
  85. package/src/constant/GridColors.ts +0 -2
  86. package/src/constant/VisibilityLayerMask.ts +0 -5
  87. package/src/controls/OrbitControls.ts +0 -211
  88. package/src/controls/__test__/OrbitControls.test.ts +0 -256
  89. package/src/converter/Converter.ts +0 -117
  90. package/src/dive.ts +0 -363
  91. package/src/events/EventExecutor.ts +0 -35
  92. package/src/exporter/Exporter.ts +0 -75
  93. package/src/gizmo/Gizmo.ts +0 -144
  94. package/src/gizmo/handles/AxisHandle.ts +0 -153
  95. package/src/gizmo/handles/RadialHandle.ts +0 -149
  96. package/src/gizmo/handles/ScaleHandle.ts +0 -195
  97. package/src/gizmo/plane/GizmoPlane.ts +0 -84
  98. package/src/gizmo/rotate/RotateGizmo.ts +0 -143
  99. package/src/gizmo/scale/ScaleGizmo.ts +0 -125
  100. package/src/gizmo/translate/TranslateGizmo.ts +0 -114
  101. package/src/grid/Grid.ts +0 -34
  102. package/src/grid/__test__/Grid.test.ts +0 -28
  103. package/src/group/Group.ts +0 -184
  104. package/src/group/__test__/Group.test.ts +0 -212
  105. package/src/helper/applyMixins/__test__/applyMixins.test.ts +0 -30
  106. package/src/helper/applyMixins/applyMixins.ts +0 -70
  107. package/src/helper/findInterface/__test__/findInterface.test.ts +0 -63
  108. package/src/helper/findInterface/findInterface.ts +0 -13
  109. package/src/helper/findSceneRecursive/__test__/findSceneRecursive.test.ts +0 -40
  110. package/src/helper/findSceneRecursive/findSceneRecursive.ts +0 -16
  111. package/src/helper/getObjectDelta/__test__/getObjectDelta.test.ts +0 -186
  112. package/src/helper/getObjectDelta/getObjectDelta.ts +0 -105
  113. package/src/helper/isInterface/__test__/implementsInterface.test.ts +0 -19
  114. package/src/helper/isInterface/implementsInterface.ts +0 -9
  115. package/src/info/Info.ts +0 -178
  116. package/src/info/__test__/Info.test.ts +0 -346
  117. package/src/interface/Draggable.ts +0 -14
  118. package/src/interface/Hoverable.ts +0 -14
  119. package/src/interface/Movable.ts +0 -12
  120. package/src/interface/Rotatable.ts +0 -10
  121. package/src/interface/Scalable.ts +0 -10
  122. package/src/interface/Selectable.ts +0 -11
  123. package/src/io/IO.ts +0 -62
  124. package/src/io/__test__/IO.test.ts +0 -116
  125. package/src/io/gltf/GLTFIO.ts +0 -56
  126. package/src/io/gltf/__test__/GLTFIO.test.ts +0 -108
  127. package/src/light/AmbientLight.ts +0 -39
  128. package/src/light/PointLight.ts +0 -115
  129. package/src/light/SceneLight.ts +0 -63
  130. package/src/light/__test__/AmbientLight.test.ts +0 -29
  131. package/src/light/__test__/PointLight.test.ts +0 -74
  132. package/src/light/__test__/SceneLight.test.ts +0 -47
  133. package/src/loader/Loader.ts +0 -48
  134. package/src/math/__test__/DIVEMath.test.ts +0 -12
  135. package/src/math/ceil/__test__/ceilExp.test.ts +0 -12
  136. package/src/math/ceil/ceilExp.ts +0 -6
  137. package/src/math/degToRad/__test__/degToRad.test.ts +0 -172
  138. package/src/math/degToRad/degToRad.ts +0 -5
  139. package/src/math/floor/__test__/floorExp.test.ts +0 -14
  140. package/src/math/floor/floorExp.ts +0 -6
  141. package/src/math/helper/__test__/shift.test.ts +0 -12
  142. package/src/math/helper/shift.ts +0 -4
  143. package/src/math/index.ts +0 -28
  144. package/src/math/radToDeg/__test__/radToDeg.test.ts +0 -155
  145. package/src/math/radToDeg/radToDeg.ts +0 -5
  146. package/src/math/round/__test__/roundExp.test.ts +0 -14
  147. package/src/math/round/roundExp.ts +0 -10
  148. package/src/math/signedAngleTo/__test__/signedAngleTo.test.ts +0 -20
  149. package/src/math/signedAngleTo/signedAngleTo.ts +0 -20
  150. package/src/math/toFixed/__test__/toFixedExp.test.ts +0 -14
  151. package/src/math/toFixed/toFixedExp.ts +0 -9
  152. package/src/math/truncate/__test__/truncateExp.test.ts +0 -14
  153. package/src/math/truncate/truncateExp.ts +0 -9
  154. package/src/mediacreator/MediaCreator.ts +0 -75
  155. package/src/mediacreator/__test__/MediaCreator.test.ts +0 -155
  156. package/src/model/Model.ts +0 -190
  157. package/src/model/__test__/Model.test.ts +0 -215
  158. package/src/module/Module.ts +0 -45
  159. package/src/module/__test__/Module.test.ts +0 -54
  160. package/src/node/Node.ts +0 -100
  161. package/src/node/__test__/Node.test.ts +0 -120
  162. package/src/primitive/Primitive.ts +0 -299
  163. package/src/primitive/__test__/Primitive.test.ts +0 -281
  164. package/src/primitive/floor/Floor.ts +0 -45
  165. package/src/primitive/floor/__test__/Floor.test.ts +0 -24
  166. package/src/renderer/Renderer.ts +0 -215
  167. package/src/renderer/__test__/Renderer.test.ts +0 -228
  168. package/src/scene/Scene.ts +0 -106
  169. package/src/scene/__test__/Scene.test.ts +0 -124
  170. package/src/scene/root/Root.ts +0 -426
  171. package/src/scene/root/__test__/Root.test.ts +0 -903
  172. package/src/scene/xrroot/XRRoot.ts +0 -56
  173. package/src/scene/xrroot/xrlightroot/XRLightRoot.ts +0 -80
  174. package/src/toolbox/BaseTool.ts +0 -319
  175. package/src/toolbox/Toolbox.ts +0 -129
  176. package/src/toolbox/__test__/BaseTool.test.ts +0 -627
  177. package/src/toolbox/__test__/Toolbox.test.ts +0 -143
  178. package/src/toolbox/select/SelectTool.ts +0 -92
  179. package/src/toolbox/select/__test__/SelectTool.test.ts +0 -237
  180. package/src/toolbox/transform/TransformTool.ts +0 -166
  181. package/src/toolbox/transform/__test__/TransformTool.test.ts +0 -147
  182. package/src/types/ExporterOptions.ts +0 -14
  183. package/src/types/FileTypes.ts +0 -37
  184. package/src/types/SceneData.ts +0 -26
  185. package/src/types/SceneObjects.ts +0 -14
  186. package/src/types/SceneType.ts +0 -14
  187. package/src/types/index.ts +0 -31
  188. package/src/types/info/index.ts +0 -76
@@ -1,45 +0,0 @@
1
- export class DIVEModule<T> {
2
- constructor(
3
- private _path: string,
4
- private _ctor: string,
5
- ) {}
6
-
7
- private _promise: Promise<T> | null = null;
8
- private _instance: T | null = null;
9
-
10
- public async get(): Promise<T> {
11
- // if we already have an instance, return it
12
- if (this._instance) {
13
- return Promise.resolve(this._instance);
14
- }
15
-
16
- // if we already have a loading process ongoing, return the already created promise
17
- if (this._promise) {
18
- return this._promise;
19
- }
20
-
21
- // if we don't have a promise yet, it means that we are not already loading the module
22
- this._promise = (async () => {
23
- const module = await import(this._path);
24
-
25
- const ModuleConstructor = module[this._ctor];
26
-
27
- if (!ModuleConstructor) {
28
- throw new Error(
29
- `DIVE: Module class ${this._ctor} not found in ${this._path}`,
30
- );
31
- }
32
-
33
- if (typeof ModuleConstructor !== 'function') {
34
- throw new Error(
35
- `DIVE: Module at ${this._path} does not export a valid constructor (${this._ctor} wanted)`,
36
- );
37
- }
38
-
39
- this._instance = new ModuleConstructor() as T;
40
- return this._instance;
41
- })();
42
-
43
- return this._promise;
44
- }
45
- }
@@ -1,54 +0,0 @@
1
- import { DIVEModule } from '../Module';
2
-
3
- jest.mock('/mock/path', () => mockModule, { virtual: true });
4
-
5
- const mockModule: Record<string, any> = {};
6
-
7
- describe('dive/module/DIVEModule', () => {
8
- beforeEach(() => {
9
- // Reset the mock module before each test
10
- Object.keys(mockModule).forEach((key) => delete mockModule[key]);
11
- });
12
-
13
- it('should return the same instance on multiple calls to get()', async () => {
14
- mockModule.TestClass = class TestClass {};
15
- const diveModule = new DIVEModule('/mock/path', 'TestClass');
16
-
17
- const instance1 = await diveModule.get();
18
- const instance2 = await diveModule.get();
19
-
20
- expect(instance1).toBe(instance2);
21
- });
22
-
23
- it('should throw an error if the constructor is not found', async () => {
24
- const diveModule = new DIVEModule('/mock/path', 'NonExistentClass');
25
-
26
- await expect(diveModule.get()).rejects.toThrow(
27
- 'DIVE: Module class NonExistentClass not found in /mock/path',
28
- );
29
- });
30
-
31
- it('should throw an error if the constructor is not a function', async () => {
32
- mockModule.InvalidClass = {};
33
- const diveModule = new DIVEModule('/mock/path', 'InvalidClass');
34
-
35
- await expect(diveModule.get()).rejects.toThrow(
36
- 'DIVE: Module at /mock/path does not export a valid constructor (InvalidClass wanted)',
37
- );
38
- });
39
-
40
- it('should only load the module once even with concurrent calls to get()', async () => {
41
- mockModule.TestClass = class TestClass {};
42
- const diveModule = new DIVEModule('/mock/path', 'TestClass');
43
-
44
- const [
45
- instance1,
46
- instance2,
47
- ] = await Promise.all([
48
- diveModule.get(),
49
- diveModule.get(),
50
- ]);
51
-
52
- expect(instance1).toBe(instance2);
53
- });
54
- });
package/src/node/Node.ts DELETED
@@ -1,100 +0,0 @@
1
- import { Box3, Object3D, Vector3, type Vector3Like } from 'three';
2
- import { PRODUCT_LAYER_MASK } from '../constant/VisibilityLayerMask';
3
- import { DIVECommunication } from '../com/Communication';
4
-
5
- import { DIVEMovable } from '../interface/Movable';
6
- import { DIVESelectable } from '../interface/Selectable';
7
- import { type TransformControls } from 'three/examples/jsm/controls/TransformControls';
8
- import { type DIVEGroup } from '../group/Group';
9
- import { applyMixins } from '../helper/applyMixins/applyMixins';
10
-
11
- export class DIVENode extends applyMixins(Object3D, [
12
- DIVESelectable,
13
- DIVEMovable,
14
- ]) {
15
- readonly isDIVENode: true = true;
16
-
17
- public gizmo: TransformControls | null = null;
18
-
19
- protected _positionWorldBuffer: Vector3;
20
- protected _boundingBox: Box3;
21
-
22
- constructor() {
23
- super();
24
-
25
- this.layers.mask = PRODUCT_LAYER_MASK;
26
-
27
- this._positionWorldBuffer = new Vector3();
28
- this._boundingBox = new Box3();
29
- }
30
-
31
- public SetPosition(position: Vector3Like): void {
32
- // if there is no parent, the object will be attached later and keep it's world position
33
- if (!this.parent) {
34
- this.position.set(position.x, position.y, position.z);
35
- return;
36
- }
37
-
38
- // if we have a parent, we have to calculate the position in the parent's coordinate system to keep the world position
39
- const newPosition = new Vector3(position.x, position.y, position.z);
40
- this.position.copy(this.parent.worldToLocal(newPosition));
41
-
42
- if ('isDIVEGroup' in this.parent) {
43
- (this.parent as unknown as DIVEGroup).UpdateLineTo(this);
44
- }
45
- }
46
-
47
- public SetRotation(rotation: Vector3Like): void {
48
- this.rotation.set(rotation.x, rotation.y, rotation.z);
49
- }
50
-
51
- public SetScale(scale: Vector3Like): void {
52
- this.scale.set(scale.x, scale.y, scale.z);
53
- }
54
-
55
- public SetVisibility(visible: boolean): void {
56
- this.visible = visible;
57
- }
58
-
59
- public SetToWorldOrigin(): void {
60
- this.position.set(0, 0, 0);
61
- DIVECommunication.get(this.userData.id)?.PerformAction(
62
- 'UPDATE_OBJECT',
63
- {
64
- id: this.userData.id,
65
- position: this.getWorldPosition(this._positionWorldBuffer),
66
- rotation: this.rotation,
67
- scale: this.scale,
68
- },
69
- );
70
- }
71
-
72
- /**
73
- * Can be called when the object is moved from a foreign object (gizmo, parent, etc.) to update the object's position.
74
- */
75
- public onMove(): void {
76
- DIVECommunication.get(this.userData.id)?.PerformAction(
77
- 'UPDATE_OBJECT',
78
- {
79
- id: this.userData.id,
80
- position: this.getWorldPosition(this._positionWorldBuffer),
81
- rotation: this.rotation,
82
- scale: this.scale,
83
- },
84
- );
85
- }
86
-
87
- public onSelect(): void {
88
- DIVECommunication.get(this.userData.id)?.PerformAction(
89
- 'SELECT_OBJECT',
90
- { id: this.userData.id },
91
- );
92
- }
93
-
94
- public onDeselect(): void {
95
- DIVECommunication.get(this.userData.id)?.PerformAction(
96
- 'DESELECT_OBJECT',
97
- { id: this.userData.id },
98
- );
99
- }
100
- }
@@ -1,120 +0,0 @@
1
- import { DIVENode } from '../Node';
2
- import { DIVECommunication } from '../../com/Communication';
3
- import { Vector3, Box3, Mesh, Color, Euler } from 'three';
4
- import { type DIVEGroup } from '../../group/Group';
5
-
6
- jest.mock('../../com/Communication.ts', () => {
7
- return {
8
- DIVECommunication: {
9
- get: jest.fn(() => {
10
- return {
11
- PerformAction: jest.fn(),
12
- };
13
- }),
14
- },
15
- };
16
- });
17
-
18
- jest.spyOn(DIVECommunication, 'get').mockReturnValue({
19
- PerformAction: jest.fn(),
20
- } as unknown as DIVECommunication);
21
-
22
- let node: DIVENode;
23
-
24
- describe('dive/node/DIVENode', () => {
25
- beforeEach(() => {
26
- node = new DIVENode();
27
- });
28
-
29
- afterEach(() => {
30
- jest.clearAllMocks();
31
- });
32
-
33
- it('should instantiate', () => {
34
- expect(node).toBeDefined();
35
- });
36
-
37
- it('should set position', () => {
38
- const spySet = jest.spyOn(node.position, 'set');
39
- const spyCopy = jest.spyOn(node.position, 'copy');
40
-
41
- // without a parent, the node should only set it's local position
42
- node.parent = null;
43
- expect(() => node.SetPosition({ x: 1, y: 2, z: 3 })).not.toThrow();
44
- expect(spySet).toHaveBeenCalledWith(1, 2, 3);
45
- expect(spyCopy).not.toHaveBeenCalled();
46
-
47
- // with a parent, the node should set it's position relative to the parent
48
- spySet.mockClear();
49
- spyCopy.mockClear();
50
- node.parent = {
51
- worldToLocal: jest.fn(() => new Vector3(4, 5, 6)),
52
- isDIVEGroup: true,
53
- UpdateLineTo: jest.fn(),
54
- } as unknown as DIVENode;
55
- const spyUpdateLineTo = jest.spyOn(
56
- node.parent as DIVEGroup,
57
- 'UpdateLineTo',
58
- );
59
- expect(() => node.SetPosition({ x: 4, y: 5, z: 6 })).not.toThrow();
60
- expect(spySet).not.toHaveBeenCalled();
61
- expect(spyCopy).toHaveBeenCalledWith(
62
- expect.objectContaining({ x: 4, y: 5, z: 6 }),
63
- );
64
- });
65
-
66
- it('should set rotation', () => {
67
- expect(() => node.SetRotation({ x: 0, y: 0, z: 0 })).not.toThrow();
68
- });
69
-
70
- it('should set scale', () => {
71
- expect(() => node.SetScale({ x: 1, y: 1, z: 1 })).not.toThrow();
72
- });
73
-
74
- it('should set visibility', () => {
75
- expect(() => node.SetVisibility(true)).not.toThrow();
76
- });
77
-
78
- it('should set to world origin', () => {
79
- node.userData.id = 'something';
80
-
81
- expect(() => node.SetToWorldOrigin()).not.toThrow();
82
- expect(node.position.x).toBe(0);
83
- expect(node.position.y).toBe(0);
84
- expect(node.position.z).toBe(0);
85
-
86
- jest.spyOn(DIVECommunication, 'get').mockReturnValueOnce(undefined);
87
- expect(() => node.SetToWorldOrigin()).not.toThrow();
88
- });
89
-
90
- it('should onMove', () => {
91
- node.userData.id = 'something';
92
- node.parent = {
93
- isDIVEGroup: true,
94
- UpdateLineTo: jest.fn(),
95
- } as unknown as DIVENode;
96
-
97
- expect(() => node.onMove()).not.toThrow();
98
-
99
- jest.spyOn(DIVECommunication, 'get').mockReturnValueOnce(undefined);
100
- expect(() => node.onMove()).not.toThrow();
101
- });
102
-
103
- it('should onSelect', () => {
104
- node.userData.id = 'something';
105
-
106
- expect(() => node.onSelect()).not.toThrow();
107
-
108
- jest.spyOn(DIVECommunication, 'get').mockReturnValueOnce(undefined);
109
- expect(() => node.onSelect()).not.toThrow();
110
- });
111
-
112
- it('should onDeselect', () => {
113
- node.userData.id = 'something';
114
-
115
- expect(() => node.onDeselect()).not.toThrow();
116
-
117
- jest.spyOn(DIVECommunication, 'get').mockReturnValueOnce(undefined);
118
- expect(() => node.onDeselect()).not.toThrow();
119
- });
120
- });
@@ -1,299 +0,0 @@
1
- import {
2
- BoxGeometry,
3
- BufferAttribute,
4
- BufferGeometry,
5
- Color,
6
- ConeGeometry,
7
- CylinderGeometry,
8
- Mesh,
9
- MeshStandardMaterial,
10
- Raycaster,
11
- SphereGeometry,
12
- Vector3,
13
- } from 'three';
14
- import { PRODUCT_LAYER_MASK } from '../constant/VisibilityLayerMask';
15
- import { findSceneRecursive } from '../helper/findSceneRecursive/findSceneRecursive';
16
- import { DIVENode } from '../node/Node';
17
- import { type COMGeometry, type COMMaterial } from '../com/types';
18
- import { DIVECommunication } from '../com/Communication';
19
-
20
- /**
21
- * A basic model class.
22
- *
23
- * It does calculate it's own bounding box which is used for positioning on the floor.
24
- *
25
- * Can be moved and selected.
26
- *
27
- * @module
28
- */
29
- export class DIVEPrimitive extends DIVENode {
30
- readonly isDIVEPrimitive: true = true;
31
-
32
- private _mesh: Mesh;
33
-
34
- constructor() {
35
- super();
36
-
37
- this._mesh = new Mesh();
38
- this._mesh.layers.mask = PRODUCT_LAYER_MASK;
39
- this._mesh.castShadow = true;
40
- this._mesh.receiveShadow = true;
41
- this._mesh.material = new MeshStandardMaterial();
42
- this.add(this._mesh);
43
- }
44
-
45
- public SetGeometry(geometry: COMGeometry): void {
46
- const geo = this.assembleGeometry(geometry);
47
- if (!geo) return;
48
-
49
- this._mesh.geometry = geo;
50
- this._boundingBox.setFromObject(this._mesh);
51
- }
52
-
53
- public SetMaterial(material: Partial<COMMaterial>): void {
54
- const primitiveMaterial = this._mesh.material as MeshStandardMaterial;
55
-
56
- if (material.vertexColors !== undefined) {
57
- primitiveMaterial.vertexColors = material.vertexColors;
58
- }
59
-
60
- // apply color if supplied
61
- if (material.color !== undefined) {
62
- primitiveMaterial.color = new Color(material.color);
63
- }
64
-
65
- // apply albedo map if supplied
66
- if (material.map !== undefined) {
67
- primitiveMaterial.map = material.map;
68
- }
69
-
70
- // apply normal map
71
- if (material.normalMap !== undefined) {
72
- primitiveMaterial.normalMap = material.normalMap;
73
- }
74
-
75
- // set roughness value
76
- // if supplied, apply roughness map
77
- // if we applied a roughness map, set roughness to 1.0
78
- if (material.roughness !== undefined) {
79
- primitiveMaterial.roughness = material.roughness;
80
- }
81
-
82
- if (material.roughnessMap !== undefined) {
83
- primitiveMaterial.roughnessMap = material.roughnessMap;
84
-
85
- if (primitiveMaterial.roughnessMap) {
86
- primitiveMaterial.roughness = 1.0;
87
- }
88
- }
89
-
90
- // set metalness value
91
- // if supplied, apply metalness map
92
- // if we applied a metalness map, set metalness to 1.0
93
- if (material.metalness !== undefined) {
94
- primitiveMaterial.metalness = material.metalness;
95
- }
96
-
97
- if (material.metalnessMap !== undefined) {
98
- primitiveMaterial.metalnessMap = material.metalnessMap;
99
-
100
- if (primitiveMaterial.metalnessMap) {
101
- primitiveMaterial.metalness = 0.0;
102
- }
103
- }
104
-
105
- // if the mesh is already set, update the material
106
- if (this._mesh) this._mesh.material = primitiveMaterial;
107
- }
108
-
109
- public PlaceOnFloor(): void {
110
- // calculate and temporary save world position
111
- const worldPos = this.getWorldPosition(this._positionWorldBuffer);
112
- const oldWorldPos = worldPos.clone();
113
-
114
- // compute the bounding box
115
- this._mesh?.geometry?.computeBoundingBox();
116
- const meshBB = this._mesh?.geometry?.boundingBox;
117
-
118
- // subtract the bounding box min y axis value from the world position y value
119
- if (!meshBB || !this._mesh) return;
120
- worldPos.y = worldPos.y - this._mesh.localToWorld(meshBB.min.clone()).y;
121
-
122
- // skip any action when the position did not change
123
- if (worldPos.y === oldWorldPos.y) return;
124
-
125
- DIVECommunication.get(this.userData.id)?.PerformAction(
126
- 'UPDATE_OBJECT',
127
- {
128
- id: this.userData.id,
129
- position: worldPos,
130
- rotation: this.rotation,
131
- scale: this.scale,
132
- },
133
- );
134
- }
135
-
136
- public DropIt(): void {
137
- if (!this.parent) {
138
- console.warn(
139
- 'DIVEPrimitive: DropIt() called on a model that is not in the scene.',
140
- this,
141
- );
142
- return;
143
- }
144
-
145
- // calculate the bottom center of the bounding box
146
- const bottomY = this._boundingBox.min.y * this.scale.y;
147
- const bbBottomCenter = this.localToWorld(
148
- this._boundingBox.getCenter(new Vector3()).multiply(this.scale),
149
- );
150
- bbBottomCenter.y = bottomY + this.position.y;
151
-
152
- // set up raycaster and raycast all scene objects (product layer)
153
- const raycaster = new Raycaster(bbBottomCenter, new Vector3(0, -1, 0));
154
- raycaster.layers.mask = PRODUCT_LAYER_MASK;
155
- const intersections = raycaster.intersectObjects(
156
- findSceneRecursive(this).Root.children,
157
- true,
158
- );
159
-
160
- // if we hit something, move the model to the top on the hit object's bounding box
161
- if (intersections.length > 0) {
162
- const mesh = intersections[0].object as Mesh;
163
- mesh.geometry.computeBoundingBox();
164
- const meshBB = mesh.geometry.boundingBox!;
165
- const worldPos = mesh.localToWorld(meshBB.max.clone());
166
-
167
- const oldPos = this.position.clone();
168
- const newPos = this.position
169
- .clone()
170
- .setY(worldPos.y)
171
- .sub(new Vector3(0, bottomY, 0));
172
- this.position.copy(newPos);
173
-
174
- // if the position changed, update the object in communication
175
- if (this.position.y === oldPos.y) return;
176
-
177
- this.onMove();
178
- }
179
- }
180
-
181
- private assembleGeometry(geometry: COMGeometry): BufferGeometry | null {
182
- // reset material to smooth shading
183
- (this._mesh.material as MeshStandardMaterial).flatShading = false;
184
-
185
- switch (geometry.name.toLowerCase()) {
186
- case 'cylinder':
187
- return this.createCylinderGeometry(geometry);
188
- case 'sphere':
189
- return this.createSphereGeometry(geometry);
190
- case 'pyramid':
191
- // set material to flat shading for pyramid
192
- (this._mesh.material as MeshStandardMaterial).flatShading =
193
- true;
194
- return this.createPyramidGeometry(geometry);
195
- case 'cube':
196
- case 'box':
197
- return this.createBoxGeometry(geometry);
198
- case 'cone':
199
- return this.createConeGeometry(geometry);
200
- case 'wall':
201
- return this.createWallGeometry(geometry);
202
- case 'plane':
203
- return this.createPlaneGeometry(geometry);
204
- default: {
205
- console.warn(
206
- 'DIVEPrimitive.assembleGeometry: Invalid geometry type:',
207
- geometry.name.toLowerCase(),
208
- );
209
- return null;
210
- }
211
- }
212
- }
213
-
214
- private createCylinderGeometry(geometry: COMGeometry): BufferGeometry {
215
- const geo = new CylinderGeometry(
216
- geometry.width / 2,
217
- geometry.width / 2,
218
- geometry.height,
219
- 64,
220
- );
221
- geo.translate(0, geometry.height / 2, 0);
222
- return geo;
223
- }
224
-
225
- private createSphereGeometry(geometry: COMGeometry): BufferGeometry {
226
- const geo = new SphereGeometry(geometry.width / 2, 256, 256);
227
- return geo;
228
- }
229
-
230
- private createPyramidGeometry(geometry: COMGeometry): BufferGeometry {
231
- // prettier-multiline-arrays-next-line-pattern: 3
232
- const vertices = new Float32Array([
233
- -geometry.width / 2, 0, -geometry.depth / 2, // 0
234
- geometry.width / 2, 0, -geometry.depth / 2, // 1
235
- geometry.width / 2, 0, geometry.depth / 2, // 2
236
- -geometry.width / 2, 0, geometry.depth / 2, // 3
237
- 0, geometry.height, 0,
238
- ]);
239
-
240
- // prettier-multiline-arrays-next-line-pattern: 3
241
- const indices = new Uint16Array([
242
- 0, 1, 2,
243
- 0, 2, 3,
244
- 0, 4, 1,
245
- 1, 4, 2,
246
- 2, 4, 3,
247
- 3, 4, 0,
248
- ]);
249
-
250
- const geometryBuffer = new BufferGeometry();
251
- geometryBuffer.setAttribute(
252
- 'position',
253
- new BufferAttribute(vertices, 3),
254
- );
255
- geometryBuffer.setIndex(new BufferAttribute(indices, 1));
256
- geometryBuffer.computeVertexNormals();
257
-
258
- geometryBuffer.computeBoundingBox();
259
- geometryBuffer.computeBoundingSphere();
260
- return geometryBuffer;
261
- }
262
-
263
- private createBoxGeometry(geometry: COMGeometry): BufferGeometry {
264
- const geo = new BoxGeometry(
265
- geometry.width,
266
- geometry.height,
267
- geometry.depth,
268
- );
269
- geo.translate(0, geometry.height / 2, 0);
270
- return geo;
271
- }
272
-
273
- private createConeGeometry(geometry: COMGeometry): BufferGeometry {
274
- const geo = new ConeGeometry(geometry.width / 2, geometry.height, 256);
275
- geo.translate(0, geometry.height / 2, 0);
276
- return geo;
277
- }
278
-
279
- private createWallGeometry(geometry: COMGeometry): BufferGeometry {
280
- const geo = new BoxGeometry(
281
- geometry.width,
282
- geometry.height,
283
- geometry.depth || 0.05,
284
- 16,
285
- );
286
- geo.translate(0, geometry.height / 2, 0);
287
- return geo;
288
- }
289
-
290
- private createPlaneGeometry(geometry: COMGeometry): BufferGeometry {
291
- const geo = new BoxGeometry(
292
- geometry.width,
293
- geometry.height,
294
- geometry.depth,
295
- );
296
- geo.translate(0, geometry.height / 2, 0);
297
- return geo;
298
- }
299
- }