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 | import { Vector2 } from '../math/Vector2.js';
|
12 |
|
13 |
|
14 |
|
15 | function CircleGeometry( radius, segments, thetaStart, thetaLength ) {
|
16 |
|
17 | Geometry.call( this );
|
18 |
|
19 | this.type = 'CircleGeometry';
|
20 |
|
21 | this.parameters = {
|
22 | radius: radius,
|
23 | segments: segments,
|
24 | thetaStart: thetaStart,
|
25 | thetaLength: thetaLength
|
26 | };
|
27 |
|
28 | this.fromBufferGeometry( new CircleBufferGeometry( radius, segments, thetaStart, thetaLength ) );
|
29 | this.mergeVertices();
|
30 |
|
31 | }
|
32 |
|
33 | CircleGeometry.prototype = Object.create( Geometry.prototype );
|
34 | CircleGeometry.prototype.constructor = CircleGeometry;
|
35 |
|
36 |
|
37 |
|
38 | function CircleBufferGeometry( radius, segments, thetaStart, thetaLength ) {
|
39 |
|
40 | BufferGeometry.call( this );
|
41 |
|
42 | this.type = 'CircleBufferGeometry';
|
43 |
|
44 | this.parameters = {
|
45 | radius: radius,
|
46 | segments: segments,
|
47 | thetaStart: thetaStart,
|
48 | thetaLength: thetaLength
|
49 | };
|
50 |
|
51 | radius = radius || 1;
|
52 | segments = segments !== undefined ? Math.max( 3, segments ) : 8;
|
53 |
|
54 | thetaStart = thetaStart !== undefined ? thetaStart : 0;
|
55 | thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
|
56 |
|
57 |
|
58 |
|
59 | var indices = [];
|
60 | var vertices = [];
|
61 | var normals = [];
|
62 | var uvs = [];
|
63 |
|
64 |
|
65 |
|
66 | var i, s;
|
67 | var vertex = new Vector3();
|
68 | var uv = new Vector2();
|
69 |
|
70 |
|
71 |
|
72 | vertices.push( 0, 0, 0 );
|
73 | normals.push( 0, 0, 1 );
|
74 | uvs.push( 0.5, 0.5 );
|
75 |
|
76 | for ( s = 0, i = 3; s <= segments; s ++, i += 3 ) {
|
77 |
|
78 | var segment = thetaStart + s / segments * thetaLength;
|
79 |
|
80 |
|
81 |
|
82 | vertex.x = radius * Math.cos( segment );
|
83 | vertex.y = radius * Math.sin( segment );
|
84 |
|
85 | vertices.push( vertex.x, vertex.y, vertex.z );
|
86 |
|
87 |
|
88 |
|
89 | normals.push( 0, 0, 1 );
|
90 |
|
91 |
|
92 |
|
93 | uv.x = ( vertices[ i ] / radius + 1 ) / 2;
|
94 | uv.y = ( vertices[ i + 1 ] / radius + 1 ) / 2;
|
95 |
|
96 | uvs.push( uv.x, uv.y );
|
97 |
|
98 | }
|
99 |
|
100 |
|
101 |
|
102 | for ( i = 1; i <= segments; i ++ ) {
|
103 |
|
104 | indices.push( i, i + 1, 0 );
|
105 |
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 | this.setIndex( indices );
|
111 | this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
|
112 | this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
|
113 | this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
|
114 |
|
115 | }
|
116 |
|
117 | CircleBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
|
118 | CircleBufferGeometry.prototype.constructor = CircleBufferGeometry;
|
119 |
|
120 |
|
121 | export { CircleGeometry, CircleBufferGeometry };
|