2

I'm trying to smoothen a mesh I loaded using THREE.OBJLoader

original result

As you can see in this original image all polygons are faceted. I've tried other loaders/formats but get the same result. After rooting around I see that a possible solution is to merge the vertices before computing the normals. When I try this i get "TypeError: geometry.mergeVertices is not a function" in my console. Here is the code, highlighting where I've inserted the mergeVertices() function.

        var loader = new THREE.OBJLoader();
        loader.load('../assets/models/nos2.obj', function (nos) {
        var material = new THREE.MeshLambertMaterial({color: 0xffffff, side:THREE.DoubleSide});

        nos.children.forEach(function (child) {
            child.material = material;
            child.geometry.mergeVertices(); /* ADDED MERGE WHICH GIVES ERROR */
            child.geometry.computeFaceNormals();
            child.geometry.computeVertexNormals();
        });

        nos.scale.set(300, 300, 300);
        nos.rotation.x = -0.3;
        scene.add(nos);}

What am I doing wrong?

3 Answers 3

8

Convert to regular geometry, apply whatever you need and convert back to buffer geometry. I'm using ES6 module loaders so no THREE. prefix

const tempGeo = new Geometry().fromBufferGeometry(child.geometry);

tempGeo.mergeVertices();

// after only mergeVertices my textrues were turning black so this fixed normals issues
tempGeo.computeVertexNormals();
tempGeo.computeFaceNormals();

child.geometry = new BufferGeometry().fromGeometry(tempGeo);
1

The function .OBJLoader() creates a BufferGeometry object - not a Geometry object .

The function .mergeVertices() is not available for a BufferGeometry object - hence the error.

As to solving the problem of achieving Smooth Shading on a loaded object I am still wrestling with that one myself :-(.

One possibility that you might explore is to make a Geometry object from the BufferGeometry object; then apply the mergeVertices() function to the geometry, then either use that geometry in your final mesh or make a new buffergeometry (from the geometry) and use it in your final mesh. Look at the documentation on geometry and buffer geometry at this location.

UPDATE

User SPACORUM posted a useful (STL source file) solution here which works equally well for OBJ source files. The code needs correcting by deleting/disabling these three lines:-

Sobject.computeBoundingBox();
Sobject.computeVertexNormals();
Sobject.center();

and amending the next line to read:-

var attrib = Sobject.geometry.getAttribute('position');
1

Happened this issue for me while loading an obj file. If you have a 3d software like 3dsmax:

Open the obj file, go to polygons selection mode and select all polygons. Under the Surface properties panel, click 'Auto Smooth' button. Export the model back to obj format

Now you won't have to call the functions geometry.mergeVertices() and geometry.computeVertexNormals();. Just load the obj and add to the scene, mesh will be smooth.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.