对于有两个或三个顶点的一般行,我在@Wilt的问题上得到了帮助:Can I merge geometry in every frame in rendering process using three.js?
这样我就可以用一个bufferGeometry生成大量的行。
使用如下代码:
var geometry = new THREE.BufferGeometry();
positions = new Float32Array(total * pointsNum * 3);
geometry.addAttribute(
'position', new THREE.BufferAttribute(positions, 3)
);
var material = new THREE.LineBasicMaterial({
transparent: true,
color: 0x0000ff,
opacity: 1
});
var line = new THREE.Line(bufferGeometry, material);
var all_vertices = [new THREE.Vector3(-1, 0, 0), new THREE.Vector3(0, 1, 0), new THREE.Vector3(1, 0, 0)];
for(var i = 0; i< all_vertices.length; i++){
positions[i] = all_vertices[i].x;
positions[i+1] = all_vertices[i].y;
positions[i+2] = all_vertices[i].z;
}
var start = all_vertices.length;
var end = start + all_vertices.length;
bufferGeometry.addGroup(start, end);
line.geometry.attributes.position.needsUpdate = true
我的想法显然太天真了,我可以通过在var all_vertices
中轻松添加更多顶点来获得CatmullRomCurve3系列。事实是,这并不容易。例如,我有一些顶点,如:
var maxX = Math.random() * 0.7 + 0.7;
var maxY = Math.random() * (maxX / 2) + 0.01;
var maxZ = Math.random() * (maxX - maxY) + maxY;
console.log(maxX + ' ' + maxY + ' ' + maxZ);
//32, 45, 38, 36, 35, 39, 41, 42
var degs = [32, 45, 38, 36, 35, 39, 41, 42];
var alpha = 32;
var tgAlpha = Math.tan(alpha);
var xpow = maxX * maxX;
var zpow = maxZ * maxZ;
orig[0] = new THREE.Vector3(-maxX, 0, 0);
var x1 = -Math.sqrt(xpow * zpow / (zpow + tgAlpha * xpow));
var z1 = x1 * tgAlpha;
orig[1] = new THREE.Vector3(x1, maxY, z1);
orig[2] = new THREE.Vector3(-x1, -maxY, z1);
orig[3] = new THREE.Vector3(maxX, 0, 0);
orig[4] = new THREE.Vector3(-x1, maxY, -z1);
orig[5] = new THREE.Vector3(x1, -maxY, -z1);
并将它们应用于var curve = new THREE.CatmullRomCurve3(orig); curve.closed = true;
,因此,我可以在3D空间中获得一条闭合线,就像无限符号一样。
那么如何使用这些顶点从bufferGeometry创建CatmullRomCurve3?我可以将它缩放到其他尺寸吗?
Jsfiddle是https://jsfiddle.net/do7ur33u/2/
感谢您的帮助。