Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TubeGeometry: Added parametric radius via THREE.Path. #13971

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 38 additions & 7 deletions src/geometries/TubeGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @author miningold / https://github.com/miningold
* @author jonobr1 / https://github.com/jonobr1
* @author Mugen87 / https://github.com/Mugen87
* @author spite / https://github.com/spite
*
*/

Expand All @@ -13,6 +14,7 @@ import { BufferGeometry } from '../core/BufferGeometry.js';
import { Float32BufferAttribute } from '../core/BufferAttribute.js';
import { Vector2 } from '../math/Vector2.js';
import { Vector3 } from '../math/Vector3.js';
import { Path } from '../extras/core/Path.js';

// TubeGeometry

Expand Down Expand Up @@ -84,7 +86,6 @@ function TubeBufferGeometry( path, tubularSegments, radius, radialSegments, clos
var vertex = new Vector3();
var normal = new Vector3();
var uv = new Vector2();
var P = new Vector3();

var i, j;

Expand Down Expand Up @@ -138,10 +139,13 @@ function TubeBufferGeometry( path, tubularSegments, radius, radialSegments, clos

// we use getPointAt to sample evenly distributed points from the given path

P = path.getPointAt( i / tubularSegments, P );
var P = path.getPointAt( i / tubularSegments );
var P1;
var P2;

// retrieve corresponding normal and binormal

var T = frames.tangents[ i ];
var N = frames.normals[ i ];
var B = frames.binormals[ i ];

Expand All @@ -161,16 +165,43 @@ function TubeBufferGeometry( path, tubularSegments, radius, radialSegments, clos
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;
var r = radius instanceof Path ? radius.getPointAt( i / tubularSegments ).y : radius;
vertex.x = P.x + r * normal.x;
vertex.y = P.y + r * normal.y;
vertex.z = P.z + r * normal.z;

vertices.push( vertex.x, vertex.y, vertex.z );

// adjust normals if the radius is a Path

if ( radius instanceof Path ) {

var delta = 0.0001;
var t1 = i / tubularSegments - delta;
var t2 = i / tubularSegments + delta;

// Capping in case of danger

if ( t1 < 0 ) t1 = 0;
if ( t2 > 1 ) t2 = 1;

P1 = path.getPointAt( t1, P1 );
P2 = path.getPointAt( t2, P2 );
var delta = P1.distanceTo( P2 );

var r1 = radius.getPointAt( t1 ).y;
var r2 = radius.getPointAt( t2 ).y;

normal.addScaledVector( T, - ( r2 - r1 ) / delta ).normalize();

}

// push the normal

normals.push( normal.x, normal.y, normal.z );

}

}
Expand Down