UNPKG

2.68 kBJavaScriptView Raw
1/**
2 * @author benaadams / https://twitter.com/ben_a_adams
3 * @author Mugen87 / https://github.com/Mugen87
4 * @author hughes
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';
11import { Vector2 } from '../math/Vector2.js';
12
13// CircleGeometry
14
15function 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
33CircleGeometry.prototype = Object.create( Geometry.prototype );
34CircleGeometry.prototype.constructor = CircleGeometry;
35
36// CircleBufferGeometry
37
38function 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 // buffers
58
59 var indices = [];
60 var vertices = [];
61 var normals = [];
62 var uvs = [];
63
64 // helper variables
65
66 var i, s;
67 var vertex = new Vector3();
68 var uv = new Vector2();
69
70 // center point
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 // vertex
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 // normal
88
89 normals.push( 0, 0, 1 );
90
91 // uvs
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 // indices
101
102 for ( i = 1; i <= segments; i ++ ) {
103
104 indices.push( i, i + 1, 0 );
105
106 }
107
108 // build geometry
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
117CircleBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
118CircleBufferGeometry.prototype.constructor = CircleBufferGeometry;
119
120
121export { CircleGeometry, CircleBufferGeometry };