Mesh.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. var matrixWorld = this.matrixWorld;
  102. if ( material === undefined ) return;
  103. // Checking boundingSphere distance to ray
  104. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  105. sphere.copy( geometry.boundingSphere );
  106. sphere.applyMatrix4( matrixWorld );
  107. if ( raycaster.ray.intersectsSphere( sphere ) === false ) return;
  108. //
  109. inverseMatrix.getInverse( matrixWorld );
  110. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  111. // Check boundingBox before continuing
  112. if ( geometry.boundingBox !== null ) {
  113. if ( ray.intersectsBox( geometry.boundingBox ) === false ) return;
  114. }
  115. var uvs, intersection;
  116. if ( geometry instanceof THREE.BufferGeometry ) {
  117. var a, b, c;
  118. var index = geometry.index;
  119. var attributes = geometry.attributes;
  120. var positions = attributes.position.array;
  121. if ( attributes.uv !== undefined ) {
  122. uvs = attributes.uv.array;
  123. }
  124. if ( index !== null ) {
  125. var indices = index.array;
  126. for ( var i = 0, l = indices.length; i < l; i += 3 ) {
  127. a = indices[ i ];
  128. b = indices[ i + 1 ];
  129. c = indices[ i + 2 ];
  130. intersection = checkBufferGeometryIntersection( this, raycaster, ray, positions, uvs, a, b, c );
  131. if ( intersection ) {
  132. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indices buffer semantics
  133. intersects.push( intersection );
  134. }
  135. }
  136. } else {
  137. for ( var i = 0, l = positions.length; i < l; i += 9 ) {
  138. a = i / 3;
  139. b = a + 1;
  140. c = a + 2;
  141. intersection = checkBufferGeometryIntersection( this, raycaster, ray, positions, uvs, a, b, c );
  142. if ( intersection ) {
  143. intersection.index = a; // triangle number in positions buffer semantics
  144. intersects.push( intersection );
  145. }
  146. }
  147. }
  148. } else if ( geometry instanceof THREE.Geometry ) {
  149. var fvA, fvB, fvC;
  150. var isFaceMaterial = material instanceof THREE.MultiMaterial;
  151. var materials = isFaceMaterial === true ? material.materials : null;
  152. var vertices = geometry.vertices;
  153. var faces = geometry.faces;
  154. var faceVertexUvs = geometry.faceVertexUvs[ 0 ];
  155. if ( faceVertexUvs.length > 0 ) uvs = faceVertexUvs;
  156. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  157. var face = faces[ f ];
  158. var faceMaterial = isFaceMaterial === true ? materials[ face.materialIndex ] : material;
  159. if ( faceMaterial === undefined ) continue;
  160. fvA = vertices[ face.a ];
  161. fvB = vertices[ face.b ];
  162. fvC = vertices[ face.c ];
  163. if ( faceMaterial.morphTargets === true ) {
  164. var morphTargets = geometry.morphTargets;
  165. var morphInfluences = this.morphTargetInfluences;
  166. vA.set( 0, 0, 0 );
  167. vB.set( 0, 0, 0 );
  168. vC.set( 0, 0, 0 );
  169. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  170. var influence = morphInfluences[ t ];
  171. if ( influence === 0 ) continue;
  172. var targets = morphTargets[ t ].vertices;
  173. vA.addScaledVector( tempA.subVectors( targets[ face.a ], fvA ), influence );
  174. vB.addScaledVector( tempB.subVectors( targets[ face.b ], fvB ), influence );
  175. vC.addScaledVector( tempC.subVectors( targets[ face.c ], fvC ), influence );
  176. }
  177. vA.add( fvA );
  178. vB.add( fvB );
  179. vC.add( fvC );
  180. fvA = vA;
  181. fvB = vB;
  182. fvC = vC;
  183. }
  184. intersection = checkIntersection( this, raycaster, ray, fvA, fvB, fvC, intersectionPoint );
  185. if ( intersection ) {
  186. if ( uvs ) {
  187. var uvs_f = uvs[ f ];
  188. uvA.copy( uvs_f[ 0 ] );
  189. uvB.copy( uvs_f[ 1 ] );
  190. uvC.copy( uvs_f[ 2 ] );
  191. intersection.uv = uvIntersection( intersectionPoint, fvA, fvB, fvC, uvA, uvB, uvC );
  192. }
  193. intersection.face = face;
  194. intersection.faceIndex = f;
  195. intersects.push( intersection );
  196. }
  197. }
  198. }
  199. };
  200. }() );
  201. THREE.Mesh.prototype.clone = function () {
  202. return new this.constructor( this.geometry, this.material ).copy( this );
  203. };