Sphere.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Sphere.as
  4. */
  5. var Sphere = function ( radius, segments_width, segments_height ) {
  6. THREE.Geometry.call( this );
  7. var gridX = segments_width || 8,
  8. gridY = segments_height || 6;
  9. var i, j;
  10. var iHor = Math.max( 3, gridX );
  11. var iVer = Math.max( 2, gridY );
  12. var aVtc = [];
  13. for ( j = 0; j < ( iVer + 1 ) ; j++ ) {
  14. var fRad1 = j / iVer;
  15. var fZ = radius * Math.cos( fRad1 * Math.PI );
  16. var fRds = radius * Math.sin( fRad1 * Math.PI );
  17. var aRow = [];
  18. var oVtx = 0;
  19. for ( i = 0; i < iHor; i++ ) {
  20. var fRad2 = 2 * i / iHor;
  21. var fX = fRds * Math.sin( fRad2 * Math.PI );
  22. var fY = fRds * Math.cos( fRad2 * Math.PI );
  23. if ( !( ( j == 0 || j == iVer ) && i > 0 ) ) {
  24. oVtx = this.vertices.push( new THREE.Vertex( new THREE.Vector3( fY, fZ, fX ) ) ) - 1;
  25. }
  26. aRow.push( oVtx );
  27. }
  28. aVtc.push( aRow );
  29. }
  30. var iVerNum = aVtc.length;
  31. for ( j = 0; j < iVerNum; j++ ) {
  32. var iHorNum = aVtc[ j ].length;
  33. if ( j > 0 ) {
  34. for ( i = 0; i < iHorNum; i++ ) {
  35. var bEnd = i == ( iHorNum - 1 );
  36. var aP1 = aVtc[ j ][ bEnd ? 0 : i + 1 ];
  37. var aP2 = aVtc[ j ][ ( bEnd ? iHorNum - 1 : i ) ];
  38. var aP3 = aVtc[ j - 1 ][ ( bEnd ? iHorNum - 1 : i ) ];
  39. var aP4 = aVtc[ j - 1 ][ bEnd ? 0 : i + 1 ];
  40. var fJ0 = j / ( iVerNum - 1 );
  41. var fJ1 = ( j - 1 ) / ( iVerNum - 1 );
  42. var fI0 = ( i + 1 ) / iHorNum;
  43. var fI1 = i / iHorNum;
  44. var aP1uv = new THREE.UV( 1 - fI0, fJ0 );
  45. var aP2uv = new THREE.UV( 1 - fI1, fJ0 );
  46. var aP3uv = new THREE.UV( 1 - fI1, fJ1 );
  47. var aP4uv = new THREE.UV( 1 - fI0, fJ1 );
  48. if ( j < ( aVtc.length - 1 ) ) {
  49. this.faces.push( new THREE.Face3( aP1, aP2, aP3 ) );
  50. this.uvs.push( [ aP1uv, aP2uv, aP3uv ] );
  51. }
  52. if ( j > 1 ) {
  53. this.faces.push( new THREE.Face3( aP1, aP3, aP4 ) );
  54. this.uvs.push( [ aP1uv, aP3uv, aP4uv ] );
  55. }
  56. }
  57. }
  58. }
  59. this.computeCentroids();
  60. this.computeNormals();
  61. }
  62. Sphere.prototype = new THREE.Geometry();
  63. Sphere.prototype.constructor = Sphere;