question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Geometry's fromBufferGeometry() should clean vertices and colors

See original GitHub issue

Geometry’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:closed
  • Created 5 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Mugen87commented, Sep 5, 2018

BufferGeometry.fromGeometry() always creates a non-indexed BufferGeometry. 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 in DirectGeometry:

https://github.com/mrdoob/three.js/blob/8b6a60ed44d017a364aa66d101789c2231c88954/src/core/DirectGeometry.js#L177-L187

0reactions
Mugen87commented, Sep 7, 2018

Okay, I’m fine with this decision.

Furthermore, vertex perturbations can have a significant impact on models defined on a small scale.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found