在 3D 动画中在镜头前绘制新球体

Draw new sphere in front of the camera in 3D animation

使用 Python 和 OpenGL,我创建了一个光流动画,其中摄像机向球体移动(GIF 中显示的圆圈)。

我尝试在逐帧更新相机位置并播放动画的循环中创建两个条件:

near_z = -0.05;
far_z = -50.0;
nSpheres = 40;

for nframes in range(200):
   camera_z -= .7  # the camera moves with a speed of .7 on the z-axis.

1 - 第一个条件是当相机到达远裁剪平面 (far_z) 时,让相机再次回到近裁剪平面 (near_z)。正如您在 GIF 中看到的那样,它正在运行。

camera_z = np.where(camera_z <= far_z, near_z, camera_z)

2- 第二个条件是当相机经过球体时,在相机前面重新绘制一个新球体,这样我会在动画中保持相同数量的球体。因此,每当一个球体从框架中消失时,重新绘制一个。由于某种原因,它不起作用。我试图找出原因,但显然我失败了。

if sphere.position.z >= camera_z: # if the sphere is behind the camera, so the sphere is not visible

           sphere.position.z = np.random.uniform(camera_z,far_z) # define a new position for the sphere in front of the camera.

在此先感谢您的帮助!!

near_zfar_z都没有绝对坐标。这是到相机当前 z 坐标的相对距离。

所以它必须是:

sphere.position.z = np.random.uniform(camera_z+near_z, camera_z+far_z)

但请注意,如果相机是 "reset",由

camera_z = np.where(camera_z <= far_z, near_z, camera_z)

然后突然所有球体都在视锥体之外并被远平面剪裁。 在这种情况下,您必须将所有球体平移近平面到远平面的距离。 在这种情况下 near_zfar_z 是 "absoulte" 坐标(暂时)。

例如(而不是 camera_z = np.where(camera_z <= far_z, near_z, camera_z):

if camera_z > far_z:
    offset_z = far_z - near_z
    camera_z -= offset_z
    for sphere in spheres:
        sphere.position.z -= offset_z