Mesh.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import { Vector3 } from '../math/Vector3';
  2. import { Vector2 } from '../math/Vector2';
  3. import { Sphere } from '../math/Sphere';
  4. import { Ray } from '../math/Ray';
  5. import { Matrix4 } from '../math/Matrix4';
  6. import { Object3D } from '../core/Object3D';
  7. import { Triangle } from '../math/Triangle';
  8. import { Face3 } from '../core/Face3';
  9. import { DoubleSide, BackSide, TrianglesDrawMode } from '../constants';
  10. import { MeshBasicMaterial } from '../materials/MeshBasicMaterial';
  11. import { BufferGeometry } from '../core/BufferGeometry';
  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. function Mesh( geometry, material ) {
  19. Object3D.call( this );
  20. this.type = 'Mesh';
  21. this.geometry = geometry !== undefined ? geometry : new BufferGeometry();
  22. this.material = material !== undefined ? material : new MeshBasicMaterial( { color: Math.random() * 0xffffff } );
  23. this.drawMode = TrianglesDrawMode;
  24. this.updateMorphTargets();
  25. }
  26. Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), {
  27. constructor: Mesh,
  28. isMesh: true,
  29. setDrawMode: function ( value ) {
  30. this.drawMode = value;
  31. },
  32. copy: function ( source ) {
  33. Object3D.prototype.copy.call( this, source );
  34. this.drawMode = source.drawMode;
  35. return this;
  36. },
  37. updateMorphTargets: function () {
  38. var geometry = this.geometry;
  39. var m, ml, name;
  40. if ( geometry.isBufferGeometry ) {
  41. var morphAttributes = geometry.morphAttributes;
  42. var keys = Object.keys( morphAttributes );
  43. if ( keys.length > 0 ) {
  44. var morphAttribute = morphAttributes[ keys[ 0 ] ];
  45. if ( morphAttribute !== undefined ) {
  46. this.morphTargetInfluences = [];
  47. this.morphTargetDictionary = {};
  48. for ( m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  49. name = morphAttribute[ m ].name || String( m );
  50. this.morphTargetInfluences.push( 0 );
  51. this.morphTargetDictionary[ name ] = m;
  52. }
  53. }
  54. }
  55. } else {
  56. var morphTargets = geometry.morphTargets;
  57. if ( morphTargets !== undefined && morphTargets.length > 0 ) {
  58. this.morphTargetInfluences = [];
  59. this.morphTargetDictionary = {};
  60. for ( m = 0, ml = morphTargets.length; m < ml; m ++ ) {
  61. name = morphTargets[ m ].name || String( m );
  62. this.morphTargetInfluences.push( 0 );
  63. this.morphTargetDictionary[ name ] = m;
  64. }
  65. }
  66. }
  67. },
  68. raycast: ( function () {
  69. var inverseMatrix = new Matrix4();
  70. var ray = new Ray();
  71. var sphere = new Sphere();
  72. var vA = new Vector3();
  73. var vB = new Vector3();
  74. var vC = new Vector3();
  75. var tempA = new Vector3();
  76. var tempB = new Vector3();
  77. var tempC = new Vector3();
  78. var uvA = new Vector2();
  79. var uvB = new Vector2();
  80. var uvC = new Vector2();
  81. var barycoord = new Vector3();
  82. var intersectionPoint = new Vector3();
  83. var intersectionPointWorld = new Vector3();
  84. function uvIntersection( point, p1, p2, p3, uv1, uv2, uv3 ) {
  85. Triangle.barycoordFromPoint( point, p1, p2, p3, barycoord );
  86. uv1.multiplyScalar( barycoord.x );
  87. uv2.multiplyScalar( barycoord.y );
  88. uv3.multiplyScalar( barycoord.z );
  89. uv1.add( uv2 ).add( uv3 );
  90. return uv1.clone();
  91. }
  92. function checkIntersection( object, raycaster, ray, pA, pB, pC, point ) {
  93. var intersect;
  94. var material = object.material;
  95. if ( material.side === BackSide ) {
  96. intersect = ray.intersectTriangle( pC, pB, pA, true, point );
  97. } else {
  98. intersect = ray.intersectTriangle( pA, pB, pC, material.side !== DoubleSide, point );
  99. }
  100. if ( intersect === null ) return null;
  101. intersectionPointWorld.copy( point );
  102. intersectionPointWorld.applyMatrix4( object.matrixWorld );
  103. var distance = raycaster.ray.origin.distanceTo( intersectionPointWorld );
  104. if ( distance < raycaster.near || distance > raycaster.far ) return null;
  105. return {
  106. distance: distance,
  107. point: intersectionPointWorld.clone(),
  108. object: object
  109. };
  110. }
  111. function checkBufferGeometryIntersection( object, raycaster, ray, position, uv, a, b, c ) {
  112. vA.fromBufferAttribute( position, a );
  113. vB.fromBufferAttribute( position, b );
  114. vC.fromBufferAttribute( position, c );
  115. var intersection = checkIntersection( object, raycaster, ray, vA, vB, vC, intersectionPoint );
  116. if ( intersection ) {
  117. if ( uv ) {
  118. uvA.fromBufferAttribute( uv, a );
  119. uvB.fromBufferAttribute( uv, b );
  120. uvC.fromBufferAttribute( uv, c );
  121. intersection.uv = uvIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC );
  122. }
  123. intersection.face = new Face3( a, b, c, Triangle.normal( vA, vB, vC ) );
  124. intersection.faceIndex = a;
  125. }
  126. return intersection;
  127. }
  128. return function raycast( raycaster, intersects ) {
  129. var geometry = this.geometry;
  130. var material = this.material;
  131. var matrixWorld = this.matrixWorld;
  132. if ( material === undefined ) return;
  133. // Checking boundingSphere distance to ray
  134. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  135. sphere.copy( geometry.boundingSphere );
  136. sphere.applyMatrix4( matrixWorld );
  137. if ( raycaster.ray.intersectsSphere( sphere ) === false ) return;
  138. //
  139. inverseMatrix.getInverse( matrixWorld );
  140. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  141. // Check boundingBox before continuing
  142. if ( geometry.boundingBox !== null ) {
  143. if ( ray.intersectsBox( geometry.boundingBox ) === false ) return;
  144. }
  145. var intersection;
  146. if ( geometry.isBufferGeometry ) {
  147. var a, b, c;
  148. var index = geometry.index;
  149. var position = geometry.attributes.position;
  150. var uv = geometry.attributes.uv;
  151. var i, l;
  152. if ( index !== null ) {
  153. // indexed buffer geometry
  154. for ( i = 0, l = index.count; i < l; i += 3 ) {
  155. a = index.getX( i );
  156. b = index.getX( i + 1 );
  157. c = index.getX( i + 2 );
  158. intersection = checkBufferGeometryIntersection( this, raycaster, ray, position, uv, a, b, c );
  159. if ( intersection ) {
  160. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indices buffer semantics
  161. intersects.push( intersection );
  162. }
  163. }
  164. } else {
  165. // non-indexed buffer geometry
  166. for ( i = 0, l = position.count; i < l; i += 3 ) {
  167. a = i;
  168. b = i + 1;
  169. c = i + 2;
  170. intersection = checkBufferGeometryIntersection( this, raycaster, ray, position, uv, a, b, c );
  171. if ( intersection ) {
  172. intersection.index = a; // triangle number in positions buffer semantics
  173. intersects.push( intersection );
  174. }
  175. }
  176. }
  177. } else if ( geometry.isGeometry ) {
  178. var fvA, fvB, fvC;
  179. var isMultiMaterial = Array.isArray( material );
  180. var vertices = geometry.vertices;
  181. var faces = geometry.faces;
  182. var uvs;
  183. var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
  184. if ( faceVertexUvs.length > 0 ) uvs = faceVertexUvs;
  185. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  186. var face = faces[ f ];
  187. var faceMaterial = isMultiMaterial ? material[ face.materialIndex ] : material;
  188. if ( faceMaterial === undefined ) continue;
  189. fvA = vertices[ face.a ];
  190. fvB = vertices[ face.b ];
  191. fvC = vertices[ face.c ];
  192. if ( faceMaterial.morphTargets === true ) {
  193. var morphTargets = geometry.morphTargets;
  194. var morphInfluences = this.morphTargetInfluences;
  195. vA.set( 0, 0, 0 );
  196. vB.set( 0, 0, 0 );
  197. vC.set( 0, 0, 0 );
  198. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  199. var influence = morphInfluences[ t ];
  200. if ( influence === 0 ) continue;
  201. var targets = morphTargets[ t ].vertices;
  202. vA.addScaledVector( tempA.subVectors( targets[ face.a ], fvA ), influence );
  203. vB.addScaledVector( tempB.subVectors( targets[ face.b ], fvB ), influence );
  204. vC.addScaledVector( tempC.subVectors( targets[ face.c ], fvC ), influence );
  205. }
  206. vA.add( fvA );
  207. vB.add( fvB );
  208. vC.add( fvC );
  209. fvA = vA;
  210. fvB = vB;
  211. fvC = vC;
  212. }
  213. intersection = checkIntersection( this, raycaster, ray, fvA, fvB, fvC, intersectionPoint );
  214. if ( intersection ) {
  215. if ( uvs && uvs[ f ] ) {
  216. var uvs_f = uvs[ f ];
  217. uvA.copy( uvs_f[ 0 ] );
  218. uvB.copy( uvs_f[ 1 ] );
  219. uvC.copy( uvs_f[ 2 ] );
  220. intersection.uv = uvIntersection( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC );
  221. }
  222. intersection.face = face;
  223. intersection.faceIndex = f;
  224. intersects.push( intersection );
  225. }
  226. }
  227. }
  228. };
  229. }() ),
  230. clone: function () {
  231. return new this.constructor( this.geometry, this.material ).copy( this );
  232. }
  233. } );
  234. export { Mesh };