-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTubePath.js
357 lines (230 loc) · 7.65 KB
/
TubePath.js
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
import {
BufferGeometry,
CatmullRomCurve3,
Float32BufferAttribute,
MathUtils,
Matrix4,
Vector2,
Vector3
} from 'three';
/**
* Forked from TubeGeometry on three.js to expose u-Mapping param
* @link https://github.com/mrdoob/three.js/blob/master/src/geometries/TubeGeometry.js
*
* Also adapted Curve.computeFrenetFrames() to use u-mapping
*/
export class TubePath extends BufferGeometry {
constructor(
path = new CatmullRomCurve3([ new Vector3(- 1, - 1, 0), new Vector3(- 1, 1, 0), new Vector3(1, 1, 0) ]),
uMappingFrames = undefined,
radius = 0.3,
radialSegments = 8,
closed = false) {
super();
this.type = 'TubePath';
this.parameters = {
path: path,
uMappingFrames: uMappingFrames,
radius: radius,
radialSegments: radialSegments,
closed: closed
};
if (!uMappingFrames) {
uMappingFrames = TubePath.pathToUMapping(path);
}
const frames = this.computeFrenetFrames( path, uMappingFrames, closed );
// expose internals
this.tangents = frames.tangents;
this.normals = frames.normals;
this.binormals = frames.binormals;
// helper variables
const vertex = new Vector3();
const normal = new Vector3();
const uv = new Vector2();
let P = new Vector3();
// buffer
const vertices = [];
const normals = [];
const uvs = [];
const indices = [];
// create buffer data
generateBufferData();
// build geometry
this.setIndex( indices );
this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
// functions
function generateBufferData() {
for ( let i = 0; i < uMappingFrames.length - 1; i ++ ) {
generateSegment( i );
}
// if the geometry is not closed, generate the last row of vertices and normals
// at the regular position on the given path
//
// if the geometry is closed, duplicate the first row of vertices and normals (uvs will differ)
generateSegment( ( closed === false ) ? uMappingFrames.length - 1 : 0 );
// uvs are generated in a separate function.
// this makes it easy compute correct values for closed geometries
generateUVs();
// finally create faces
generateIndices();
}
function generateSegment( i ) {
// we use getPointAt to sample evenly distributed points from the given path
P = path.getPointAt( uMappingFrames[ i ], P );
// retrieve corresponding normal and binormal
const N = frames.normals[ i ];
const B = frames.binormals[ i ];
// generate normals and vertices for the current segment
for ( let j = 0; j <= radialSegments; j ++ ) {
const v = j / radialSegments * Math.PI * 2;
const sin = Math.sin( v );
const cos = - Math.cos( v );
// normal
normal.x = ( cos * N.x + sin * B.x );
normal.y = ( cos * N.y + sin * B.y );
normal.z = ( cos * N.z + sin * B.z );
normal.normalize();
normals.push( normal.x, normal.y, normal.z );
// vertex
vertex.x = P.x + radius * normal.x;
vertex.y = P.y + radius * normal.y;
vertex.z = P.z + radius * normal.z;
vertices.push( vertex.x, vertex.y, vertex.z );
}
}
function generateIndices() {
for ( let j = 1; j <= uMappingFrames.length - 1; j ++ ) {
for ( let i = 1; i <= radialSegments; i ++ ) {
const a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 );
const b = ( radialSegments + 1 ) * j + ( i - 1 );
const c = ( radialSegments + 1 ) * j + i;
const d = ( radialSegments + 1 ) * ( j - 1 ) + i;
// faces
indices.push( a, b, d );
indices.push( b, c, d );
}
}
}
function generateUVs() {
for ( let i = 0; i <= uMappingFrames.length - 1; i ++ ) {
for ( let j = 0; j <= radialSegments; j ++ ) {
uv.x = i / (uMappingFrames.length - 1);
uv.y = j / radialSegments;
uvs.push( uv.x, uv.y );
}
}
}
}
computeFrenetFrames( path, uMappingFrames, closed ) {
// see http://www.cs.indiana.edu/pub/techreports/TR425.pdf
const normal = new Vector3();
const tangents = [];
const normals = [];
const binormals = [];
const vec = new Vector3();
const mat = new Matrix4();
const segments = uMappingFrames.length - 1;
// compute the tangent vectors for each segment on the curve
for ( let i = 0; i <= segments; i ++ ) {
const u = uMappingFrames[ i ];
tangents[ i ] = path.getTangentAt( u, new Vector3() );
}
// select an initial normal vector perpendicular to the first tangent vector,
// and in the direction of the minimum tangent xyz component
normals[ 0 ] = new Vector3();
binormals[ 0 ] = new Vector3();
let min = Number.MAX_VALUE;
const tx = Math.abs( tangents[ 0 ].x );
const ty = Math.abs( tangents[ 0 ].y );
const tz = Math.abs( tangents[ 0 ].z );
if ( tx <= min ) {
min = tx;
normal.set( 1, 0, 0 );
}
if ( ty <= min ) {
min = ty;
normal.set( 0, 1, 0 );
}
if ( tz <= min ) {
normal.set( 0, 0, 1 );
}
vec.crossVectors( tangents[ 0 ], normal ).normalize();
normals[ 0 ].crossVectors( tangents[ 0 ], vec );
binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] );
// compute the slowly-varying normal and binormal vectors for each segment on the curve
for ( let i = 1; i <= segments; i ++ ) {
normals[ i ] = normals[ i - 1 ].clone();
binormals[ i ] = binormals[ i - 1 ].clone();
vec.crossVectors( tangents[ i - 1 ], tangents[ i ] );
if ( vec.length() > Number.EPSILON ) {
vec.normalize();
const theta = Math.acos( MathUtils.clamp( tangents[ i - 1 ].dot( tangents[ i ] ), - 1, 1 ) ); // clamp for floating pt errors
normals[ i ].applyMatrix4( mat.makeRotationAxis( vec, theta ) );
}
binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
}
// if the curve is closed, postprocess the vectors so the first and last normal vectors are the same
if ( closed === true ) {
let theta = Math.acos( MathUtils.clamp( normals[ 0 ].dot( normals[ segments ] ), - 1, 1 ) );
theta /= segments;
if ( tangents[ 0 ].dot( vec.crossVectors( normals[ 0 ], normals[ segments ] ) ) > 0 ) {
theta = - theta;
}
for ( let i = 1; i <= segments; i ++ ) {
// twist a little...
normals[ i ].applyMatrix4( mat.makeRotationAxis( tangents[ i ], theta * i ) );
binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
}
}
return {
tangents: tangents,
normals: normals,
binormals: binormals
};
}
toJSON() {
const data = super.toJSON();
data.path = this.parameters.path.toJSON();
return data;
}
static fromJSON( data ) {
// This only works for built-in curves (e.g. CatmullRomCurve3).
// User defined curves or instances of CurvePath will not be deserialized.
return new TubePath(
new Curves[ data.path.type ]().fromJSON( data.path ),
data.uMappingFrames,
data.radius,
data.radialSegments,
data.closed
);
}
static pathToUMapping(
path = new CatmullRomCurve3([ new Vector3(- 1, - 1, 0), new Vector3(- 1, 1, 0), new Vector3(1, 1, 0) ]),
elbowSegmentNum = 2,
elbowSegmentOffset = 0.1) {
const lengths = [0];
path.points.forEach((p, i, arr) => {
if (i > 0) {
const last = lengths.at(-1);
const dist = p.distanceTo(arr[i - 1]);
const next = last + dist;
const numElbow = Math.min(elbowSegmentNum, dist / 2 / elbowSegmentOffset - 1);
if (i > 1) {
for (let j = 1; j <= numElbow; ++j) {
lengths.push(last + j * elbowSegmentOffset);
}
}
if (i < arr.length - 1) {
for (let j = numElbow; j >= 1; --j) {
lengths.push(next - j * elbowSegmentOffset);
}
}
lengths.push(next);
}
});
const uMappingFrames = lengths.map(l => l / lengths.at(-1));
return uMappingFrames;
}
}