Mesh.js 10 KB

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