// Warning: this method creates a grid at least as big as the total bounding box of the
        // game object, if children are included there may be empty space. Consider to use
        // CreateMultipleGridsWithGameObjectMeshShell in order to save memory.
        public static AABCGrid CreateGridWithGameObjectMesh(GameObject gameObj,
                                                            float cubeSide, bool includeChildren, bool includeInside, bool debug, ComputeShader shader)
        {
            AABCGrid          aABCGrid;
            short             width;
            short             height;
            short             depth;
            var               origin = new Vector3();
            List <GameObject> gameObjectsWithMesh;
            var               gridBoundsMin = new Vector3(Mathf.Infinity, Mathf.Infinity, Mathf.Infinity);
            var               gridBoundsMax = new Vector3(Mathf.NegativeInfinity, Mathf.NegativeInfinity, Mathf.NegativeInfinity);

            if (includeChildren)
            {
                gameObjectsWithMesh = GetChildrenWithMesh(gameObj);
            }
            else
            {
                gameObjectsWithMesh = new List <GameObject>();
            }
            if (gameObj.renderer)
            {
                gameObjectsWithMesh.Add(gameObj);
            }


            foreach (GameObject gameObjectWithMesh in gameObjectsWithMesh)
            {
                gridBoundsMin = MinVectorComponents(gridBoundsMin,
                                                    gameObjectWithMesh.renderer.bounds.min);
                gridBoundsMax = MaxVectorComponents(gridBoundsMax,
                                                    gameObjectWithMesh.renderer.bounds.max);
            }
            width    = (short)(Mathf.Ceil((gridBoundsMax.x - gridBoundsMin.x) / cubeSide) + 2);
            height   = (short)(Mathf.Ceil((gridBoundsMax.y - gridBoundsMin.y) / cubeSide) + 2);
            depth    = (short)(Mathf.Ceil((gridBoundsMax.z - gridBoundsMin.z) / cubeSide) + 2);
            origin   = gridBoundsMin - new Vector3(cubeSide, cubeSide, cubeSide);
            aABCGrid = new AABCGrid(width, height, depth, cubeSide, origin);

            aABCGrid.SetDebug(debug);

            foreach (GameObject gameObjectWithMesh in gameObjectsWithMesh)
            {
                if (includeInside)
                {
                    aABCGrid.FillGridWithGameObjectMesh(gameObjectWithMesh, shader);
                }
                else
                {
                    aABCGrid.FillGridWithGameObjectMeshShell(gameObjectWithMesh, shader);
                }
            }

            return(aABCGrid);
        }
Beispiel #2
0
        public static AABCGrid CreateGridWithGameObjectMesh(GameObject gameObj, float cubeSide, bool includeChildren, bool includeInside)
        {
            Vector3           ori = new Vector3();
            List <GameObject> childrenWithMesh = new List <GameObject>();
            Vector3           a       = new Vector3(MAX_FLOAT, MAX_FLOAT, MAX_FLOAT);
            Vector3           vector3 = new Vector3(MIN_FLOAT, MIN_FLOAT, MIN_FLOAT);

            if (includeChildren)
            {
                childrenWithMesh = GetChildrenWithMesh(gameObj);
            }
            else
            {
                childrenWithMesh = new List <GameObject>();
            }
            if (gameObj.GetComponent <Renderer>() != null)
            {
                childrenWithMesh.Add(gameObj);
            }
            foreach (GameObject obj2 in childrenWithMesh)
            {
                a       = MinVectorComponents(a, obj2.GetComponent <Renderer>().bounds.min);
                vector3 = MaxVectorComponents(vector3, obj2.GetComponent <Renderer>().bounds.max);
            }
            int x = (int)(Mathf.Ceil((vector3.x - a.x) / cubeSide) + 2f);
            int y = (int)(Mathf.Ceil((vector3.y - a.y) / cubeSide) + 2f);
            int z = (int)(Mathf.Ceil((vector3.z - a.z) / cubeSide) + 2f);

            ori = a - new Vector3(cubeSide, cubeSide, cubeSide);
            AABCGrid grid = new AABCGrid(x, y, z, cubeSide, ori);

            foreach (GameObject obj3 in childrenWithMesh)
            {
                if (includeInside)
                {
                    grid.FillGridWithGameObjectMesh(obj3);
                }
                else
                {
                    grid.FillGridWithGameObjectMeshShell(obj3);
                }
            }
            return(grid);
        }