javascriptmergethree.jsbuffer-geometry

How to merge two BufferGeometries in one BufferGeometry in Three.JS?


How to merge two buffer geometries in one THREE.BufferGeometry in ThreeJS ?

var modelGeometry = null;

geometry = new THREE.CylinderGeometry( 10, 10, 10 );

if (modelGeometry == null)
{
    modelGeometry = new THREE.BufferGeometry().fromGeometry(geometry);
    console.log(modelGeometry);
}

bufGeometry = new THREE.SphereBufferGeometry( 20 , 20, 20 );

var mesh = new THREE.Mesh( bufGeometry, material );

modelGeometry.merge(mesh.geometry, mesh.matrix);

Doesn't do anything with modelGeometry. How merge these geometries correct?


Solution

  • I'll dance all night long. Did it!

    sumPosArr = new Float32Array(poslen);
    sumNormArr = new Float32Array(normlen);
    sumUvArr = new Float32Array(uvlen);
    
    var postotalarr = new Array();
    
    sumPosCursor = 0;
    sumNormCursor = 0;
    sumUvCursor = 0;
    
    for (a = 0; a < objects.length; a++ )
    {
        var posAttArr = objects[a].geometry.getAttribute('position').array;
    
        for (b = 0; b < posAttArr.length; b++)
        {
            sumPosArr[b + sumPosCursor] = posAttArr[b];
        }
    
        sumPosCursor += posAttArr.length;
    
    
        var numAttArr = objects[a].geometry.getAttribute('normal').array;
    
        for (b = 0; b < numAttArr.length; b++)
        {
            sumNormArr[b + sumNormCursor] = numAttArr[b];
        }
    
        sumNormCursor += numAttArr.length;
    
    
        var uvAttArr = objects[a].geometry.getAttribute('uv').array;
    
        for (b = 0; b < uvAttArr.length; b++)
        {
            sumUvArr[b + sumUvCursor] = uvAttArr[b];
        }
    
        sumUvCursor += uvAttArr.length;
    
    }
    
    modelGeometry.addAttribute('position', new THREE.BufferAttribute(sumPosArr, 3 ));
    modelGeometry.addAttribute('normal', new THREE.BufferAttribute(sumNormArr, 3 ));
    modelGeometry.addAttribute('uv', new THREE.BufferAttribute(sumUvArr, 2 ));