Exemplo n.º 1
0
    /// <summary>
    /// Grow the octree to fit in all objects.
    /// </summary>
    /// <param name="direction">Direction to grow.</param>
    void Grow(Vector3 direction)
    {
        int xDirection = direction.x >= 0 ? 1 : -1;
        int yDirection = direction.y >= 0 ? 1 : -1;
        int zDirection = direction.z >= 0 ? 1 : -1;
        BoundsOctreeNode <T> oldRoot = rootNode;
        float   half      = rootNode.BaseLength / 2;
        float   newLength = rootNode.BaseLength * 2;
        Vector3 newCenter = rootNode.Center + new Vector3(xDirection * half, yDirection * half, zDirection * half);



        GameObject go = GameObject.Find("Node " + rootNode.GetHashCode().ToString());

        // Create a new, bigger octree root node
        rootNode = new BoundsOctreeNode <T>(newLength, minSize, looseness, newCenter);

        if (go != null)
        {
            go.transform.position   = newCenter;
            go.transform.localScale = Vector3.one * newLength;
            go.name = "Node " + rootNode.GetHashCode();

            Debug.Log("Node: Change scale #" + rootNode.GetHashCode().ToString());
        }
        else
        {
            GameObject newGameObject = GameObject.Instantiate(GameObject.Find("TempNode"), newCenter, Quaternion.identity);
            newGameObject.transform.localScale = Vector3.one * newLength;
            newGameObject.name = "Node " + rootNode.GetHashCode();

            Debug.Log("Node: New game object #" + rootNode.GetHashCode());
        }

        if (oldRoot.HasAnyObjects())
        {
            // Create 7 new octree children to go with the old root as children of the new root
            int rootPos = GetRootPosIndex(xDirection, yDirection, zDirection);
            BoundsOctreeNode <T>[] children = new BoundsOctreeNode <T> [8];
            for (int i = 0; i < 8; i++)
            {
                if (i == rootPos)
                {
                    children[i] = oldRoot;
                }
                else
                {
                    xDirection  = i % 2 == 0 ? -1 : 1;
                    yDirection  = i > 3 ? -1 : 1;
                    zDirection  = (i < 2 || (i > 3 && i < 6)) ? -1 : 1;
                    children[i] = new BoundsOctreeNode <T>(rootNode.BaseLength, minSize, looseness, newCenter + new Vector3(xDirection * half, yDirection * half, zDirection * half));
                }
            }

            // Attach the new children to the new root node
            rootNode.SetChildren(children);
        }
    }
Exemplo n.º 2
0
    /// <summary>
    /// Constructor for the bounds octree.
    /// </summary>
    /// <param name="initialWorldSize">Size of the sides of the initial node, in metres. The octree will never shrink smaller than this.</param>
    /// <param name="initialWorldPos">Position of the centre of the initial node.</param>
    /// <param name="minNodeSize">Nodes will stop splitting if the new nodes would be smaller than this (metres).</param>
    /// <param name="loosenessVal">Clamped between 1 and 2. Values > 1 let nodes overlap.</param>
    public BoundsOctree(float initialWorldSize, Vector3 initialWorldPos, float minNodeSize, float loosenessVal)
    {
        if (minNodeSize > initialWorldSize)
        {
            Debug.LogWarning("Minimum node size must be at least as big as the initial world size. Was: " + minNodeSize + " Adjusted to: " + initialWorldSize);
            minNodeSize = initialWorldSize;
        }
        Count       = 0;
        initialSize = initialWorldSize;
        minSize     = minNodeSize;
        looseness   = Mathf.Clamp(loosenessVal, 1.0f, 2.0f);
        rootNode    = new BoundsOctreeNode <T>(initialSize, minSize, loosenessVal, initialWorldPos);


        GameObject newGameObject = GameObject.Instantiate(GameObject.Find("TempNode"), initialWorldPos, Quaternion.identity);

        newGameObject.transform.localScale = Vector3.one * initialSize;
        newGameObject.name = "Node " + rootNode.GetHashCode();

        Debug.Log("Node: New game object #" + rootNode.GetHashCode());
    }