TorusGeometry.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @author oosmoxiecode
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author Mugen87 / https://github.com/Mugen87
  5. */
  6. import { Geometry } from '../core/Geometry.js';
  7. import { BufferGeometry } from '../core/BufferGeometry.js';
  8. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  9. import { Vector3 } from '../math/Vector3.js';
  10. // TorusGeometry
  11. function TorusGeometry( radius, tube, radialSegments, tubularSegments, arc ) {
  12. Geometry.call( this );
  13. this.type = 'TorusGeometry';
  14. this.parameters = {
  15. radius: radius,
  16. tube: tube,
  17. radialSegments: radialSegments,
  18. tubularSegments: tubularSegments,
  19. arc: arc
  20. };
  21. this.fromBufferGeometry( new TorusBufferGeometry( radius, tube, radialSegments, tubularSegments, arc ) );
  22. this.mergeVertices();
  23. }
  24. TorusGeometry.prototype = Object.create( Geometry.prototype );
  25. TorusGeometry.prototype.constructor = TorusGeometry;
  26. // TorusBufferGeometry
  27. function TorusBufferGeometry( radius, tube, radialSegments, tubularSegments, arc ) {
  28. BufferGeometry.call( this );
  29. this.type = 'TorusBufferGeometry';
  30. this.parameters = {
  31. radius: radius,
  32. tube: tube,
  33. radialSegments: radialSegments,
  34. tubularSegments: tubularSegments,
  35. arc: arc
  36. };
  37. radius = radius || 1;
  38. tube = tube || 0.4;
  39. radialSegments = Math.floor( radialSegments ) || 8;
  40. tubularSegments = Math.floor( tubularSegments ) || 6;
  41. arc = arc || Math.PI * 2;
  42. // buffers
  43. var indices = [];
  44. var vertices = [];
  45. var normals = [];
  46. var uvs = [];
  47. // helper variables
  48. var center = new Vector3();
  49. var vertex = new Vector3();
  50. var normal = new Vector3();
  51. var j, i;
  52. // generate vertices, normals and uvs
  53. for ( j = 0; j <= radialSegments; j ++ ) {
  54. for ( i = 0; i <= tubularSegments; i ++ ) {
  55. var u = i / tubularSegments * arc;
  56. var v = j / radialSegments * Math.PI * 2;
  57. // vertex
  58. vertex.x = ( radius + tube * Math.cos( v ) ) * Math.cos( u );
  59. vertex.y = ( radius + tube * Math.cos( v ) ) * Math.sin( u );
  60. vertex.z = tube * Math.sin( v );
  61. vertices.push( vertex.x, vertex.y, vertex.z );
  62. // normal
  63. center.x = radius * Math.cos( u );
  64. center.y = radius * Math.sin( u );
  65. normal.subVectors( vertex, center ).normalize();
  66. normals.push( normal.x, normal.y, normal.z );
  67. // uv
  68. uvs.push( i / tubularSegments );
  69. uvs.push( j / radialSegments );
  70. }
  71. }
  72. // generate indices
  73. for ( j = 1; j <= radialSegments; j ++ ) {
  74. for ( i = 1; i <= tubularSegments; i ++ ) {
  75. // indices
  76. var a = ( tubularSegments + 1 ) * j + i - 1;
  77. var b = ( tubularSegments + 1 ) * ( j - 1 ) + i - 1;
  78. var c = ( tubularSegments + 1 ) * ( j - 1 ) + i;
  79. var d = ( tubularSegments + 1 ) * j + i;
  80. // faces
  81. indices.push( a, b, d );
  82. indices.push( b, c, d );
  83. }
  84. }
  85. // build geometry
  86. this.setIndex( indices );
  87. this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  88. this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  89. this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  90. }
  91. TorusBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  92. TorusBufferGeometry.prototype.constructor = TorusBufferGeometry;
  93. export { TorusGeometry, TorusBufferGeometry };
粤ICP备19079148号