提交 efe24f89 编写于 作者: M Mugen87

Example: Remove soft tabs from geometries

上级 a17655cf
......@@ -24,16 +24,16 @@
function ConvexBufferGeometry( points ) {
THREE.BufferGeometry.call( this );
THREE.BufferGeometry.call( this );
this.type = 'ConvexBufferGeometry';
// buffers
// buffers
var vertices = [];
var normals = [];
var vertices = [];
var normals = [];
// execute QuickHull
// execute QuickHull
if ( THREE.QuickHull === undefined ) {
......@@ -41,36 +41,36 @@
}
var quickHull = new THREE.QuickHull().setFromPoints( points );
var quickHull = new THREE.QuickHull().setFromPoints( points );
// generate vertices and normals
// generate vertices and normals
var faces = quickHull.faces;
var faces = quickHull.faces;
for ( var i = 0; i < faces.length; i ++ ) {
for ( var i = 0; i < faces.length; i ++ ) {
var face = faces[ i ];
var edge = face.edge;
var face = faces[ i ];
var edge = face.edge;
// we move along a doubly-connected edge list to access all face points (see HalfEdge docs)
// we move along a doubly-connected edge list to access all face points (see HalfEdge docs)
do {
do {
var point = edge.head().point;
var point = edge.head().point;
vertices.push( point.x, point.y, point.z );
normals.push( face.normal.x, face.normal.y, face.normal.z );
vertices.push( point.x, point.y, point.z );
normals.push( face.normal.x, face.normal.y, face.normal.z );
edge = edge.next;
edge = edge.next;
} while ( edge !== face.edge );
} while ( edge !== face.edge );
}
}
// build geometry
// build geometry
this.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
this.addAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
this.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
this.addAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
}
......
......@@ -20,317 +20,317 @@
function DecalGeometry( mesh, position, orientation, size ) {
THREE.BufferGeometry.call( this );
THREE.BufferGeometry.call( this );
this.type = 'DecalGeometry';
this.type = 'DecalGeometry';
// buffers
// buffers
var vertices = [];
var normals = [];
var uvs = [];
var vertices = [];
var normals = [];
var uvs = [];
// helpers
// helpers
var plane = new THREE.Vector3();
var plane = new THREE.Vector3();
// this matrix represents the transformation of the decal projector
// this matrix represents the transformation of the decal projector
var projectorMatrix = new THREE.Matrix4();
projectorMatrix.makeRotationFromEuler( orientation );
projectorMatrix.setPosition( position );
var projectorMatrix = new THREE.Matrix4();
projectorMatrix.makeRotationFromEuler( orientation );
projectorMatrix.setPosition( position );
var projectorMatrixInverse = new THREE.Matrix4().getInverse( projectorMatrix );
var projectorMatrixInverse = new THREE.Matrix4().getInverse( projectorMatrix );
// generate buffers
// generate buffers
generate();
generate();
// build geometry
// build geometry
this.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
this.addAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
this.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
this.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
this.addAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
this.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
function generate() {
function generate() {
var i;
var geometry = new THREE.BufferGeometry();
var decalVertices = [];
var i;
var geometry = new THREE.BufferGeometry();
var decalVertices = [];
var vertex = new THREE.Vector3();
var normal = new THREE.Vector3();
var vertex = new THREE.Vector3();
var normal = new THREE.Vector3();
// handle different geometry types
// handle different geometry types
if ( mesh.geometry.isGeometry ) {
if ( mesh.geometry.isGeometry ) {
geometry.fromGeometry( mesh.geometry );
geometry.fromGeometry( mesh.geometry );
} else {
} else {
geometry.copy( mesh.geometry );
geometry.copy( mesh.geometry );
}
}
var positionAttribute = geometry.attributes.position;
var normalAttribute = geometry.attributes.normal;
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
// 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 ) {
if ( geometry.index !== null ) {
// indexed BufferGeometry
// indexed BufferGeometry
var index = geometry.index;
var index = geometry.index;
for ( i = 0; i < index.count; i ++ ) {
for ( i = 0; i < index.count; i ++ ) {
vertex.fromBufferAttribute( positionAttribute, index.getX( i ) );
normal.fromBufferAttribute( normalAttribute, index.getX( i ) );
vertex.fromBufferAttribute( positionAttribute, index.getX( i ) );
normal.fromBufferAttribute( normalAttribute, index.getX( i ) );
pushDecalVertex( decalVertices, vertex, normal );
pushDecalVertex( decalVertices, vertex, normal );
}
}
} else {
} else {
// non-indexed BufferGeometry
// non-indexed BufferGeometry
for ( i = 0; i < positionAttribute.count; i ++ ) {
for ( i = 0; i < positionAttribute.count; i ++ ) {
vertex.fromBufferAttribute( positionAttribute, i );
normal.fromBufferAttribute( normalAttribute, i );
vertex.fromBufferAttribute( positionAttribute, i );
normal.fromBufferAttribute( normalAttribute, i );
pushDecalVertex( decalVertices, vertex, normal );
pushDecalVertex( decalVertices, vertex, normal );
}
}
}
}
// second, clip the geometry so that it doesn't extend out from the projector
// 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 ) );
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 ) );
// third, generate final vertices, normals and uvs
// third, generate final vertices, normals and uvs
for ( i = 0; i < decalVertices.length; i ++ ) {
for ( i = 0; i < decalVertices.length; i ++ ) {
var decalVertex = decalVertices[ i ];
var decalVertex = decalVertices[ i ];
// create texture coordinates (we are still in projector space)
// create texture coordinates (we are still in projector space)
uvs.push(
0.5 + ( decalVertex.position.x / size.x ),
0.5 + ( decalVertex.position.y / size.y )
);
uvs.push(
0.5 + ( decalVertex.position.x / size.x ),
0.5 + ( decalVertex.position.y / size.y )
);
// transform the vertex back to world space
// transform the vertex back to world space
decalVertex.position.applyMatrix4( projectorMatrix );
decalVertex.position.applyMatrix4( projectorMatrix );
// now create vertex and normal buffer data
// 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 );
vertices.push( decalVertex.position.x, decalVertex.position.y, decalVertex.position.z );
normals.push( decalVertex.normal.x, decalVertex.normal.y, decalVertex.normal.z );
}
}
}
}
function pushDecalVertex( decalVertices, vertex, normal ) {
function pushDecalVertex( decalVertices, vertex, normal ) {
// transform the vertex to world space, then to projector space
// transform the vertex to world space, then to projector space
vertex.applyMatrix4( mesh.matrix );
vertex.applyMatrix4( projectorMatrixInverse );
vertex.applyMatrix4( mesh.matrix );
vertex.applyMatrix4( projectorMatrixInverse );
decalVertices.push( new DecalVertex( vertex.clone(), normal.clone() ) );
decalVertices.push( new DecalVertex( vertex.clone(), normal.clone() ) );
}
}
function clipGeometry( inVertices, plane ) {
function clipGeometry( inVertices, plane ) {
var outVertices = [];
var outVertices = [];
var s = 0.5 * Math.abs( size.dot( plane ) );
var s = 0.5 * Math.abs( size.dot( plane ) );
// a single iteration clips one face,
// which consists of three consecutive 'DecalVertex' objects
// a single iteration clips one face,
// which consists of three consecutive 'DecalVertex' objects
for ( var i = 0; i < inVertices.length; i += 3 ) {
for ( var i = 0; i < inVertices.length; i += 3 ) {
var v1Out, v2Out, v3Out, total = 0;
var nV1, nV2, nV3, nV4;
var v1Out, v2Out, v3Out, total = 0;
var nV1, nV2, nV3, nV4;
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;
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;
v1Out = d1 > 0;
v2Out = d2 > 0;
v3Out = d3 > 0;
// calculate, how many vertices of the face lie outside of the clipping plane
// calculate, how many vertices of the face lie outside of the clipping plane
total = ( v1Out ? 1 : 0 ) + ( v2Out ? 1 : 0 ) + ( v3Out ? 1 : 0 );
total = ( v1Out ? 1 : 0 ) + ( v2Out ? 1 : 0 ) + ( v3Out ? 1 : 0 );
switch ( total ) {
switch ( total ) {
case 0: {
case 0: {
// the entire face lies inside of the plane, no clipping needed
// 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;
outVertices.push( inVertices[ i ] );
outVertices.push( inVertices[ i + 1 ] );
outVertices.push( inVertices[ i + 2 ] );
break;
}
}
case 1: {
case 1: {
// one vertex lies outside of the plane, perform clipping
// one vertex lies outside of the plane, perform clipping
if ( v1Out ) {
if ( v1Out ) {
nV1 = inVertices[ i + 1 ];
nV2 = inVertices[ i + 2 ];
nV3 = clip( inVertices[ i ], nV1, plane, s );
nV4 = clip( inVertices[ i ], nV2, plane, s );
nV1 = inVertices[ i + 1 ];
nV2 = inVertices[ i + 2 ];
nV3 = clip( inVertices[ i ], nV1, plane, s );
nV4 = clip( inVertices[ i ], nV2, plane, s );
}
}
if ( v2Out ) {
if ( v2Out ) {
nV1 = inVertices[ i ];
nV2 = inVertices[ i + 2 ];
nV3 = clip( inVertices[ i + 1 ], nV1, plane, s );
nV4 = clip( inVertices[ i + 1 ], nV2, plane, s );
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() );
outVertices.push( nV1.clone() );
outVertices.push( nV3 );
outVertices.push( nV2.clone() );
outVertices.push( nV1.clone() );
outVertices.push( nV2.clone() );
outVertices.push( nV3.clone() );
outVertices.push( nV4 );
break;
outVertices.push( nV2.clone() );
outVertices.push( nV3.clone() );
outVertices.push( nV4 );
break;
}
}
if ( v3Out ) {
if ( v3Out ) {
nV1 = inVertices[ i ];
nV2 = inVertices[ i + 1 ];
nV3 = clip( inVertices[ i + 2 ], nV1, plane, s );
nV4 = clip( inVertices[ i + 2 ], nV2, plane, s );
nV1 = inVertices[ i ];
nV2 = inVertices[ i + 1 ];
nV3 = clip( inVertices[ i + 2 ], nV1, plane, s );
nV4 = clip( inVertices[ i + 2 ], nV2, plane, s );
}
}
outVertices.push( nV1.clone() );
outVertices.push( nV2.clone() );
outVertices.push( nV3 );
outVertices.push( nV1.clone() );
outVertices.push( nV2.clone() );
outVertices.push( nV3 );
outVertices.push( nV4 );
outVertices.push( nV3.clone() );
outVertices.push( nV2.clone() );
outVertices.push( nV4 );
outVertices.push( nV3.clone() );
outVertices.push( nV2.clone() );
break;
break;
}
}
case 2: {
case 2: {
// two vertices lies outside of the plane, perform clipping
// two vertices lies outside of the plane, perform clipping
if ( ! v1Out ) {
if ( ! v1Out ) {
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 );
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 );
}
}
if ( ! v2Out ) {
if ( ! v2Out ) {
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 );
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 );
}
}
if ( ! v3Out ) {
if ( ! v3Out ) {
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 );
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 );
}
}
break;
break;
}
}
case 3: {
case 3: {
// the entire face lies outside of the plane, so let's discard the corresponding vertices
// the entire face lies outside of the plane, so let's discard the corresponding vertices
break;
break;
}
}
}
}
}
}
return outVertices;
return outVertices;
}
}
function clip( v0, v1, p, s ) {
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 ) - s;
var d1 = v1.position.dot( p ) - s;
var s0 = d0 / ( d0 - d1 );
var s0 = d0 / ( d0 - d1 );
var v = new DecalVertex(
new THREE.Vector3(
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 THREE.Vector3(
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 )
)
);
var v = new DecalVertex(
new THREE.Vector3(
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 THREE.Vector3(
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 )
)
);
// need to clip more values (texture coordinates)? do it this way:
// intersectpoint.value = a.value + s * ( b.value - a.value );
// need to clip more values (texture coordinates)? do it this way:
// intersectpoint.value = a.value + s * ( b.value - a.value );
return v;
return v;
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册