Mesh.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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.morphTargetInfluences = [];
  20. this.morphTargetDictionary = {};
  21. for ( var m = 0, ml = this.geometry.morphTargets.length; m < ml; m ++ ) {
  22. this.morphTargetInfluences.push( 0 );
  23. this.morphTargetDictionary[ this.geometry.morphTargets[ m ].name ] = m;
  24. }
  25. }
  26. };
  27. THREE.Mesh.prototype.getMorphTargetIndexByName = function ( name ) {
  28. if ( this.morphTargetDictionary[ name ] !== undefined ) {
  29. return this.morphTargetDictionary[ name ];
  30. }
  31. console.warn( 'THREE.Mesh.getMorphTargetIndexByName: morph target ' + name + ' does not exist. Returning 0.' );
  32. return 0;
  33. };
  34. THREE.Mesh.prototype.raycast = ( function () {
  35. var inverseMatrix = new THREE.Matrix4();
  36. var ray = new THREE.Ray();
  37. var sphere = new THREE.Sphere();
  38. var vA = new THREE.Vector3();
  39. var vB = new THREE.Vector3();
  40. var vC = new THREE.Vector3();
  41. var tempA = new THREE.Vector3();
  42. var tempB = new THREE.Vector3();
  43. var tempC = new THREE.Vector3();
  44. var uvA = new THREE.Vector2();
  45. var uvB = new THREE.Vector2();
  46. var uvC = new THREE.Vector2();
  47. var barycoord = new THREE.Vector3();
  48. var intersectionPoint = new THREE.Vector3();
  49. var intersectionPointWorld = new THREE.Vector3();
  50. function uvIntersection( point, p1, p2, p3, uv1, uv2, uv3 ) {
  51. THREE.Triangle.barycoordFromPoint( point, p1, p2, p3, barycoord );
  52. uv1.multiplyScalar( barycoord.x );
  53. uv2.multiplyScalar( barycoord.y );
  54. uv3.multiplyScalar( barycoord.z );
  55. uv1.add( uv2 ).add( uv3 );
  56. return uv1.clone();
  57. }
  58. return function raycast( raycaster, intersects ) {
  59. var geometry = this.geometry;
  60. var material = this.material;
  61. if ( material === undefined ) return;
  62. // Checking boundingSphere distance to ray
  63. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  64. sphere.copy( geometry.boundingSphere );
  65. sphere.applyMatrix4( this.matrixWorld );
  66. if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) {
  67. return;
  68. }
  69. // Check boundingBox before continuing
  70. inverseMatrix.getInverse( this.matrixWorld );
  71. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  72. if ( geometry.boundingBox !== null ) {
  73. if ( ray.isIntersectionBox( geometry.boundingBox ) === false ) {
  74. return;
  75. }
  76. }
  77. var a, b, c;
  78. if ( geometry instanceof THREE.BufferGeometry ) {
  79. var index = geometry.index;
  80. var attributes = geometry.attributes;
  81. if ( index !== null ) {
  82. var indices = index.array;
  83. var positions = attributes.position.array;
  84. var offsets = geometry.groups;
  85. if ( offsets.length === 0 ) {
  86. offsets = [ {
  87. start: 0,
  88. count: indices.length
  89. } ];
  90. }
  91. for ( var oi = 0, ol = offsets.length; oi < ol; ++ oi ) {
  92. var offset = offsets[ oi ];
  93. var start = offset.start;
  94. var count = offset.count;
  95. for ( var i = start, il = start + count; i < il; i += 3 ) {
  96. a = indices[ i ];
  97. b = indices[ i + 1 ];
  98. c = indices[ i + 2 ];
  99. vA.fromArray( positions, a * 3 );
  100. vB.fromArray( positions, b * 3 );
  101. vC.fromArray( positions, c * 3 );
  102. if ( material.side === THREE.BackSide ) {
  103. if ( ray.intersectTriangle( vC, vB, vA, true, intersectionPoint ) === null ) continue;
  104. } else {
  105. if ( ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide, intersectionPoint ) === null ) continue;
  106. }
  107. intersectionPointWorld.copy( intersectionPoint );
  108. intersectionPointWorld.applyMatrix4( this.matrixWorld );
  109. var distance = raycaster.ray.origin.distanceTo( intersectionPointWorld );
  110. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  111. var uv;
  112. if ( attributes.uv !== undefined ) {
  113. var uvs = attributes.uv.array;
  114. uvA.fromArray( uvs, a * 2 );
  115. uvB.fromArray( uvs, b * 2 );
  116. uvC.fromArray( uvs, c * 2 );
  117. uv = uvIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC );
  118. }
  119. intersects.push( {
  120. distance: distance,
  121. point: intersectionPointWorld.clone(),
  122. uv: uv,
  123. face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
  124. faceIndex: Math.floor( i / 3 ), // triangle number in indices buffer semantics
  125. object: this
  126. } );
  127. }
  128. }
  129. } else {
  130. var positions = attributes.position.array;
  131. for ( var i = 0, il = positions.length; i < il; i += 9 ) {
  132. vA.fromArray( positions, i );
  133. vB.fromArray( positions, i + 3 );
  134. vC.fromArray( positions, i + 6 );
  135. if ( material.side === THREE.BackSide ) {
  136. if ( ray.intersectTriangle( vC, vB, vA, true, intersectionPoint ) === null ) continue;
  137. } else {
  138. if ( ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide, intersectionPoint ) === null ) continue;
  139. }
  140. intersectionPointWorld.copy( intersectionPoint );
  141. intersectionPointWorld.applyMatrix4( this.matrixWorld );
  142. var distance = raycaster.ray.origin.distanceTo( intersectionPointWorld );
  143. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  144. var uv;
  145. if ( attributes.uv !== undefined ) {
  146. var uvs = attributes.uv.array;
  147. uvA.fromArray( uvs, i );
  148. uvB.fromArray( uvs, i + 2 );
  149. uvC.fromArray( uvs, i + 4 );
  150. uv = uvIntersection( intersectionPoint, vA, vB, vC, uvA, uvB, uvC );
  151. }
  152. a = i / 3;
  153. b = a + 1;
  154. c = a + 2;
  155. intersects.push( {
  156. distance: distance,
  157. point: intersectionPointWorld.clone(),
  158. uv: uv,
  159. face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
  160. index: a, // triangle number in positions buffer semantics
  161. object: this
  162. } );
  163. }
  164. }
  165. } else if ( geometry instanceof THREE.Geometry ) {
  166. var isFaceMaterial = material instanceof THREE.MeshFaceMaterial;
  167. var materials = isFaceMaterial === true ? material.materials : null;
  168. var vertices = geometry.vertices;
  169. var faces = geometry.faces;
  170. for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
  171. var face = faces[ f ];
  172. var faceMaterial = isFaceMaterial === true ? materials[ face.materialIndex ] : material;
  173. if ( faceMaterial === undefined ) continue;
  174. a = vertices[ face.a ];
  175. b = vertices[ face.b ];
  176. c = vertices[ face.c ];
  177. if ( faceMaterial.morphTargets === true ) {
  178. var morphTargets = geometry.morphTargets;
  179. var morphInfluences = this.morphTargetInfluences;
  180. vA.set( 0, 0, 0 );
  181. vB.set( 0, 0, 0 );
  182. vC.set( 0, 0, 0 );
  183. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  184. var influence = morphInfluences[ t ];
  185. if ( influence === 0 ) continue;
  186. var targets = morphTargets[ t ].vertices;
  187. vA.addScaledVector( tempA.subVectors( targets[ face.a ], a ), influence );
  188. vB.addScaledVector( tempB.subVectors( targets[ face.b ], b ), influence );
  189. vC.addScaledVector( tempC.subVectors( targets[ face.c ], c ), influence );
  190. }
  191. vA.add( a );
  192. vB.add( b );
  193. vC.add( c );
  194. a = vA;
  195. b = vB;
  196. c = vC;
  197. }
  198. if ( faceMaterial.side === THREE.BackSide ) {
  199. if ( ray.intersectTriangle( c, b, a, true, intersectionPoint ) === null ) continue;
  200. } else {
  201. if ( ray.intersectTriangle( a, b, c, faceMaterial.side !== THREE.DoubleSide, intersectionPoint ) === null ) continue;
  202. }
  203. intersectionPointWorld.copy( intersectionPoint );
  204. intersectionPointWorld.applyMatrix4( this.matrixWorld );
  205. var distance = raycaster.ray.origin.distanceTo( intersectionPointWorld );
  206. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  207. var uv;
  208. if ( geometry.faceVertexUvs[ 0 ].length > 0 ) {
  209. var uvs = geometry.faceVertexUvs[ 0 ][ f ];
  210. uvA.copy( uvs[ 0 ] );
  211. uvB.copy( uvs[ 1 ] );
  212. uvC.copy( uvs[ 2 ] );
  213. uv = uvIntersection( intersectionPoint, a, b, c, uvA, uvB, uvC );
  214. }
  215. intersects.push( {
  216. distance: distance,
  217. point: intersectionPointWorld.clone(),
  218. uv: uv,
  219. face: face,
  220. faceIndex: f,
  221. object: this
  222. } );
  223. }
  224. }
  225. };
  226. }() );
  227. THREE.Mesh.prototype.clone = function () {
  228. return new this.constructor( this.geometry, this.material ).copy( this );
  229. };
  230. THREE.Mesh.prototype.toJSON = function ( meta ) {
  231. var data = THREE.Object3D.prototype.toJSON.call( this, meta );
  232. // only serialize if not in meta geometries cache
  233. if ( meta.geometries[ this.geometry.uuid ] === undefined ) {
  234. meta.geometries[ this.geometry.uuid ] = this.geometry.toJSON( meta );
  235. }
  236. // only serialize if not in meta materials cache
  237. if ( meta.materials[ this.material.uuid ] === undefined ) {
  238. meta.materials[ this.material.uuid ] = this.material.toJSON( meta );
  239. }
  240. data.object.geometry = this.geometry.uuid;
  241. data.object.material = this.material.uuid;
  242. return data;
  243. };
粤ICP备19079148号