如何在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
不做任何事情。如何合并这些几何图形?
答案 0 :(得分:2)
奇怪地在BufferGeometry上调用merge()给了我一些错误,但是有一个解决方法:
以下是为我工作的事情:
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 ));