RingGeometry.js 3.5 KB
Newer Older
1 2
/**
 * @author Kaleb Murphy
K
Kaleb Murphy 已提交
3 4
 */

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

M
Mr.doob 已提交
7
function RingGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) {
K
Kaleb Murphy 已提交
8

R
Rich Harris 已提交
9
	Geometry.call( this );
K
Kaleb Murphy 已提交
10

11 12 13 14 15 16 17 18 19 20 21
	this.type = 'RingGeometry';

	this.parameters = {
		innerRadius: innerRadius,
		outerRadius: outerRadius,
		thetaSegments: thetaSegments,
		phiSegments: phiSegments,
		thetaStart: thetaStart,
		thetaLength: thetaLength
	};

R
Rich Harris 已提交
22
	this.fromBufferGeometry( new RingBufferGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) );
K
Kaleb Murphy 已提交
23

M
Mr.doob 已提交
24
}
K
Kaleb Murphy 已提交
25

R
Rich Harris 已提交
26 27 28
RingGeometry.prototype = Object.create( Geometry.prototype );
RingGeometry.prototype.constructor = RingGeometry;

M
Mr.doob 已提交
29 30 31 32
/**
 * @author Mugen87 / https://github.com/Mugen87
 */

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

function RingBufferGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) {

	BufferGeometry.call( this );

	this.type = 'RingBufferGeometry';

	this.parameters = {
		innerRadius: innerRadius,
		outerRadius: outerRadius,
		thetaSegments: thetaSegments,
		phiSegments: phiSegments,
		thetaStart: thetaStart,
		thetaLength: thetaLength
	};

	innerRadius = innerRadius || 20;
	outerRadius = outerRadius || 50;

	thetaStart = thetaStart !== undefined ? thetaStart : 0;
	thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;

	thetaSegments = thetaSegments !== undefined ? Math.max( 3, thetaSegments ) : 8;
	phiSegments = phiSegments !== undefined ? Math.max( 1, phiSegments ) : 1;

	// buffers
M
Mugen87 已提交
63 64 65 66 67

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

	// some helper variables
M
Mugen87 已提交
70 71

	var segment;
M
Mr.doob 已提交
72 73 74 75 76 77 78 79 80 81 82 83
	var radius = innerRadius;
	var radiusStep = ( ( outerRadius - innerRadius ) / phiSegments );
	var vertex = new Vector3();
	var uv = new Vector2();
	var j, i;

	// generate vertices, normals and uvs

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

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

M
Mugen87 已提交
84 85
			// values are generate from the inside of the ring to the outside

M
Mr.doob 已提交
86 87 88
			segment = thetaStart + i / thetaSegments * thetaLength;

			// vertex
M
Mugen87 已提交
89

M
Mr.doob 已提交
90 91
			vertex.x = radius * Math.cos( segment );
			vertex.y = radius * Math.sin( segment );
M
Mugen87 已提交
92 93

			vertices.push( vertex.x, vertex.y, vertex.z );
M
Mr.doob 已提交
94 95

			// normal
M
Mugen87 已提交
96 97

			normals.push( 0, 0, 1 );
M
Mr.doob 已提交
98 99

			// uv
M
Mugen87 已提交
100
			
M
Mr.doob 已提交
101 102 103
			uv.x = ( vertex.x / outerRadius + 1 ) / 2;
			uv.y = ( vertex.y / outerRadius + 1 ) / 2;

M
Mugen87 已提交
104
			uvs.push( uv.x, uv.y );
M
Mr.doob 已提交
105 106 107 108

		}

		// increase the radius for next row of vertices
M
Mugen87 已提交
109

M
Mr.doob 已提交
110 111 112 113
		radius += radiusStep;

	}

M
Mugen87 已提交
114
	// indices
M
Mr.doob 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128

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

		var thetaSegmentLevel = j * ( thetaSegments + 1 );

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

			segment = i + thetaSegmentLevel;

			var a = segment;
			var b = segment + thetaSegments + 1;
			var c = segment + thetaSegments + 2;
			var d = segment + 1;

M
Mugen87 已提交
129
			// faces
M
Mr.doob 已提交
130

M
Mugen87 已提交
131 132
			indices.push( a, b, d );
			indices.push( b, c, d );
M
Mr.doob 已提交
133 134 135 136 137 138 139

		}

	}

	// build geometry

140
	this.setIndex( new ( vertices.length > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 ) );
M
Mugen87 已提交
141 142 143
	this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
	this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
	this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
M
Mr.doob 已提交
144 145 146 147 148

}

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

M
Mr.doob 已提交
150
export { RingGeometry, RingBufferGeometry };