-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreeTubeGeometry.js
More file actions
345 lines (259 loc) · 9.87 KB
/
TreeTubeGeometry.js
File metadata and controls
345 lines (259 loc) · 9.87 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
/**
* Creates a tube which extrudes along a 3d, branching curve
*
*/
THREE.TreeTubeGeometry = function( treeCurve, options ) {
options = options || {};
THREE.Geometry.call( this );
var path = treeCurve;
this.path = path;
this.segments = options.segments || 64;
this.radius = options.radius || 1;
this.radiusSegments = options.radiusSegments || 8;
this.closed = false;
if ( debug ) this.debug = new THREE.Object3D();
var _this = this;
options.maxDepth = treeCurve.maxDepth();
options.segmentDivisionSize = treeCurve.totalLength() / this.segments;
options.radiusSegments = this.radiusSegments;
options.existingGeometry = { vertices: [], normals: [], faces: [], treeDepths: [] };
var rootCapGeometry = makeCornerGeometryAt(treeCurve.root, options, 0.0);
var geometry = makeExtrudedGeometryForTree(treeCurve.root, options, 0.0);
appendGeometry( geometry, rootCapGeometry );
/*{
segmentDivisionSize: treeCurve.totalLength() / this.segments,
radiusSegments: this.radiusSegments
});*/
this.vertices = geometry.vertices;
this.normals = geometry.normals;
this.faces = geometry.faces;
this.faceVertexUvs[0] = geometry.treeDepths;
this.faceVertexUvs[1] = geometry.treeDepths;
this.computeCentroids();
//this.computeFaceNormals();
//this.computeVertexNormals();
};
function appendGeometry(a, b)
{
for(var f = 0; f < b.faces.length; f++)
{
b.faces[f].a += a.vertices.length;
b.faces[f].b += a.vertices.length;
b.faces[f].c += a.vertices.length;
if(b.faces[f] instanceof THREE.Face4)
b.faces[f].d += a.vertices.length;
}
a.vertices = a.vertices.concat(b.vertices);
a.normals = a.normals.concat(b.normals);
a.faces = a.faces.concat(b.faces);
a.treeDepths = a.treeDepths.concat(b.treeDepths);
}
function makeExtrudedGeometryForTree(tree, options, startingDepth)
{
var geometry = options.existingGeometry || { vertices: [], normals: [], faces: [], treeDepths: [] };
for(var c = 0; c < tree.children.length; c++)
{
var child = tree.children[c];
var childDepth = startingDepth + tree.distanceTo(child);
// tube from me to child
var segmentGeometry = makeExtrudedGeometryForSegment(tree, child, options.segmentDivisionSize, options.radiusSegments, options.radius, startingDepth, options.maxDepth);
appendGeometry(geometry, segmentGeometry);
// sphere at child
var segmentCapGeometry = makeCornerGeometryAt(child, options, childDepth / options.maxDepth);
appendGeometry(geometry, segmentCapGeometry);
options.existingGeometry = geometry;
makeExtrudedGeometryForTree(child, options, childDepth);
}
return geometry;
}
function makeCornerGeometryAt(node, options, depth)
{
var geometry = { vertices: [], normals: [], faces: [], treeDepths: [] };
var sphere3Geometry = new THREE.SphereGeometry( 1.0, options.radiusSegments, options.radiusSegments );
for(var v = 0; v < sphere3Geometry.vertices.length; v++)
{
geometry.normals.push( sphere3Geometry.vertices[v] );
geometry.vertices.push( new THREE.Vector3( node.x, node.y, node.z ) );
}
for(var f = 0; f < sphere3Geometry.faces.length; f++)
{
geometry.faces.push( sphere3Geometry.faces[f] );
geometry.treeDepths.push([
new THREE.UV(depth, 0.0),
new THREE.UV(depth, 0.0),
new THREE.UV(depth, 0.0),
new THREE.UV(depth, 0.0)
]);
}
return geometry;
}
function makeExtrudedGeometryForSegment(nodeA, nodeB, segmentDivisionSize, radiusSegments, radius, startingDepth, maxDepth)
{
var geometry = { vertices: [], normals: [], faces: [], treeDepths: [] };
var segmentLength = nodeA.distanceTo(nodeB);
var aToB = nodeA.to(nodeB);
var curveTangent = aToB.clone().normalize();
var curveNormal = normalToVec3(curveTangent);
var curveBinormal = (new THREE.Vector3()).cross(curveNormal, curveTangent).normalize();
var numberOfDivisions = Math.ceil(segmentLength / segmentDivisionSize);
for(var d = 0; d <= numberOfDivisions; d++)
{
var fraction = d / numberOfDivisions;
var curvePos = aToB.clone().multiplyScalar(fraction).addSelf(nodeA);
var circleGeometry = makeCircleGeometry(curvePos, curveNormal, curveBinormal, radiusSegments, radius)
geometry.vertices = geometry.vertices.concat( circleGeometry.vertices );
geometry.normals = geometry.normals.concat( circleGeometry.normals );
if(d > 0)
{
var previousFraction = (d-1) / numberOfDivisions;
for(var f = 0; f < radiusSegments; f++)
{
var radiusIndexA = f;
var radiusIndexB = (f+1) % radiusSegments;
var v1 = d * radiusSegments + radiusIndexA;
var v2 = d * radiusSegments + radiusIndexB;
var v3 = (d - 1) * radiusSegments + radiusIndexB;
var v4 = (d - 1) * radiusSegments + radiusIndexA;
var face = new THREE.Face4(
v1, v2, v3, v4
//, [geometry.normals[v1], geometry.normals[v2], geometry.normals[v3], geometry.normals[v4]]
//, geometry.normals[v1].clone().addSelf( geometry.normals[v2] ).addSelf( geometry.normals[v3] ).addSelf( geometry.normals[v4] ).multiplyScalar( 0.25 )
//, geometry.normals[v1]
);
face.vertexNormals = [geometry.normals[v1], geometry.normals[v2], geometry.normals[v3], geometry.normals[v4]];
face.normal = geometry.normals[v1];
geometry.faces.push(face);
var previousDepth = startingDepth + previousFraction * segmentLength;
var currentDepth = startingDepth + fraction * segmentLength;
var previousDepthFraction = previousDepth / maxDepth;
var currentDepthFraction = currentDepth / maxDepth;
geometry.treeDepths.push([
new THREE.UV(currentDepthFraction, 0.0),
new THREE.UV(currentDepthFraction, 0.0),
new THREE.UV(previousDepthFraction, 0.0),
new THREE.UV(previousDepthFraction, 0.0)
]);
}
}
}
return geometry;
}
function makeCircleGeometry(center, basisX, basisY, radiusSegments, radius)
{
var vertices = [];
var normals = [];
for(var v = 0; v < radiusSegments; v++)
{
var angle = Math.PI * 2 * v / radiusSegments;
var xVec = basisX.clone().multiplyScalar( Math.cos(angle) );
var yVec = basisY.clone().multiplyScalar( Math.sin(angle) );
var fromCenter = (new THREE.Vector3()).add(xVec, yVec);
vertices.push( center );
//vertices.push( center.clone().addSelf(fromCenter) );
normals.push( fromCenter );
}
return {vertices:vertices, normals:normals};
}
function normalToVec3(vec) {
var normal = new THREE.Vector3();
var smallest = Number.MAX_VALUE;
var tx = Math.abs( vec.x );
var ty = Math.abs( vec.y );
var tz = Math.abs( vec.z );
if ( tx <= smallest ) {
smallest = tx;
normal.set( 1, 0, 0 );
}
if ( ty <= smallest ) {
smallest = ty;
normal.set( 0, 1, 0 );
}
if ( tz <= smallest ) {
normal.set( 0, 0, 1 );
}
var intermediateVec = (new THREE.Vector3()).cross( vec, normal ).normalize();
normal.cross( vec, intermediateVec );
return normal;
}
THREE.TreeTubeGeometry.prototype = Object.create( THREE.Geometry.prototype );
// For computing of Frenet frames, exposing the tangents, normals and binormals the spline
THREE.TreeTubeGeometry.FrenetFrames = function(path, segments, closed) {
var
tangent = new THREE.Vector3(),
normal = new THREE.Vector3(),
binormal = new THREE.Vector3(),
tangents = [],
normals = [],
binormals = [],
vec = new THREE.Vector3(),
mat = new THREE.Matrix4(),
numpoints = segments + 1,
theta,
epsilon = 0.0001,
smallest,
tx, ty, tz,
i, u, v;
// expose internals
this.tangents = tangents;
this.normals = normals;
this.binormals = binormals;
// compute the tangent vectors for each segment on the path
for ( i = 0; i < numpoints; i++ ) {
u = i / ( numpoints - 1 );
tangents[ i ] = path.getTangentAt( u );
tangents[ i ].normalize();
}
initialNormal3();
function initialNormal1(lastBinormal) {
// fixed start binormal. Has dangers of 0 vectors
normals[ 0 ] = new THREE.Vector3();
binormals[ 0 ] = new THREE.Vector3();
if (lastBinormal===undefined) lastBinormal = new THREE.Vector3( 0, 0, 1 );
normals[ 0 ].cross( lastBinormal, tangents[ 0 ] ).normalize();
binormals[ 0 ].cross( tangents[ 0 ], normals[ 0 ] ).normalize();
}
function initialNormal2() {
// This uses the Frenet-Serret formula for deriving binormal
var t2 = path.getTangentAt( epsilon );
normals[ 0 ] = new THREE.Vector3().sub( t2, tangents[ 0 ] ).normalize();
binormals[ 0 ] = new THREE.Vector3().cross( tangents[ 0 ], normals[ 0 ] );
normals[ 0 ].cross( binormals[ 0 ], tangents[ 0 ] ).normalize(); // last binormal x tangent
binormals[ 0 ].cross( tangents[ 0 ], normals[ 0 ] ).normalize();
}
function initialNormal3() {
// select an initial normal vector perpenicular to the first tangent vector,
// and in the direction of the smallest tangent xyz component
normals[ 0 ] = new THREE.Vector3();
binormals[ 0 ] = new THREE.Vector3();
smallest = Number.MAX_VALUE;
tx = Math.abs( tangents[ 0 ].x );
ty = Math.abs( tangents[ 0 ].y );
tz = Math.abs( tangents[ 0 ].z );
if ( tx <= smallest ) {
smallest = tx;
normal.set( 1, 0, 0 );
}
if ( ty <= smallest ) {
smallest = ty;
normal.set( 0, 1, 0 );
}
if ( tz <= smallest ) {
normal.set( 0, 0, 1 );
}
vec.cross( tangents[ 0 ], normal ).normalize();
normals[ 0 ].cross( tangents[ 0 ], vec );
binormals[ 0 ].cross( tangents[ 0 ], normals[ 0 ] );
}
// compute the slowly-varying normal and binormal vectors for each segment on the path
for ( i = 1; i < numpoints; i++ ) {
normals[ i ] = normals[ i-1 ].clone();
binormals[ i ] = binormals[ i-1 ].clone();
vec.cross( tangents[ i-1 ], tangents[ i ] );
if ( vec.length() > epsilon ) {
vec.normalize();
theta = Math.acos( tangents[ i-1 ].dot( tangents[ i ] ) );
mat.makeRotationAxis( vec, theta ).multiplyVector3( normals[ i ] );
}
binormals[ i ].cross( tangents[ i ], normals[ i ] );
}
};