Mesh.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 { BackSide, FrontSide } from '../constants.js';
  9. import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
  10. import { BufferGeometry } from '../core/BufferGeometry.js';
  11. const _inverseMatrix = /*@__PURE__*/ new Matrix4();
  12. const _ray = /*@__PURE__*/ new Ray();
  13. const _sphere = /*@__PURE__*/ new Sphere();
  14. const _sphereHitAt = /*@__PURE__*/ new Vector3();
  15. const _vA = /*@__PURE__*/ new Vector3();
  16. const _vB = /*@__PURE__*/ new Vector3();
  17. const _vC = /*@__PURE__*/ new Vector3();
  18. const _tempA = /*@__PURE__*/ new Vector3();
  19. const _morphA = /*@__PURE__*/ new Vector3();
  20. const _uvA = /*@__PURE__*/ new Vector2();
  21. const _uvB = /*@__PURE__*/ new Vector2();
  22. const _uvC = /*@__PURE__*/ new Vector2();
  23. const _normalA = /*@__PURE__*/ new Vector3();
  24. const _normalB = /*@__PURE__*/ new Vector3();
  25. const _normalC = /*@__PURE__*/ new Vector3();
  26. const _intersectionPoint = /*@__PURE__*/ new Vector3();
  27. const _intersectionPointWorld = /*@__PURE__*/ new Vector3();
  28. class Mesh extends Object3D {
  29. constructor( geometry = new BufferGeometry(), material = new MeshBasicMaterial() ) {
  30. super();
  31. this.isMesh = true;
  32. this.type = 'Mesh';
  33. this.geometry = geometry;
  34. this.material = material;
  35. this.updateMorphTargets();
  36. }
  37. copy( source, recursive ) {
  38. super.copy( source, recursive );
  39. if ( source.morphTargetInfluences !== undefined ) {
  40. this.morphTargetInfluences = source.morphTargetInfluences.slice();
  41. }
  42. if ( source.morphTargetDictionary !== undefined ) {
  43. this.morphTargetDictionary = Object.assign( {}, source.morphTargetDictionary );
  44. }
  45. this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
  46. this.geometry = source.geometry;
  47. return this;
  48. }
  49. updateMorphTargets() {
  50. const geometry = this.geometry;
  51. const morphAttributes = geometry.morphAttributes;
  52. const keys = Object.keys( morphAttributes );
  53. if ( keys.length > 0 ) {
  54. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  55. if ( morphAttribute !== undefined ) {
  56. this.morphTargetInfluences = [];
  57. this.morphTargetDictionary = {};
  58. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  59. const name = morphAttribute[ m ].name || String( m );
  60. this.morphTargetInfluences.push( 0 );
  61. this.morphTargetDictionary[ name ] = m;
  62. }
  63. }
  64. }
  65. }
  66. getVertexPosition( index, target ) {
  67. const geometry = this.geometry;
  68. const position = geometry.attributes.position;
  69. const morphPosition = geometry.morphAttributes.position;
  70. const morphTargetsRelative = geometry.morphTargetsRelative;
  71. target.fromBufferAttribute( position, index );
  72. const morphInfluences = this.morphTargetInfluences;
  73. if ( morphPosition && morphInfluences ) {
  74. _morphA.set( 0, 0, 0 );
  75. for ( let i = 0, il = morphPosition.length; i < il; i ++ ) {
  76. const influence = morphInfluences[ i ];
  77. const morphAttribute = morphPosition[ i ];
  78. if ( influence === 0 ) continue;
  79. _tempA.fromBufferAttribute( morphAttribute, index );
  80. if ( morphTargetsRelative ) {
  81. _morphA.addScaledVector( _tempA, influence );
  82. } else {
  83. _morphA.addScaledVector( _tempA.sub( target ), influence );
  84. }
  85. }
  86. target.add( _morphA );
  87. }
  88. return target;
  89. }
  90. raycast( raycaster, intersects ) {
  91. const geometry = this.geometry;
  92. const material = this.material;
  93. const matrixWorld = this.matrixWorld;
  94. if ( material === undefined ) return;
  95. // test with bounding sphere in world space
  96. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  97. _sphere.copy( geometry.boundingSphere );
  98. _sphere.applyMatrix4( matrixWorld );
  99. // check distance from ray origin to bounding sphere
  100. _ray.copy( raycaster.ray ).recast( raycaster.near );
  101. if ( _sphere.containsPoint( _ray.origin ) === false ) {
  102. if ( _ray.intersectSphere( _sphere, _sphereHitAt ) === null ) return;
  103. if ( _ray.origin.distanceToSquared( _sphereHitAt ) > ( raycaster.far - raycaster.near ) ** 2 ) return;
  104. }
  105. // convert ray to local space of mesh
  106. _inverseMatrix.copy( matrixWorld ).invert();
  107. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  108. // test with bounding box in local space
  109. if ( geometry.boundingBox !== null ) {
  110. if ( _ray.intersectsBox( geometry.boundingBox ) === false ) return;
  111. }
  112. // test for intersections with geometry
  113. this._computeIntersections( raycaster, intersects, _ray );
  114. }
  115. _computeIntersections( raycaster, intersects, rayLocalSpace ) {
  116. let intersection;
  117. const geometry = this.geometry;
  118. const material = this.material;
  119. const index = geometry.index;
  120. const position = geometry.attributes.position;
  121. const uv = geometry.attributes.uv;
  122. const uv1 = geometry.attributes.uv1;
  123. const normal = geometry.attributes.normal;
  124. const groups = geometry.groups;
  125. const drawRange = geometry.drawRange;
  126. if ( index !== null ) {
  127. // indexed buffer geometry
  128. if ( Array.isArray( material ) ) {
  129. for ( let i = 0, il = groups.length; i < il; i ++ ) {
  130. const group = groups[ i ];
  131. const groupMaterial = material[ group.materialIndex ];
  132. const start = Math.max( group.start, drawRange.start );
  133. const end = Math.min( index.count, Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ) );
  134. for ( let j = start, jl = end; j < jl; j += 3 ) {
  135. const a = index.getX( j );
  136. const b = index.getX( j + 1 );
  137. const c = index.getX( j + 2 );
  138. intersection = checkGeometryIntersection( this, groupMaterial, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  139. if ( intersection ) {
  140. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
  141. intersection.face.materialIndex = group.materialIndex;
  142. intersects.push( intersection );
  143. }
  144. }
  145. }
  146. } else {
  147. const start = Math.max( 0, drawRange.start );
  148. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  149. for ( let i = start, il = end; i < il; i += 3 ) {
  150. const a = index.getX( i );
  151. const b = index.getX( i + 1 );
  152. const c = index.getX( i + 2 );
  153. intersection = checkGeometryIntersection( this, material, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  154. if ( intersection ) {
  155. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
  156. intersects.push( intersection );
  157. }
  158. }
  159. }
  160. } else if ( position !== undefined ) {
  161. // non-indexed buffer geometry
  162. if ( Array.isArray( material ) ) {
  163. for ( let i = 0, il = groups.length; i < il; i ++ ) {
  164. const group = groups[ i ];
  165. const groupMaterial = material[ group.materialIndex ];
  166. const start = Math.max( group.start, drawRange.start );
  167. const end = Math.min( position.count, Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ) );
  168. for ( let j = start, jl = end; j < jl; j += 3 ) {
  169. const a = j;
  170. const b = j + 1;
  171. const c = j + 2;
  172. intersection = checkGeometryIntersection( this, groupMaterial, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  173. if ( intersection ) {
  174. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
  175. intersection.face.materialIndex = group.materialIndex;
  176. intersects.push( intersection );
  177. }
  178. }
  179. }
  180. } else {
  181. const start = Math.max( 0, drawRange.start );
  182. const end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
  183. for ( let i = start, il = end; i < il; i += 3 ) {
  184. const a = i;
  185. const b = i + 1;
  186. const c = i + 2;
  187. intersection = checkGeometryIntersection( this, material, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  188. if ( intersection ) {
  189. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
  190. intersects.push( intersection );
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point ) {
  198. let intersect;
  199. if ( material.side === BackSide ) {
  200. intersect = ray.intersectTriangle( pC, pB, pA, true, point );
  201. } else {
  202. intersect = ray.intersectTriangle( pA, pB, pC, ( material.side === FrontSide ), point );
  203. }
  204. if ( intersect === null ) return null;
  205. _intersectionPointWorld.copy( point );
  206. _intersectionPointWorld.applyMatrix4( object.matrixWorld );
  207. const distance = raycaster.ray.origin.distanceTo( _intersectionPointWorld );
  208. if ( distance < raycaster.near || distance > raycaster.far ) return null;
  209. return {
  210. distance: distance,
  211. point: _intersectionPointWorld.clone(),
  212. object: object
  213. };
  214. }
  215. function checkGeometryIntersection( object, material, raycaster, ray, uv, uv1, normal, a, b, c ) {
  216. object.getVertexPosition( a, _vA );
  217. object.getVertexPosition( b, _vB );
  218. object.getVertexPosition( c, _vC );
  219. const intersection = checkIntersection( object, material, raycaster, ray, _vA, _vB, _vC, _intersectionPoint );
  220. if ( intersection ) {
  221. if ( uv ) {
  222. _uvA.fromBufferAttribute( uv, a );
  223. _uvB.fromBufferAttribute( uv, b );
  224. _uvC.fromBufferAttribute( uv, c );
  225. intersection.uv = Triangle.getInterpolation( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() );
  226. }
  227. if ( uv1 ) {
  228. _uvA.fromBufferAttribute( uv1, a );
  229. _uvB.fromBufferAttribute( uv1, b );
  230. _uvC.fromBufferAttribute( uv1, c );
  231. intersection.uv1 = Triangle.getInterpolation( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() );
  232. }
  233. if ( normal ) {
  234. _normalA.fromBufferAttribute( normal, a );
  235. _normalB.fromBufferAttribute( normal, b );
  236. _normalC.fromBufferAttribute( normal, c );
  237. intersection.normal = Triangle.getInterpolation( _intersectionPoint, _vA, _vB, _vC, _normalA, _normalB, _normalC, new Vector3() );
  238. if ( intersection.normal.dot( ray.direction ) > 0 ) {
  239. intersection.normal.multiplyScalar( - 1 );
  240. }
  241. }
  242. const face = {
  243. a: a,
  244. b: b,
  245. c: c,
  246. normal: new Vector3(),
  247. materialIndex: 0
  248. };
  249. Triangle.getNormal( _vA, _vB, _vC, face.normal );
  250. intersection.face = face;
  251. }
  252. return intersection;
  253. }
  254. export { Mesh };