Exemple #1
0
        internal bool GetMeshBuildData(UnityEngine.Vector3 origin, float tileWorldSize, TileZone zone
                                       , out NavmeshParams config
                                       , out NavmeshTileData[] tiles)
        {
            // Is there anything to bake?

            config = null;
            tiles  = null;

            int maxPolyCount;

            int tileCount;

            BakeableCount(out tileCount, out maxPolyCount);

            if (tileCount == 0)
            {
                return(false);
            }

            config = new NavmeshParams(origin.ToVector3()
                                       , tileWorldSize, tileWorldSize
                                       , Mathf.Max(1, tileCount)
                                       , Mathf.Max(1, maxPolyCount));

            // Add the tiles.

            List <NavmeshTileData> ltiles = new List <NavmeshTileData>();

            for (int tx = zone.xmin; tx <= zone.xmax; tx++)
            {
                for (int tz = zone.zmin; tz <= zone.zmax; tz++)
                {
                    int             trash;
                    NavmeshTileData td = GetTileData(tx, tz, out trash);
                    if (td == null)
                    {
                        // Tile is not available.
                        continue;
                    }

                    ltiles.Add(td);
                }
            }

            tiles = ltiles.ToArray();

            return(true);
        }
Exemple #2
0
        private static void TriangulateSurface(Terrain terrain
                                               , UnityEngine.Vector3 origin
                                               , UnityEngine.Vector3 scale
                                               , int xCount
                                               , int zCount
                                               , float yOffset
                                               , TriangleMesh buffer)
        {
            // Create the vertices by sampling the terrain.
            for (int ix = 0; ix < xCount; ix++)
            {
                float x = origin.x + ix * scale.x;
                for (int iz = 0; iz < zCount; iz++)
                {
                    float z = origin.z + iz * scale.z;
                    UnityEngine.Vector3 pos = new UnityEngine.Vector3(x, origin.y, z);
                    pos.y += terrain.SampleHeight(pos) + yOffset;
                    buffer.verts[buffer.vertCount] = pos.ToVector3();
                    buffer.vertCount++;
                }
            }

            // Triangulate surface sample points.
            for (int ix = 0; ix < xCount - 1; ix++)
            {
                for (int iz = 0; iz < zCount - 1; iz++)
                {
                    int i    = iz + (ix * zCount);
                    int irow = i + zCount;

                    buffer.tris[buffer.triCount * 3 + 0] = i;
                    buffer.tris[buffer.triCount * 3 + 1] = irow + 1;
                    buffer.tris[buffer.triCount * 3 + 2] = irow;
                    buffer.triCount++;

                    buffer.tris[buffer.triCount * 3 + 0] = i;
                    buffer.tris[buffer.triCount * 3 + 1] = i + 1;
                    buffer.tris[buffer.triCount * 3 + 2] = irow + 1;
                    buffer.triCount++;
                }
            }
        }
Exemple #3
0
    internal bool InitializeBuild(BuildContext context, bool fromTarget)
    {
        Navmesh navmesh = null;

        if (fromTarget)
        {
            if (!CanLoadFromTarget(context, true))
            {
                return(false);
            }

            navmesh = BuildTarget.GetNavmesh();

            SetConfigFromTargetIntern(navmesh);
        }

        mIsDirty = true;

        // Note: If loading from the target, the tile size was already validated.
        // So it won't trigger this adjustment.
        if (mConfig.TileSize != 0 &&
            mConfig.TileSize < MinAllowedTileSize)
        {
            string msg = string.Format("Tile size too small. Reverting tile size from"
                                       + " {0} to 0 (non-tiled). Minimum tile size is {1}"
                                       , mConfig.TileSize, MinAllowedTileSize);

            context.LogWarning(msg, this);

            mConfig.TileSize = 0;
        }

        if (mConfig.TileSize == 0)
        {
            mBuildData.Resize(1, 1);
        }
        else
        {
            // Need to check to see if the the build is truly tiled.

            int w;
            int d = 0;

            if (navmesh == null)
            {
                NMGen.DeriveSizeOfTileGrid(mBoundsMin.ToVector3(), mBoundsMax.ToVector3()
                                           , mConfig.XZCellSize, mConfig.TileSize
                                           , out w, out d);
            }
            else
            {
                // Existing navmesh will always be tiled.
                w = 2;
            }

            if (w > 1 || d > 1)
            {
                mTileSet = TileSetDefinition.Create(mBoundsMin.ToVector3(), mBoundsMax.ToVector3()
                                                    , mConfig.GetConfig()
                                                    , mInputGeom);

                if (mTileSet == null)
                {
                    context.LogError("Create tile build definition: Unexpected error."
                                     + " Invalid input data or configuration."
                                     , this);

                    return(false);
                }

                mBuildData.Resize(mTileSet.Width, mTileSet.Depth);
            }
            else
            {
                // Not really tiled.
                mBuildData.Resize(1, 1);
            }
        }

        if (navmesh != null)
        {
            // Need to load the tiles from existing navmesh.

            NavmeshTileExtract[] tiles;
            NavmeshParams        meshConfig;

            NavStatus status = Navmesh.ExtractTileData(navmesh.GetSerializedMesh()
                                                       , out tiles
                                                       , out meshConfig);

            if ((status & NavStatus.Failure) != 0)
            {
                context.LogError("Could not extract the tile data from the target's"
                                 + " navigation mesh. (Can't initialize from build target.)"
                                 , this);
                return(false);
            }

            foreach (NavmeshTileExtract tile in tiles)
            {
                int polyCount = tile.header.polyCount;

                if (polyCount == 0)
                {
                    continue;
                }

                int tx = tile.header.tileX;
                int tz = tile.header.tileZ;

                if (tx >= mBuildData.Width || tz >= mBuildData.Depth)
                {
                    // Shouldn't happen.  Probably indicates in internal error
                    // of some type.

                    string msg = string.Format("The existing navigation mesh"
                                               + " contains a tile outside the expected range. Ignoring"
                                               + " the tile. (Tile: [{0},{1}])"
                                               , tx, tz);

                    context.LogWarning(msg, this);

                    continue;
                }

                mBuildData.SetAsBaked(tx, tz, tile.data, polyCount);
            }
        }

        // Note: Dirty state was set earlier in the code.

        return(true);
    }