Mesh.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. 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, material, raycaster, ray, pA, pB, pC, point ) {
  93. var intersect;
  94. if ( material.side === BackSide ) {
  95. intersect = ray.intersectTriangle( pC, pB, pA, true, point );
  96. } else {
  97. intersect = ray.intersectTriangle( pA, pB, pC, material.side !== DoubleSide, point );
  98. }
  99. if ( intersect === null ) return null;
  100. intersectionPointWorld.copy( point );
  101. intersectionPointWorld.applyMatrix4( object.matrixWorld );
  102. var distance = raycaster.ray.origin.distanceTo( intersectionPointWorld );
  103. if ( distance < raycaster.near || distance > raycaster.far ) return null;
  104. return {
  105. distance: distance,
  106. point: intersectionPointWorld.clone(),
  107. object: object
  108. };
  109. }
  110. function checkBufferGeometryIntersection( object, raycaster, ray, position, uv, a, b, c ) {
  111. vA.fromBufferAttribute( position, a );
  112. vB.fromBufferAttribute( position, b );
  113. vC.fromBufferAttribute( position, c );
  114. var intersection = checkIntersection( object, object.material, raycaster, ray, vA, vB, vC, intersectionPoint );
  115. if ( intersection ) {
  116. if ( uv ) {
  117. uvA.fromBufferAttribute( uv, a );
  118. uvB.fromBufferAttribute( uv, b );
  119. uvC.fromBufferAttribute( uv, c );
  120. intersection.uv = uvIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC );
  121. }
  122. intersection.face = new Face3( a, b, c, Triangle.normal( vA, vB, vC ) );
  123. intersection.faceIndex = a;
  124. }
  125. return intersection;
  126. }
  127. return function raycast( raycaster, intersects ) {
  128. var geometry = this.geometry;
  129. var material = this.material;
  130. var matrixWorld = this.matrixWorld;
  131. if ( material === undefined ) return;
  132. // Checking boundingSphere distance to ray
  133. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  134. sphere.copy( geometry.boundingSphere );
  135. sphere.applyMatrix4( matrixWorld );
  136. if ( raycaster.ray.intersectsSphere( sphere ) === false ) return;
  137. //
  138. inverseMatrix.getInverse( matrixWorld );
  139. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  140. // Check boundingBox before continuing
  141. if ( geometry.boundingBox !== null ) {
  142. if ( ray.intersectsBox( geometry.boundingBox ) === false ) return;
  143. }
  144. var intersection;
  145. if ( geometry.isBufferGeometry ) {
  146. var a, b, c;
  147. var index = geometry.index;
  148. var position = geometry.attributes.position;
  149. var uv = geometry.attributes.uv;
  150. var i, l;
  151. if ( index !== null ) {
  152. // indexed buffer geometry
  153. for ( i = 0, l = index.count; i < l; i += 3 ) {
  154. a = index.getX( i );
  155. b = index.getX( i + 1 );
  156. c = index.getX( i + 2 );
  157. intersection = checkBufferGeometryIntersection( this, raycaster, ray, position, uv, a, b, c );
  158. if ( intersection ) {
  159. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indices buffer semantics
  160. intersects.push( intersection );
  161. }
  162. }
  163. } else {
  164. // non-indexed buffer geometry
  165. if ( position ) {
  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. }
  178. } else if ( geometry.isGeometry ) {
  179. var fvA, fvB, fvC;
  180. var isMultiMaterial = Array.isArray( material );
  181. var vertices = geometry.vertices;
  182. var faces = geometry.faces;
  183. var uvs;
  184. var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
  185. if ( faceVertexUvs.length > 0 ) uvs = faceVertexUvs;
  186. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  187. var face = faces[ f ];
  188. var faceMaterial = isMultiMaterial ? material[ face.materialIndex ] : material;
  189. if ( faceMaterial === undefined ) continue;
  190. fvA = vertices[ face.a ];
  191. fvB = vertices[ face.b ];
  192. fvC = vertices[ face.c ];
  193. if ( faceMaterial.morphTargets === true ) {
  194. var morphTargets = geometry.morphTargets;
  195. var morphInfluences = this.morphTargetInfluences;
  196. vA.set( 0, 0, 0 );
  197. vB.set( 0, 0, 0 );
  198. vC.set( 0, 0, 0 );
  199. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  200. var influence = morphInfluences[ t ];
  201. if ( influence === 0 ) continue;
  202. var targets = morphTargets[ t ].vertices;
  203. vA.addScaledVector( tempA.subVectors( targets[ face.a ], fvA ), influence );
  204. vB.addScaledVector( tempB.subVectors( targets[ face.b ], fvB ), influence );
  205. vC.addScaledVector( tempC.subVectors( targets[ face.c ], fvC ), influence );
  206. }
  207. vA.add( fvA );
  208. vB.add( fvB );
  209. vC.add( fvC );
  210. fvA = vA;
  211. fvB = vB;
  212. fvC = vC;
  213. }
  214. intersection = checkIntersection( this, faceMaterial, raycaster, ray, fvA, fvB, fvC, intersectionPoint );
  215. if ( intersection ) {
  216. if ( uvs && uvs[ f ] ) {
  217. var uvs_f = uvs[ f ];
  218. uvA.copy( uvs_f[ 0 ] );
  219. uvB.copy( uvs_f[ 1 ] );
  220. uvC.copy( uvs_f[ 2 ] );
  221. intersection.uv = uvIntersection( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC );
  222. }
  223. intersection.face = face;
  224. intersection.faceIndex = f;
  225. intersects.push( intersection );
  226. }
  227. }
  228. }
  229. };
  230. }() ),
  231. clone: function () {
  232. return new this.constructor( this.geometry, this.material ).copy( this );
  233. }
  234. } );
  235. export { Mesh };