draw.js
10.1 KB
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 * as THREE from 'three';
import { FontLoader } from 'three/examples/jsm/loaders/FontLoader.js';
import { TextGeometry } from 'three/examples/jsm/geometries/TextGeometry.js';
import { Line2 } from 'three/examples/jsm/lines/Line2.js';
import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial.js';
import { LineGeometry } from 'three/examples/jsm/lines/LineGeometry.js';
import { mergeGeometries } from 'three/examples/jsm/utils/BufferGeometryUtils.js';
export const drawTriangle = (pointa, pointb, pointc, color = 0xffa41e) => {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
'position',
new THREE.Float32BufferAttribute(
[
pointa.x,
pointa.y,
pointa.z,
pointb.x,
pointb.y,
pointb.z,
pointc.x,
pointc.y,
pointc.z
],
3,
false
)
);
const material = new THREE.MeshBasicMaterial({
color,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(geometry, material);
return mesh;
};
export const drawDoubleSideText = (
group,
text,
color,
size,
offset,
path,
callback
) => {
const loader = new FontLoader();
loader.load(path, (font) => {
const geometry = new TextGeometry(text, {
font,
size,
height: 0.005,
curveSegments: 12
});
geometry.computeBoundingBox();
geometry.translate(-geometry.boundingBox.max.x * 0.5, offset, 0.005);
const material = new THREE.MeshBasicMaterial({
color
});
const mesh = new THREE.Mesh(geometry, material);
const copyText = mesh.clone();
copyText.rotation.y = Math.PI;
group.add(copyText);
group.add(mesh);
if (callback) callback(group);
});
};
export const drawTubeLine = (points, color, lineWidth = 0.08) => {
const fittedCurve = new THREE.CatmullRomCurve3(points);
const geometry = new THREE.TubeGeometry(fittedCurve, 5, lineWidth, 8);
const material = new THREE.MeshBasicMaterial({ color });
const mesh = new THREE.Mesh(geometry, material);
return mesh;
};
export const drawRectriangle = (
pointa,
pointb,
pointc,
pointd,
color = 0x000000,
lineWidth = 0.08
) => {
const edgea = drawTubeLine([pointa, pointb], color, lineWidth);
const edgeb = drawTubeLine([pointb, pointc], color, lineWidth);
const edgec = drawTubeLine([pointc, pointd], color, lineWidth);
const edged = drawTubeLine([pointd, pointa], color, lineWidth);
const group = new THREE.Object3D();
group.add(edgeb).add(edgea).add(edgec).add(edged);
return group;
};
export const drawDashLine = (points, color = 0x000000) => {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineDashedMaterial({
color,
linewidth: 10,
scale: 1,
dashSize: 0.6,
gapSize: 0.4,
transparent: true,
opacity: 0.9
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
return line;
};
/**
*
* @param {*[v0,v1,v2,...,]} points
* @param {*0xXXXXXX} color
* @returns {new THREE.Line}
*/
export const drawLine = (points, color = 0x000000) => {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color,
linewidth: 20
});
const line = new THREE.Line(geometry, material);
return line;
};
/**
*
* @param {*Array [v0.x , v0.y , v0.z , v1.x , v1.y , v1.z]} positions
* @param {*0xXXXXXX} color
* @param {*THREE.Vector2} resolution
* @param {*Number} lineWidth
* @param {*Number} opacity
* @param {*Boolean} depthTest
* @returns
*/
export const drawFatLine = (
positions,
resolution,
color = 0x000000,
lineWidth = 1,
opacity = 1,
depthTest = true
) => {
const geometry = new LineGeometry();
geometry.setPositions(positions);
const matrial = new LineMaterial({
color,
resolution,
dashed: false,
linewidth: lineWidth,
transparent: true,
opacity,
depthTest
});
const line = new Line2(geometry, matrial);
line.renderOrder = 4;
line.computeLineDistances();
return line;
};
export const drawFatDashLine = (positions, color, resolution) => {
const geometry = new LineGeometry();
geometry.setPositions(positions);
const matrial = new LineMaterial({
color,
resolution,
dashed: true,
linewidth: 1.8,
transparent: true,
opacity: 0.95,
dashSize: 0.7,
gapSize: 0.6
});
const line = new Line2(geometry, matrial);
line.computeLineDistances();
return line;
};
export const drawPoints = (points, color = 0x000000, radius = 0.1) => {
const object = new THREE.Object3D();
let i = 0;
while (i < points.length) {
const point = points[i];
const geometry = new THREE.SphereGeometry(radius, 32, 16);
const material = new THREE.MeshBasicMaterial({ color });
const sphere = new THREE.Mesh(geometry, material);
sphere.position.set(point.x, point.y, point.z);
object.add(sphere);
i += 1;
}
return object;
};
export const drawPoint = (point, color = 0x000000, radius = 0.1) => {
const geometry = new THREE.SphereGeometry(radius, 32, 16);
const material = new THREE.MeshBasicMaterial({ color });
const sphere = new THREE.Mesh(geometry, material);
sphere.position.set(point.x, point.y, point.z);
return sphere;
};
export const drawCone = (color) => {
const geometry = new THREE.ConeGeometry(0.3, 0.7, 32, 10, false, 5, 7);
const material = new THREE.MeshPhongMaterial({
color
});
const matrix = new THREE.Matrix4().makeTranslation(0, -0.3, 0);
geometry.applyMatrix4(matrix);
const mesh = new THREE.Mesh(geometry, material);
return mesh;
};
const CylinderLathe = (
outerRadius,
innerRadius,
height,
phistart,
philength
) => {
const halfH = height * 0.5;
const points = [
new THREE.Vector2(innerRadius, -halfH),
new THREE.Vector2(outerRadius, -halfH),
new THREE.Vector2(outerRadius, halfH),
new THREE.Vector2(innerRadius, halfH),
new THREE.Vector2(innerRadius, -halfH)
];
const geometry = new THREE.LatheGeometry(points, 72, phistart, philength);
return geometry;
};
export const drawRingMesh = (
color,
outerRadius,
innerRadius,
height,
phistart = 0,
philength = Math.PI * 2
) => {
const geometry = CylinderLathe(
outerRadius,
innerRadius,
height,
phistart,
philength
);
const material = new THREE.MeshBasicMaterial({
color,
depthTest: false,
transparent: true,
opacity: 0.5,
side: THREE.BackSide
});
const mesh = new THREE.Mesh(geometry, material);
mesh.renderOrder = 4;
return mesh;
};
export const scaleRingLine = (points) => {
const scalarGroup = new THREE.Object3D();
const segment = 360 / 45;
for (let i = 0; i < segment; i += 1) {
const contourGeometry = new THREE.BufferGeometry().setFromPoints(
points
);
const cmaterial = new THREE.LineBasicMaterial({
color: 0xffffff,
depthTest: false,
transparent: true
});
const contour = new THREE.Line(contourGeometry, cmaterial);
contour.renderOrder = 4;
contour.geometry.applyMatrix4(
new THREE.Matrix4().makeRotationY((Math.PI / 180) * i * 45)
);
scalarGroup.add(contour);
}
return scalarGroup;
};
export const drawArrow2D = (size, color) => {
const width = size * 0.3;
const height = size * 1.2;
const boxGeometry = new THREE.BoxGeometry(width, height, 0);
const material = new THREE.MeshBasicMaterial({
color,
depthTest: false,
transparent: true,
opacity: 0.5
});
const TILE_SIZE = 0.27;
const arrowGeometry = new THREE.CylinderGeometry(
TILE_SIZE * 3,
TILE_SIZE * 3,
0.1,
3
);
arrowGeometry
.applyMatrix4(new THREE.Matrix4().makeRotationX(-Math.PI * 0.5))
.applyMatrix4(new THREE.Matrix4().makeTranslation(0, height * 0.67, 0));
const geometry = mergeGeometries([boxGeometry, arrowGeometry]);
const mesh = new THREE.Mesh(geometry, material);
mesh.renderOrder = 4;
return mesh;
};
export const drawTorusGeometry = (radius, color) => {
const tube = 0.25;
const geometry = new THREE.TorusGeometry(
radius,
tube,
3,
100,
(Math.PI / 180) * 36
);
geometry.applyMatrix4(
new THREE.Matrix4().makeRotationZ((Math.PI / 180) * 4.5)
);
const material = new THREE.MeshBasicMaterial({
color,
depthTest: false,
transparent: true,
opacity: 0.5
});
const torus = new THREE.Mesh(geometry, material);
return torus;
};
export const drawCircleArrow = (radius, color) => {
const circleGeometry = drawTorusGeometry(radius, color);
const size = 1.1;
const coneGeometry = new THREE.ConeGeometry(size * 0.4, size, 32);
const material = new THREE.MeshBasicMaterial({
color,
depthTest: false,
transparent: true,
opacity: 0.8
});
let matrix = new THREE.Matrix4()
.makeTranslation(radius, 0, 0)
.multiply(new THREE.Matrix4().makeRotationX(Math.PI));
coneGeometry.applyMatrix4(matrix);
const coneGeometryleft = new THREE.ConeGeometry(size * 0.4, size, 32);
matrix = new THREE.Matrix4()
.makeRotationZ((Math.PI / 180) * 45)
.multiply(new THREE.Matrix4().makeTranslation(radius, 0, 0));
coneGeometryleft.applyMatrix4(matrix);
const geometry = mergeGeometries([
circleGeometry.geometry,
coneGeometry,
coneGeometryleft
]);
const mesh = new THREE.Mesh(geometry, material);
mesh.renderOrder = 4;
return mesh;
};