|
@@ -5,16 +5,18 @@ import { Matrix4 } from '../math/Matrix4';
|
|
|
|
|
|
/**
|
|
|
* @author Mugen87 / https://github.com/Mugen87
|
|
|
+ * @author spite / https://github.com/spite
|
|
|
+ *
|
|
|
+ * reference: http://blog.wolfire.com/2009/06/how-to-project-decals/
|
|
|
+ *
|
|
|
*/
|
|
|
|
|
|
-function DecalGeometry( mesh, position, rotation, dimensions, check ) {
|
|
|
+function DecalGeometry( mesh, position, orientation, size ) {
|
|
|
|
|
|
BufferGeometry.call( this );
|
|
|
|
|
|
this.type = 'DecalGeometry';
|
|
|
|
|
|
- check = check || new Vector3( 1, 1, 1 );
|
|
|
-
|
|
|
// buffers
|
|
|
|
|
|
var vertices = [];
|
|
@@ -23,14 +25,16 @@ function DecalGeometry( mesh, position, rotation, dimensions, check ) {
|
|
|
|
|
|
// helpers
|
|
|
|
|
|
+ var plane = new Vector3();
|
|
|
+
|
|
|
+ // this matrix represents the transformation of the decal projector
|
|
|
+
|
|
|
var projectorMatrix = new Matrix4();
|
|
|
- projectorMatrix.makeRotationFromEuler( rotation );
|
|
|
+ projectorMatrix.makeRotationFromEuler( orientation );
|
|
|
projectorMatrix.setPosition( position );
|
|
|
|
|
|
var projectorMatrixInverse = new Matrix4().getInverse( projectorMatrix );
|
|
|
|
|
|
- var plane = new Vector3();
|
|
|
-
|
|
|
// generate buffers
|
|
|
|
|
|
generate();
|
|
@@ -65,6 +69,11 @@ function DecalGeometry( mesh, position, rotation, dimensions, check ) {
|
|
|
var positionAttribute = geometry.attributes.position;
|
|
|
var normalAttribute = geometry.attributes.normal;
|
|
|
|
|
|
+ // first, create an array of 'DecalVertex' objects
|
|
|
+ // three consecutive 'DecalVertex' objects represent a single face
|
|
|
+ //
|
|
|
+ // this data structure will be later used to perform the clipping
|
|
|
+
|
|
|
if ( geometry.index !== null ) {
|
|
|
|
|
|
// indexed BufferGeometry
|
|
@@ -76,10 +85,7 @@ function DecalGeometry( mesh, position, rotation, dimensions, check ) {
|
|
|
vertex.fromBufferAttribute( positionAttribute, index[ i ] );
|
|
|
normal.fromBufferAttribute( normalAttribute, index[ i ] );
|
|
|
|
|
|
- vertex.applyMatrix4( mesh.matrix );
|
|
|
- vertex.applyMatrix4( projectorMatrixInverse );
|
|
|
-
|
|
|
- decalVertices.push( new DecalVertex( vertex.clone(), normal.clone() ) );
|
|
|
+ pushDecalVertex( decalVertices, vertex, normal );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -92,49 +98,40 @@ function DecalGeometry( mesh, position, rotation, dimensions, check ) {
|
|
|
vertex.fromBufferAttribute( positionAttribute, i );
|
|
|
normal.fromBufferAttribute( normalAttribute, i );
|
|
|
|
|
|
- vertex.applyMatrix4( mesh.matrix );
|
|
|
- vertex.applyMatrix4( projectorMatrixInverse );
|
|
|
-
|
|
|
- decalVertices.push( new DecalVertex( vertex.clone(), normal.clone() ) );
|
|
|
+ pushDecalVertex( decalVertices, vertex, normal );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- // check
|
|
|
-
|
|
|
- if ( check.x ) {
|
|
|
-
|
|
|
- decalVertices = clipGeometry( decalVertices, plane.set( 1, 0, 0 ) );
|
|
|
- decalVertices = clipGeometry( decalVertices, plane.set( - 1, 0, 0 ) );
|
|
|
-
|
|
|
- }
|
|
|
- if ( check.y ) {
|
|
|
-
|
|
|
- decalVertices = clipGeometry( decalVertices, plane.set( 0, 1, 0 ) );
|
|
|
- decalVertices = clipGeometry( decalVertices, plane.set( 0, - 1, 0 ) );
|
|
|
-
|
|
|
- }
|
|
|
- if ( check.z ) {
|
|
|
-
|
|
|
- decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, 1 ) );
|
|
|
- decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, - 1 ) );
|
|
|
+ // second, clip the geometry so that it doesn't extend out from the projector
|
|
|
|
|
|
- }
|
|
|
+ decalVertices = clipGeometry( decalVertices, plane.set( 1, 0, 0 ) );
|
|
|
+ decalVertices = clipGeometry( decalVertices, plane.set( - 1, 0, 0 ) );
|
|
|
+ decalVertices = clipGeometry( decalVertices, plane.set( 0, 1, 0 ) );
|
|
|
+ decalVertices = clipGeometry( decalVertices, plane.set( 0, - 1, 0 ) );
|
|
|
+ decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, 1 ) );
|
|
|
+ decalVertices = clipGeometry( decalVertices, plane.set( 0, 0, - 1 ) );
|
|
|
|
|
|
- // generate vertices, normals and uvs
|
|
|
+ // third, generate final vertices, normals and uvs
|
|
|
|
|
|
for ( i = 0; i < decalVertices.length; i ++ ) {
|
|
|
|
|
|
var decalVertex = decalVertices[ i ];
|
|
|
|
|
|
+ // create texture coordinates (we are still in projector space)
|
|
|
+
|
|
|
uvs.push(
|
|
|
- 0.5 + ( decalVertex.position.x / dimensions.x ),
|
|
|
- 0.5 + ( decalVertex.position.y / dimensions.y )
|
|
|
+ 0.5 + ( decalVertex.position.x / size.x ),
|
|
|
+ 0.5 + ( decalVertex.position.y / size.y )
|
|
|
);
|
|
|
|
|
|
+ // transform the vertex back to world space
|
|
|
+
|
|
|
decalVertex.position.applyMatrix4( projectorMatrix );
|
|
|
|
|
|
+ // now create vertex and normal buffer data
|
|
|
+
|
|
|
vertices.push( decalVertex.position.x, decalVertex.position.y, decalVertex.position.z );
|
|
|
normals.push( decalVertex.normal.x, decalVertex.normal.y, decalVertex.normal.z );
|
|
|
|
|
@@ -142,55 +139,75 @@ function DecalGeometry( mesh, position, rotation, dimensions, check ) {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ function pushDecalVertex( decalVertices, vertex, normal ) {
|
|
|
+
|
|
|
+ // transform the vertex to world space, then to projector space
|
|
|
+
|
|
|
+ vertex.applyMatrix4( mesh.matrix );
|
|
|
+ vertex.applyMatrix4( projectorMatrixInverse );
|
|
|
+
|
|
|
+ decalVertices.push( new DecalVertex( vertex.clone(), normal.clone() ) );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
function clipGeometry( inVertices, plane ) {
|
|
|
|
|
|
var outVertices = [];
|
|
|
|
|
|
- var size = 0.5 * Math.abs( dimensions.dot( plane ) );
|
|
|
+ var s = 0.5 * Math.abs( size.dot( plane ) );
|
|
|
+
|
|
|
+ // a single iteration clips one face,
|
|
|
+ // which consists of three consecutive 'DecalVertex' objects
|
|
|
|
|
|
- for ( var j = 0; j < inVertices.length; j += 3 ) {
|
|
|
+ for ( var i = 0; i < inVertices.length; i += 3 ) {
|
|
|
|
|
|
var v1Out, v2Out, v3Out, total = 0;
|
|
|
var nV1, nV2, nV3, nV4;
|
|
|
|
|
|
- var d1 = inVertices[ j + 0 ].position.dot( plane ) - size;
|
|
|
- var d2 = inVertices[ j + 1 ].position.dot( plane ) - size;
|
|
|
- var d3 = inVertices[ j + 2 ].position.dot( plane ) - size;
|
|
|
+ var d1 = inVertices[ i + 0 ].position.dot( plane ) - s;
|
|
|
+ var d2 = inVertices[ i + 1 ].position.dot( plane ) - s;
|
|
|
+ var d3 = inVertices[ i + 2 ].position.dot( plane ) - s;
|
|
|
|
|
|
v1Out = d1 > 0;
|
|
|
v2Out = d2 > 0;
|
|
|
v3Out = d3 > 0;
|
|
|
|
|
|
+ // calculate, how many vertices of the face lie outside of the clipping plane
|
|
|
+
|
|
|
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 ] );
|
|
|
+ // the entire face lies inside of the plane, no clipping needed
|
|
|
+
|
|
|
+ outVertices.push( inVertices[ i ] );
|
|
|
+ outVertices.push( inVertices[ i + 1 ] );
|
|
|
+ outVertices.push( inVertices[ i + 2 ] );
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case 1: {
|
|
|
|
|
|
+ // one vertex lies outside of the plane, perform clipping
|
|
|
+
|
|
|
if ( v1Out ) {
|
|
|
|
|
|
- nV1 = inVertices[ j + 1 ];
|
|
|
- nV2 = inVertices[ j + 2 ];
|
|
|
- nV3 = clip( inVertices[ j ], nV1, plane, size );
|
|
|
- nV4 = clip( inVertices[ j ], nV2, plane, size );
|
|
|
+ nV1 = inVertices[ i + 1 ];
|
|
|
+ nV2 = inVertices[ i + 2 ];
|
|
|
+ nV3 = clip( inVertices[ i ], nV1, plane, s );
|
|
|
+ nV4 = clip( inVertices[ i ], nV2, plane, s );
|
|
|
|
|
|
}
|
|
|
|
|
|
if ( v2Out ) {
|
|
|
|
|
|
- nV1 = inVertices[ j ];
|
|
|
- nV2 = inVertices[ j + 2 ];
|
|
|
- nV3 = clip( inVertices[ j + 1 ], nV1, plane, size );
|
|
|
- nV4 = clip( inVertices[ j + 1 ], nV2, plane, size );
|
|
|
+ nV1 = inVertices[ i ];
|
|
|
+ nV2 = inVertices[ i + 2 ];
|
|
|
+ nV3 = clip( inVertices[ i + 1 ], nV1, plane, s );
|
|
|
+ nV4 = clip( inVertices[ i + 1 ], nV2, plane, s );
|
|
|
|
|
|
outVertices.push( nV3 );
|
|
|
outVertices.push( nV2.clone() );
|
|
@@ -205,10 +222,10 @@ function DecalGeometry( mesh, position, rotation, dimensions, check ) {
|
|
|
|
|
|
if ( v3Out ) {
|
|
|
|
|
|
- nV1 = inVertices[ j ];
|
|
|
- nV2 = inVertices[ j + 1 ];
|
|
|
- nV3 = clip( inVertices[ j + 2 ], nV1, plane, size );
|
|
|
- nV4 = clip( inVertices[ j + 2 ], nV2, plane, size );
|
|
|
+ nV1 = inVertices[ i ];
|
|
|
+ nV2 = inVertices[ i + 1 ];
|
|
|
+ nV3 = clip( inVertices[ i + 2 ], nV1, plane, s );
|
|
|
+ nV4 = clip( inVertices[ i + 2 ], nV2, plane, s );
|
|
|
|
|
|
}
|
|
|
|
|
@@ -226,11 +243,13 @@ function DecalGeometry( mesh, position, rotation, dimensions, check ) {
|
|
|
|
|
|
case 2: {
|
|
|
|
|
|
+ // two vertices lies outside of the plane, perform clipping
|
|
|
+
|
|
|
if ( ! v1Out ) {
|
|
|
|
|
|
- nV1 = inVertices[ j ].clone();
|
|
|
- nV2 = clip( nV1, inVertices[ j + 1 ], plane, size );
|
|
|
- nV3 = clip( nV1, inVertices[ j + 2 ], plane, size );
|
|
|
+ nV1 = inVertices[ i ].clone();
|
|
|
+ nV2 = clip( nV1, inVertices[ i + 1 ], plane, s );
|
|
|
+ nV3 = clip( nV1, inVertices[ i + 2 ], plane, s );
|
|
|
outVertices.push( nV1 );
|
|
|
outVertices.push( nV2 );
|
|
|
outVertices.push( nV3 );
|
|
@@ -239,9 +258,9 @@ function DecalGeometry( mesh, position, rotation, dimensions, check ) {
|
|
|
|
|
|
if ( ! v2Out ) {
|
|
|
|
|
|
- nV1 = inVertices[ j + 1 ].clone();
|
|
|
- nV2 = clip( nV1, inVertices[ j + 2 ], plane, size );
|
|
|
- nV3 = clip( nV1, inVertices[ j ], plane, size );
|
|
|
+ nV1 = inVertices[ i + 1 ].clone();
|
|
|
+ nV2 = clip( nV1, inVertices[ i + 2 ], plane, s );
|
|
|
+ nV3 = clip( nV1, inVertices[ i ], plane, s );
|
|
|
outVertices.push( nV1 );
|
|
|
outVertices.push( nV2 );
|
|
|
outVertices.push( nV3 );
|
|
@@ -250,9 +269,9 @@ function DecalGeometry( mesh, position, rotation, dimensions, check ) {
|
|
|
|
|
|
if ( ! v3Out ) {
|
|
|
|
|
|
- nV1 = inVertices[ j + 2 ].clone();
|
|
|
- nV2 = clip( nV1, inVertices[ j ], plane, size );
|
|
|
- nV3 = clip( nV1, inVertices[ j + 1 ], plane, size );
|
|
|
+ nV1 = inVertices[ i + 2 ].clone();
|
|
|
+ nV2 = clip( nV1, inVertices[ i ], plane, s );
|
|
|
+ nV3 = clip( nV1, inVertices[ i + 1 ], plane, s );
|
|
|
outVertices.push( nV1 );
|
|
|
outVertices.push( nV2 );
|
|
|
outVertices.push( nV3 );
|
|
@@ -265,6 +284,8 @@ function DecalGeometry( mesh, position, rotation, dimensions, check ) {
|
|
|
|
|
|
case 3: {
|
|
|
|
|
|
+ // the entire face lies outside of the plane, so let's discard the corresponding vertices
|
|
|
+
|
|
|
break;
|
|
|
|
|
|
}
|
|
@@ -277,22 +298,23 @@ function DecalGeometry( mesh, position, rotation, dimensions, check ) {
|
|
|
|
|
|
}
|
|
|
|
|
|
- function clip( v0, v1, p, size ) {
|
|
|
+ function clip( v0, v1, p, s ) {
|
|
|
+
|
|
|
+ var d0 = v0.position.dot( p ) - s;
|
|
|
+ var d1 = v1.position.dot( p ) - s;
|
|
|
|
|
|
- var d0 = v0.position.dot( p ) - size;
|
|
|
- var d1 = v1.position.dot( p ) - size;
|
|
|
+ var s0 = d0 / ( d0 - d1 );
|
|
|
|
|
|
- var s = d0 / ( d0 - d1 );
|
|
|
var v = new DecalVertex(
|
|
|
new Vector3(
|
|
|
- v0.position.x + s * ( v1.position.x - v0.position.x ),
|
|
|
- v0.position.y + s * ( v1.position.y - v0.position.y ),
|
|
|
- v0.position.z + s * ( v1.position.z - v0.position.z )
|
|
|
+ v0.position.x + s0 * ( v1.position.x - v0.position.x ),
|
|
|
+ v0.position.y + s0 * ( v1.position.y - v0.position.y ),
|
|
|
+ v0.position.z + s0 * ( v1.position.z - v0.position.z )
|
|
|
),
|
|
|
new 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 )
|
|
|
+ v0.normal.x + s0 * ( v1.normal.x - v0.normal.x ),
|
|
|
+ v0.normal.y + s0 * ( v1.normal.y - v0.normal.y ),
|
|
|
+ v0.normal.z + s0 * ( v1.normal.z - v0.normal.z )
|
|
|
)
|
|
|
);
|
|
|
|