TorusKnotGeometry.js 5.0 KB
Newer Older
1 2 3 4
/**
 * @author oosmoxiecode
 */

M
Mr.doob 已提交
5 6
import { Geometry } from '../core/Geometry';

M
Mr.doob 已提交
7
function TorusKnotGeometry( radius, tube, tubularSegments, radialSegments, p, q, heightScale ) {
8

R
Rich Harris 已提交
9
	Geometry.call( this );
M
Mr.doob 已提交
10

11 12
	this.type = 'TorusKnotGeometry';

M
Mr.doob 已提交
13 14 15 16
	this.parameters = {
		radius: radius,
		tube: tube,
		tubularSegments: tubularSegments,
17
		radialSegments: radialSegments,
M
Mr.doob 已提交
18
		p: p,
19
		q: q
M
Mr.doob 已提交
20 21
	};

M
Mr.doob 已提交
22
	if ( heightScale !== undefined ) console.warn( 'THREE.TorusKnotGeometry: heightScale has been deprecated. Use .scale( x, y, z ) instead.' );
23

R
Rich Harris 已提交
24
	this.fromBufferGeometry( new TorusKnotBufferGeometry( radius, tube, tubularSegments, radialSegments, p, q ) );
M
Mugen87 已提交
25
	this.mergeVertices();
26

M
Mr.doob 已提交
27
}
28

R
Rich Harris 已提交
29 30 31
TorusKnotGeometry.prototype = Object.create( Geometry.prototype );
TorusKnotGeometry.prototype.constructor = TorusKnotGeometry;

M
Mr.doob 已提交
32 33 34 35 36
/**
 * @author Mugen87 / https://github.com/Mugen87
 * see: http://www.blackpawn.com/texts/pqtorus/
 */

M
Mugen87 已提交
37
import { Float32BufferAttribute, Uint16BufferAttribute, Uint32BufferAttribute } from '../core/BufferAttribute';
M
Mr.doob 已提交
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
import { BufferGeometry } from '../core/BufferGeometry';
import { Vector3 } from '../math/Vector3';
import { Vector2 } from '../math/Vector2';

function TorusKnotBufferGeometry( radius, tube, tubularSegments, radialSegments, p, q ) {

	BufferGeometry.call( this );

	this.type = 'TorusKnotBufferGeometry';

	this.parameters = {
		radius: radius,
		tube: tube,
		tubularSegments: tubularSegments,
		radialSegments: radialSegments,
		p: p,
		q: q
	};

	radius = radius || 100;
	tube = tube || 40;
	tubularSegments = Math.floor( tubularSegments ) || 64;
	radialSegments = Math.floor( radialSegments ) || 8;
	p = p || 2;
	q = q || 3;

	// buffers
M
Mugen87 已提交
65 66 67 68 69

	var indices = [];
	var vertices = [];
	var normals = [];
	var uvs = [];
M
Mr.doob 已提交
70 71

	// helper variables
M
Mugen87 已提交
72 73

	var i, j;
M
Mr.doob 已提交
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

	var vertex = new Vector3();
	var normal = new Vector3();
	var uv = new Vector2();

	var P1 = new Vector3();
	var P2 = new Vector3();

	var B = new Vector3();
	var T = new Vector3();
	var N = new Vector3();

	// generate vertices, normals and uvs

	for ( i = 0; i <= tubularSegments; ++ i ) {

		// the radian "u" is used to calculate the position on the torus curve of the current tubular segement

		var u = i / tubularSegments * p * Math.PI * 2;

		// now we calculate two points. P1 is our current position on the curve, P2 is a little farther ahead.
		// these points are used to create a special "coordinate space", which is necessary to calculate the correct vertex positions

		calculatePositionOnCurve( u, p, q, radius, P1 );
		calculatePositionOnCurve( u + 0.01, p, q, radius, P2 );

		// calculate orthonormal basis

		T.subVectors( P2, P1 );
		N.addVectors( P2, P1 );
		B.crossVectors( T, N );
		N.crossVectors( B, T );

		// normalize B, N. T can be ignored, we don't use it

		B.normalize();
		N.normalize();

		for ( j = 0; j <= radialSegments; ++ j ) {

			// now calculate the vertices. they are nothing more than an extrusion of the torus curve.
			// because we extrude a shape in the xy-plane, there is no need to calculate a z-value.

			var v = j / radialSegments * Math.PI * 2;
			var cx = - tube * Math.cos( v );
			var cy = tube * Math.sin( v );

			// now calculate the final vertex position.
			// first we orient the extrusion with our basis vectos, then we add it to the current position on the curve

			vertex.x = P1.x + ( cx * N.x + cy * B.x );
			vertex.y = P1.y + ( cx * N.y + cy * B.y );
			vertex.z = P1.z + ( cx * N.z + cy * B.z );

M
Mugen87 已提交
128
			vertices.push( vertex.x, vertex.y, vertex.z );
M
Mr.doob 已提交
129 130

			// normal (P1 is always the center/origin of the extrusion, thus we can use it to calculate the normal)
M
Mugen87 已提交
131

M
Mr.doob 已提交
132
			normal.subVectors( vertex, P1 ).normalize();
M
Mugen87 已提交
133 134

			normals.push( normal.x, normal.y, normal.z );
M
Mr.doob 已提交
135 136 137

			// uv

M
Mugen87 已提交
138 139
			uvs.push( i / tubularSegments );
			uvs.push( j / radialSegments );
M
Mr.doob 已提交
140 141 142 143 144 145 146 147 148 149 150 151

		}

	}

	// generate indices

	for ( j = 1; j <= tubularSegments; j ++ ) {

		for ( i = 1; i <= radialSegments; i ++ ) {

			// indices
M
Mugen87 已提交
152

M
Mr.doob 已提交
153 154 155 156 157
			var a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 );
			var b = ( radialSegments + 1 ) * j + ( i - 1 );
			var c = ( radialSegments + 1 ) * j + i;
			var d = ( radialSegments + 1 ) * ( j - 1 ) + i;

M
Mugen87 已提交
158
			// faces
M
Mr.doob 已提交
159

M
Mugen87 已提交
160 161
			indices.push( a, b, d );
			indices.push( b, c, d );
M
Mr.doob 已提交
162 163 164 165 166 167 168

		}

	}

	// build geometry

169
	this.setIndex( new ( vertices.length > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 ) );
M
Mugen87 已提交
170 171 172
	this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
	this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
	this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
M
Mr.doob 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

	// this function calculates the current position on the torus curve

	function calculatePositionOnCurve( u, p, q, radius, position ) {

		var cu = Math.cos( u );
		var su = Math.sin( u );
		var quOverP = q / p * u;
		var cs = Math.cos( quOverP );

		position.x = radius * ( 2 + cs ) * 0.5 * cu;
		position.y = radius * ( 2 + cs ) * su * 0.5;
		position.z = radius * Math.sin( quOverP ) * 0.5;

	}

}

TorusKnotBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
TorusKnotBufferGeometry.prototype.constructor = TorusKnotBufferGeometry;
R
Rich Harris 已提交
193

M
Mr.doob 已提交
194
export { TorusKnotGeometry, TorusKnotBufferGeometry };