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