如何在Three.JS中的一个BufferGeometry中合并两个BufferGeometries?

时间:2016-04-06 12:05:00

标签: javascript merge three.js buffer-geometry

如何在ThreeJS中的一个THREE.BufferGeometry中合并两个缓冲区几何?

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);

modelGeometry不做任何事情。如何合并这些几何图形?

2 个答案:

答案 0 :(得分:2)

奇怪地在BufferGeometry上调用merge()给了我一些错误,但是有一个解决方法:

  1. 合并两个Geometry对象;
  2. 转换为BufferGeometry;
  3. 添加到网格
  4. 以下是为我工作的事情:

        var modelGeometry = new THREE.Geometry();
    
        cylGeometry = new THREE.CylinderGeometry( 10, 10, 10 );
    
        // if (modelGeometry == null)
        // {
        //     modelGeometry = new THREE.BufferGeometry().fromGeometry(geometry);
        //     console.log(modelGeometry);
        // }
    
        modelGeometry.merge(cylGeometry);
    
        sphereGeometry = new THREE.SphereGeometry( 20 , 20, 20 );
    
        modelGeometry.merge(sphereGeometry);
    
        bufGeometry = new THREE.BufferGeometry().fromGeometry(modelGeometry);
    
        var mesh = new THREE.Mesh( bufGeometry, material );
    
        scene.add(mesh);
    

答案 1 :(得分:1)

我整夜都会跳舞。做到了!

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 ));