-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhelpers.ts
367 lines (314 loc) · 10.7 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import { IParsedShape } from '@jupytercad/schema';
import * as THREE from 'three';
import {
acceleratedRaycast,
computeBoundsTree,
disposeBoundsTree
} from 'three-mesh-bvh';
import { LineGeometry } from 'three/examples/jsm/lines/LineGeometry.js';
import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial.js';
import { LineSegments2 } from 'three/examples/jsm/lines/LineSegments2.js';
import { IJCadObject } from '@jupytercad/schema';
import { getCSSVariableColor } from '../tools';
export const DEFAULT_LINEWIDTH = 2;
export const SELECTED_LINEWIDTH = 6;
// Apply the BVH extension
THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
THREE.BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;
THREE.Mesh.prototype.raycast = acceleratedRaycast;
export const DEFAULT_MESH_COLOR_CSS = '--jp-inverse-layout-color4';
export const DEFAULT_EDGE_COLOR_CSS = '--jp-inverse-layout-color2';
export const BOUNDING_BOX_COLOR_CSS = '--jp-brand-color0';
export const SPLITVIEW_BACKGROUND_COLOR_CSS = '--jcad-splitview-background';
export const SELECTION_BOUNDING_BOX = 'selectionBoundingBox';
export const DEFAULT_MESH_COLOR = new THREE.Color(
getCSSVariableColor(DEFAULT_MESH_COLOR_CSS)
);
export const DEFAULT_EDGE_COLOR = new THREE.Color(
getCSSVariableColor(DEFAULT_EDGE_COLOR_CSS)
);
export const BOUNDING_BOX_COLOR = new THREE.Color(
getCSSVariableColor(BOUNDING_BOX_COLOR_CSS)
);
export const SPLITVIEW_BACKGROUND_COLOR = new THREE.Color(
getCSSVariableColor(SPLITVIEW_BACKGROUND_COLOR_CSS)
);
export type BasicMesh = THREE.Mesh<
THREE.BufferGeometry,
THREE.MeshBasicMaterial | THREE.MeshStandardMaterial
>;
/**
* The interface for a 3D pointer
*/
export interface IPointer {
parent: BasicMesh;
readonly mesh: BasicMesh;
}
/**
* The result of mesh picking, contains the picked mesh and the 3D position of the pointer.
*/
export interface IPickedResult {
mesh: BasicMesh;
position: THREE.Vector3;
}
/**
* The interface defining a mouse drag by its start and end position in pixels.
*/
export interface IMouseDrag {
start: THREE.Vector2;
end: THREE.Vector2;
}
export interface IMeshGroupMetadata {
type: string;
jcObject: IJCadObject;
[key: string]: any;
}
export interface IMeshGroup extends THREE.Group {
userData: IMeshGroupMetadata;
}
export function projectVector(options: {
vector: THREE.Vector3;
camera: THREE.Camera;
width: number;
height: number;
}): THREE.Vector2 {
const { vector, camera, width, height } = options;
const copy = new THREE.Vector3().copy(vector);
copy.project(camera);
return new THREE.Vector2(
(0.5 + copy.x / 2) * width,
(0.5 - copy.y / 2) * height
);
}
export function getQuaternion(jcObject: IJCadObject): THREE.Quaternion {
const placement = jcObject?.parameters?.Placement;
const angle = placement.Angle;
const axis = placement.Axis;
const axisVector = new THREE.Vector3(axis[0], axis[1], axis[2]);
axisVector.normalize();
const angleRad = (angle * Math.PI) / 180;
const halfAngle = angleRad / 2;
const sinHalfAngle = Math.sin(halfAngle);
return new THREE.Quaternion(
axisVector.x * sinHalfAngle,
axisVector.y * sinHalfAngle,
axisVector.z * sinHalfAngle,
Math.cos(halfAngle)
).normalize();
}
export function computeExplodedState(options: {
mesh: BasicMesh;
boundingGroup: THREE.Box3;
factor: number;
}) {
const { mesh, boundingGroup, factor } = options;
const center = new THREE.Vector3();
const meshGroup = mesh.parent as THREE.Object3D;
boundingGroup.getCenter(center);
const oldGeometryCenter = new THREE.Vector3();
mesh.geometry.boundingBox?.getCenter(oldGeometryCenter);
const meshGroupQuaternion = getQuaternion(meshGroup.userData.jcObject);
const meshGroupPositionArray =
meshGroup.userData.jcObject.parameters?.Placement.Position;
const meshGroupPosition = new THREE.Vector3(
meshGroupPositionArray[0],
meshGroupPositionArray[1],
meshGroupPositionArray[2]
);
oldGeometryCenter.applyQuaternion(meshGroupQuaternion).add(meshGroupPosition);
const centerToMesh = new THREE.Vector3(
oldGeometryCenter.x - center.x,
oldGeometryCenter.y - center.y,
oldGeometryCenter.z - center.z
);
const distance = centerToMesh.length() * factor;
centerToMesh.normalize();
const newGeometryCenter = new THREE.Vector3(
oldGeometryCenter.x + distance * centerToMesh.x,
oldGeometryCenter.y + distance * centerToMesh.y,
oldGeometryCenter.z + distance * centerToMesh.z
);
return {
oldGeometryCenter,
newGeometryCenter,
vector: centerToMesh,
distance: distance
};
}
export function buildShape(options: {
objName: string;
data: IParsedShape;
clippingPlanes: THREE.Plane[];
isSolid: boolean;
isWireframe: boolean;
objColor?: THREE.Color | string | number;
}): {
meshGroup: IMeshGroup;
mainMesh: THREE.Mesh<THREE.BufferGeometry, THREE.MeshStandardMaterial>;
edgesMeshes: LineSegments2[];
} | null {
const { objName, data, isSolid, isWireframe, clippingPlanes, objColor } =
options;
const { faceList, edgeList, jcObject } = data;
const vertices: Array<number> = [];
const triangles: Array<number> = [];
const placement = data?.jcObject?.parameters?.Placement;
const objPosition = placement.Position;
const objQuaternion = getQuaternion(jcObject);
const inverseQuaternion = objQuaternion.clone().invert();
let vInd = 0;
if (faceList.length === 0 && edgeList.length === 0) {
return null;
}
for (const face of faceList) {
const vertexCoorLength = face.vertexCoord.length;
for (let ii = 0; ii < vertexCoorLength; ii += 3) {
const vertex = new THREE.Vector3(
face.vertexCoord[ii],
face.vertexCoord[ii + 1],
face.vertexCoord[ii + 2]
);
// Undo placement from the vertices, we want the placement done on the THREE.Object3D (Mesh), not the geometry
vertex.sub(
new THREE.Vector3(objPosition[0], objPosition[1], objPosition[2])
);
vertex.applyQuaternion(inverseQuaternion);
vertices.push(vertex.x, vertex.y, vertex.z);
}
const triIndexesLength = face.triIndexes.length;
for (let i = 0; i < triIndexesLength; i += 3) {
triangles.push(
face.triIndexes[i + 0] + vInd,
face.triIndexes[i + 1] + vInd,
face.triIndexes[i + 2] + vInd
);
}
vInd += vertexCoorLength / 3;
}
const color = objColor || DEFAULT_MESH_COLOR;
const visible = jcObject.visible;
// Compile the connected vertices and faces into a model
// And add to the scene
// We need one material per-mesh because we will set the uniform color independently later
// it's too bad Three.js does not easily allow setting uniforms independently per-mesh
const material = new THREE.MeshStandardMaterial({
color: new THREE.Color(color),
wireframe: isWireframe,
flatShading: false,
clippingPlanes,
metalness: 0.5,
roughness: 0.5
});
const geometry = new THREE.BufferGeometry();
geometry.setIndex(triangles);
geometry.setAttribute(
'position',
new THREE.Float32BufferAttribute(vertices, 3)
);
geometry.computeVertexNormals();
geometry.computeBoundingBox();
if (vertices.length > 0) {
geometry.computeBoundsTree();
}
const meshGroup = new THREE.Group() as IMeshGroup;
meshGroup.name = `${objName}-group`;
meshGroup.visible = visible;
meshGroup.userData = {
jcObject,
type: 'shape'
};
// We only build the stencil logic for solid meshes
if (isSolid) {
const baseMat = new THREE.MeshBasicMaterial();
baseMat.depthWrite = false;
baseMat.depthTest = false;
baseMat.colorWrite = false;
baseMat.stencilWrite = true;
baseMat.stencilFunc = THREE.AlwaysStencilFunc;
// back faces
const mat0 = baseMat.clone();
mat0.side = THREE.BackSide;
mat0.clippingPlanes = clippingPlanes;
mat0.stencilFail = THREE.IncrementWrapStencilOp;
mat0.stencilZFail = THREE.IncrementWrapStencilOp;
mat0.stencilZPass = THREE.IncrementWrapStencilOp;
const backFaces = new THREE.Mesh(geometry, mat0);
backFaces.name = `${objName}-back`;
meshGroup.add(backFaces);
// front faces
const mat1 = baseMat.clone();
mat1.side = THREE.FrontSide;
mat1.clippingPlanes = clippingPlanes;
mat1.stencilFail = THREE.DecrementWrapStencilOp;
mat1.stencilZFail = THREE.DecrementWrapStencilOp;
mat1.stencilZPass = THREE.DecrementWrapStencilOp;
const frontFaces = new THREE.Mesh(geometry, mat1);
frontFaces.name = `${objName}-front`;
meshGroup.add(frontFaces);
} else {
material.side = THREE.DoubleSide;
}
const mainMesh = new THREE.Mesh(geometry, material);
mainMesh.name = objName;
mainMesh.userData = {
type: 'shape'
};
let edgeIdx = 0;
const edgesMeshes: LineSegments2[] = [];
for (const edge of edgeList) {
const edgeMaterial = new LineMaterial({
linewidth: DEFAULT_LINEWIDTH,
color: new THREE.Color(DEFAULT_EDGE_COLOR).getHex(),
clippingPlanes,
// Depth offset so that lines are most always on top of faces
polygonOffset: true,
polygonOffsetFactor: -5,
polygonOffsetUnits: -5
});
const transformedVertices: number[] = [];
for (let i = 0; i < edge.vertexCoord.length; i += 3) {
const vertex = new THREE.Vector3(
edge.vertexCoord[i],
edge.vertexCoord[i + 1],
edge.vertexCoord[i + 2]
);
// Undo placement from the vertices, we want the placement done on the THREE.Object3D (Mesh), not the geometry
vertex.sub(
new THREE.Vector3(objPosition[0], objPosition[1], objPosition[2])
);
vertex.applyQuaternion(inverseQuaternion);
transformedVertices.push(vertex.x, vertex.y, vertex.z);
}
const edgeGeometry = new LineGeometry();
edgeGeometry.setPositions(transformedVertices);
const edgesMesh = new LineSegments2(edgeGeometry, edgeMaterial);
edgesMesh.name = `edge-${objName}-${edgeIdx}`;
edgesMesh.userData = {
type: 'edge',
edgeIndex: edgeIdx,
parent: objName
};
edgesMeshes.push(edgesMesh);
meshGroup.add(edgesMesh);
edgeIdx++;
}
const bbox = new THREE.Box3().setFromObject(meshGroup);
const size = new THREE.Vector3();
bbox.getSize(size);
const center = new THREE.Vector3();
bbox.getCenter(center);
const boundingBox = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(size.x, size.y, size.z)),
new THREE.LineBasicMaterial({ color: BOUNDING_BOX_COLOR, depthTest: false })
);
boundingBox.position.copy(center);
boundingBox.visible = false;
boundingBox.name = SELECTION_BOUNDING_BOX;
meshGroup.add(boundingBox);
meshGroup.add(mainMesh);
meshGroup.applyQuaternion(objQuaternion);
meshGroup.position.copy(
new THREE.Vector3(objPosition[0], objPosition[1], objPosition[2])
);
return { meshGroup, mainMesh, edgesMeshes };
}