Geometry's fromBufferGeometry() should clean vertices and colors
See original GitHub issueGeometry’s fromBufferGeometry() exports duplicated vertices and colors.
Code to reproduce:
var box = new THREE.BoxGeometry(1,1,1);
var bufferBox = new THREE.BufferGeometry();
bufferBox.fromGeometry(box);
var newBox = new THREE.Geometry();
newBox.fromBufferGeometry(bufferBox);
Result: box : {vertices: Array(8), colors: Array(0)} newBox : {vertices: Array(36), colors: Array(36)}
If you see useful, we have written a basic method to remove the vertices and fix the vertex index in the faces:
function clearDuplicateVertices(geometry) {
//store the vertex index changes
var data = [];
var vertices = [];
var faces = [];
//vertices
for (var v = 0; v < geometry.vertices.length; v++) {
var vertex = geometry.vertices[v];
//see if we have that vertex already
var newIndex = -1;
for (var i = 0; i < vertices.length; i++) {
if (vertices[i].equals(vertex))
{
newIndex = i;
break;
}
}
if (newIndex > -1)
{
data.push({oldIndex: v, newIndex: newIndex});
} else
{
vertices.push(vertex);
data.push({oldIndex: v, newIndex: vertices.length - 1});
}
}
//faces
for (var f = 0; f < geometry.faces.length; f++) {
var currentFace = geometry.faces[f];
var a = currentFace.a;
var b = currentFace.b;
var c = currentFace.c;
for (var i = 0; i < data.length; i++) {
var currentData = data[i];
if (a === currentData.oldIndex) {
a = currentData.newIndex;
}
if (b === currentData.oldIndex) {
b = currentData.newIndex;
}
if (c === currentData.oldIndex) {
c = currentData.newIndex;
}
}
faces.push(new THREE.Face3(a, b, c));
}
//copy
var geo = new THREE.Geometry();
geo.vertices = vertices;
geo.faces = faces;
geo.computeFaceNormals();
return geo;
}
Three.js version
- r96
Issue Analytics
- State:
- Created 5 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
BufferGeometry – three.js docs
A representation of mesh, line, or point geometry. Includes vertex positions, face indices, normals, colors, UVs, and custom attributes within buffers, ...
Read more >How to assign different color for each vertex in a buffer ...
Here is an article on how to use BufferGeometry. To add colors you make a Float32Array of colors and add it as an...
Read more >Updating THREE.Geometry to THREE.BufferGeometry
Modifying the vertices of a geometry by repositioning them outwards from the center by a factor of 2. THREE.Geometry. const vertices = ( ......
Read more >How to repair a mesh in Blender - Artisticrender.com
This will color the front side blue and the back side red of all faces. ... Most often this geometry is only made...
Read more >Geometry - Verge3D User Manual - Soft8Soft
computeBoundingSphere(). Default is null. # .colors : Array. Array of vertex colors, matching number and order of vertices. This is used by ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
BufferGeometry.fromGeometry()
always creates a non-indexedBufferGeometry
. Read #11776 from more information.It’s more interesting to investigate if it’s possible to avoid the creation of a
color
attribute. This happens in all cases because of this code inDirectGeometry
:https://github.com/mrdoob/three.js/blob/8b6a60ed44d017a364aa66d101789c2231c88954/src/core/DirectGeometry.js#L177-L187
Okay, I’m fine with this decision.
It’s not complicated to scale the epsilon or precision value based on the min/max values of the geometry. Many algorithms in the area computational geometry do this and produce proper results. A fix for this in
.mergeVertices()
is doable.