Mesh.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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, uv2, 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. if ( uv2 ) {
  137. uvA.fromBufferAttribute( uv2, a );
  138. uvB.fromBufferAttribute( uv2, b );
  139. uvC.fromBufferAttribute( uv2, c );
  140. intersection.uv2 = Triangle.getUV( intersectionPoint, vA, vB, vC, uvA, uvB, uvC, new Vector2() );
  141. }
  142. var face = new Face3( a, b, c );
  143. Triangle.getNormal( vA, vB, vC, face.normal );
  144. intersection.face = face;
  145. }
  146. return intersection;
  147. }
  148. return function raycast( raycaster, intersects ) {
  149. var geometry = this.geometry;
  150. var material = this.material;
  151. var matrixWorld = this.matrixWorld;
  152. if ( material === undefined ) return;
  153. // Checking boundingSphere distance to ray
  154. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  155. sphere.copy( geometry.boundingSphere );
  156. sphere.applyMatrix4( matrixWorld );
  157. if ( raycaster.ray.intersectsSphere( sphere ) === false ) return;
  158. //
  159. inverseMatrix.getInverse( matrixWorld );
  160. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  161. // Check boundingBox before continuing
  162. if ( geometry.boundingBox !== null ) {
  163. if ( ray.intersectsBox( geometry.boundingBox ) === false ) return;
  164. }
  165. var intersection;
  166. if ( geometry.isBufferGeometry ) {
  167. var a, b, c;
  168. var index = geometry.index;
  169. var position = geometry.attributes.position;
  170. var morphPosition = geometry.morphAttributes.position;
  171. var uv = geometry.attributes.uv;
  172. var uv2 = geometry.attributes.uv2;
  173. var groups = geometry.groups;
  174. var drawRange = geometry.drawRange;
  175. var i, j, il, jl;
  176. var group, groupMaterial;
  177. var start, end;
  178. if ( index !== null ) {
  179. // indexed buffer geometry
  180. if ( Array.isArray( material ) ) {
  181. for ( i = 0, il = groups.length; i < il; i ++ ) {
  182. group = groups[ i ];
  183. groupMaterial = material[ group.materialIndex ];
  184. start = Math.max( group.start, drawRange.start );
  185. end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
  186. for ( j = start, jl = end; j < jl; j += 3 ) {
  187. a = index.getX( j );
  188. b = index.getX( j + 1 );
  189. c = index.getX( j + 2 );
  190. intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, ray, position, morphPosition, uv, uv2, a, b, c );
  191. if ( intersection ) {
  192. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
  193. intersection.face.materialIndex = group.materialIndex;
  194. intersects.push( intersection );
  195. }
  196. }
  197. }
  198. } else {
  199. start = Math.max( 0, drawRange.start );
  200. end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  201. for ( i = start, il = end; i < il; i += 3 ) {
  202. a = index.getX( i );
  203. b = index.getX( i + 1 );
  204. c = index.getX( i + 2 );
  205. intersection = checkBufferGeometryIntersection( this, material, raycaster, ray, position, morphPosition, uv, uv2, a, b, c );
  206. if ( intersection ) {
  207. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
  208. intersects.push( intersection );
  209. }
  210. }
  211. }
  212. } else if ( position !== undefined ) {
  213. // non-indexed buffer geometry
  214. if ( Array.isArray( material ) ) {
  215. for ( i = 0, il = groups.length; i < il; i ++ ) {
  216. group = groups[ i ];
  217. groupMaterial = material[ group.materialIndex ];
  218. start = Math.max( group.start, drawRange.start );
  219. end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) );
  220. for ( j = start, jl = end; j < jl; j += 3 ) {
  221. a = j;
  222. b = j + 1;
  223. c = j + 2;
  224. intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, ray, position, morphPosition, uv, uv2, a, b, c );
  225. if ( intersection ) {
  226. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
  227. intersection.face.materialIndex = group.materialIndex;
  228. intersects.push( intersection );
  229. }
  230. }
  231. }
  232. } else {
  233. start = Math.max( 0, drawRange.start );
  234. end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
  235. for ( i = start, il = end; i < il; i += 3 ) {
  236. a = i;
  237. b = i + 1;
  238. c = i + 2;
  239. intersection = checkBufferGeometryIntersection( this, material, raycaster, ray, position, morphPosition, uv, uv2, a, b, c );
  240. if ( intersection ) {
  241. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
  242. intersects.push( intersection );
  243. }
  244. }
  245. }
  246. }
  247. } else if ( geometry.isGeometry ) {
  248. var fvA, fvB, fvC;
  249. var isMultiMaterial = Array.isArray( material );
  250. var vertices = geometry.vertices;
  251. var faces = geometry.faces;
  252. var uvs;
  253. var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
  254. if ( faceVertexUvs.length > 0 ) uvs = faceVertexUvs;
  255. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  256. var face = faces[ f ];
  257. var faceMaterial = isMultiMaterial ? material[ face.materialIndex ] : material;
  258. if ( faceMaterial === undefined ) continue;
  259. fvA = vertices[ face.a ];
  260. fvB = vertices[ face.b ];
  261. fvC = vertices[ face.c ];
  262. intersection = checkIntersection( this, faceMaterial, raycaster, ray, fvA, fvB, fvC, intersectionPoint );
  263. if ( intersection ) {
  264. if ( uvs && uvs[ f ] ) {
  265. var uvs_f = uvs[ f ];
  266. uvA.copy( uvs_f[ 0 ] );
  267. uvB.copy( uvs_f[ 1 ] );
  268. uvC.copy( uvs_f[ 2 ] );
  269. intersection.uv = Triangle.getUV( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC, new Vector2() );
  270. }
  271. intersection.face = face;
  272. intersection.faceIndex = f;
  273. intersects.push( intersection );
  274. }
  275. }
  276. }
  277. };
  278. }() ),
  279. clone: function () {
  280. return new this.constructor( this.geometry, this.material ).copy( this );
  281. }
  282. } );
  283. export { Mesh };
粤ICP备19079148号