未验证 提交 cf38d5bd 编写于 作者: M Michael Herzog 提交者: GitHub

WireframeGeometry: Prevent duplicate edges. (#22097)

* WireframeGeometry: Prevent duplicate edges.

* Update WireframeGeometry.js
上级 c7444b70
......@@ -19,12 +19,12 @@ class WireframeGeometry extends BufferGeometry {
// buffer
const vertices = [];
const edges = new Set();
// helper variables
const edge = [ 0, 0 ], edges = {};
const vertex = new Vector3();
const start = new Vector3();
const end = new Vector3();
if ( geometry.index !== null ) {
......@@ -46,23 +46,23 @@ class WireframeGeometry extends BufferGeometry {
const group = groups[ o ];
const start = group.start;
const count = group.count;
const groupStart = group.start;
const groupCount = group.count;
for ( let i = start, l = ( start + count ); i < l; i += 3 ) {
for ( let i = groupStart, l = ( groupStart + groupCount ); i < l; i += 3 ) {
for ( let j = 0; j < 3; j ++ ) {
const edge1 = indices.getX( i + j );
const edge2 = indices.getX( i + ( j + 1 ) % 3 );
edge[ 0 ] = Math.min( edge1, edge2 ); // sorting prevents duplicates
edge[ 1 ] = Math.max( edge1, edge2 );
const index1 = indices.getX( i + j );
const index2 = indices.getX( i + ( j + 1 ) % 3 );
const key = edge[ 0 ] + ',' + edge[ 1 ];
start.fromBufferAttribute( position, index1 );
end.fromBufferAttribute( position, index2 );
if ( edges[ key ] === undefined ) {
if ( isUniqueEdge( start, end, edges ) === true ) {
edges[ key ] = { index1: edge[ 0 ], index2: edge[ 1 ] };
vertices.push( start.x, start.y, start.z );
vertices.push( end.x, end.y, end.z );
}
......@@ -72,20 +72,6 @@ class WireframeGeometry extends BufferGeometry {
}
// generate vertices
for ( const key in edges ) {
const e = edges[ key ];
vertex.fromBufferAttribute( position, e.index1 );
vertices.push( vertex.x, vertex.y, vertex.z );
vertex.fromBufferAttribute( position, e.index2 );
vertices.push( vertex.x, vertex.y, vertex.z );
}
} else {
// non-indexed BufferGeometry
......@@ -100,12 +86,17 @@ class WireframeGeometry extends BufferGeometry {
// e.g. the first triangle has the following edges: (0,1),(1,2),(2,0)
const index1 = 3 * i + j;
vertex.fromBufferAttribute( position, index1 );
vertices.push( vertex.x, vertex.y, vertex.z );
const index2 = 3 * i + ( ( j + 1 ) % 3 );
vertex.fromBufferAttribute( position, index2 );
vertices.push( vertex.x, vertex.y, vertex.z );
start.fromBufferAttribute( position, index1 );
end.fromBufferAttribute( position, index2 );
if ( isUniqueEdge( start, end, edges ) === true ) {
vertices.push( start.x, start.y, start.z );
vertices.push( end.x, end.y, end.z );
}
}
......@@ -121,5 +112,23 @@ class WireframeGeometry extends BufferGeometry {
}
function isUniqueEdge( start, end, edges ) {
const hash1 = `${start.x},${start.y},${start.z}-${end.x},${end.y},${end.z}`;
const hash2 = `${end.x},${end.y},${end.z}-${start.x},${start.y},${start.z}`; // coincident edge
if ( edges.has( hash1 ) === true || edges.has( hash2 ) === true ) {
return false;
} else {
edges.add( hash1, hash2 );
return true;
}
}
export { WireframeGeometry };
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册