solar-system
Version:
Solar System with Threejs
64 lines (50 loc) • 2.63 kB
JavaScript
/*
* RingsGeometry.js
* @Description Rings' Planet geometry.
* @link https://github.com/kdaimiel/solar-system#readme
* @author Enrique Daimiel Ruiz <k.daimiel@gmail.com>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
THREE.RingsGeometry = function (innerRadius, outerRadius, thetaStart, thetaLength, thetaSegments, phiSegments) {
THREE.Geometry.call( this );
this.type = 'RingsGeometry';
this.innerRadius = innerRadius || 0;
this.outerRadius = outerRadius || 50;
this.thetaStart = thetaStart !== undefined ? thetaStart : 0;
this.thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
this.thetaSegments = thetaSegments !== undefined ? Math.max( 3, thetaSegments ) : 50;
this.phiSegments = phiSegments !== undefined ? Math.max( 1, phiSegments ) : 50;
var i, o, uvs = [], radius = this.innerRadius, radiusStep = ( ( outerRadius - innerRadius ) / this.phiSegments ), segment;
for ( i = 0; i < this.phiSegments + 1; i ++ ) { // concentric circles inside ring
for ( o = 0; o < this.thetaSegments + 1; o ++ ) { // number of segments per circle
var vertex = new THREE.Vector3();
segment = this.thetaStart + o / this.thetaSegments * this.thetaLength;
vertex.x = radius * Math.cos( segment );
vertex.y = radius * Math.sin( segment );
this.vertices.push( vertex );
uvs.push( new THREE.Vector2( i/(this.thetaSegments-1), o/ (this.phiSegments-1) ) );
}
radius += radiusStep;
}
var n = new THREE.Vector3( 0, 0, 1 );
for ( i = 0; i < this.phiSegments; i ++ ) { // concentric circles inside ring
var thetaSegment = i * (this.thetaSegments + 1);
for ( o = 0; o < this.thetaSegments ; o ++ ) { // number of segments per circle
segment = o + thetaSegment;
var v1 = segment;
var v2 = segment + this.thetaSegments + 1;
var v3 = segment + this.thetaSegments + 2;
this.faces.push( new THREE.Face3( v1, v2, v3, [ n.clone(), n.clone(), n.clone() ] ) );
this.faceVertexUvs[ 0 ].push( [ uvs[ v1 ].clone(), uvs[ v2 ].clone(), uvs[ v3 ].clone() ]);
v1 = segment;
v2 = segment + this.thetaSegments + 2;
v3 = segment + 1;
this.faces.push( new THREE.Face3( v1, v2, v3, [ n.clone(), n.clone(), n.clone() ] ) );
this.faceVertexUvs[ 0 ].push( [ uvs[ v1 ].clone(), uvs[ v2 ].clone(), uvs[ v3 ].clone() ]);
}
}
this.computeFaceNormals();
this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius );
};
THREE.RingsGeometry.prototype = Object.create( THREE.RingGeometry.prototype );
THREE.RingsGeometry.prototype.constructor = THREE.RingsGeometry;