123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- THREE.DecalVertex = function( v, n ) {
- this.vertex = v;
- this.normal = n;
- };
- THREE.DecalVertex.prototype.clone = function() {
- return new THREE.DecalVertex( this.vertex.clone(), this.normal.clone() );
- };
- THREE.DecalGeometry = function( mesh, position, rotation, dimensions, check ) {
- THREE.Geometry.call( this );
- if ( check === undefined ) check = null;
- check = check || new THREE.Vector3( 1, 1, 1 );
- this.uvs = [];
- this.cube = new THREE.Mesh( new THREE.BoxGeometry( dimensions.x, dimensions.y, dimensions.z ), new THREE.MeshBasicMaterial() );
- this.cube.rotation.set( rotation.x, rotation.y, rotation.z );
- this.cube.position.copy( position );
- this.cube.scale.set( 1, 1, 1 );
- this.cube.updateMatrix();
- this.iCubeMatrix = ( new THREE.Matrix4() ).getInverse( this.cube.matrix );
- this.faceIndices = [ 'a', 'b', 'c', 'd' ];
- this.clipFace = function( inVertices, plane ) {
- var size = .5 * Math.abs( ( dimensions.clone() ).dot( plane ) );
- function clip( v0, v1, p ) {
- var d0 = v0.vertex.dot( p ) - size,
- d1 = v1.vertex.dot( p ) - size;
- var s = d0 / ( d0 - d1 );
- var v = new THREE.DecalVertex(
- new THREE.Vector3(
- v0.vertex.x + s * ( v1.vertex.x - v0.vertex.x ),
- v0.vertex.y + s * ( v1.vertex.y - v0.vertex.y ),
- v0.vertex.z + s * ( v1.vertex.z - v0.vertex.z )
- ),
- new THREE.Vector3(
- v0.normal.x + s * ( v1.normal.x - v0.normal.x ),
- v0.normal.y + s * ( v1.normal.y - v0.normal.y ),
- v0.normal.z + s * ( v1.normal.z - v0.normal.z )
- )
- );
- // need to clip more values (texture coordinates)? do it this way:
- //intersectpoint.value = a.value + s*(b.value-a.value);
- return v;
- }
- if ( inVertices.length === 0 ) return [];
- var outVertices = [];
- for ( var j = 0; j < inVertices.length; j += 3 ) {
- var v1Out, v2Out, v3Out, total = 0;
- var d1 = inVertices[ j + 0 ].vertex.dot( plane ) - size,
- d2 = inVertices[ j + 1 ].vertex.dot( plane ) - size,
- d3 = inVertices[ j + 2 ].vertex.dot( plane ) - size;
- v1Out = d1 > 0;
- v2Out = d2 > 0;
- v3Out = d3 > 0;
- total = ( v1Out ? 1 : 0 ) + ( v2Out ? 1 : 0 ) + ( v3Out ? 1 : 0 );
- switch ( total ) {
- case 0: {
- outVertices.push( inVertices[ j ] );
- outVertices.push( inVertices[ j + 1 ] );
- outVertices.push( inVertices[ j + 2 ] );
- break;
- }
- case 1: {
- var nV1, nV2, nV3, nV4;
- if ( v1Out ) {
- nV1 = inVertices[ j + 1 ];
- nV2 = inVertices[ j + 2 ];
- nV3 = clip( inVertices[ j ], nV1, plane );
- nV4 = clip( inVertices[ j ], nV2, plane );
- }
- if ( v2Out ) {
- nV1 = inVertices[ j ];
- nV2 = inVertices[ j + 2 ];
- nV3 = clip( inVertices[ j + 1 ], nV1, plane );
- nV4 = clip( inVertices[ j + 1 ], nV2, plane );
- outVertices.push( nV3 );
- outVertices.push( nV2.clone() );
- outVertices.push( nV1.clone() );
- outVertices.push( nV2.clone() );
- outVertices.push( nV3.clone() );
- outVertices.push( nV4 );
- break;
- }
- if ( v3Out ) {
- nV1 = inVertices[ j ];
- nV2 = inVertices[ j + 1 ];
- nV3 = clip( inVertices[ j + 2 ], nV1, plane );
- nV4 = clip( inVertices[ j + 2 ], nV2, plane );
- }
- outVertices.push( nV1.clone() );
- outVertices.push( nV2.clone() );
- outVertices.push( nV3 );
- outVertices.push( nV4 );
- outVertices.push( nV3.clone() );
- outVertices.push( nV2.clone() );
- break;
- }
- case 2: {
- var nV1, nV2, nV3;
- if ( ! v1Out ) {
- nV1 = inVertices[ j ].clone();
- nV2 = clip( nV1, inVertices[ j + 1 ], plane );
- nV3 = clip( nV1, inVertices[ j + 2 ], plane );
- outVertices.push( nV1 );
- outVertices.push( nV2 );
- outVertices.push( nV3 );
- }
- if ( ! v2Out ) {
- nV1 = inVertices[ j + 1 ].clone();
- nV2 = clip( nV1, inVertices[ j + 2 ], plane );
- nV3 = clip( nV1, inVertices[ j ], plane );
- outVertices.push( nV1 );
- outVertices.push( nV2 );
- outVertices.push( nV3 );
- }
- if ( ! v3Out ) {
- nV1 = inVertices[ j + 2 ].clone();
- nV2 = clip( nV1, inVertices[ j ], plane );
- nV3 = clip( nV1, inVertices[ j + 1 ], plane );
- outVertices.push( nV1 );
- outVertices.push( nV2 );
- outVertices.push( nV3 );
- }
- break;
- }
- case 3: {
- break;
- }
- }
- }
- return outVertices;
- };
- this.pushVertex = function( vertices, id, n ) {
- var v = mesh.geometry.vertices[ id ].clone();
- v.applyMatrix4( mesh.matrix );
- v.applyMatrix4( this.iCubeMatrix );
- vertices.push( new THREE.DecalVertex( v, n.clone() ) );
- };
- this.computeDecal = function() {
- var finalVertices = [];
- for ( var i = 0; i < mesh.geometry.faces.length; i ++ ) {
- var f = mesh.geometry.faces[ i ];
- var vertices = [];
- this.pushVertex( vertices, f[ this.faceIndices[ 0 ] ], f.vertexNormals[ 0 ] );
- this.pushVertex( vertices, f[ this.faceIndices[ 1 ] ], f.vertexNormals[ 1 ] );
- this.pushVertex( vertices, f[ this.faceIndices[ 2 ] ], f.vertexNormals[ 2 ] );
- if ( check.x ) {
- vertices = this.clipFace( vertices, new THREE.Vector3( 1, 0, 0 ) );
- vertices = this.clipFace( vertices, new THREE.Vector3( - 1, 0, 0 ) );
- }
- if ( check.y ) {
- vertices = this.clipFace( vertices, new THREE.Vector3( 0, 1, 0 ) );
- vertices = this.clipFace( vertices, new THREE.Vector3( 0, - 1, 0 ) );
- }
- if ( check.z ) {
- vertices = this.clipFace( vertices, new THREE.Vector3( 0, 0, 1 ) );
- vertices = this.clipFace( vertices, new THREE.Vector3( 0, 0, - 1 ) );
- }
- for ( var j = 0; j < vertices.length; j ++ ) {
- var v = vertices[ j ];
- this.uvs.push( new THREE.Vector2(
- .5 + ( v.vertex.x / dimensions.x ),
- .5 + ( v.vertex.y / dimensions.y )
- ) );
- vertices[ j ].vertex.applyMatrix4( this.cube.matrix );
- }
- if ( vertices.length === 0 ) continue;
- finalVertices = finalVertices.concat( vertices );
- }
- for ( var k = 0; k < finalVertices.length; k += 3 ) {
- this.vertices.push(
- finalVertices[ k ].vertex,
- finalVertices[ k + 1 ].vertex,
- finalVertices[ k + 2 ].vertex
- );
- var f = new THREE.Face3(
- k,
- k + 1,
- k + 2
- );
- f.vertexNormals.push( finalVertices[ k + 0 ].normal );
- f.vertexNormals.push( finalVertices[ k + 1 ].normal );
- f.vertexNormals.push( finalVertices[ k + 2 ].normal );
- this.faces.push( f );
- this.faceVertexUvs[ 0 ].push( [
- this.uvs[ k ],
- this.uvs[ k + 1 ],
- this.uvs[ k + 2 ]
- ] );
- }
- this.verticesNeedUpdate = true;
- this.elementsNeedUpdate = true;
- this.morphTargetsNeedUpdate = true;
- this.uvsNeedUpdate = true;
- this.normalsNeedUpdate = true;
- this.colorsNeedUpdate = true;
- this.computeFaceNormals();
- };
- this.computeDecal();
- };
- THREE.DecalGeometry.prototype = Object.create( THREE.Geometry.prototype );
- THREE.DecalGeometry.prototype.constructor = THREE.DecalGeometry;
|