Mesh.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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 _intersectionPoint = /*@__PURE__*/ new Vector3();
  21. const _intersectionPointWorld = /*@__PURE__*/ new Vector3();
  22. /**
  23. * Class representing triangular polygon mesh based objects.
  24. *
  25. * ```js
  26. * const geometry = new THREE.BoxGeometry( 1, 1, 1 );
  27. * const material = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
  28. * const mesh = new THREE.Mesh( geometry, material );
  29. * scene.add( mesh );
  30. * ```
  31. *
  32. * @augments Object3D
  33. */
  34. class Mesh extends Object3D {
  35. /**
  36. * Constructs a new mesh.
  37. *
  38. * @param {BufferGeometry} [geometry] - The mesh geometry.
  39. * @param {Material|Array<Material>} [material] - The mesh material.
  40. */
  41. constructor( geometry = new BufferGeometry(), material = new MeshBasicMaterial() ) {
  42. super();
  43. /**
  44. * This flag can be used for type testing.
  45. *
  46. * @type {boolean}
  47. * @readonly
  48. * @default true
  49. */
  50. this.isMesh = true;
  51. this.type = 'Mesh';
  52. /**
  53. * The mesh geometry.
  54. *
  55. * @type {BufferGeometry}
  56. */
  57. this.geometry = geometry;
  58. /**
  59. * The mesh material.
  60. *
  61. * @type {Material|Array<Material>}
  62. * @default MeshBasicMaterial
  63. */
  64. this.material = material;
  65. /**
  66. * A dictionary representing the morph targets in the geometry. The key is the
  67. * morph targets name, the value its attribute index. This member is `undefined`
  68. * by default and only set when morph targets are detected in the geometry.
  69. *
  70. * @type {Object<String,number>|undefined}
  71. * @default undefined
  72. */
  73. this.morphTargetDictionary = undefined;
  74. /**
  75. * An array of weights typically in the range `[0,1]` that specify how much of the morph
  76. * is applied. This member is `undefined` by default and only set when morph targets are
  77. * detected in the geometry.
  78. *
  79. * @type {Array<number>|undefined}
  80. * @default undefined
  81. */
  82. this.morphTargetInfluences = undefined;
  83. /**
  84. * The number of instances of this mesh.
  85. * Can only be used with {@link WebGPURenderer}.
  86. *
  87. * @type {number}
  88. * @default 1
  89. */
  90. this.count = 1;
  91. this.updateMorphTargets();
  92. }
  93. copy( source, recursive ) {
  94. super.copy( source, recursive );
  95. if ( source.morphTargetInfluences !== undefined ) {
  96. this.morphTargetInfluences = source.morphTargetInfluences.slice();
  97. }
  98. if ( source.morphTargetDictionary !== undefined ) {
  99. this.morphTargetDictionary = Object.assign( {}, source.morphTargetDictionary );
  100. }
  101. this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
  102. this.geometry = source.geometry;
  103. return this;
  104. }
  105. /**
  106. * Sets the values of {@link Mesh#morphTargetDictionary} and {@link Mesh#morphTargetInfluences}
  107. * to make sure existing morph targets can influence this 3D object.
  108. */
  109. updateMorphTargets() {
  110. const geometry = this.geometry;
  111. const morphAttributes = geometry.morphAttributes;
  112. const keys = Object.keys( morphAttributes );
  113. if ( keys.length > 0 ) {
  114. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  115. if ( morphAttribute !== undefined ) {
  116. this.morphTargetInfluences = [];
  117. this.morphTargetDictionary = {};
  118. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  119. const name = morphAttribute[ m ].name || String( m );
  120. this.morphTargetInfluences.push( 0 );
  121. this.morphTargetDictionary[ name ] = m;
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * Returns the local-space position of the vertex at the given index, taking into
  128. * account the current animation state of both morph targets and skinning.
  129. *
  130. * @param {number} index - The vertex index.
  131. * @param {Vector3} target - The target object that is used to store the method's result.
  132. * @return {Vector3} The vertex position in local space.
  133. */
  134. getVertexPosition( index, target ) {
  135. const geometry = this.geometry;
  136. const position = geometry.attributes.position;
  137. const morphPosition = geometry.morphAttributes.position;
  138. const morphTargetsRelative = geometry.morphTargetsRelative;
  139. target.fromBufferAttribute( position, index );
  140. const morphInfluences = this.morphTargetInfluences;
  141. if ( morphPosition && morphInfluences ) {
  142. _morphA.set( 0, 0, 0 );
  143. for ( let i = 0, il = morphPosition.length; i < il; i ++ ) {
  144. const influence = morphInfluences[ i ];
  145. const morphAttribute = morphPosition[ i ];
  146. if ( influence === 0 ) continue;
  147. _tempA.fromBufferAttribute( morphAttribute, index );
  148. if ( morphTargetsRelative ) {
  149. _morphA.addScaledVector( _tempA, influence );
  150. } else {
  151. _morphA.addScaledVector( _tempA.sub( target ), influence );
  152. }
  153. }
  154. target.add( _morphA );
  155. }
  156. return target;
  157. }
  158. /**
  159. * Computes intersection points between a casted ray and this line.
  160. *
  161. * @param {Raycaster} raycaster - The raycaster.
  162. * @param {Array<Object>} intersects - The target array that holds the intersection points.
  163. */
  164. raycast( raycaster, intersects ) {
  165. const geometry = this.geometry;
  166. const material = this.material;
  167. const matrixWorld = this.matrixWorld;
  168. if ( material === undefined ) return;
  169. // test with bounding sphere in world space
  170. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  171. _sphere.copy( geometry.boundingSphere );
  172. _sphere.applyMatrix4( matrixWorld );
  173. // check distance from ray origin to bounding sphere
  174. _ray.copy( raycaster.ray ).recast( raycaster.near );
  175. if ( _sphere.containsPoint( _ray.origin ) === false ) {
  176. if ( _ray.intersectSphere( _sphere, _sphereHitAt ) === null ) return;
  177. if ( _ray.origin.distanceToSquared( _sphereHitAt ) > ( raycaster.far - raycaster.near ) ** 2 ) return;
  178. }
  179. // convert ray to local space of mesh
  180. _inverseMatrix.copy( matrixWorld ).invert();
  181. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  182. // test with bounding box in local space
  183. if ( geometry.boundingBox !== null ) {
  184. if ( _ray.intersectsBox( geometry.boundingBox ) === false ) return;
  185. }
  186. // test for intersections with geometry
  187. this._computeIntersections( raycaster, intersects, _ray );
  188. }
  189. _computeIntersections( raycaster, intersects, rayLocalSpace ) {
  190. let intersection;
  191. const geometry = this.geometry;
  192. const material = this.material;
  193. const index = geometry.index;
  194. const position = geometry.attributes.position;
  195. const uv = geometry.attributes.uv;
  196. const uv1 = geometry.attributes.uv1;
  197. const normal = geometry.attributes.normal;
  198. const groups = geometry.groups;
  199. const drawRange = geometry.drawRange;
  200. if ( index !== null ) {
  201. // indexed buffer geometry
  202. if ( Array.isArray( material ) ) {
  203. for ( let i = 0, il = groups.length; i < il; i ++ ) {
  204. const group = groups[ i ];
  205. const groupMaterial = material[ group.materialIndex ];
  206. const start = Math.max( group.start, drawRange.start );
  207. const end = Math.min( index.count, Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ) );
  208. for ( let j = start, jl = end; j < jl; j += 3 ) {
  209. const a = index.getX( j );
  210. const b = index.getX( j + 1 );
  211. const c = index.getX( j + 2 );
  212. intersection = checkGeometryIntersection( this, groupMaterial, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  213. if ( intersection ) {
  214. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
  215. intersection.face.materialIndex = group.materialIndex;
  216. intersects.push( intersection );
  217. }
  218. }
  219. }
  220. } else {
  221. const start = Math.max( 0, drawRange.start );
  222. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  223. for ( let i = start, il = end; i < il; i += 3 ) {
  224. const a = index.getX( i );
  225. const b = index.getX( i + 1 );
  226. const c = index.getX( i + 2 );
  227. intersection = checkGeometryIntersection( this, material, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  228. if ( intersection ) {
  229. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
  230. intersects.push( intersection );
  231. }
  232. }
  233. }
  234. } else if ( position !== undefined ) {
  235. // non-indexed buffer geometry
  236. if ( Array.isArray( material ) ) {
  237. for ( let i = 0, il = groups.length; i < il; i ++ ) {
  238. const group = groups[ i ];
  239. const groupMaterial = material[ group.materialIndex ];
  240. const start = Math.max( group.start, drawRange.start );
  241. const end = Math.min( position.count, Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ) );
  242. for ( let j = start, jl = end; j < jl; j += 3 ) {
  243. const a = j;
  244. const b = j + 1;
  245. const c = j + 2;
  246. intersection = checkGeometryIntersection( this, groupMaterial, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  247. if ( intersection ) {
  248. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
  249. intersection.face.materialIndex = group.materialIndex;
  250. intersects.push( intersection );
  251. }
  252. }
  253. }
  254. } else {
  255. const start = Math.max( 0, drawRange.start );
  256. const end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
  257. for ( let i = start, il = end; i < il; i += 3 ) {
  258. const a = i;
  259. const b = i + 1;
  260. const c = i + 2;
  261. intersection = checkGeometryIntersection( this, material, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  262. if ( intersection ) {
  263. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
  264. intersects.push( intersection );
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271. function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point ) {
  272. let intersect;
  273. if ( material.side === BackSide ) {
  274. intersect = ray.intersectTriangle( pC, pB, pA, true, point );
  275. } else {
  276. intersect = ray.intersectTriangle( pA, pB, pC, ( material.side === FrontSide ), point );
  277. }
  278. if ( intersect === null ) return null;
  279. _intersectionPointWorld.copy( point );
  280. _intersectionPointWorld.applyMatrix4( object.matrixWorld );
  281. const distance = raycaster.ray.origin.distanceTo( _intersectionPointWorld );
  282. if ( distance < raycaster.near || distance > raycaster.far ) return null;
  283. return {
  284. distance: distance,
  285. point: _intersectionPointWorld.clone(),
  286. object: object
  287. };
  288. }
  289. function checkGeometryIntersection( object, material, raycaster, ray, uv, uv1, normal, a, b, c ) {
  290. object.getVertexPosition( a, _vA );
  291. object.getVertexPosition( b, _vB );
  292. object.getVertexPosition( c, _vC );
  293. const intersection = checkIntersection( object, material, raycaster, ray, _vA, _vB, _vC, _intersectionPoint );
  294. if ( intersection ) {
  295. const barycoord = new Vector3();
  296. Triangle.getBarycoord( _intersectionPoint, _vA, _vB, _vC, barycoord );
  297. if ( uv ) {
  298. intersection.uv = Triangle.getInterpolatedAttribute( uv, a, b, c, barycoord, new Vector2() );
  299. }
  300. if ( uv1 ) {
  301. intersection.uv1 = Triangle.getInterpolatedAttribute( uv1, a, b, c, barycoord, new Vector2() );
  302. }
  303. if ( normal ) {
  304. intersection.normal = Triangle.getInterpolatedAttribute( normal, a, b, c, barycoord, new Vector3() );
  305. if ( intersection.normal.dot( ray.direction ) > 0 ) {
  306. intersection.normal.multiplyScalar( - 1 );
  307. }
  308. }
  309. const face = {
  310. a: a,
  311. b: b,
  312. c: c,
  313. normal: new Vector3(),
  314. materialIndex: 0
  315. };
  316. Triangle.getNormal( _vA, _vB, _vC, face.normal );
  317. intersection.face = face;
  318. intersection.barycoord = barycoord;
  319. }
  320. return intersection;
  321. }
  322. export { Mesh };
粤ICP备19079148号