Mesh.js 7.9 KB

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