GeometryUtils.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.GeometryUtils = {
  6. merge: function ( geometry1, object2 /* mesh | geometry */ ) {
  7. var matrix, matrixRotation,
  8. vertexOffset = geometry1.vertices.length,
  9. uvPosition = geometry1.faceVertexUvs[ 0 ].length,
  10. geometry2 = object2 instanceof THREE.Mesh ? object2.geometry : object2,
  11. vertices1 = geometry1.vertices,
  12. vertices2 = geometry2.vertices,
  13. faces1 = geometry1.faces,
  14. faces2 = geometry2.faces,
  15. uvs1 = geometry1.faceVertexUvs[ 0 ],
  16. uvs2 = geometry2.faceVertexUvs[ 0 ];
  17. if ( object2 instanceof THREE.Mesh ) {
  18. object2.matrixAutoUpdate && object2.updateMatrix();
  19. matrix = object2.matrix;
  20. matrixRotation = new THREE.Matrix4();
  21. matrixRotation.extractRotation( matrix, object2.scale );
  22. }
  23. // vertices
  24. for ( var i = 0, il = vertices2.length; i < il; i ++ ) {
  25. var vertex = vertices2[ i ];
  26. var vertexCopy = new THREE.Vertex( vertex.position.clone() );
  27. if ( matrix ) matrix.multiplyVector3( vertexCopy.position );
  28. vertices1.push( vertexCopy );
  29. }
  30. // faces
  31. for ( i = 0, il = faces2.length; i < il; i ++ ) {
  32. var face = faces2[ i ], faceCopy, normal, color,
  33. faceVertexNormals = face.vertexNormals,
  34. faceVertexColors = face.vertexColors;
  35. if ( face instanceof THREE.Face3 ) {
  36. faceCopy = new THREE.Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
  37. } else if ( face instanceof THREE.Face4 ) {
  38. faceCopy = new THREE.Face4( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset, face.d + vertexOffset );
  39. }
  40. faceCopy.normal.copy( face.normal );
  41. if ( matrixRotation ) matrixRotation.multiplyVector3( faceCopy.normal );
  42. for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
  43. normal = faceVertexNormals[ j ].clone();
  44. if ( matrixRotation ) matrixRotation.multiplyVector3( normal );
  45. faceCopy.vertexNormals.push( normal );
  46. }
  47. faceCopy.color.copy( face.color );
  48. for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
  49. color = faceVertexColors[ j ];
  50. faceCopy.vertexColors.push( color.clone() );
  51. }
  52. faceCopy.materials = face.materials.slice();
  53. faceCopy.centroid.copy( face.centroid );
  54. if ( matrix ) matrix.multiplyVector3( faceCopy.centroid );
  55. faces1.push( faceCopy );
  56. }
  57. // uvs
  58. for ( i = 0, il = uvs2.length; i < il; i ++ ) {
  59. var uv = uvs2[ i ], uvCopy = [];
  60. for ( var j = 0, jl = uv.length; j < jl; j ++ ) {
  61. uvCopy.push( new THREE.UV( uv[ j ].u, uv[ j ].v ) );
  62. }
  63. uvs1.push( uvCopy );
  64. }
  65. },
  66. clone: function ( geometry ) {
  67. var cloneGeo = new THREE.Geometry();
  68. var i, il;
  69. var vertices = geometry.vertices,
  70. faces = geometry.faces,
  71. uvs = geometry.faceVertexUvs[ 0 ];
  72. // vertices
  73. for ( i = 0, il = vertices.length; i < il; i ++ ) {
  74. var vertex = vertices[ i ];
  75. var vertexCopy = new THREE.Vertex( vertex.position.clone() );
  76. cloneGeo.vertices.push( vertexCopy );
  77. }
  78. // faces
  79. for ( i = 0, il = faces.length; i < il; i ++ ) {
  80. var face = faces[ i ], faceCopy, normal, color,
  81. faceVertexNormals = face.vertexNormals,
  82. faceVertexColors = face.vertexColors;
  83. if ( face instanceof THREE.Face3 ) {
  84. faceCopy = new THREE.Face3( face.a, face.b, face.c );
  85. } else if ( face instanceof THREE.Face4 ) {
  86. faceCopy = new THREE.Face4( face.a, face.b, face.c, face.d );
  87. }
  88. faceCopy.normal.copy( face.normal );
  89. for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
  90. normal = faceVertexNormals[ j ];
  91. faceCopy.vertexNormals.push( normal.clone() );
  92. }
  93. faceCopy.color.copy( face.color );
  94. for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
  95. color = faceVertexColors[ j ];
  96. faceCopy.vertexColors.push( color.clone() );
  97. }
  98. faceCopy.materials = face.materials.slice();
  99. faceCopy.centroid.copy( face.centroid );
  100. cloneGeo.faces.push( faceCopy );
  101. }
  102. // uvs
  103. for ( i = 0, il = uvs.length; i < il; i ++ ) {
  104. var uv = uvs[ i ], uvCopy = [];
  105. for ( var j = 0, jl = uv.length; j < jl; j ++ ) {
  106. uvCopy.push( new THREE.UV( uv[ j ].u, uv[ j ].v ) );
  107. }
  108. cloneGeo.faceVertexUvs[ 0 ].push( uvCopy );
  109. }
  110. return cloneGeo;
  111. },
  112. // Get random point in triangle (via barycentric coordinates)
  113. // (uniform distribution)
  114. // http://www.cgafaq.info/wiki/Random_Point_In_Triangle
  115. randomPointInTriangle: function( vectorA, vectorB, vectorC ) {
  116. var a, b, c,
  117. point = new THREE.Vector3(),
  118. tmp = THREE.GeometryUtils.__v1;
  119. a = THREE.GeometryUtils.random();
  120. b = THREE.GeometryUtils.random();
  121. if ( ( a + b ) > 1 ) {
  122. a = 1 - a;
  123. b = 1 - b;
  124. }
  125. c = 1 - a - b;
  126. point.copy( vectorA );
  127. point.multiplyScalar( a );
  128. tmp.copy( vectorB );
  129. tmp.multiplyScalar( b );
  130. point.addSelf( tmp );
  131. tmp.copy( vectorC );
  132. tmp.multiplyScalar( c );
  133. point.addSelf( tmp );
  134. return point;
  135. },
  136. // Get random point in face (triangle / quad)
  137. // (uniform distribution)
  138. randomPointInFace: function( face, geometry, useCachedAreas ) {
  139. var vA, vB, vC, vD;
  140. if ( face instanceof THREE.Face3 ) {
  141. vA = geometry.vertices[ face.a ].position;
  142. vB = geometry.vertices[ face.b ].position;
  143. vC = geometry.vertices[ face.c ].position;
  144. return THREE.GeometryUtils.randomPointInTriangle( vA, vB, vC );
  145. } else if ( face instanceof THREE.Face4 ) {
  146. vA = geometry.vertices[ face.a ].position;
  147. vB = geometry.vertices[ face.b ].position;
  148. vC = geometry.vertices[ face.c ].position;
  149. vD = geometry.vertices[ face.d ].position;
  150. var area1, area2;
  151. if ( useCachedAreas ) {
  152. if ( face._area1 && face._area2 ) {
  153. area1 = face._area1;
  154. area2 = face._area2;
  155. } else {
  156. area1 = THREE.GeometryUtils.triangleArea( vA, vB, vD );
  157. area2 = THREE.GeometryUtils.triangleArea( vB, vC, vD );
  158. face._area1 = area1;
  159. face._area2 = area2;
  160. }
  161. } else {
  162. area1 = THREE.GeometryUtils.triangleArea( vA, vB, vD ),
  163. area2 = THREE.GeometryUtils.triangleArea( vB, vC, vD );
  164. }
  165. var r = THREE.GeometryUtils.random() * ( area1 + area2 );
  166. if ( r < area1 ) {
  167. return THREE.GeometryUtils.randomPointInTriangle( vA, vB, vD );
  168. } else {
  169. return THREE.GeometryUtils.randomPointInTriangle( vB, vC, vD );
  170. }
  171. }
  172. },
  173. // Get uniformly distributed random points in mesh
  174. // - create array with cumulative sums of face areas
  175. // - pick random number from 0 to total area
  176. // - find corresponding place in area array by binary search
  177. // - get random point in face
  178. randomPointsInGeometry: function( geometry, n ) {
  179. var face, i,
  180. faces = geometry.faces,
  181. vertices = geometry.vertices,
  182. il = faces.length,
  183. totalArea = 0,
  184. cumulativeAreas = [],
  185. vA, vB, vC, vD;
  186. // precompute face areas
  187. for ( i = 0; i < il; i ++ ) {
  188. face = faces[ i ];
  189. if ( face instanceof THREE.Face3 ) {
  190. vA = vertices[ face.a ].position;
  191. vB = vertices[ face.b ].position;
  192. vC = vertices[ face.c ].position;
  193. face._area = THREE.GeometryUtils.triangleArea( vA, vB, vC );
  194. } else if ( face instanceof THREE.Face4 ) {
  195. vA = vertices[ face.a ].position;
  196. vB = vertices[ face.b ].position;
  197. vC = vertices[ face.c ].position;
  198. vD = vertices[ face.d ].position;
  199. face._area1 = THREE.GeometryUtils.triangleArea( vA, vB, vD );
  200. face._area2 = THREE.GeometryUtils.triangleArea( vB, vC, vD );
  201. face._area = face._area1 + face._area2;
  202. }
  203. totalArea += face._area;
  204. cumulativeAreas[ i ] = totalArea;
  205. }
  206. // binary search cumulative areas array
  207. function binarySearchIndices( value ) {
  208. function binarySearch( start, end ) {
  209. // return closest larger index
  210. // if exact number is not found
  211. if ( end < start )
  212. return start;
  213. var mid = start + Math.floor( ( end - start ) / 2 );
  214. if ( cumulativeAreas[ mid ] > value ) {
  215. return binarySearch( start, mid - 1 );
  216. } else if ( cumulativeAreas[ mid ] < value ) {
  217. return binarySearch( mid + 1, end );
  218. } else {
  219. return mid;
  220. }
  221. }
  222. var result = binarySearch( 0, cumulativeAreas.length - 1 )
  223. return result;
  224. }
  225. // pick random face weighted by face area
  226. var r, index,
  227. result = [];
  228. var stats = {};
  229. for ( i = 0; i < n; i ++ ) {
  230. r = THREE.GeometryUtils.random() * totalArea;
  231. index = binarySearchIndices( r );
  232. result[ i ] = THREE.GeometryUtils.randomPointInFace( faces[ index ], geometry, true );
  233. if ( ! stats[ index ] ) {
  234. stats[ index ] = 1;
  235. } else {
  236. stats[ index ] += 1;
  237. }
  238. }
  239. return result;
  240. },
  241. // Get triangle area (by Heron's formula)
  242. // http://en.wikipedia.org/wiki/Heron%27s_formula
  243. triangleArea: function( vectorA, vectorB, vectorC ) {
  244. var s, a, b, c,
  245. tmp = THREE.GeometryUtils.__v1;
  246. tmp.sub( vectorA, vectorB );
  247. a = tmp.length();
  248. tmp.sub( vectorA, vectorC );
  249. b = tmp.length();
  250. tmp.sub( vectorB, vectorC );
  251. c = tmp.length();
  252. s = 0.5 * ( a + b + c );
  253. return Math.sqrt( s * ( s - a ) * ( s - b ) * ( s - c ) );
  254. },
  255. // Get 16 bits of randomness
  256. // (standard Math.random() creates repetitive patterns when applied over larger space)
  257. random16: function() {
  258. return ( 65280 * Math.random() + 255 * Math.random() ) / 65535;
  259. },
  260. center: function( geometry ) {
  261. geometry.computeBoundingBox();
  262. var matrix = new THREE.Matrix4();
  263. var dx = -0.5 * ( geometry.boundingBox.x[ 1 ] + geometry.boundingBox.x[ 0 ] );
  264. var dy = -0.5 * ( geometry.boundingBox.y[ 1 ] + geometry.boundingBox.y[ 0 ] );
  265. var dz = -0.5 * ( geometry.boundingBox.z[ 1 ] + geometry.boundingBox.z[ 0 ] );
  266. matrix.setTranslation( dx, dy, dz );
  267. geometry.applyMatrix( matrix );
  268. geometry.computeBoundingBox();
  269. }
  270. };
  271. THREE.GeometryUtils.random = THREE.GeometryUtils.random16;
  272. THREE.GeometryUtils.__v1 = new THREE.Vector3();
粤ICP备19079148号