@shopware-ag/dive 1.8.0 → 1.9.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shopware-ag/dive",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "Shopware Spatial Framework",
5
5
  "type": "module",
6
6
  "main": "./build/dive.cjs",
package/src/com/index.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  import DIVECommunication from './Communication.ts';
2
2
  export type { Actions } from './actions/index.ts';
3
- export type { COMLight, COMModel, COMPov, COMEntity } from './types.ts';
4
3
  export default DIVECommunication;
package/src/com/types.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { Vector3Like } from "three";
1
+ import { type Texture, type Vector3Like } from "three";
2
2
 
3
3
  type COMBaseEntity = {
4
4
  id: string;
5
5
  name: string;
6
- entityType: 'pov' | 'light' | 'model';
6
+ entityType: 'pov' | 'light' | 'model' | 'primitive';
7
7
  visible: boolean;
8
8
  }
9
9
  export type COMPov = COMBaseEntity & {
@@ -28,4 +28,27 @@ export type COMModel = COMBaseEntity & {
28
28
  loaded: boolean;
29
29
  };
30
30
 
31
- export type COMEntity = COMPov | COMLight | COMModel;
31
+ export type COMGeometry = {
32
+ name: string
33
+ width: number;
34
+ height: number;
35
+ depth: number;
36
+ }
37
+
38
+ export type COMMaterial = {
39
+ color: string | number;
40
+ roughness: number;
41
+ roughnessMap: Texture | null;
42
+ metalness: number;
43
+ metalnessMap: Texture | null;
44
+ }
45
+
46
+ export type COMPrimitive = COMBaseEntity & {
47
+ position: Vector3Like;
48
+ rotation: Vector3Like;
49
+ scale: Vector3Like;
50
+ geometry: COMGeometry;
51
+ material: COMMaterial;
52
+ };
53
+
54
+ export type COMEntity = COMPov | COMLight | COMModel | COMPrimitive;
@@ -2,7 +2,7 @@ import { DIVEMediaCreator } from '../MediaCreator';
2
2
  import { DIVERenderer } from '../../renderer/Renderer';
3
3
  import DIVEScene from '../../scene/Scene';
4
4
  import DIVEPerspectiveCamera, { DIVEPerspectiveCameraDefaultSettings } from '../../camera/PerspectiveCamera';
5
- import { COMPov } from '../../com';
5
+ import { type COMPov } from '../../com/types';
6
6
  import DIVEOrbitControls from '../../controls/OrbitControls';
7
7
  import { DIVEAnimationSystem } from '../../animation/AnimationSystem';
8
8
 
@@ -0,0 +1,210 @@
1
+ import { Box3, BoxGeometry, BufferGeometry, Color, CylinderGeometry, Float32BufferAttribute, Mesh, MeshStandardMaterial, Object3D, Raycaster, SphereGeometry, Uint32BufferAttribute, Vector3, Vector3Like } from 'three';
2
+ import DIVECommunication from '../com/Communication';
3
+ import { PRODUCT_LAYER_MASK } from '../constant/VisibilityLayerMask';
4
+ import { findSceneRecursive } from '../helper/findSceneRecursive/findSceneRecursive';
5
+ import { type DIVESelectable } from '../interface/Selectable';
6
+ import { type DIVEMoveable } from '../interface/Moveable';
7
+ import { type TransformControls } from 'three/examples/jsm/controls/TransformControls';
8
+ import { type COMGeometry, type COMMaterial } from '../com/types';
9
+
10
+ /**
11
+ * A basic model class.
12
+ *
13
+ * It does calculate it's own bounding box which is used for positioning on the floor.
14
+ *
15
+ * Can be moved and selected.
16
+ *
17
+ * @module
18
+ */
19
+
20
+ export class DIVEPrimitive extends Object3D implements DIVESelectable, DIVEMoveable {
21
+ public isSelectable: true = true;
22
+ public isMoveable: true = true;
23
+
24
+ public gizmo: TransformControls | null = null;
25
+
26
+ private _mesh: Mesh;
27
+ private _boundingBox: Box3;
28
+
29
+ constructor() {
30
+ super();
31
+
32
+ this.layers.mask = PRODUCT_LAYER_MASK;
33
+
34
+ this._mesh = new Mesh();
35
+ this._mesh.layers.mask = PRODUCT_LAYER_MASK;
36
+ this._mesh.castShadow = true;
37
+ this._mesh.receiveShadow = true;
38
+ this.add(this._mesh);
39
+
40
+ this._boundingBox = new Box3();
41
+ }
42
+
43
+ public SetGeometry(geometry: COMGeometry): void {
44
+ this.clear();
45
+
46
+ this._mesh.geometry = this.assembleGeometry(geometry);
47
+ this._boundingBox.setFromObject(this._mesh);
48
+ }
49
+
50
+ public SetPosition(position: Vector3Like): void {
51
+ this.position.set(position.x, position.y, position.z);
52
+ }
53
+
54
+ public SetRotation(rotation: Vector3Like): void {
55
+ this.rotation.setFromVector3(new Vector3(rotation.x, rotation.y, rotation.z));
56
+ }
57
+
58
+ public SetScale(scale: Vector3Like): void {
59
+ this.scale.set(scale.x, scale.y, scale.z);
60
+ }
61
+
62
+ public SetVisibility(visible: boolean): void {
63
+ this.traverse((child) => {
64
+ child.visible = visible;
65
+ });
66
+ }
67
+
68
+ public SetMaterial(material: COMMaterial): void {
69
+ const primitiveMaterial = this._mesh.material as MeshStandardMaterial;
70
+
71
+ primitiveMaterial.color = new Color(material.color);
72
+
73
+ // if there is a roughness map, use it, otherwise use the roughness value
74
+ if (material.roughnessMap) {
75
+ primitiveMaterial.roughnessMap = material.roughnessMap;
76
+ primitiveMaterial.roughness = 1.0;
77
+ } else {
78
+ primitiveMaterial.roughness = material.roughness;
79
+ }
80
+
81
+ // if there is a metalness map, use it, otherwise use the metalness value
82
+ if (material.metalnessMap) {
83
+ primitiveMaterial.metalnessMap = material.metalnessMap;
84
+ primitiveMaterial.metalness = 0.0;
85
+ } else {
86
+ primitiveMaterial.metalness = material.metalness;
87
+ }
88
+ }
89
+
90
+ public SetToWorldOrigin(): void {
91
+ this.position.set(0, 0, 0);
92
+ DIVECommunication.get(this.userData.id)?.PerformAction('UPDATE_OBJECT', { id: this.userData.id, position: this.position, rotation: this.rotation, scale: this.scale });
93
+ }
94
+
95
+ public PlaceOnFloor(): void {
96
+ this.position.y = -this._boundingBox.min.y * this.scale.y;
97
+ DIVECommunication.get(this.userData.id)?.PerformAction('UPDATE_OBJECT', { id: this.userData.id, position: this.position, rotation: this.rotation, scale: this.scale });
98
+ }
99
+
100
+ public DropIt(): void {
101
+ if (!this.parent) {
102
+ console.warn('DIVEModel: DropIt() called on a model that is not in the scene.', this);
103
+ return;
104
+ }
105
+
106
+ // calculate the bottom center of the bounding box
107
+ const bottomY = this._boundingBox.min.y * this.scale.y;
108
+ const bbBottomCenter = this.localToWorld(this._boundingBox.getCenter(new Vector3()).multiply(this.scale));
109
+ bbBottomCenter.y = bottomY + this.position.y;
110
+
111
+ // set up raycaster and raycast all scene objects (product layer)
112
+ const raycaster = new Raycaster(bbBottomCenter, new Vector3(0, -1, 0));
113
+ raycaster.layers.mask = PRODUCT_LAYER_MASK;
114
+ const intersections = raycaster.intersectObjects(findSceneRecursive(this).Root.children, true);
115
+
116
+ // if we hit something, move the model to the top on the hit object's bounding box
117
+ if (intersections.length > 0) {
118
+ const mesh = intersections[0].object as Mesh;
119
+ mesh.geometry.computeBoundingBox();
120
+ const meshBB = mesh.geometry.boundingBox!;
121
+ const worldPos = mesh.localToWorld(meshBB.max.clone());
122
+
123
+ const oldPos = this.position.clone();
124
+ const newPos = this.position.clone().setY(worldPos.y).sub(new Vector3(0, bottomY, 0));
125
+ this.position.copy(newPos);
126
+
127
+ // if the position changed, update the object in communication
128
+ if (this.position.y === oldPos.y) return;
129
+ DIVECommunication.get(this.userData.id)?.PerformAction('UPDATE_OBJECT', { id: this.userData.id, position: this.position, rotation: this.rotation, scale: this.scale });
130
+ }
131
+ }
132
+
133
+ public onMove(): void {
134
+ DIVECommunication.get(this.userData.id)?.PerformAction('UPDATE_OBJECT', { id: this.userData.id, position: this.position, rotation: this.rotation, scale: this.scale });
135
+ }
136
+
137
+ public onSelect(): void {
138
+ DIVECommunication.get(this.userData.id)?.PerformAction('SELECT_OBJECT', { id: this.userData.id });
139
+ }
140
+
141
+ public onDeselect(): void {
142
+ DIVECommunication.get(this.userData.id)?.PerformAction('DESELECT_OBJECT', { id: this.userData.id });
143
+ }
144
+
145
+ private assembleGeometry(geometry: COMGeometry): BufferGeometry {
146
+ switch (geometry.name) {
147
+ case 'cylinder':
148
+ return this.createCylinderGeometry(geometry);
149
+ case 'sphere':
150
+ return this.createSphereGeometry(geometry);
151
+ case 'pyramid':
152
+ return this.createPyramidGeometry(geometry);
153
+ case 'box':
154
+ return this.createBoxGeometry(geometry);
155
+ case 'cone':
156
+ return this.createConeGeometry(geometry);
157
+ case 'wall':
158
+ return this.createWallGeometry(geometry);
159
+ case 'plane':
160
+ return this.createPlaneGeometry(geometry);
161
+ default:
162
+ return new BufferGeometry();
163
+ }
164
+ }
165
+
166
+ private createCylinderGeometry(geometry: COMGeometry): BufferGeometry {
167
+ return new CylinderGeometry(geometry.width * 2, geometry.width * 2, geometry.height, 64);
168
+ }
169
+
170
+ private createSphereGeometry(geometry: COMGeometry): BufferGeometry {
171
+ return new SphereGeometry(geometry.width * 2, 64);
172
+ }
173
+
174
+ private createPyramidGeometry(geometry: COMGeometry): BufferGeometry {
175
+ const geo = new BufferGeometry();
176
+ const { width, height, depth } = geometry;
177
+ geo.setAttribute('position', new Float32BufferAttribute([
178
+ width / 2, 0, depth / 2, // right back
179
+ width / 2, 0, -depth / 2, // right front
180
+ -width / 2, 0, -depth / 2, // left front
181
+ -width / 2, 0, depth / 2, // left back
182
+ 0, height, 0, // top
183
+ ], 3));
184
+ geo.setIndex(new Uint32BufferAttribute([
185
+ 1, 0, 4,
186
+ 2, 1, 4,
187
+ 3, 2, 4,
188
+ 3, 0, 4,
189
+ 0, 1, 2,
190
+ 0, 2, 3,
191
+ ], 1));
192
+ return geo;
193
+ }
194
+
195
+ private createBoxGeometry(geometry: COMGeometry): BufferGeometry {
196
+ return new BoxGeometry(geometry.width, geometry.height, geometry.depth);
197
+ }
198
+
199
+ private createConeGeometry(geometry: COMGeometry): BufferGeometry {
200
+ return new CylinderGeometry(0, geometry.width * 2, geometry.height, 64);
201
+ }
202
+
203
+ private createWallGeometry(geometry: COMGeometry): BufferGeometry {
204
+ return new BoxGeometry(geometry.width, geometry.height, geometry.depth, 16);
205
+ }
206
+
207
+ private createPlaneGeometry(geometry: COMGeometry): BufferGeometry {
208
+ return new BoxGeometry(geometry.width, geometry.height, geometry.depth);
209
+ }
210
+ }