Sphere.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, smooth ) {
  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 n1, n2, n3, 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. n1 = this.vertices[ aP1 ].position.clone();
  50. n2 = this.vertices[ aP2 ].position.clone();
  51. n3 = this.vertices[ aP3 ].position.clone();
  52. n1.normalize();
  53. n2.normalize();
  54. n3.normalize();
  55. if ( smooth ) {
  56. this.faces.push( new THREE.Face3( aP1, aP2, aP3, [ new THREE.Vector3( n1.x, n1.y, n1.z ), new THREE.Vector3( n2.x, n2.y, n2.z ), new THREE.Vector3( n3.x, n3.y, n3.z ) ] ) );
  57. } else {
  58. this.faces.push( new THREE.Face3( aP1, aP2, aP3 ) );
  59. }
  60. this.uvs.push( [ aP1uv, aP2uv, aP3uv ] );
  61. }
  62. if ( j > 1 ) {
  63. n1 = this.vertices[aP1].position.clone();
  64. n2 = this.vertices[aP3].position.clone();
  65. n3 = this.vertices[aP4].position.clone();
  66. n1.normalize();
  67. n2.normalize();
  68. n3.normalize();
  69. if ( smooth ) {
  70. this.faces.push( new THREE.Face3( aP1, aP3, aP4, [ new THREE.Vector3( n1.x, n1.y, n1.z ), new THREE.Vector3( n2.x, n2.y, n2.z ), new THREE.Vector3( n3.x, n3.y, n3.z ) ] ) );
  71. } else {
  72. this.faces.push( new THREE.Face3( aP1, aP3, aP4 ) );
  73. }
  74. this.uvs.push( [ aP1uv, aP3uv, aP4uv ] );
  75. }
  76. }
  77. }
  78. }
  79. this.computeCentroids();
  80. this.computeNormals();
  81. }
  82. Sphere.prototype = new THREE.Geometry();
  83. Sphere.prototype.constructor = Sphere;