Mesh.js 11 KB

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