Three.js,从极坐标计算球体

问题描述 投票:0回答:1

我正在使用

three.js
来计算球体。我使用两个
for
循环,一个用于 theta,一个用于 phi,然后将极坐标转换为笛卡尔坐标。对于每个计算点,我添加一个
Point
。结果不是球体。

这是嵌套的

for
循环:

const distance = 1000;
for (var i = 0; i < 360; i += 10) {
  for (let j = 0; j < 360; j += 10) {
    let theta = i * (Math.PI / 180);
    let phi = j * (Math.PI / 180);
    let x = Math.sin(theta) * Math.cos(phi) * distance;
    let y = Math.sin(theta) * Math.sin(phi) * distance;
    let z = distance * Math.sin(phi);
    const lightSphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
    lightSphere.position.set(x, y, z);
    scn.add(lightSphere);
  }
}

这是完整的代码,包括结果:链接

javascript reactjs three.js trigonometry
1个回答
1
投票

j 必须在 [-90, 90] 9 范围内,而不是 [0, 260] 范围内。请注意,您正在创建从南极到北极 (180°) 的切片 (360°)。

您必须计算

sin(theta)
cos(theta)
并将两者与
cos(phi)
相乘:

let x = Math.sin(theta) * Math.cos(phi) * distance;

let y = Math.sin(theta) * Math.sin(phi) * distance;

let x = Math.cos(theta) * Math.cos(phi) * distance;
let y = Math.sin(theta) * Math.cos(phi) * distance;

完整算法:

const distance = 1000;
for (var i = 0; i < 360; i += 10) {
    for (let j = -90; j < 90; j += 10) {
        let theta = i * (Math.PI / 180);
        let phi = j * (Math.PI / 180);
        let x = Math.cos(theta) * Math.cos(phi) * distance;
        let y = Math.sin(theta) * Math.cos(phi) * distance;
        let z = distance * Math.sin(phi);
        const lightSphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
        lightSphere.position.set(x, y, z);
        scn.add(lightSphere);
   }
}

您可以使用极坐标

distance
theta
)创建圆(切片)。 笛卡尔坐标可以通过以下方式获得:

xc = cos(theta) * distance
yc = sin(theta) * distance

最后你对球体做类似的事情:

x = xc * cos(phi)
y = yc * cos(phi)
z = sin(phi) * distance
© www.soinside.com 2019 - 2024. All rights reserved.