Geometry.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * @author kile / http://kile.stravaganza.org/
  4. */
  5. THREE.Geometry = function () {
  6. this.vertices = [];
  7. this.faces = [];
  8. this.uvs = [];
  9. };
  10. THREE.Geometry.prototype = {
  11. computeCentroids: function () {
  12. var f, fl, face;
  13. for ( f = 0, fl = this.faces.length; f < fl; f++ ) {
  14. face = this.faces[ f ];
  15. face.centroid.set( 0, 0, 0 );
  16. face.centroid.addSelf( this.vertices[ face.a ].position );
  17. face.centroid.addSelf( this.vertices[ face.b ].position );
  18. face.centroid.addSelf( this.vertices[ face.c ].position );
  19. if ( face instanceof THREE.Face3 ) {
  20. face.centroid.divideScalar( 3 );
  21. } else if ( face instanceof THREE.Face4 ) {
  22. face.centroid.addSelf( this.vertices[ face.d ].position );
  23. face.centroid.divideScalar( 4 );
  24. }
  25. }
  26. },
  27. computeNormals: function ( useVertexNormals ) {
  28. var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC,
  29. cb = new THREE.Vector3(), ab = new THREE.Vector3();
  30. for ( v = 0, vl = this.vertices.length; v < vl; v++ ) {
  31. vertex = this.vertices[ v ];
  32. vertex.normal.set( 0, 0, 0 );
  33. }
  34. for ( f = 0, fl = this.faces.length; f < fl; f++ ) {
  35. face = this.faces[ f ];
  36. if ( useVertexNormals && face.vertexNormals.length ) {
  37. cb.set( 0, 0, 0 );
  38. for ( n = 0, nl = face.normal.length; n < nl; n++ ) {
  39. cb.addSelf( face.vertexNormals[n] );
  40. }
  41. cb.divideScalar( 3 );
  42. if ( ! cb.isZero() ) {
  43. cb.normalize();
  44. }
  45. face.normal.copy( cb );
  46. } else {
  47. vA = this.vertices[ face.a ];
  48. vB = this.vertices[ face.b ];
  49. vC = this.vertices[ face.c ];
  50. cb.sub( vC.position, vB.position );
  51. ab.sub( vA.position, vB.position );
  52. cb.crossSelf( ab );
  53. if ( !cb.isZero() ) {
  54. cb.normalize();
  55. }
  56. face.normal.copy( cb );
  57. }
  58. }
  59. },
  60. computeBoundingBox: function ( ) {
  61. if ( this.vertices.length > 0 ) {
  62. this.bbox = { 'x': [ this.vertices[ 0 ].position.x, this.vertices[ 0 ].position.x ],
  63. 'y': [ this.vertices[ 0 ].position.y, this.vertices[ 0 ].position.y ],
  64. 'z': [ this.vertices[ 0 ].position.z, this.vertices[ 0 ].position.z ] };
  65. for ( var v = 1, vl = this.vertices.length; v < vl; v++ ) {
  66. vertex = this.vertices[ v ];
  67. if ( vertex.position.x < this.bbox.x[ 0 ] ) {
  68. this.bbox.x[ 0 ] = vertex.position.x;
  69. } else if ( vertex.position.x > this.bbox.x[ 1 ] ) {
  70. this.bbox.x[ 1 ] = vertex.position.x;
  71. }
  72. if ( vertex.position.y < this.bbox.y[ 0 ] ) {
  73. this.bbox.y[ 0 ] = vertex.position.y;
  74. } else if ( vertex.position.y > this.bbox.y[ 1 ] ) {
  75. this.bbox.y[ 1 ] = vertex.position.y;
  76. }
  77. if ( vertex.position.z < this.bbox.z[ 0 ] ) {
  78. this.bbox.z[ 0 ] = vertex.position.z;
  79. } else if ( vertex.position.z > this.bbox.z[ 1 ] ) {
  80. this.bbox.z[ 1 ] = vertex.position.z;
  81. }
  82. }
  83. }
  84. },
  85. toString: function () {
  86. return 'THREE.Geometry ( vertices: ' + this.vertices + ', faces: ' + this.faces + ' )';
  87. }
  88. };
粤ICP备19079148号