Mesh.js 8.1 KB

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