GeometryUtils.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.GeometryUtils = {
  6. // Merge two geometries or geometry and geometry from object (using object's transform)
  7. merge: function ( geometry1, object2 /* mesh | geometry */, materialIndexOffset ) {
  8. var matrix, normalMatrix,
  9. vertexOffset = geometry1.vertices.length,
  10. uvPosition = geometry1.faceVertexUvs[ 0 ].length,
  11. geometry2 = object2 instanceof THREE.Mesh ? object2.geometry : object2,
  12. vertices1 = geometry1.vertices,
  13. vertices2 = geometry2.vertices,
  14. faces1 = geometry1.faces,
  15. faces2 = geometry2.faces,
  16. uvs1 = geometry1.faceVertexUvs[ 0 ],
  17. uvs2 = geometry2.faceVertexUvs[ 0 ];
  18. if ( materialIndexOffset === undefined ) materialIndexOffset = 0;
  19. if ( object2 instanceof THREE.Mesh ) {
  20. object2.matrixAutoUpdate && object2.updateMatrix();
  21. matrix = object2.matrix;
  22. normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  23. }
  24. // vertices
  25. for ( var i = 0, il = vertices2.length; i < il; i ++ ) {
  26. var vertex = vertices2[ i ];
  27. var vertexCopy = vertex.clone();
  28. if ( matrix ) vertexCopy.applyMatrix4( matrix );
  29. vertices1.push( vertexCopy );
  30. }
  31. // faces
  32. for ( i = 0, il = faces2.length; i < il; i ++ ) {
  33. var face = faces2[ i ], faceCopy, normal, color,
  34. faceVertexNormals = face.vertexNormals,
  35. faceVertexColors = face.vertexColors;
  36. faceCopy = new THREE.Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
  37. faceCopy.normal.copy( face.normal );
  38. if ( normalMatrix ) {
  39. faceCopy.normal.applyMatrix3( normalMatrix ).normalize();
  40. }
  41. for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
  42. normal = faceVertexNormals[ j ].clone();
  43. if ( normalMatrix ) {
  44. normal.applyMatrix3( normalMatrix ).normalize();
  45. }
  46. faceCopy.vertexNormals.push( normal );
  47. }
  48. faceCopy.color.copy( face.color );
  49. for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
  50. color = faceVertexColors[ j ];
  51. faceCopy.vertexColors.push( color.clone() );
  52. }
  53. faceCopy.materialIndex = face.materialIndex + materialIndexOffset;
  54. faces1.push( faceCopy );
  55. }
  56. // uvs
  57. for ( i = 0, il = uvs2.length; i < il; i ++ ) {
  58. var uv = uvs2[ i ], uvCopy = [];
  59. if ( uv === undefined ) {
  60. continue;
  61. }
  62. for ( var j = 0, jl = uv.length; j < jl; j ++ ) {
  63. uvCopy.push( new THREE.Vector2( uv[ j ].x, uv[ j ].y ) );
  64. }
  65. uvs1.push( uvCopy );
  66. }
  67. },
  68. // Get random point in triangle (via barycentric coordinates)
  69. // (uniform distribution)
  70. // http://www.cgafaq.info/wiki/Random_Point_In_Triangle
  71. randomPointInTriangle: function () {
  72. var vector = new THREE.Vector3();
  73. return function ( vectorA, vectorB, vectorC ) {
  74. var point = new THREE.Vector3();
  75. var a = THREE.Math.random16();
  76. var b = THREE.Math.random16();
  77. if ( ( a + b ) > 1 ) {
  78. a = 1 - a;
  79. b = 1 - b;
  80. }
  81. var c = 1 - a - b;
  82. point.copy( vectorA );
  83. point.multiplyScalar( a );
  84. vector.copy( vectorB );
  85. vector.multiplyScalar( b );
  86. point.add( vector );
  87. vector.copy( vectorC );
  88. vector.multiplyScalar( c );
  89. point.add( vector );
  90. return point;
  91. };
  92. }(),
  93. // Get random point in face (triangle / quad)
  94. // (uniform distribution)
  95. randomPointInFace: function ( face, geometry, useCachedAreas ) {
  96. var vA, vB, vC, vD;
  97. vA = geometry.vertices[ face.a ];
  98. vB = geometry.vertices[ face.b ];
  99. vC = geometry.vertices[ face.c ];
  100. return THREE.GeometryUtils.randomPointInTriangle( vA, vB, vC );
  101. },
  102. // Get uniformly distributed random points in mesh
  103. // - create array with cumulative sums of face areas
  104. // - pick random number from 0 to total area
  105. // - find corresponding place in area array by binary search
  106. // - get random point in face
  107. randomPointsInGeometry: function ( geometry, n ) {
  108. var face, i,
  109. faces = geometry.faces,
  110. vertices = geometry.vertices,
  111. il = faces.length,
  112. totalArea = 0,
  113. cumulativeAreas = [],
  114. vA, vB, vC, vD;
  115. // precompute face areas
  116. for ( i = 0; i < il; i ++ ) {
  117. face = faces[ i ];
  118. vA = vertices[ face.a ];
  119. vB = vertices[ face.b ];
  120. vC = vertices[ face.c ];
  121. face._area = THREE.GeometryUtils.triangleArea( vA, vB, vC );
  122. totalArea += face._area;
  123. cumulativeAreas[ i ] = totalArea;
  124. }
  125. // binary search cumulative areas array
  126. function binarySearchIndices( value ) {
  127. function binarySearch( start, end ) {
  128. // return closest larger index
  129. // if exact number is not found
  130. if ( end < start )
  131. return start;
  132. var mid = start + Math.floor( ( end - start ) / 2 );
  133. if ( cumulativeAreas[ mid ] > value ) {
  134. return binarySearch( start, mid - 1 );
  135. } else if ( cumulativeAreas[ mid ] < value ) {
  136. return binarySearch( mid + 1, end );
  137. } else {
  138. return mid;
  139. }
  140. }
  141. var result = binarySearch( 0, cumulativeAreas.length - 1 )
  142. return result;
  143. }
  144. // pick random face weighted by face area
  145. var r, index,
  146. result = [];
  147. var stats = {};
  148. for ( i = 0; i < n; i ++ ) {
  149. r = THREE.Math.random16() * totalArea;
  150. index = binarySearchIndices( r );
  151. result[ i ] = THREE.GeometryUtils.randomPointInFace( faces[ index ], geometry, true );
  152. if ( ! stats[ index ] ) {
  153. stats[ index ] = 1;
  154. } else {
  155. stats[ index ] += 1;
  156. }
  157. }
  158. return result;
  159. },
  160. // Get triangle area (half of parallelogram)
  161. // http://mathworld.wolfram.com/TriangleArea.html
  162. triangleArea: function () {
  163. var vector1 = new THREE.Vector3();
  164. var vector2 = new THREE.Vector3();
  165. return function ( vectorA, vectorB, vectorC ) {
  166. vector1.subVectors( vectorB, vectorA );
  167. vector2.subVectors( vectorC, vectorA );
  168. vector1.cross( vector2 );
  169. return 0.5 * vector1.length();
  170. };
  171. }(),
  172. // Center geometry so that 0,0,0 is in center of bounding box
  173. center: function ( geometry ) {
  174. geometry.computeBoundingBox();
  175. var bb = geometry.boundingBox;
  176. var offset = new THREE.Vector3();
  177. offset.addVectors( bb.min, bb.max );
  178. offset.multiplyScalar( -0.5 );
  179. geometry.applyMatrix( new THREE.Matrix4().makeTranslation( offset.x, offset.y, offset.z ) );
  180. geometry.computeBoundingBox();
  181. return offset;
  182. }
  183. };
粤ICP备19079148号