modify method
Implementation
BufferGeometry modify(BufferGeometry geometry, int count ) {
geometry = geometry.clone();
final attributes = geometry.attributes;
List<String> keys = [];// this modifier can only process indexed and non-indexed geomtries with a position attribute
attributes.keys.forEach((name){
keys.add(name);
});
for(String name in keys){
if(name != 'position' ){
geometry.deleteAttribute( name );
}
}
geometry = BufferGeometryUtils.mergeVertices( geometry );
//
// put data of original geometry in different data structures
//
final List<Vertex> vertices = [];
final List<Tri> faces = [];
// add vertices
final positionAttribute = geometry.getAttribute( 'position' );
for ( int i = 0; i < positionAttribute.count; i ++ ) {
final v = Vector3().fromBufferAttribute( positionAttribute, i );
final vertex = Vertex( v );
vertices.add(vertex);
}
// add faces
BufferAttribute<NativeArray<num>>? index = geometry.getIndex();
if ( index != null ) {
for ( int i = 0; i < index.count; i += 3 ) {
final a = index.getX( i )!.toInt();
final b = index.getX( i + 1 )!.toInt();
final c = index.getX( i + 2 )!.toInt();
final triangle = Tri( vertices[ a ], vertices[ b ], vertices[ c ], a, b, c );
faces.add( triangle );
}
}
else{
for(int i = 0; i < positionAttribute.count; i += 3){
final a = i;
final b = i + 1;
final c = i + 2;
final triangle = Tri(vertices[a], vertices[b], vertices[c], a, b, c);
faces.add(triangle);
}
}
// compute all edge collapse costs
for ( int i = 0, il = vertices.length; i < il; i ++ ) {
SMUtil.computeEdgeCostAtVertex( vertices[ i ] );
}
Vertex? nextVertex;
int z = count;
while (z != 0) {
nextVertex = SMUtil.minimumCostEdge(vertices);
if (nextVertex == null) {
print( 'THREE.SimplifyModifier: No next vertex' );
break;
}
SMUtil.collapse( vertices, faces, nextVertex, nextVertex.collapseNeighbor);
z--;
}
//
final simplifiedGeometry = BufferGeometry();
List<double> position = [];
//
for (int i = 0; i < vertices.length; i ++ ) {
final vertex = vertices[i].position;
position.addAll([vertex.x,vertex.y,vertex.z]);
// cache final index to GREATLY speed up faces reconstruction
vertices[i].id = i;
}
//
List<int> indexs = [];
for (int i = 0; i < faces.length; i ++ ) {
final face = faces[i];
indexs.addAll([face.v1!.id,face.v2!.id,face.v3!.id]);
}
//
simplifiedGeometry.setAttribute( 'position', Float32BufferAttribute( Float32Array.from(position), 3 ) );
simplifiedGeometry.setIndex( indexs );
return simplifiedGeometry;
}