【问题标题】:3D: avoid pinching at poles when creating sphere from polar coordinates3D:从极坐标创建球体时避免夹在两极
【发布时间】:2017-02-14 07:58:45
【问题描述】:

我正在使用维基百科的spherical coordinate system article 在 Three.js 中创建一个由粒子制成的球体。根据这篇文章,我创建了一个小的Polarizer 类,它使用setPolar(rho, theta, phi) 接收极坐标并返回其对应的x、y、z

这是setPolar() 函数:

// Rho: radius
// theta θ: polar angle on Y axis
// phi φ: azimuthal angle on Z axis

Polarizer.prototype.setPolar = function(rho, theta, phi){
  // Limit values to zero
  this.rho = Math.max(0, rho);
  this.theta = Math.max(0, theta);
  this.phi = Math.max(0, phi);

  // Calculate x,y,z
  this.x = this.rho * Math.sin(this.theta) * Math.sin(this.phi);
  this.y = this.rho * Math.cos(this.theta);
  this.z = this.rho * Math.sin(this.theta) * Math.cos(this.phi);

  return this;
}

我用它来定位我的粒子如下:

var tempPolarizer = new Polarizer();

for(var i = 0; i < geometry.vertices.length; i++){
  tempPolarizer.setPolar(
    50,                         // Radius of 50
    Math.random() * Math.PI,    // Theta ranges from 0 - PI
    Math.random() * 2 * Math.PI // Phi ranges from 0 - 2PI
  );

  // Set new vertex positions
  geometry.vertices[i].set(
    tempPolarizer.x,
    tempPolarizer.y,
    tempPolarizer.z
  );
}

效果非常好,只是我得到了高粒子密度,或者在两极“挤压”:

我不知道如何避免这种情况发生。我想将加权随机数传递给纬度,但我希望在不使经度也减慢并在两极聚集的情况下为粒子设置动画。

是否有不同的公式来生成两极没有那么重的球体?我应该改用四元数吗?

【问题讨论】:

标签: math three.js geometry quaternions polar-coordinates


【解决方案1】:
  1. 用于随机均匀抽样

    在单位立方体中使用随机点,将其作为向量处理并将其长度设置为球体的半径。例如在 C++ 中是这样的:

    x = 2.0*Random()-1.0; 
    y = 2.0*Random()-1.0; 
    z = 2.0*Random()-1.0; 
    m=r/sqrt(x*x+y*y+z*z);
    x*=m;
    y*=m;
    z*=m;
    

    &lt;0.0,1.0&gt; 中的随机返回数字。欲了解更多信息,请参阅:

  2. 用于均匀非随机抽样

    查看相关的QA

【讨论】:

    【解决方案2】:

    为了避免两极的高密度,我不得不降低 theta(纬度)降落在 0 和 PI 附近的可能性。我的意见

    Math.random() * Math.PI, 表示所有值的可能性相同(橙色)。

    Math.acos((Math.random() * 2) - 1) 完美地加权输出,使 0 和 PI 不太可能沿着球体表面(黄色)

    现在我什至不知道两极在哪里!

    【讨论】:

      猜你喜欢
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多