Mesh.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. import { Vector3 } from '../math/Vector3.js';
  2. import { Vector2 } from '../math/Vector2.js';
  3. import { Sphere } from '../math/Sphere.js';
  4. import { Ray } from '../math/Ray.js';
  5. import { Matrix4 } from '../math/Matrix4.js';
  6. import { Object3D } from '../core/Object3D.js';
  7. import { Triangle } from '../math/Triangle.js';
  8. import { Face3 } from '../core/Face3.js';
  9. import { DoubleSide, BackSide, TrianglesDrawMode } from '../constants.js';
  10. import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
  11. import { BufferGeometry } from '../core/BufferGeometry.js';
  12. /**
  13. * @author mrdoob / http://mrdoob.com/
  14. * @author alteredq / http://alteredqualia.com/
  15. * @author mikael emtinger / http://gomo.se/
  16. * @author jonobr1 / http://jonobr1.com/
  17. */
  18. var _inverseMatrix = new Matrix4();
  19. var _ray = new Ray();
  20. var _sphere = new Sphere();
  21. var _vA = new Vector3();
  22. var _vB = new Vector3();
  23. var _vC = new Vector3();
  24. var _tempA = new Vector3();
  25. var _tempB = new Vector3();
  26. var _tempC = new Vector3();
  27. var _morphA = new Vector3();
  28. var _morphB = new Vector3();
  29. var _morphC = new Vector3();
  30. var _uvA = new Vector2();
  31. var _uvB = new Vector2();
  32. var _uvC = new Vector2();
  33. var _intersectionPoint = new Vector3();
  34. var _intersectionPointWorld = new Vector3();
  35. function Mesh( geometry, material ) {
  36. Object3D.call( this );
  37. this.type = 'Mesh';
  38. this.geometry = geometry !== undefined ? geometry : new BufferGeometry();
  39. this.material = material !== undefined ? material : new MeshBasicMaterial( { color: Math.random() * 0xffffff } );
  40. this.drawMode = TrianglesDrawMode;
  41. this.updateMorphTargets();
  42. }
  43. Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
  44. constructor: Mesh,
  45. isMesh: true,
  46. setDrawMode: function ( value ) {
  47. this.drawMode = value;
  48. },
  49. copy: function ( source ) {
  50. Object3D.prototype.copy.call( this, source );
  51. this.drawMode = source.drawMode;
  52. if ( source.morphTargetInfluences !== undefined ) {
  53. this.morphTargetInfluences = source.morphTargetInfluences.slice();
  54. }
  55. if ( source.morphTargetDictionary !== undefined ) {
  56. this.morphTargetDictionary = Object.assign( {}, source.morphTargetDictionary );
  57. }
  58. return this;
  59. },
  60. updateMorphTargets: function () {
  61. var geometry = this.geometry;
  62. var m, ml, name;
  63. if ( geometry.isBufferGeometry ) {
  64. var morphAttributes = geometry.morphAttributes;
  65. var keys = Object.keys( morphAttributes );
  66. if ( keys.length > 0 ) {
  67. var morphAttribute = morphAttributes[ keys[ 0 ] ];
  68. if ( morphAttribute !== undefined ) {
  69. this.morphTargetInfluences = [];
  70. this.morphTargetDictionary = {};
  71. for ( m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  72. name = morphAttribute[ m ].name || String( m );
  73. this.morphTargetInfluences.push( 0 );
  74. this.morphTargetDictionary[ name ] = m;
  75. }
  76. }
  77. }
  78. } else {
  79. var morphTargets = geometry.morphTargets;
  80. if ( morphTargets !== undefined && morphTargets.length > 0 ) {
  81. console.error( 'THREE.Mesh.updateMorphTargets() no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' );
  82. }
  83. }
  84. },
  85. raycast: function ( raycaster, intersects ) {
  86. var geometry = this.geometry;
  87. var material = this.material;
  88. var matrixWorld = this.matrixWorld;
  89. if ( material === undefined ) return;
  90. // Checking boundingSphere distance to ray
  91. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  92. _sphere.copy( geometry.boundingSphere );
  93. _sphere.applyMatrix4( matrixWorld );
  94. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  95. //
  96. _inverseMatrix.getInverse( matrixWorld );
  97. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  98. // Check boundingBox before continuing
  99. if ( geometry.boundingBox !== null ) {
  100. if ( _ray.intersectsBox( geometry.boundingBox ) === false ) return;
  101. }
  102. // check unsupported draw modes
  103. if ( this.drawMode !== TrianglesDrawMode ) {
  104. console.warn( 'THREE.Mesh: TriangleStripDrawMode and TriangleFanDrawMode are not supported by .raycast().' );
  105. return;
  106. }
  107. var intersection;
  108. if ( geometry.isBufferGeometry ) {
  109. var a, b, c;
  110. var index = geometry.index;
  111. var position = geometry.attributes.position;
  112. var morphPosition = geometry.morphAttributes.position;
  113. var uv = geometry.attributes.uv;
  114. var uv2 = geometry.attributes.uv2;
  115. var groups = geometry.groups;
  116. var drawRange = geometry.drawRange;
  117. var i, j, il, jl;
  118. var group, groupMaterial;
  119. var start, end;
  120. if ( index !== null ) {
  121. // indexed buffer geometry
  122. if ( Array.isArray( material ) ) {
  123. for ( i = 0, il = groups.length; i < il; i ++ ) {
  124. group = groups[ i ];
  125. groupMaterial = material[ group.materialIndex ];
  126. start = Math.max( group.start, drawRange.start );
  127. end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
  128. for ( j = start, jl = end; j < jl; j += 3 ) {
  129. a = index.getX( j );
  130. b = index.getX( j + 1 );
  131. c = index.getX( j + 2 );
  132. intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, uv, uv2, a, b, c );
  133. if ( intersection ) {
  134. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
  135. intersection.face.materialIndex = group.materialIndex;
  136. intersects.push( intersection );
  137. }
  138. }
  139. }
  140. } else {
  141. start = Math.max( 0, drawRange.start );
  142. end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  143. for ( i = start, il = end; i < il; i += 3 ) {
  144. a = index.getX( i );
  145. b = index.getX( i + 1 );
  146. c = index.getX( i + 2 );
  147. intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, uv, uv2, a, b, c );
  148. if ( intersection ) {
  149. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
  150. intersects.push( intersection );
  151. }
  152. }
  153. }
  154. } else if ( position !== undefined ) {
  155. // non-indexed buffer geometry
  156. if ( Array.isArray( material ) ) {
  157. for ( i = 0, il = groups.length; i < il; i ++ ) {
  158. group = groups[ i ];
  159. groupMaterial = material[ group.materialIndex ];
  160. start = Math.max( group.start, drawRange.start );
  161. end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
  162. for ( j = start, jl = end; j < jl; j += 3 ) {
  163. a = j;
  164. b = j + 1;
  165. c = j + 2;
  166. intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, uv, uv2, a, b, c );
  167. if ( intersection ) {
  168. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
  169. intersection.face.materialIndex = group.materialIndex;
  170. intersects.push( intersection );
  171. }
  172. }
  173. }
  174. } else {
  175. start = Math.max( 0, drawRange.start );
  176. end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
  177. for ( i = start, il = end; i < il; i += 3 ) {
  178. a = i;
  179. b = i + 1;
  180. c = i + 2;
  181. intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, uv, uv2, a, b, c );
  182. if ( intersection ) {
  183. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
  184. intersects.push( intersection );
  185. }
  186. }
  187. }
  188. }
  189. } else if ( geometry.isGeometry ) {
  190. var fvA, fvB, fvC;
  191. var isMultiMaterial = Array.isArray( material );
  192. var vertices = geometry.vertices;
  193. var faces = geometry.faces;
  194. var uvs;
  195. var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
  196. if ( faceVertexUvs.length > 0 ) uvs = faceVertexUvs;
  197. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  198. var face = faces[ f ];
  199. var faceMaterial = isMultiMaterial ? material[ face.materialIndex ] : material;
  200. if ( faceMaterial === undefined ) continue;
  201. fvA = vertices[ face.a ];
  202. fvB = vertices[ face.b ];
  203. fvC = vertices[ face.c ];
  204. intersection = checkIntersection( this, faceMaterial, raycaster, _ray, fvA, fvB, fvC, _intersectionPoint );
  205. if ( intersection ) {
  206. if ( uvs && uvs[ f ] ) {
  207. var uvs_f = uvs[ f ];
  208. _uvA.copy( uvs_f[ 0 ] );
  209. _uvB.copy( uvs_f[ 1 ] );
  210. _uvC.copy( uvs_f[ 2 ] );
  211. intersection.uv = Triangle.getUV( _intersectionPoint, fvA, fvB, fvC, _uvA, _uvB, _uvC, new Vector2() );
  212. }
  213. intersection.face = face;
  214. intersection.faceIndex = f;
  215. intersects.push( intersection );
  216. }
  217. }
  218. }
  219. },
  220. clone: function () {
  221. return new this.constructor( this.geometry, this.material ).copy( this );
  222. }
  223. } );
  224. function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point ) {
  225. var intersect;
  226. if ( material.side === BackSide ) {
  227. intersect = ray.intersectTriangle( pC, pB, pA, true, point );
  228. } else {
  229. intersect = ray.intersectTriangle( pA, pB, pC, material.side !== DoubleSide, point );
  230. }
  231. if ( intersect === null ) return null;
  232. _intersectionPointWorld.copy( point );
  233. _intersectionPointWorld.applyMatrix4( object.matrixWorld );
  234. var distance = raycaster.ray.origin.distanceTo( _intersectionPointWorld );
  235. if ( distance < raycaster.near || distance > raycaster.far ) return null;
  236. return {
  237. distance: distance,
  238. point: _intersectionPointWorld.clone(),
  239. object: object
  240. };
  241. }
  242. function checkBufferGeometryIntersection( object, material, raycaster, ray, position, morphPosition, uv, uv2, a, b, c ) {
  243. _vA.fromBufferAttribute( position, a );
  244. _vB.fromBufferAttribute( position, b );
  245. _vC.fromBufferAttribute( position, c );
  246. var morphInfluences = object.morphTargetInfluences;
  247. if ( material.morphTargets && morphPosition && morphInfluences ) {
  248. _morphA.set( 0, 0, 0 );
  249. _morphB.set( 0, 0, 0 );
  250. _morphC.set( 0, 0, 0 );
  251. for ( var i = 0, il = morphPosition.length; i < il; i ++ ) {
  252. var influence = morphInfluences[ i ];
  253. var morphAttribute = morphPosition[ i ];
  254. if ( influence === 0 ) continue;
  255. _tempA.fromBufferAttribute( morphAttribute, a );
  256. _tempB.fromBufferAttribute( morphAttribute, b );
  257. _tempC.fromBufferAttribute( morphAttribute, c );
  258. _morphA.addScaledVector( _tempA.sub( _vA ), influence );
  259. _morphB.addScaledVector( _tempB.sub( _vB ), influence );
  260. _morphC.addScaledVector( _tempC.sub( _vC ), influence );
  261. }
  262. _vA.add( _morphA );
  263. _vB.add( _morphB );
  264. _vC.add( _morphC );
  265. }
  266. var intersection = checkIntersection( object, material, raycaster, ray, _vA, _vB, _vC, _intersectionPoint );
  267. if ( intersection ) {
  268. if ( uv ) {
  269. _uvA.fromBufferAttribute( uv, a );
  270. _uvB.fromBufferAttribute( uv, b );
  271. _uvC.fromBufferAttribute( uv, c );
  272. intersection.uv = Triangle.getUV( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() );
  273. }
  274. if ( uv2 ) {
  275. _uvA.fromBufferAttribute( uv2, a );
  276. _uvB.fromBufferAttribute( uv2, b );
  277. _uvC.fromBufferAttribute( uv2, c );
  278. intersection.uv2 = Triangle.getUV( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() );
  279. }
  280. var face = new Face3( a, b, c );
  281. Triangle.getNormal( _vA, _vB, _vC, face.normal );
  282. intersection.face = face;
  283. }
  284. return intersection;
  285. }
  286. export { Mesh };
粤ICP备19079148号