1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import { Geometry } from '../core/Geometry.js';
|
8 | import { BufferGeometry } from '../core/BufferGeometry.js';
|
9 | import { Float32BufferAttribute } from '../core/BufferAttribute.js';
|
10 | import { Vector3 } from '../math/Vector3.js';
|
11 |
|
12 |
|
13 |
|
14 | function 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 |
|
33 | TorusGeometry.prototype = Object.create( Geometry.prototype );
|
34 | TorusGeometry.prototype.constructor = TorusGeometry;
|
35 |
|
36 |
|
37 |
|
38 | function 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 |
|
59 |
|
60 | var indices = [];
|
61 | var vertices = [];
|
62 | var normals = [];
|
63 | var uvs = [];
|
64 |
|
65 |
|
66 |
|
67 | var center = new Vector3();
|
68 | var vertex = new Vector3();
|
69 | var normal = new Vector3();
|
70 |
|
71 | var j, i;
|
72 |
|
73 |
|
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 |
|
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 |
|
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 |
|
99 |
|
100 | uvs.push( i / tubularSegments );
|
101 | uvs.push( j / radialSegments );
|
102 |
|
103 | }
|
104 |
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 | for ( j = 1; j <= radialSegments; j ++ ) {
|
110 |
|
111 | for ( i = 1; i <= tubularSegments; i ++ ) {
|
112 |
|
113 |
|
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 |
|
121 |
|
122 | indices.push( a, b, d );
|
123 | indices.push( b, c, d );
|
124 |
|
125 | }
|
126 |
|
127 | }
|
128 |
|
129 |
|
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 |
|
138 | TorusBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
139 | TorusBufferGeometry.prototype.constructor = TorusBufferGeometry;
|
140 |
|
141 |
|
142 | export { TorusGeometry, TorusBufferGeometry };
|