123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- /**
- * @author mrdoob / http://mrdoob.com/
- * @author alteredq / http://alteredqualia.com/
- * @author mikael emtinger / http://gomo.se/
- * @author jonobr1 / http://jonobr1.com/
- */
- THREE.Mesh = function ( geometry, material ) {
- THREE.Object3D.call( this );
- this.type = 'Mesh';
- this.geometry = geometry !== undefined ? geometry : new THREE.Geometry();
- this.material = material !== undefined ? material : new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } );
- this.updateMorphTargets();
- };
- THREE.Mesh.prototype = Object.create( THREE.Object3D.prototype );
- THREE.Mesh.prototype.constructor = THREE.Mesh;
- THREE.Mesh.prototype.updateMorphTargets = function () {
- if ( this.geometry.morphTargets !== undefined && this.geometry.morphTargets.length > 0 ) {
- this.morphTargetBase = - 1;
- this.morphTargetForcedOrder = [];
- this.morphTargetInfluences = [];
- this.morphTargetDictionary = {};
- for ( var m = 0, ml = this.geometry.morphTargets.length; m < ml; m ++ ) {
- this.morphTargetInfluences.push( 0 );
- this.morphTargetDictionary[ this.geometry.morphTargets[ m ].name ] = m;
- }
- }
- };
- THREE.Mesh.prototype.getMorphTargetIndexByName = function ( name ) {
- if ( this.morphTargetDictionary[ name ] !== undefined ) {
- return this.morphTargetDictionary[ name ];
- }
- console.warn( 'THREE.Mesh.getMorphTargetIndexByName: morph target ' + name + ' does not exist. Returning 0.' );
- return 0;
- };
- THREE.Mesh.prototype.raycast = ( function () {
- var inverseMatrix = new THREE.Matrix4();
- var ray = new THREE.Ray();
- var sphere = new THREE.Sphere();
- var vA = new THREE.Vector3();
- var vB = new THREE.Vector3();
- var vC = new THREE.Vector3();
-
- var tempA = new THREE.Vector3();
- var tempB = new THREE.Vector3();
- var tempC = new THREE.Vector3();
- return function ( raycaster, intersects ) {
- var geometry = this.geometry;
- var material = this.material;
- if ( material === undefined ) return;
- // Checking boundingSphere distance to ray
- if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
- sphere.copy( geometry.boundingSphere );
- sphere.applyMatrix4( this.matrixWorld );
- if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) {
- return;
- }
- // Check boundingBox before continuing
- inverseMatrix.getInverse( this.matrixWorld );
- ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
- if ( geometry.boundingBox !== null ) {
- if ( ray.isIntersectionBox( geometry.boundingBox ) === false ) {
- return;
- }
- }
- var a, b, c;
- if ( geometry instanceof THREE.BufferGeometry ) {
- var attributes = geometry.attributes;
- if ( attributes.index !== undefined ) {
- var indices = attributes.index.array;
- var positions = attributes.position.array;
- var offsets = geometry.drawcalls;
- if ( offsets.length === 0 ) {
- offsets = [ { start: 0, count: indices.length, index: 0 } ];
- }
- for ( var oi = 0, ol = offsets.length; oi < ol; ++ oi ) {
- var start = offsets[ oi ].start;
- var count = offsets[ oi ].count;
- var index = offsets[ oi ].index;
- for ( var i = start, il = start + count; i < il; i += 3 ) {
- a = index + indices[ i ];
- b = index + indices[ i + 1 ];
- c = index + indices[ i + 2 ];
- vA.fromArray( positions, a * 3 );
- vB.fromArray( positions, b * 3 );
- vC.fromArray( positions, c * 3 );
- if ( material.side === THREE.BackSide ) {
- var intersectionPoint = ray.intersectTriangle( vC, vB, vA, true );
- } else {
- var intersectionPoint = ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
- }
- if ( intersectionPoint === null ) continue;
- intersectionPoint.applyMatrix4( this.matrixWorld );
- var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
- if ( distance < raycaster.near || distance > raycaster.far ) continue;
- intersects.push( {
- distance: distance,
- point: intersectionPoint,
- face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
- faceIndex: Math.floor( i / 3 ), // triangle number in indices buffer semantics
- object: this
- } );
- }
- }
- } else {
- var positions = attributes.position.array;
- for ( var i = 0, j = 0, il = positions.length; i < il; i += 3, j += 9 ) {
- a = i;
- b = i + 1;
- c = i + 2;
- vA.fromArray( positions, j );
- vB.fromArray( positions, j + 3 );
- vC.fromArray( positions, j + 6 );
- if ( material.side === THREE.BackSide ) {
- var intersectionPoint = ray.intersectTriangle( vC, vB, vA, true );
- } else {
- var intersectionPoint = ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
- }
- if ( intersectionPoint === null ) continue;
- intersectionPoint.applyMatrix4( this.matrixWorld );
- var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
- if ( distance < raycaster.near || distance > raycaster.far ) continue;
- intersects.push( {
- distance: distance,
- point: intersectionPoint,
- face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
- index: Math.floor(i/3), // triangle number in positions buffer semantics
- object: this
- } );
- }
- }
- } else if ( geometry instanceof THREE.Geometry ) {
- var isFaceMaterial = material instanceof THREE.MeshFaceMaterial;
- var materials = isFaceMaterial === true ? material.materials : null;
- var vertices = geometry.vertices;
- var faces = geometry.faces;
- for ( var f = 0, fl = faces.length; f < fl; f ++ ) {
- var face = faces[ f ];
- var faceMaterial = isFaceMaterial === true ? materials[ face.materialIndex ] : material;
- if ( faceMaterial === undefined ) continue;
- a = vertices[ face.a ];
- b = vertices[ face.b ];
- c = vertices[ face.c ];
- if ( faceMaterial.morphTargets === true ) {
- var morphTargets = geometry.morphTargets;
- var morphInfluences = this.morphTargetInfluences;
- vA.set( 0, 0, 0 );
- vB.set( 0, 0, 0 );
- vC.set( 0, 0, 0 );
- for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
- var influence = morphInfluences[ t ];
- if ( influence === 0 ) continue;
- var targets = morphTargets[ t ].vertices;
-
- vA.addScaledVector(tempA.subVectors(targets[ face.a ], a),influence);
- vB.addScaledVector(tempB.subVectors(targets[ face.b ], b),influence);
- vC.addScaledVector(tempC.subVectors(targets[ face.c ], c),influence);
- }
- vA.add( a );
- vB.add( b );
- vC.add( c );
- a = vA;
- b = vB;
- c = vC;
- }
- if ( faceMaterial.side === THREE.BackSide ) {
- var intersectionPoint = ray.intersectTriangle( c, b, a, true );
- } else {
- var intersectionPoint = ray.intersectTriangle( a, b, c, faceMaterial.side !== THREE.DoubleSide );
- }
- if ( intersectionPoint === null ) continue;
- intersectionPoint.applyMatrix4( this.matrixWorld );
- var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
- if ( distance < raycaster.near || distance > raycaster.far ) continue;
- intersects.push( {
- distance: distance,
- point: intersectionPoint,
- face: face,
- faceIndex: f,
- object: this
- } );
- }
- }
- };
- }() );
- THREE.Mesh.prototype.clone = function ( object, recursive ) {
- if ( object === undefined ) object = new THREE.Mesh( this.geometry, this.material );
- THREE.Object3D.prototype.clone.call( this, object, recursive );
- return object;
- };
- THREE.Mesh.prototype.toJSON = function ( meta ) {
- var data = THREE.Object3D.prototype.toJSON.call( this, meta );
- // only serialize if not in meta geometries cache
- if ( meta.geometries[ this.geometry.uuid ] === undefined ) {
- meta.geometries[ this.geometry.uuid ] = this.geometry.toJSON( meta );
- }
- // only serialize if not in meta materials cache
- if ( meta.materials[ this.material.uuid ] === undefined ) {
- meta.materials[ this.material.uuid ] = this.material.toJSON( meta );
- }
- data.object.geometry = this.geometry.uuid;
- data.object.material = this.material.uuid;
- return data;
- };
|