javascript - Three.js 获取可见区域中心的粒子

标签 javascript three.js

我有一个粒子系统,其中 1000 个粒子散布在屏幕上。当我使用 OrbitControls 进行缩放和平移时,我希望粒子最接近可视区域的中心。

我认为这有两个部分。首先,我们需要找到可视区域的中心顶点。接下来,我们需要对所有粒子运行距离公式,以找到距离中心顶点最近的粒子。希望有比这更有效的方法。第二部分使用蛮力相当简单。第一部分是我不确定的。如何找到可视区域的中心顶点?

最佳答案

您可能应该获得一个位于您正前方 10 个单位的点,然后获得距离该点最近的粒子。

这篇文章帮助我告诉你这一点,顺便说一句,我之前已经完成了这一切。

Three.js Projector and Ray objects

用于单击屏幕上的一个点,并从中获取矢量。

projector = new THREE.Projector();

// This is like clicking on the center of the screen, 0, 0 is center
straight_ahead = new THREE.Vector3(0,0,.5);

// Now we have straight_ahead relative to the camera
projector.unprojectVector(straight_ahead, camera);

// Now straight_ahead is just the direction of the camera, since we're taking away the camera's position from it
straight_ahead.sub(camera.position)

// It's a direction of where the camera is looking, so lets make it 10 units away( pick your own number)
straight_ahead.normalize().multiplyScalar(10)

// Now we we have a length of 10 units, we can get 10 units in front of the camera
straight_ahead.add(camera.position)

// At this point, straight ahead is a point in space 10 units in front of you, so lets find the closest point to that

// here's where you put a simple loop in
min_point = null
min_distance = 999999999
_.each( my_particles, function(particle) {
  distance = particle.position.distanceTo(straight_ahead)
  if (distance < min_distance) {
    min_point = particle
    min_distance = distance
  }
});

// now you have min_point, which should be the closest point to 10 feet in front of your face

关于javascript - Three.js 获取可见区域中心的粒子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19523083/

相关文章:

javascript - get 函数无法与 JSON 字符串中的本地存储正常工作

javascript - 强制 Youtube 嵌入以高清播放(2016 版)

javascript - 制服实际上是在 THREE.js RawShaderMaterial 中定义的吗?

javascript - Three.js 纹理未显示且为黑色

javascript - 胜利图 - 布局问题

javascript - Return false 在我的 javascript 函数中不起作用

javascript - 使用 Node js 进行 RTSP 流传输?

javascript - 将 HTML Canvas 导出为图像序列

three.js - 围绕给定轴和角度的枢轴点正确旋转对象

three.js - 无法克隆()纹理