在 3D 动画中保持恒定数量的可见圆圈 [英] Keep constant number of visible circles in 3D animation

查看:21
本文介绍了在 3D 动画中保持恒定数量的可见圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 3D 动画,其中白色圆圈的透视投影在投影在 2D 计算机屏幕 (GIF 1) 上的假 3D 空间中随机移动.

因为我需要保持相同数量的可见圆圈,所以每次从框架中消失一个圆圈时,我必须在框架内创建一个新的可见圆圈.为此,我编写了这段代码:

  • 首先我创建了初始坐标和两个运动角度(球坐标):

     for circle in circles:circle.position.xy = np.random.uniform(-25, 25, size=2)z = np.random.uniform(near_z, far_z)circle.position.z = zcircle.position.x *= z/-50circle.position.y *= z/-50circle.theta_deg = np.random.rand(1) * 360circle.phi_deg = np.random.rand(1) * 360theta_rad = circle.theta_deg * np.pi/180phi_rad = circle.phi_deg* np.pi/180circle.dx = 速度 * np.sin(-phi_rad - theta_rad)/frameRatecircle.dy = -speed * np.cos(phi_rad + theta_rad)/frameRatecircle.dz = -speed * np.cos(theta_rad)/frameRate

    • 然后,在播放动画并更新每个圆圈的位置的循环中,我将这个条件放在为同一类型问题提供的相同答案之后

      我的情况有什么问题,如何检测从框架中消失的圆圈并在框架内重新创建一个圆圈?

      按照@Fabian N.(答案如下)的建议,我已将 z 坐标以及 x 和 y 坐标重新设置如下:

       max_dist = max(abs(circle.position.x), abs(circle.position.y)) # 求圆到视图中心的最大距离:limit_dist = 25 * abs((circle.position.z-near_z)/far_z)如果 circle.position.z <= near_z 或 max_dist >limit_dist:z_rel = np.random.uniform(near_z,far_z)circle.position.z = z_rel + near_zcircle.position.x = np.random.uniform(-25, 25) * z_rel/far_zcircle.position.y = np.random.uniform(-25, 25) * z_rel/far_z

      我得到了这个结果:

      解决方案

      在您添加的代码中,您重置 z 参数的方式与初始化它的方式不同.

      在代码的第一部分,您使用

      z = np.random.uniform(near_z, far_z)circle.position.z = zcircle.position.x *= z/-50circle.position.y *= z/-50

      while 在 for 循环中使用

      z_rel = np.random.uniform(near_z,far_z)circle.position.z = z_rel + near_zcircle.position.x = np.random.uniform(-25, 25) * z_rel/far_zcircle.position.y = np.random.uniform(-25, 25) * z_rel/far_z

      这些似乎并不等价.也许您应该对两者使用相同的参数.

      您还应该检查更新 x 和 y 位置的方式,尤其是参数 dx 和 dy,因为它们可能会变得很大,以至于您的圆圈会立即飞出屏幕

      I have created a 3D animation with a perspective projection of white circles moving randomly in a fake 3D space projected on a 2D computer screen (GIF 1).

      Since I need to keep the same number of visible circles, every time a circle disappears from the frame, I have to create a new visible one within the frame. To do so, I have written this piece of code:

      • First I created initial coordinates and the two angles of movements (spherical coordinates):

        for circle in circles:
        
            circle.position.xy = np.random.uniform(-25, 25, size=2)
            z = np.random.uniform(near_z, far_z)
        
            circle.position.z = z
            circle.position.x *= z/-50
            circle.position.y *= z/-50
        
            circle.theta_deg = np.random.rand(1) * 360
            circle.phi_deg = np.random.rand(1) * 360
        
            theta_rad = circle.theta_deg * np.pi / 180
            phi_rad = circle.phi_deg* np.pi / 180
        
        
            circle.dx = speed * np.sin(-phi_rad - theta_rad) / frameRate
            circle.dy = -speed * np.cos(phi_rad + theta_rad) / frameRate
            circle.dz = -speed * np.cos(theta_rad) / frameRate
        

        • Then, in the loop that plays the animation, and updates the position of each circle, I have put this condition following the same answer that was provided to the same kind of issue here:

          max_dist = max(abs(circle.position.x),abs(circle.position.y))
          limit_dist = 25 * abs((circle.position.z-near_z) / far_z)
          
          z_rel = np.random.uniform(near_z,far_z)
          
          if max_dist > limit_dist: 
              circle.position.x = np.random.uniform(-25, 25) * z_rel/far_z
              circle.position.y = np.random.uniform(-25, 25) * z_rel/far_z
          

      I got a weird result as shown in GIF 2

      What is wrong with my condition and how can I detect a circle that disappears from the frame and recreate one inside the frame?

      Following the suggestion of @Fabian N. (answer below), I have reset the z-coordinates along with the x and y coordinates as follows:

          max_dist   = max(abs(circle.position.x), abs(circle.position.y))        # Find maximum distance of a circle to the center of the view:
          limit_dist = 25 * abs((circle.position.z-near_z) / far_z)
      
      
          if circle.position.z <= near_z or max_dist > limit_dist:
      
              z_rel = np.random.uniform(near_z,far_z) 
              circle.position.z = z_rel + near_z
      
              circle.position.x = np.random.uniform(-25, 25) * z_rel/far_z
              circle.position.y = np.random.uniform(-25, 25) * z_rel/far_z
      

      And I got this result:

      解决方案

      In the code that you've added, you are resetting the z parameter differently than the way you initialized it.

      In the first part of your code, you use

      z = np.random.uniform(near_z, far_z)
      circle.position.z = z
      circle.position.x *= z/-50
      circle.position.y *= z/-50
      

      while in the for loop you use

      z_rel = np.random.uniform(near_z,far_z)
      circle.position.z = z_rel + near_z
      circle.position.x = np.random.uniform(-25, 25) * z_rel/far_z
      circle.position.y = np.random.uniform(-25, 25) * z_rel/far_z
      

      These do not seem to be equivalent. Perhaps you should use the same parameters for both.

      You should also check the way that you update your x and y positions, especially your parameters dx and dy, as these may be getting so big that your circles fly out of the screen immediately

      这篇关于在 3D 动画中保持恒定数量的可见圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆