Mesh.js 7.8 KB

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