Mesh.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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, positions, uvs, a, b, c ) {
  92. vA.fromArray( positions, a * 3 );
  93. vB.fromArray( positions, b * 3 );
  94. vC.fromArray( positions, c * 3 );
  95. var intersection = checkIntersection( object, raycaster, ray, vA, vB, vC, intersectionPoint );
  96. if ( intersection ) {
  97. if ( uvs ) {
  98. uvA.fromArray( uvs, a * 2 );
  99. uvB.fromArray( uvs, b * 2 );
  100. uvC.fromArray( uvs, c * 2 );
  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 uvs, intersection;
  126. if ( (geometry && geometry.isBufferGeometry) ) {
  127. var a, b, c;
  128. var index = geometry.index;
  129. var attributes = geometry.attributes;
  130. var positions = attributes.position.array;
  131. if ( attributes.uv !== undefined ) {
  132. uvs = attributes.uv.array;
  133. }
  134. if ( index !== null ) {
  135. var indices = index.array;
  136. for ( var i = 0, l = indices.length; i < l; i += 3 ) {
  137. a = indices[ i ];
  138. b = indices[ i + 1 ];
  139. c = indices[ i + 2 ];
  140. intersection = checkBufferGeometryIntersection( this, raycaster, ray, positions, uvs, a, b, c );
  141. if ( intersection ) {
  142. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indices buffer semantics
  143. intersects.push( intersection );
  144. }
  145. }
  146. } else {
  147. for ( var i = 0, l = positions.length; i < l; i += 9 ) {
  148. a = i / 3;
  149. b = a + 1;
  150. c = a + 2;
  151. intersection = checkBufferGeometryIntersection( this, raycaster, ray, positions, uvs, a, b, c );
  152. if ( intersection ) {
  153. intersection.index = a; // triangle number in positions buffer semantics
  154. intersects.push( intersection );
  155. }
  156. }
  157. }
  158. } else if ( (geometry && geometry.isGeometry) ) {
  159. var fvA, fvB, fvC;
  160. var isFaceMaterial = (material && material.isMultiMaterial);
  161. var materials = isFaceMaterial === true ? material.materials : null;
  162. var vertices = geometry.vertices;
  163. var faces = geometry.faces;
  164. var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
  165. if ( faceVertexUvs.length > 0 ) uvs = faceVertexUvs;
  166. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  167. var face = faces[ f ];
  168. var faceMaterial = isFaceMaterial === true ? materials[ face.materialIndex ] : material;
  169. if ( faceMaterial === undefined ) continue;
  170. fvA = vertices[ face.a ];
  171. fvB = vertices[ face.b ];
  172. fvC = vertices[ face.c ];
  173. if ( faceMaterial.morphTargets === true ) {
  174. var morphTargets = geometry.morphTargets;
  175. var morphInfluences = this.morphTargetInfluences;
  176. vA.set( 0, 0, 0 );
  177. vB.set( 0, 0, 0 );
  178. vC.set( 0, 0, 0 );
  179. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  180. var influence = morphInfluences[ t ];
  181. if ( influence === 0 ) continue;
  182. var targets = morphTargets[ t ].vertices;
  183. vA.addScaledVector( tempA.subVectors( targets[ face.a ], fvA ), influence );
  184. vB.addScaledVector( tempB.subVectors( targets[ face.b ], fvB ), influence );
  185. vC.addScaledVector( tempC.subVectors( targets[ face.c ], fvC ), influence );
  186. }
  187. vA.add( fvA );
  188. vB.add( fvB );
  189. vC.add( fvC );
  190. fvA = vA;
  191. fvB = vB;
  192. fvC = vC;
  193. }
  194. intersection = checkIntersection( this, raycaster, ray, fvA, fvB, fvC, intersectionPoint );
  195. if ( intersection ) {
  196. if ( uvs ) {
  197. var uvs_f = uvs[ f ];
  198. uvA.copy( uvs_f[ 0 ] );
  199. uvB.copy( uvs_f[ 1 ] );
  200. uvC.copy( uvs_f[ 2 ] );
  201. intersection.uv = uvIntersection( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC );
  202. }
  203. intersection.face = face;
  204. intersection.faceIndex = f;
  205. intersects.push( intersection );
  206. }
  207. }
  208. }
  209. };
  210. }() ),
  211. clone: function () {
  212. return new this.constructor( this.geometry, this.material ).copy( this );
  213. }
  214. } );
  215. export { Mesh };