UNPKG

3.19 kBJavaScriptView Raw
1/**
2 * @author oosmoxiecode
3 * @author mrdoob / http://mrdoob.com/
4 * @author Mugen87 / https://github.com/Mugen87
5 */
6
7import { Geometry } from '../core/Geometry.js';
8import { BufferGeometry } from '../core/BufferGeometry.js';
9import { Float32BufferAttribute } from '../core/BufferAttribute.js';
10import { Vector3 } from '../math/Vector3.js';
11
12// TorusGeometry
13
14function TorusGeometry( radius, tube, radialSegments, tubularSegments, arc ) {
15
16 Geometry.call( this );
17
18 this.type = 'TorusGeometry';
19
20 this.parameters = {
21 radius: radius,
22 tube: tube,
23 radialSegments: radialSegments,
24 tubularSegments: tubularSegments,
25 arc: arc
26 };
27
28 this.fromBufferGeometry( new TorusBufferGeometry( radius, tube, radialSegments, tubularSegments, arc ) );
29 this.mergeVertices();
30
31}
32
33TorusGeometry.prototype = Object.create( Geometry.prototype );
34TorusGeometry.prototype.constructor = TorusGeometry;
35
36// TorusBufferGeometry
37
38function TorusBufferGeometry( radius, tube, radialSegments, tubularSegments, arc ) {
39
40 BufferGeometry.call( this );
41
42 this.type = 'TorusBufferGeometry';
43
44 this.parameters = {
45 radius: radius,
46 tube: tube,
47 radialSegments: radialSegments,
48 tubularSegments: tubularSegments,
49 arc: arc
50 };
51
52 radius = radius || 1;
53 tube = tube || 0.4;
54 radialSegments = Math.floor( radialSegments ) || 8;
55 tubularSegments = Math.floor( tubularSegments ) || 6;
56 arc = arc || Math.PI * 2;
57
58 // buffers
59
60 var indices = [];
61 var vertices = [];
62 var normals = [];
63 var uvs = [];
64
65 // helper variables
66
67 var center = new Vector3();
68 var vertex = new Vector3();
69 var normal = new Vector3();
70
71 var j, i;
72
73 // generate vertices, normals and uvs
74
75 for ( j = 0; j <= radialSegments; j ++ ) {
76
77 for ( i = 0; i <= tubularSegments; i ++ ) {
78
79 var u = i / tubularSegments * arc;
80 var v = j / radialSegments * Math.PI * 2;
81
82 // vertex
83
84 vertex.x = ( radius + tube * Math.cos( v ) ) * Math.cos( u );
85 vertex.y = ( radius + tube * Math.cos( v ) ) * Math.sin( u );
86 vertex.z = tube * Math.sin( v );
87
88 vertices.push( vertex.x, vertex.y, vertex.z );
89
90 // normal
91
92 center.x = radius * Math.cos( u );
93 center.y = radius * Math.sin( u );
94 normal.subVectors( vertex, center ).normalize();
95
96 normals.push( normal.x, normal.y, normal.z );
97
98 // uv
99
100 uvs.push( i / tubularSegments );
101 uvs.push( j / radialSegments );
102
103 }
104
105 }
106
107 // generate indices
108
109 for ( j = 1; j <= radialSegments; j ++ ) {
110
111 for ( i = 1; i <= tubularSegments; i ++ ) {
112
113 // indices
114
115 var a = ( tubularSegments + 1 ) * j + i - 1;
116 var b = ( tubularSegments + 1 ) * ( j - 1 ) + i - 1;
117 var c = ( tubularSegments + 1 ) * ( j - 1 ) + i;
118 var d = ( tubularSegments + 1 ) * j + i;
119
120 // faces
121
122 indices.push( a, b, d );
123 indices.push( b, c, d );
124
125 }
126
127 }
128
129 // build geometry
130
131 this.setIndex( indices );
132 this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
133 this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
134 this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
135
136}
137
138TorusBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
139TorusBufferGeometry.prototype.constructor = TorusBufferGeometry;
140
141
142export { TorusGeometry, TorusBufferGeometry };