コード例 #1
0
    private static bool CalculateNavMeshQueryParams(ref Vector3 from, Vector3 to, ref Vector3 fromNodePos, ref List <ObstacleVertex> obstacles, bool clampFromInNavMesh, Simulator sim)
    {
        if (null == AstarPath.active)
        {
            return(false);
        }

        CreateAllPointsList();
        _allPoints.Clear();
        _allPoints.Add(from); _allPoints.Add(to);

        NNInfo fromInfo = AstarPath.active.GetNearest(from);

        if (null == fromInfo.node)
        {
            return(false);
        }
        fromNodePos = (Vector3)fromInfo.node.position;
        _allPoints.Add(fromNodePos);

        Bounds bounds = GameUtils.CalculateBounds(_allPoints); // calculate the bounding box to encapsulate all points we need to consider
        // sim.GetObstacles() is an alternative - this may be faster
        const float SearchPadding = 0.1f;

        sim.GetStaticAndDynamicObstacles(obstacles, bounds.center, (bounds.max - bounds.center).sqrMagnitude + SearchPadding); // get all obstacles near our line

        if (clampFromInNavMesh)
        {
            from = CalculatePointOnNavMesh(from, sim, 0.1f, fromInfo, obstacles);
        }
        return(true);
    }
コード例 #2
0
    private Bounds GetBounds()
    {
        m_RectangleContourPoints.Clear();
        CalculateRectangleContourPoints(ref m_RectangleContourPoints);
        m_Bounds      = GameUtils.CalculateBounds(m_RectangleContourPoints);    // calculate the bounding box to encapsulate all points we need to consider
        m_Bounds.size = new Vector3(m_Bounds.size.x, m_Height, m_Bounds.size.z);

        return(m_Bounds);
    }
コード例 #3
0
        protected void GenerateTileFromInputMesh(AStarPathfindingWalkableArea walkArea)
        {
            if (null == walkArea)
            {
                EB.Debug.LogWarning("GenerateTileFromInputMesh: walkArea is empty");
                return;
            }

            Mesh inputMesh = walkArea.gameObject.GetComponent <MeshFilter>().sharedMesh;

            if (null == inputMesh)
            {
                EB.Debug.LogWarning("GenerateTileFromInputMesh: inputMesh is empty");
                return;
            }

            List <Vector3> allPoints = new List <Vector3>(inputMesh.vertices);
            Bounds         bounds    = GameUtils.CalculateBounds(allPoints);

            forcedBoundsCenter = walkArea.transform.TransformPoint(bounds.center);
            forcedBoundsSize   = bounds.size;

            // this section changes the size of the bounds and tile, so that we always get one single tile
            SetUpNavMeshToFitOnOneTile();
            CalculateNumberOfTiles(ref tileXCount, ref tileZCount);
            Debug.Assert(tileXCount == 1 && tileZCount == 1, "Their should be only one tile when using a prebuilt nav mesh");

            tiles = new NavmeshTile[tileXCount * tileZCount];             // only one tile

            // ignore this setting
            scanEmptyGraph = false;

            // the Vector3 vertices in the mesh need to be converted to the APP Int3 format
            Int3[] Int3Verts = new Int3[inputMesh.vertices.Length];
            for (int i = 0; i < Int3Verts.Length; ++i)
            {
                Vector3 tempVert = inputMesh.vertices[i];
                tempVert = walkArea.transform.TransformPoint(tempVert);                 // get the world space position, rather than local space

                Int3Verts[i] = (Int3)tempVert;
            }
            tiles[0] = CreateTile(inputMesh.triangles, Int3Verts, 0, 0);             // our single tile

            //Assign graph index to nodes
            uint graphIndex = (uint)AstarPath.active.astarData.GetGraphIndex(this);
            GraphNodeDelegateCancelable del = delegate(GraphNode n)
            {
                n.GraphIndex = graphIndex;
                return(true);
            };

            GetNodes(del);
        }
コード例 #4
0
    // calculate all the obstacles which will be used in multiple nav mesh IsVisible tests (optimization to avoid doing multiple sim.KDTree.GetObstacles())
    // if all the from points are the same in the 'theInput' list, set 'areFromPointsEqual' to true for an optimization
    public static bool CalculateNavMeshQueryObstacles(ref List <NavMeshQueryInput> theInput, ref List <ObstacleVertex> obstacles, Simulator sim, bool areFromPointsEqual)
    {
        if (null == AstarPath.active)
        {
            return(false);
        }

        CreateAllPointsList();
        _allPoints.Clear();
        for (int inputIndex = 0; inputIndex < theInput.Count; ++inputIndex)
        {
            NavMeshQueryInput query = theInput[inputIndex];
            query.isInputValid = true;

            _allPoints.Add(query.from);
            _allPoints.Add(query.to);

            if (!areFromPointsEqual || 0 == inputIndex) // if from points are not equal or this is the first in the list
            {
                query.fromInfo = AstarPath.active.GetNearest(query.from);
            }
            else // (areFromPointsEqual && inputIndex > 0) // from points are equal and we've already calculated one GetNearest()
            {
                query.fromInfo = theInput[0].fromInfo;
            }

            if (null == query.fromInfo.node)
            {
                query.isInputValid = false;
                continue;
            }
            query.fromNodePos = (Vector3)query.fromInfo.node.position;
            _allPoints.Add(query.fromNodePos);
        }

        Bounds bounds = GameUtils.CalculateBounds(_allPoints); // calculate the bounding box to encapsulate all points we need to consider
        // sim.GetObstacles() is an alternative - this may be faster
        const float SearchPadding = 0.1f;

        sim.GetStaticAndDynamicObstacles(obstacles, bounds.center, (bounds.max - bounds.center).sqrMagnitude + SearchPadding); // get all obstacles near our line
        return(true);
    }
コード例 #5
0
        public Bounds GetBounds()
        {
            var bounds = new Bounds();

            switch (type)
            {
            case MeshType.Rectangle:
#if BNICKSON_UPDATED
                rectangleContourPoints.Clear();
                CalculateRectangleContourPoints(ref rectangleContourPoints);
                bounds      = GameUtils.CalculateBounds(rectangleContourPoints);                    // calculate the bounding box to encapsulate all points we need to consider
                bounds.size = new Vector3(bounds.size.x, height, bounds.size.z);
#else
                if (useRotation)
                {
                    Matrix4x4 m = tr.localToWorldMatrix;
                    // Calculate the bounds by encapsulating each of the 8 corners in a bounds object
                    bounds = new Bounds(m.MultiplyPoint3x4(center + new Vector3(-rectangleSize.x, -height, -rectangleSize.y) * 0.5f), Vector3.zero);
                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(rectangleSize.x, -height, -rectangleSize.y) * 0.5f));
                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(rectangleSize.x, -height, rectangleSize.y) * 0.5f));
                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(-rectangleSize.x, -height, rectangleSize.y) * 0.5f));

                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(-rectangleSize.x, height, -rectangleSize.y) * 0.5f));
                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(rectangleSize.x, height, -rectangleSize.y) * 0.5f));
                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(rectangleSize.x, height, rectangleSize.y) * 0.5f));
                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(-rectangleSize.x, height, rectangleSize.y) * 0.5f));
                }
                else
                {
                    bounds = new Bounds(tr.position + center, new Vector3(rectangleSize.x, height, rectangleSize.y));
                }
#endif
                break;

            case MeshType.Circle:
                if (useRotation)
                {
                    Matrix4x4 m = tr.localToWorldMatrix;
                    bounds = new Bounds(m.MultiplyPoint3x4(center), new Vector3(circleRadius * 2, height, circleRadius * 2));
                }
                else
                {
                    bounds = new Bounds(transform.position + center, new Vector3(circleRadius * 2, height, circleRadius * 2));
                }
                break;

            case MeshType.CustomMesh:
                if (mesh == null)
                {
                    break;
                }

                Bounds b = mesh.bounds;
                if (useRotation)
                {
                    Matrix4x4 m = tr.localToWorldMatrix;
                    b.center *= meshScale;
                    b.size   *= meshScale;

                    bounds = new Bounds(m.MultiplyPoint3x4(center + b.center), Vector3.zero);

                    Vector3 mx = b.max;
                    Vector3 mn = b.min;

                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(mx.x, mx.y, mx.z)));
                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(mn.x, mx.y, mx.z)));
                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(mn.x, mx.y, mn.z)));
                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(mx.x, mx.y, mn.z)));

                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(mx.x, mn.y, mx.z)));
                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(mn.x, mn.y, mx.z)));
                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(mn.x, mn.y, mn.z)));
                    bounds.Encapsulate(m.MultiplyPoint3x4(center + new Vector3(mx.x, mn.y, mn.z)));

                    Vector3 size = bounds.size;
                    size.y      = Mathf.Max(size.y, height * tr.lossyScale.y);
                    bounds.size = size;
                }
                else
                {
                    Vector3 size = b.size * meshScale;
                    size.y = Mathf.Max(size.y, height);
                    bounds = new Bounds(transform.position + center + b.center * meshScale, size);
                }
                break;

            default:
                throw new System.Exception("Invalid mesh type");
            }
            return(bounds);
        }