Mesh.js 11 KB

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