Mesh.js 11 KB

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