Mesh.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author mikael emtinger / http://gomo.se/
  5. * @author jonobr1 / http://jonobr1.com/
  6. */
  7. THREE.Mesh = function ( geometry, material ) {
  8. THREE.Object3D.call( this );
  9. this.type = 'Mesh';
  10. this.geometry = geometry !== undefined ? geometry : new THREE.Geometry();
  11. this.material = material !== undefined ? material : new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } );
  12. this.drawMode = THREE.TrianglesDrawMode;
  13. this.updateMorphTargets();
  14. };
  15. THREE.Mesh.prototype = Object.create( THREE.Object3D.prototype );
  16. THREE.Mesh.prototype.constructor = THREE.Mesh;
  17. THREE.Mesh.prototype.setDrawMode = function ( value ) {
  18. this.drawMode = value;
  19. };
  20. THREE.Mesh.prototype.updateMorphTargets = function () {
  21. if ( this.geometry.morphTargets !== undefined && this.geometry.morphTargets.length > 0 ) {
  22. this.morphTargetBase = - 1;
  23. this.morphTargetInfluences = [];
  24. this.morphTargetDictionary = {};
  25. for ( var m = 0, ml = this.geometry.morphTargets.length; m < ml; m ++ ) {
  26. this.morphTargetInfluences.push( 0 );
  27. this.morphTargetDictionary[ this.geometry.morphTargets[ m ].name ] = m;
  28. }
  29. }
  30. };
  31. THREE.Mesh.prototype.getMorphTargetIndexByName = function ( name ) {
  32. if ( this.morphTargetDictionary[ name ] !== undefined ) {
  33. return this.morphTargetDictionary[ name ];
  34. }
  35. console.warn( 'THREE.Mesh.getMorphTargetIndexByName: morph target ' + name + ' does not exist. Returning 0.' );
  36. return 0;
  37. };
  38. THREE.Mesh.prototype.raycast = ( function () {
  39. var inverseMatrix = new THREE.Matrix4();
  40. var ray = new THREE.Ray();
  41. var sphere = new THREE.Sphere();
  42. var vA = new THREE.Vector3();
  43. var vB = new THREE.Vector3();
  44. var vC = new THREE.Vector3();
  45. var tempA = new THREE.Vector3();
  46. var tempB = new THREE.Vector3();
  47. var tempC = new THREE.Vector3();
  48. var uvA = new THREE.Vector2();
  49. var uvB = new THREE.Vector2();
  50. var uvC = new THREE.Vector2();
  51. var barycoord = new THREE.Vector3();
  52. var intersectionPoint = new THREE.Vector3();
  53. var intersectionPointWorld = new THREE.Vector3();
  54. function uvIntersection( point, p1, p2, p3, uv1, uv2, uv3 ) {
  55. THREE.Triangle.barycoordFromPoint( point, p1, p2, p3, barycoord );
  56. uv1.multiplyScalar( barycoord.x );
  57. uv2.multiplyScalar( barycoord.y );
  58. uv3.multiplyScalar( barycoord.z );
  59. uv1.add( uv2 ).add( uv3 );
  60. return uv1.clone();
  61. }
  62. function checkIntersection( object, raycaster, ray, pA, pB, pC, point ) {
  63. var intersect;
  64. var material = object.material;
  65. if ( material.side === THREE.BackSide ) {
  66. intersect = ray.intersectTriangle( pC, pB, pA, true, point );
  67. } else {
  68. intersect = ray.intersectTriangle( pA, pB, pC, material.side !== THREE.DoubleSide, point );
  69. }
  70. if ( intersect === null ) return null;
  71. intersectionPointWorld.copy( point );
  72. intersectionPointWorld.applyMatrix4( object.matrixWorld );
  73. var distance = raycaster.ray.origin.distanceTo( intersectionPointWorld );
  74. if ( distance < raycaster.near || distance > raycaster.far ) return null;
  75. return {
  76. distance: distance,
  77. point: intersectionPointWorld.clone(),
  78. object: object
  79. };
  80. }
  81. function checkBufferGeometryIntersection( object, raycaster, ray, positions, uvs, a, b, c ) {
  82. vA.fromArray( positions, a * 3 );
  83. vB.fromArray( positions, b * 3 );
  84. vC.fromArray( positions, c * 3 );
  85. var intersection = checkIntersection( object, raycaster, ray, vA, vB, vC, intersectionPoint );
  86. if ( intersection ) {
  87. if ( uvs ) {
  88. uvA.fromArray( uvs, a * 2 );
  89. uvB.fromArray( uvs, b * 2 );
  90. uvC.fromArray( uvs, c * 2 );
  91. intersection.uv = uvIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC );
  92. }
  93. intersection.face = new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) );
  94. intersection.faceIndex = a;
  95. }
  96. return intersection;
  97. }
  98. return function raycast( raycaster, intersects ) {
  99. var geometry = this.geometry;
  100. var material = this.material;
  101. if ( material === undefined ) return;
  102. // Checking boundingSphere distance to ray
  103. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  104. var matrixWorld = this.matrixWorld;
  105. sphere.copy( geometry.boundingSphere );
  106. sphere.applyMatrix4( matrixWorld );
  107. if ( raycaster.ray.intersectsSphere( sphere ) === false ) return;
  108. // Check boundingBox before continuing
  109. inverseMatrix.getInverse( matrixWorld );
  110. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  111. if ( geometry.boundingBox !== null ) {
  112. if ( ray.intersectsBox( geometry.boundingBox ) === false ) return;
  113. }
  114. var uvs, intersection;
  115. if ( geometry instanceof THREE.BufferGeometry ) {
  116. var a, b, c;
  117. var index = geometry.index;
  118. var attributes = geometry.attributes;
  119. var positions = attributes.position.array;
  120. if ( attributes.uv !== undefined ) {
  121. uvs = attributes.uv.array;
  122. }
  123. if ( index !== null ) {
  124. var indices = index.array;
  125. for ( var i = 0, l = indices.length; i < l; i += 3 ) {
  126. a = indices[ i ];
  127. b = indices[ i + 1 ];
  128. c = indices[ i + 2 ];
  129. intersection = checkBufferGeometryIntersection( this, raycaster, ray, positions, uvs, a, b, c );
  130. if ( intersection ) {
  131. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indices buffer semantics
  132. intersects.push( intersection );
  133. }
  134. }
  135. } else {
  136. for ( var i = 0, l = positions.length; i < l; i += 9 ) {
  137. a = i / 3;
  138. b = a + 1;
  139. c = a + 2;
  140. intersection = checkBufferGeometryIntersection( this, raycaster, ray, positions, uvs, a, b, c );
  141. if ( intersection ) {
  142. intersection.index = a; // triangle number in positions buffer semantics
  143. intersects.push( intersection );
  144. }
  145. }
  146. }
  147. } else if ( geometry instanceof THREE.Geometry ) {
  148. var fvA, fvB, fvC;
  149. var isFaceMaterial = material instanceof THREE.MeshFaceMaterial;
  150. var materials = isFaceMaterial === true ? material.materials : null;
  151. var vertices = geometry.vertices;
  152. var faces = geometry.faces;
  153. var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
  154. if ( faceVertexUvs.length > 0 ) uvs = faceVertexUvs;
  155. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  156. var face = faces[ f ];
  157. var faceMaterial = isFaceMaterial === true ? materials[ face.materialIndex ] : material;
  158. if ( faceMaterial === undefined ) continue;
  159. fvA = vertices[ face.a ];
  160. fvB = vertices[ face.b ];
  161. fvC = vertices[ face.c ];
  162. if ( faceMaterial.morphTargets === true ) {
  163. var morphTargets = geometry.morphTargets;
  164. var morphInfluences = this.morphTargetInfluences;
  165. vA.set( 0, 0, 0 );
  166. vB.set( 0, 0, 0 );
  167. vC.set( 0, 0, 0 );
  168. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  169. var influence = morphInfluences[ t ];
  170. if ( influence === 0 ) continue;
  171. var targets = morphTargets[ t ].vertices;
  172. vA.addScaledVector( tempA.subVectors( targets[ face.a ], fvA ), influence );
  173. vB.addScaledVector( tempB.subVectors( targets[ face.b ], fvB ), influence );
  174. vC.addScaledVector( tempC.subVectors( targets[ face.c ], fvC ), influence );
  175. }
  176. vA.add( fvA );
  177. vB.add( fvB );
  178. vC.add( fvC );
  179. fvA = vA;
  180. fvB = vB;
  181. fvC = vC;
  182. }
  183. intersection = checkIntersection( this, raycaster, ray, fvA, fvB, fvC, intersectionPoint );
  184. if ( intersection ) {
  185. if ( uvs ) {
  186. var uvs_f = uvs[ f ];
  187. uvA.copy( uvs_f[ 0 ] );
  188. uvB.copy( uvs_f[ 1 ] );
  189. uvC.copy( uvs_f[ 2 ] );
  190. intersection.uv = uvIntersection( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC );
  191. }
  192. intersection.face = face;
  193. intersection.faceIndex = f;
  194. intersects.push( intersection );
  195. }
  196. }
  197. }
  198. };
  199. }() );
  200. THREE.Mesh.prototype.clone = function () {
  201. return new this.constructor( this.geometry, this.material ).copy( this );
  202. };
粤ICP备19079148号