模拟无限数量的对象

时间:2015-09-29 06:41:29

标签: javascript three.js

在这个example上,我们可以移动到球体的一个区域内但是在某些限制内。我希望能够在他们中间无限移动。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

诀窍是重复使用相机后面的球体并将它们放在前面。看看它是如何在this example中完成的。程序员在这里知道用户将继续朝同一方向移动,以便移除某个位置的树。

如果您使用类似您引用的示例,则无法知道用户将采用哪个方向。因此,您可以使用相同的技巧,但必须以其他方式对其进行编码。如果用户移动,最明显的是定期检查所有球体的距离。如果一个球体落在相机后面太远,则在镜像后面对着它进行镜像。

'定期'取决于场景中的实际球体数量,可能意味着两件事:

  • 如果您有一个小场景和几个球体,您可以在渲染循环中检查这些距离。既不便宜也不实用,每秒60次,但这可能是第一个编码步骤

  • 然后最好的方法是使用网络工作者:你发送相机的位置和球体的位置,你让工人计算其线程中的所有东西,并发回指令:&# 39;将这些领域移到那些位置'。在threejs示例中,每一秒都更合理,但由您决定取决于您的场景。

注意:如果您有很多球体,或者您使用的任何网格,例如超过20-30,那么每个网格都会有较慢的表现。我连接的示例中只有很少的树可以,但是有更多的对象和/或更重的场景,

  • 考虑将它们全部合并到一个几何体中。您可以通过从顶点索引推导或添加定义每个球体的属性来检查哪个球体在哪里。
  • 这也会影响工作人员的延迟:它会有更多的计算时间,因此需要更多的时间。

注意2:注1当然会删除示例旨在说明的细节级别:)(除非你在检查球体的距离时也实现自己的......)

答案 1 :(得分:0)

如果你想拥有无限世界的幻想,那么你可以:

  1. 将您的世界空间划分为多个区域(例如立方体)。
  2. 检测您当前所在的地区。
  3. 确保相邻区域中有对象(球体)。如果某些地区是空的 - 修复它。
  4. 清除不再需要的区域。
  5. 为此您可能希望有这样的课程:

        <!-- list_row.xml --> 
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >
    
      <ImageView 
         android:id="@+id/item_image"
         android:layout_marginRight="5dip"
         android:padding="3dip"
         android:layout_alignParentLeft="true"
         android:layout_width="@dimen/img_side"
         android:layout_height="@dimen/img_side" />
    
    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/item_image"
        android:layout_centerVertical="true"
        android:textColor="@color/black"
        android:textSize="@dimen/textnorm"
        />
    
    </RelativeLayout>
    

    定期做这样的事情:

    Class Region {
        bool isEmpty = true;
        Vector3 center;
        float radius; // or 'range'
        Array<Sphere> = null; // storage of your objects
    
        // constructors / destructor
    
        generateObjects(params); // perlin noise might be helpful there
        removeObjects();
    }