Mesh.js 7.9 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.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. console.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. var tempA = new THREE.Vector3();
  43. var tempB = new THREE.Vector3();
  44. var tempC = new THREE.Vector3();
  45. return function ( raycaster, intersects ) {
  46. var geometry = this.geometry;
  47. var material = this.material;
  48. if ( material === undefined ) return;
  49. // Checking boundingSphere distance to ray
  50. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  51. sphere.copy( geometry.boundingSphere );
  52. sphere.applyMatrix4( this.matrixWorld );
  53. if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) {
  54. return;
  55. }
  56. // Check boundingBox before continuing
  57. inverseMatrix.getInverse( this.matrixWorld );
  58. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  59. if ( geometry.boundingBox !== null ) {
  60. if ( ray.isIntersectionBox( geometry.boundingBox ) === false ) {
  61. return;
  62. }
  63. }
  64. var a, b, c;
  65. if ( geometry instanceof THREE.BufferGeometry ) {
  66. var attributes = geometry.attributes;
  67. if ( attributes.index !== undefined ) {
  68. var indices = attributes.index.array;
  69. var positions = attributes.position.array;
  70. var offsets = geometry.drawcalls;
  71. if ( offsets.length === 0 ) {
  72. offsets = [ { start: 0, count: indices.length, index: 0 } ];
  73. }
  74. for ( var oi = 0, ol = offsets.length; oi < ol; ++ oi ) {
  75. var start = offsets[ oi ].start;
  76. var count = offsets[ oi ].count;
  77. var index = offsets[ oi ].index;
  78. for ( var i = start, il = start + count; i < il; i += 3 ) {
  79. a = index + indices[ i ];
  80. b = index + indices[ i + 1 ];
  81. c = index + indices[ i + 2 ];
  82. vA.fromArray( positions, a * 3 );
  83. vB.fromArray( positions, b * 3 );
  84. vC.fromArray( positions, c * 3 );
  85. if ( material.side === THREE.BackSide ) {
  86. var intersectionPoint = ray.intersectTriangle( vC, vB, vA, true );
  87. } else {
  88. var intersectionPoint = ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
  89. }
  90. if ( intersectionPoint === null ) continue;
  91. intersectionPoint.applyMatrix4( this.matrixWorld );
  92. var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
  93. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  94. intersects.push( {
  95. distance: distance,
  96. point: intersectionPoint,
  97. face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
  98. faceIndex: Math.floor( i / 3 ), // triangle number in indices buffer semantics
  99. object: this
  100. } );
  101. }
  102. }
  103. } else {
  104. var positions = attributes.position.array;
  105. for ( var i = 0, j = 0, il = positions.length; i < il; i += 3, j += 9 ) {
  106. a = i;
  107. b = i + 1;
  108. c = i + 2;
  109. vA.fromArray( positions, j );
  110. vB.fromArray( positions, j + 3 );
  111. vC.fromArray( positions, j + 6 );
  112. if ( material.side === THREE.BackSide ) {
  113. var intersectionPoint = ray.intersectTriangle( vC, vB, vA, true );
  114. } else {
  115. var intersectionPoint = ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
  116. }
  117. if ( intersectionPoint === null ) continue;
  118. intersectionPoint.applyMatrix4( this.matrixWorld );
  119. var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
  120. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  121. intersects.push( {
  122. distance: distance,
  123. point: intersectionPoint,
  124. face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
  125. index: Math.floor(i/3), // triangle number in positions buffer semantics
  126. object: this
  127. } );
  128. }
  129. }
  130. } else if ( geometry instanceof THREE.Geometry ) {
  131. var isFaceMaterial = material instanceof THREE.MeshFaceMaterial;
  132. var materials = isFaceMaterial === true ? material.materials : null;
  133. var vertices = geometry.vertices;
  134. var faces = geometry.faces;
  135. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  136. var face = faces[ f ];
  137. var faceMaterial = isFaceMaterial === true ? materials[ face.materialIndex ] : material;
  138. if ( faceMaterial === undefined ) continue;
  139. a = vertices[ face.a ];
  140. b = vertices[ face.b ];
  141. c = vertices[ face.c ];
  142. if ( faceMaterial.morphTargets === true ) {
  143. var morphTargets = geometry.morphTargets;
  144. var morphInfluences = this.morphTargetInfluences;
  145. vA.set( 0, 0, 0 );
  146. vB.set( 0, 0, 0 );
  147. vC.set( 0, 0, 0 );
  148. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  149. var influence = morphInfluences[ t ];
  150. if ( influence === 0 ) continue;
  151. var targets = morphTargets[ t ].vertices;
  152. vA.addScaledVector(tempA.subVectors(targets[ face.a ], a),influence);
  153. vB.addScaledVector(tempB.subVectors(targets[ face.b ], b),influence);
  154. vC.addScaledVector(tempC.subVectors(targets[ face.c ], c),influence);
  155. }
  156. vA.add( a );
  157. vB.add( b );
  158. vC.add( c );
  159. a = vA;
  160. b = vB;
  161. c = vC;
  162. }
  163. if ( faceMaterial.side === THREE.BackSide ) {
  164. var intersectionPoint = ray.intersectTriangle( c, b, a, true );
  165. } else {
  166. var intersectionPoint = ray.intersectTriangle( a, b, c, faceMaterial.side !== THREE.DoubleSide );
  167. }
  168. if ( intersectionPoint === null ) continue;
  169. intersectionPoint.applyMatrix4( this.matrixWorld );
  170. var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
  171. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  172. intersects.push( {
  173. distance: distance,
  174. point: intersectionPoint,
  175. face: face,
  176. faceIndex: f,
  177. object: this
  178. } );
  179. }
  180. }
  181. };
  182. }() );
  183. THREE.Mesh.prototype.clone = function ( object, recursive ) {
  184. if ( object === undefined ) object = new THREE.Mesh( this.geometry, this.material );
  185. THREE.Object3D.prototype.clone.call( this, object, recursive );
  186. return object;
  187. };
  188. THREE.Mesh.prototype.toJSON = function ( meta ) {
  189. var data = THREE.Object3D.prototype.toJSON.call( this, meta );
  190. // only serialize if not in meta geometries cache
  191. if ( meta.geometries[ this.geometry.uuid ] === undefined ) {
  192. meta.geometries[ this.geometry.uuid ] = this.geometry.toJSON( meta );
  193. }
  194. // only serialize if not in meta materials cache
  195. if ( meta.materials[ this.material.uuid ] === undefined ) {
  196. meta.materials[ this.material.uuid ] = this.material.toJSON( meta );
  197. }
  198. data.object.geometry = this.geometry.uuid;
  199. data.object.material = this.material.uuid;
  200. return data;
  201. };