Files
three.js/examples/primitives/Sphere.js
T
Mr.doob a7bed157a3 - Added Sphere primitive
- `three_debug.js` checks for broken UVs and display these faces with pink color instead
2010-07-19 13:23:06 +01:00

93 lines
2.0 KiB
JavaScript

/**
* @author mr.doob / http://mrdoob.com/
* based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Sphere.as
*/
var Sphere = function ( radius, segments_width, segments_height ) {
THREE.Geometry.call( this );
var gridX = segments_width || 8,
gridY = segments_height || 6;
var i, j;
var iHor = Math.max( 3, gridX );
var iVer = Math.max( 2, gridY );
var aVtc = [];
for ( j = 0; j < ( iVer + 1 ) ; j++ ) {
var fRad1 = j / iVer;
var fZ = radius * Math.cos( fRad1 * Math.PI );
var fRds = radius * Math.sin( fRad1 * Math.PI );
var aRow = [];
var oVtx = 0;
for ( i = 0; i < iHor; i++ ) {
var fRad2 = 2 * i / iHor;
var fX = fRds * Math.sin( fRad2 * Math.PI );
var fY = fRds * Math.cos( fRad2 * Math.PI );
if ( !( ( j == 0 || j == iVer ) && i > 0 ) ) {
oVtx = this.vertices.push( new THREE.Vertex( new THREE.Vector3( fY, fZ, fX ) ) ) - 1;
}
aRow.push( oVtx );
}
aVtc.push( aRow );
}
var iVerNum = aVtc.length;
for ( j = 0; j < iVerNum; j++ ) {
var iHorNum = aVtc[ j ].length;
if ( j > 0 ) {
for ( i = 0; i < iHorNum; i++ ) {
var bEnd = i == ( iHorNum - 1 );
var aP1 = aVtc[ j ][ bEnd ? 0 : i + 1 ];
var aP2 = aVtc[ j ][ ( bEnd ? iHorNum - 1 : i ) ];
var aP3 = aVtc[ j - 1 ][ ( bEnd ? iHorNum - 1 : i ) ];
var aP4 = aVtc[ j - 1 ][ bEnd ? 0 : i + 1 ];
var fJ0 = j / ( iVerNum - 1 );
var fJ1 = ( j - 1 ) / ( iVerNum - 1 );
var fI0 = ( i + 1 ) / iHorNum;
var fI1 = i / iHorNum;
var aP1uv = new THREE.UV( 1 - fI0, fJ0 );
var aP2uv = new THREE.UV( 1 - fI1, fJ0 );
var aP3uv = new THREE.UV( 1 - fI1, fJ1 );
var aP4uv = new THREE.UV( 1 - fI0, fJ1 );
if ( j < ( aVtc.length - 1 ) ) {
this.faces.push( new THREE.Face3( aP1, aP2, aP3 ) );
this.uvs.push( [ aP1uv, aP2uv, aP3uv ] );
}
if ( j > 1 ) {
this.faces.push( new THREE.Face3( aP1, aP3, aP4 ) );
this.uvs.push( [ aP1uv, aP3uv, aP4uv ] );
}
}
}
}
}
Sphere.prototype = new THREE.Geometry();
Sphere.prototype.constructor = Sphere;