Mesh.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 = 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. if ( this.isSkinnedMesh ) {
  89. this.applyBoneTransform( index, target );
  90. }
  91. return target;
  92. }
  93. raycast( raycaster, intersects ) {
  94. const geometry = this.geometry;
  95. const material = this.material;
  96. const matrixWorld = this.matrixWorld;
  97. if ( material === undefined ) return;
  98. // Checking boundingSphere distance to ray
  99. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  100. _sphere.copy( geometry.boundingSphere );
  101. _sphere.applyMatrix4( matrixWorld );
  102. _ray.copy( raycaster.ray ).recast( raycaster.near );
  103. if ( _sphere.containsPoint( _ray.origin ) === false ) {
  104. if ( _ray.intersectSphere( _sphere, _sphereHitAt ) === null ) return;
  105. if ( _ray.origin.distanceToSquared( _sphereHitAt ) > ( raycaster.far - raycaster.near ) ** 2 ) return;
  106. }
  107. //
  108. _inverseMatrix.copy( matrixWorld ).invert();
  109. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  110. // Check boundingBox before continuing
  111. if ( geometry.boundingBox !== null ) {
  112. if ( _ray.intersectsBox( geometry.boundingBox ) === false ) return;
  113. }
  114. let intersection;
  115. const index = geometry.index;
  116. const position = geometry.attributes.position;
  117. const uv = geometry.attributes.uv;
  118. const uv2 = geometry.attributes.uv2;
  119. const normal = geometry.attributes.normal;
  120. const groups = geometry.groups;
  121. const drawRange = geometry.drawRange;
  122. if ( index !== null ) {
  123. // indexed buffer geometry
  124. if ( Array.isArray( material ) ) {
  125. for ( let i = 0, il = groups.length; i < il; i ++ ) {
  126. const group = groups[ i ];
  127. const groupMaterial = material[ group.materialIndex ];
  128. const start = Math.max( group.start, drawRange.start );
  129. const end = Math.min( index.count, Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ) );
  130. for ( let j = start, jl = end; j < jl; j += 3 ) {
  131. const a = index.getX( j );
  132. const b = index.getX( j + 1 );
  133. const c = index.getX( j + 2 );
  134. intersection = checkGeometryIntersection( this, groupMaterial, raycaster, _ray, uv, uv2, normal, a, b, c );
  135. if ( intersection ) {
  136. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
  137. intersection.face.materialIndex = group.materialIndex;
  138. intersects.push( intersection );
  139. }
  140. }
  141. }
  142. } else {
  143. const start = Math.max( 0, drawRange.start );
  144. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  145. for ( let i = start, il = end; i < il; i += 3 ) {
  146. const a = index.getX( i );
  147. const b = index.getX( i + 1 );
  148. const c = index.getX( i + 2 );
  149. intersection = checkGeometryIntersection( this, material, raycaster, _ray, uv, uv2, normal, a, b, c );
  150. if ( intersection ) {
  151. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
  152. intersects.push( intersection );
  153. }
  154. }
  155. }
  156. } else if ( position !== undefined ) {
  157. // non-indexed buffer geometry
  158. if ( Array.isArray( material ) ) {
  159. for ( let i = 0, il = groups.length; i < il; i ++ ) {
  160. const group = groups[ i ];
  161. const groupMaterial = material[ group.materialIndex ];
  162. const start = Math.max( group.start, drawRange.start );
  163. const end = Math.min( position.count, Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ) );
  164. for ( let j = start, jl = end; j < jl; j += 3 ) {
  165. const a = j;
  166. const b = j + 1;
  167. const c = j + 2;
  168. intersection = checkGeometryIntersection( this, groupMaterial, raycaster, _ray, uv, uv2, normal, a, b, c );
  169. if ( intersection ) {
  170. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
  171. intersection.face.materialIndex = group.materialIndex;
  172. intersects.push( intersection );
  173. }
  174. }
  175. }
  176. } else {
  177. const start = Math.max( 0, drawRange.start );
  178. const end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
  179. for ( let i = start, il = end; i < il; i += 3 ) {
  180. const a = i;
  181. const b = i + 1;
  182. const c = i + 2;
  183. intersection = checkGeometryIntersection( this, material, raycaster, _ray, uv, uv2, normal, a, b, c );
  184. if ( intersection ) {
  185. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
  186. intersects.push( intersection );
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point ) {
  194. let intersect;
  195. if ( material.side === BackSide ) {
  196. intersect = ray.intersectTriangle( pC, pB, pA, true, point );
  197. } else {
  198. intersect = ray.intersectTriangle( pA, pB, pC, ( material.side === FrontSide ), point );
  199. }
  200. if ( intersect === null ) return null;
  201. _intersectionPointWorld.copy( point );
  202. _intersectionPointWorld.applyMatrix4( object.matrixWorld );
  203. const distance = raycaster.ray.origin.distanceTo( _intersectionPointWorld );
  204. if ( distance < raycaster.near || distance > raycaster.far ) return null;
  205. return {
  206. distance: distance,
  207. point: _intersectionPointWorld.clone(),
  208. object: object
  209. };
  210. }
  211. function checkGeometryIntersection( object, material, raycaster, ray, uv, uv2, normal, a, b, c ) {
  212. object.getVertexPosition( a, _vA );
  213. object.getVertexPosition( b, _vB );
  214. object.getVertexPosition( c, _vC );
  215. const intersection = checkIntersection( object, material, raycaster, ray, _vA, _vB, _vC, _intersectionPoint );
  216. if ( intersection ) {
  217. if ( uv ) {
  218. _uvA.fromBufferAttribute( uv, a );
  219. _uvB.fromBufferAttribute( uv, b );
  220. _uvC.fromBufferAttribute( uv, c );
  221. intersection.uv = Triangle.getInterpolation( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() );
  222. }
  223. if ( uv2 ) {
  224. _uvA.fromBufferAttribute( uv2, a );
  225. _uvB.fromBufferAttribute( uv2, b );
  226. _uvC.fromBufferAttribute( uv2, c );
  227. intersection.uv2 = Triangle.getInterpolation( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() );
  228. }
  229. if ( normal ) {
  230. _normalA.fromBufferAttribute( normal, a );
  231. _normalB.fromBufferAttribute( normal, b );
  232. _normalC.fromBufferAttribute( normal, c );
  233. intersection.normal = Triangle.getInterpolation( _intersectionPoint, _vA, _vB, _vC, _normalA, _normalB, _normalC, new Vector3() );
  234. if ( intersection.normal.dot( ray.direction ) > 0 ) {
  235. intersection.normal.multiplyScalar( - 1 );
  236. }
  237. }
  238. const face = {
  239. a: a,
  240. b: b,
  241. c: c,
  242. normal: new Vector3(),
  243. materialIndex: 0
  244. };
  245. Triangle.getNormal( _vA, _vB, _vC, face.normal );
  246. intersection.face = face;
  247. }
  248. return intersection;
  249. }
  250. export { Mesh };