Mesh.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.updateMorphTargets();
  13. };
  14. THREE.Mesh.prototype = Object.create( THREE.Object3D.prototype );
  15. THREE.Mesh.prototype.constructor = THREE.Mesh;
  16. THREE.Mesh.prototype.updateMorphTargets = function () {
  17. if ( this.geometry.morphTargets !== undefined && this.geometry.morphTargets.length > 0 ) {
  18. this.morphTargetBase = - 1;
  19. this.morphTargetForcedOrder = [];
  20. this.morphTargetInfluences = [];
  21. this.morphTargetDictionary = {};
  22. for ( var m = 0, ml = this.geometry.morphTargets.length; m < ml; m ++ ) {
  23. this.morphTargetInfluences.push( 0 );
  24. this.morphTargetDictionary[ this.geometry.morphTargets[ m ].name ] = m;
  25. }
  26. }
  27. };
  28. THREE.Mesh.prototype.getMorphTargetIndexByName = function ( name ) {
  29. if ( this.morphTargetDictionary[ name ] !== undefined ) {
  30. return this.morphTargetDictionary[ name ];
  31. }
  32. THREE.warn( 'THREE.Mesh.getMorphTargetIndexByName: morph target ' + name + ' does not exist. Returning 0.' );
  33. return 0;
  34. };
  35. THREE.Mesh.prototype.raycast = ( function () {
  36. var inverseMatrix = new THREE.Matrix4();
  37. var ray = new THREE.Ray();
  38. var sphere = new THREE.Sphere();
  39. var vA = new THREE.Vector3();
  40. var vB = new THREE.Vector3();
  41. var vC = new THREE.Vector3();
  42. return function ( raycaster, intersects ) {
  43. var geometry = this.geometry;
  44. // Checking boundingSphere distance to ray
  45. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  46. sphere.copy( geometry.boundingSphere );
  47. sphere.applyMatrix4( this.matrixWorld );
  48. if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) {
  49. return;
  50. }
  51. // Check boundingBox before continuing
  52. inverseMatrix.getInverse( this.matrixWorld );
  53. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  54. if ( geometry.boundingBox !== null ) {
  55. if ( ray.isIntersectionBox( geometry.boundingBox ) === false ) {
  56. return;
  57. }
  58. }
  59. if ( geometry instanceof THREE.BufferGeometry ) {
  60. var material = this.material;
  61. if ( material === undefined ) return;
  62. var attributes = geometry.attributes;
  63. var a, b, c;
  64. var precision = raycaster.precision;
  65. if ( attributes.index !== undefined ) {
  66. var indices = attributes.index.array;
  67. var positions = attributes.position.array;
  68. var offsets = geometry.offsets;
  69. if ( offsets.length === 0 ) {
  70. offsets = [ { start: 0, count: indices.length, index: 0 } ];
  71. }
  72. for ( var oi = 0, ol = offsets.length; oi < ol; ++ oi ) {
  73. var start = offsets[ oi ].start;
  74. var count = offsets[ oi ].count;
  75. var index = offsets[ oi ].index;
  76. for ( var i = start, il = start + count; i < il; i += 3 ) {
  77. a = index + indices[ i ];
  78. b = index + indices[ i + 1 ];
  79. c = index + indices[ i + 2 ];
  80. vA.fromArray( positions, a * 3 );
  81. vB.fromArray( positions, b * 3 );
  82. vC.fromArray( positions, c * 3 );
  83. if ( material.side === THREE.BackSide ) {
  84. var intersectionPoint = ray.intersectTriangle( vC, vB, vA, true );
  85. } else {
  86. var intersectionPoint = ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
  87. }
  88. if ( intersectionPoint === null ) continue;
  89. intersectionPoint.applyMatrix4( this.matrixWorld );
  90. var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
  91. if ( distance < precision || distance < raycaster.near || distance > raycaster.far ) continue;
  92. intersects.push( {
  93. distance: distance,
  94. point: intersectionPoint,
  95. face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
  96. faceIndex: null,
  97. object: this
  98. } );
  99. }
  100. }
  101. } else {
  102. var positions = attributes.position.array;
  103. for ( var i = 0, j = 0, il = positions.length; i < il; i += 3, j += 9 ) {
  104. a = i;
  105. b = i + 1;
  106. c = i + 2;
  107. vA.fromArray( positions, j );
  108. vB.fromArray( positions, j + 3 );
  109. vC.fromArray( positions, j + 6 );
  110. if ( material.side === THREE.BackSide ) {
  111. var intersectionPoint = ray.intersectTriangle( vC, vB, vA, true );
  112. } else {
  113. var intersectionPoint = ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
  114. }
  115. if ( intersectionPoint === null ) continue;
  116. intersectionPoint.applyMatrix4( this.matrixWorld );
  117. var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
  118. if ( distance < precision || distance < raycaster.near || distance > raycaster.far ) continue;
  119. intersects.push( {
  120. distance: distance,
  121. point: intersectionPoint,
  122. face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
  123. faceIndex: null,
  124. object: this
  125. } );
  126. }
  127. }
  128. } else if ( geometry instanceof THREE.Geometry ) {
  129. var isFaceMaterial = this.material instanceof THREE.MeshFaceMaterial;
  130. var objectMaterials = isFaceMaterial === true ? this.material.materials : null;
  131. var a, b, c, d;
  132. var precision = raycaster.precision;
  133. var vertices = geometry.vertices;
  134. for ( var f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  135. var face = geometry.faces[ f ];
  136. var material = isFaceMaterial === true ? objectMaterials[ face.materialIndex ] : this.material;
  137. if ( material === undefined ) continue;
  138. a = vertices[ face.a ];
  139. b = vertices[ face.b ];
  140. c = vertices[ face.c ];
  141. if ( material.morphTargets === true ) {
  142. var morphTargets = geometry.morphTargets;
  143. var morphInfluences = this.morphTargetInfluences;
  144. vA.set( 0, 0, 0 );
  145. vB.set( 0, 0, 0 );
  146. vC.set( 0, 0, 0 );
  147. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  148. var influence = morphInfluences[ t ];
  149. if ( influence === 0 ) continue;
  150. var targets = morphTargets[ t ].vertices;
  151. vA.x += ( targets[ face.a ].x - a.x ) * influence;
  152. vA.y += ( targets[ face.a ].y - a.y ) * influence;
  153. vA.z += ( targets[ face.a ].z - a.z ) * influence;
  154. vB.x += ( targets[ face.b ].x - b.x ) * influence;
  155. vB.y += ( targets[ face.b ].y - b.y ) * influence;
  156. vB.z += ( targets[ face.b ].z - b.z ) * influence;
  157. vC.x += ( targets[ face.c ].x - c.x ) * influence;
  158. vC.y += ( targets[ face.c ].y - c.y ) * influence;
  159. vC.z += ( targets[ face.c ].z - c.z ) * influence;
  160. }
  161. vA.add( a );
  162. vB.add( b );
  163. vC.add( c );
  164. a = vA;
  165. b = vB;
  166. c = vC;
  167. }
  168. if ( material.side === THREE.BackSide ) {
  169. var intersectionPoint = ray.intersectTriangle( c, b, a, true );
  170. } else {
  171. var intersectionPoint = ray.intersectTriangle( a, b, c, material.side !== THREE.DoubleSide );
  172. }
  173. if ( intersectionPoint === null ) continue;
  174. intersectionPoint.applyMatrix4( this.matrixWorld );
  175. var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
  176. if ( distance < precision || distance < raycaster.near || distance > raycaster.far ) continue;
  177. intersects.push( {
  178. distance: distance,
  179. point: intersectionPoint,
  180. face: face,
  181. faceIndex: f,
  182. object: this
  183. } );
  184. }
  185. }
  186. };
  187. }() );
  188. THREE.Mesh.prototype.clone = function ( object, recursive ) {
  189. if ( object === undefined ) object = new THREE.Mesh( this.geometry, this.material );
  190. THREE.Object3D.prototype.clone.call( this, object, recursive );
  191. return object;
  192. };
粤ICP备19079148号