简体   繁体   中英

Can I add Faces from an array to a THREE.BufferGeometry

I will create a Mesh which is based on a BufferGeometry. At the moment I have a worker which is responsible for my geometry.

Worker: First of all I create a Three.Geometry. Then I build my bufferGeometry and send data back to main-thread.

1.) I convert my geometry into a THREE.BufferGeometry

var bufferGeometry = new THREE.BufferGeometry().fromGeometry ( geometry );

2.) Next I get the BufferAttributes

var attributePosition = bufferGeometry.getAttribute(name);//name = position, color, normal)

3.) Next I create a bufferArray

bufferArrayPosition.push(attributePosition);

4.) At the end I send these array back to the main thread

postMessage([bufferArrayColor, bufferArrayNormal, bufferArrayPosition]);

Main-Thread: In the main-thread I rebuild my bufferGeometry and convert this to a THREE.Geometry

 //receive messages from web worker
    worker.onmessage = function (e) {
        alert(e.data);

        var bufferGeometry = new THREE.BufferGeometry();
        var geometry = new THREE.Geometry();

        for (var i = 0; i < e.data[0].length; i ++){
            bufferGeometry.addAttribute('color', e.data[0][i].array, 3); 
            bufferGeometry.addAttribute('normal', e.data[1][i].array, 3);
            bufferGeometry.addAttribute('position', e.data[2][i].array, 3);

            var t = new THREE.Geometry().fromBufferGeometry(bufferGeometry);
            material.side = THREE.DoubleSide;
            mesh.push(new THREE.Mesh(t, material));
            Scene.scene.add(mesh[i]);
        }

    };

At the end the triangulate faces are lost!!! I have only standard face indices (0,1,2) (3,4,5), ... Actually I am doing some triangulation in the worker thread. These faces are still existing in the THREE.Geometry before the conversion to a bufferGeometry (step 1).

How can I add these faces to the bufferGeometry?

There are two types of buffer geometries:

  • indexed:

    In indexed buffer geometries the positions are stored in the position buffer attribute and indexes are stored in the index buffer attribute. Positions can be reused in a indexed buffer geometry.

  • non-indexed (also known as triangle soup)

    In a non-indexed array there is no index attribute all positions are stored in the position buffer attribute and every three subsequent points in the position attribute form one single triangle.

In case your original buffer geometry is indexed you will have to make sure your triangle definitions (so the index array from the index attribute) is also sent back to your main thread and used to recreate the buffer geometry.

You can get it from the buffer geometry using the getIndex method like this:

indexAttribute = bufferGeometry.getIndex();

UPDATE

Here a fiddle that demonstrates geometry vs buffer geometry for a simple square.

// What you need to send to main thread from your worker:
positions = bufferGeometry.attributes['position'].array;
indices = bufferGeometry.index.array;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM