예제 #1
0
        /// <summary>
        /// Derives the <see cref="NavmeshParams"/> for a tile.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This method is useful for getting the configuration required to build a single-tile
        /// navigation mesh for the tile.
        /// </para>
        /// </remarks>
        /// <param name="tile">The tile.</param>
        /// <returns>The <see cref="NavmeshParams"/> for the tile.</returns>
        public static NavmeshParams DeriveConfig(NavmeshTileData tile)
        {
            NavmeshTileHeader header = tile.GetHeader();

            return new NavmeshParams(header.boundsMin
                    , header.boundsMax.x - header.boundsMin.x
                    , header.boundsMax.z - header.boundsMin.z
                    , 1  // Max tiles.
                    , header.polyCount);
        }
예제 #2
0
파일: NavUtil.cs 프로젝트: zwong91/Titan
        /// <summary>
        /// Derives the <see cref="NavmeshParams"/> for a tile.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This method is useful for getting the configuration required to build a single-tile
        /// navigation mesh for the tile.
        /// </para>
        /// </remarks>
        /// <param name="tile">The tile.</param>
        /// <returns>The <see cref="NavmeshParams"/> for the tile.</returns>
        public static NavmeshParams DeriveConfig(NavmeshTileData tile)
        {
            NavmeshTileHeader header = tile.GetHeader();

            return(new NavmeshParams(header.boundsMin
                                     , header.boundsMax.x - header.boundsMin.x
                                     , header.boundsMax.z - header.boundsMin.z
                                     , 1 // Max tiles.
                                     , header.polyCount));
        }
예제 #3
0
        /// <summary>
        /// Create tile data from the provided build data.
        /// </summary>
        /// <param name="buildData">The build data.</param>
        /// <returns>A new tile data object, or null on error.</returns>
        public static NavmeshTileData Create(NavmeshTileBuildData buildData)
        {
            if (buildData == null || buildData.IsDisposed)
                return null;

            NavmeshTileData result = new NavmeshTileData();

            if (NavmeshTileEx.dtnmBuildTileData(buildData, result))
                return result;

            return null;
        }
예제 #4
0
        /// <summary>
        /// Creates tile data from a serialized data created by <see cref="GetData"/>.
        /// </summary>
        /// <param name="rawTileData">The serialized tile data.</param>
        /// <returns>A new tile data object, or null on error.</returns>
        public static NavmeshTileData Create(byte[] rawTileData)
        {
            if (rawTileData == null)
                return null;

            NavmeshTileData result = new NavmeshTileData();

            if (NavmeshTileEx.dtnmBuildTileDataRaw(rawTileData, rawTileData.Length, result))
                return result;

            return null;
        }
예제 #5
0
        /// <summary>
        /// Create tile data from the provided build data.
        /// </summary>
        /// <param name="buildData">The build data.</param>
        /// <returns>A new tile data object, or null on error.</returns>
        public static NavmeshTileData Create(NavmeshTileBuildData buildData)
        {
            if (buildData == null || buildData.IsDisposed)
            {
                return(null);
            }

            NavmeshTileData result = new NavmeshTileData();

            if (NavmeshTileEx.dtnmBuildTileData(buildData, result))
            {
                return(result);
            }

            return(null);
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <remarks>
        /// <para>
        /// A 'no result' object will be created if the <paramref name="tile"/> parameter 
        /// is null or polygon count is less than one.
        /// </para>
        /// </remarks>
        /// <param name="tx">The x-index of the tile within the tile grid. (tx, tz)</param>
        /// <param name="tz">The z-index of the tile within the tile grid. (tx, tz)</param>
        /// <param name="tile">The tile data.</param>
        /// <param name="polyCount">The polygons in the tile.</param>
        public TileBuildAssets(int tx, int tz, NavmeshTileData tile, int polyCount)
        {
            mTileX = tx;
            mTileZ = tz;

            if (tile == null || polyCount < 1)
            {
                mTile = null;
                mPolyCount = 0;
            }
            else
            {
                mTile = tile;
                mPolyCount = polyCount;
            }
        }
예제 #7
0
        /// <summary>
        /// Creates tile data from a serialized data created by <see cref="GetData"/>.
        /// </summary>
        /// <param name="rawTileData">The serialized tile data.</param>
        /// <returns>A new tile data object, or null on error.</returns>
        public static NavmeshTileData Create(byte[] rawTileData)
        {
            if (rawTileData == null)
            {
                return(null);
            }

            NavmeshTileData result = new NavmeshTileData();

            if (NavmeshTileEx.dtnmBuildTileDataRaw(rawTileData, rawTileData.Length, result))
            {
                return(result);
            }

            return(null);
        }
예제 #8
0
        /// <summary>
        /// Adds a tile to the navigation mesh.
        /// </summary>
        /// <param name="tileData">The tile data.</param>
        /// <param name="desiredTileRef">
        /// The desired reference for the tile.
        /// (Or <see cref="NullTile"/> if the reference doesn't matter or is not known.)
        /// </param>
        /// <param name="resultTileRef">The actual reference assigned to the tile.</param>
        /// <returns>The <see cref="NavStatus"/> flags for the operation.</returns>
        public NavStatus AddTile(NavmeshTileData tileData
                                 , uint desiredTileRef
                                 , out uint resultTileRef)
        {
            if (tileData == null ||
                tileData.IsOwned ||
                tileData.Size == 0)
            {
                resultTileRef = 0;
                return(NavStatus.Failure | NavStatus.InvalidParam);
            }

            resultTileRef = 0;

            return(NavmeshEx.dtnmAddTile(root
                                         , tileData
                                         , desiredTileRef
                                         , ref resultTileRef));
        }
예제 #9
0
        private void HandleWorkingNavmesh(TileSelection selection)
        {
            NavmeshBuild build = selection.Build;
            TileBuildData tdata = build.BuildData;

            if (mDebugObject == null)
            {
                Navmesh navmesh = null;

                if (tdata.BakeableCount() == 0)
                    // Nothing to display.
                    return;

                bool success = true;

                TileSetDefinition tdef = build.TileSetDefinition;

                NavmeshParams nconfig;
                NavmeshTileData[] tiles;

                if (tdef == null)
                {
                    tiles = new NavmeshTileData[1] { tdata.GetTileData(0, 0) };
                    nconfig = NavUtil.DeriveConfig(tiles[0]);
                }
                else
                {
                    TileZone zone;

                    if (selection.HasSelection)
                        zone = selection.Zone;
                    else
                        zone = new TileZone(0, 0, tdef.Width - 1, tdef.Depth - 1);

                    success = tdata.GetMeshBuildData(tdef.BoundsMin, tdef.TileWorldSize, zone
                        , out nconfig, out tiles);
                }

                NavStatus status = NavStatus.Sucess;

                if (success)
                {
                    status = Navmesh.Create(nconfig, out navmesh);

                    if ((status & NavStatus.Failure) == 0)
                    {
                        foreach (NavmeshTileData tile in tiles)
                        {
                            uint trash;
                            status = navmesh.AddTile(tile, Navmesh.NullTile, out trash);

                            if ((status & NavStatus.Sucess) == 0)
                            {
                                navmesh = null;
                                break;
                            }
                        }
                    }
                }

                if ((status & NavStatus.Sucess) == 0)
                {
                    Show = MeshDebugOption.None;  // Use property.
                    Debug.LogError("Mesh Debug View: Error creating working navigation mesh: "
                            + status + ". Disabled display.", build);
                }
                else
                    mDebugObject = navmesh;
            }

            if (mDebugObject != null)
            {
                Navmesh nm = (Navmesh)mDebugObject;
                NavDebug.Draw(nm, NavmeshSceneDraw.Instance.ColorByArea);
            }

        }
예제 #10
0
        /// <summary>
        /// Adds a tile to the navigation mesh.
        /// </summary>
        /// <param name="tileData">The tile data.</param>
        /// <param name="desiredTileRef">
        /// The desired reference for the tile.
        /// (Or <see cref="NullTile"/> if the reference doesn't matter or is not known.)
        /// </param>
        /// <param name="resultTileRef">The actual reference assigned to the tile.</param>
        /// <returns>The <see cref="NavStatus"/> flags for the operation.</returns>
        public NavStatus AddTile(NavmeshTileData tileData
            , uint desiredTileRef
            , out uint resultTileRef)
        {
            if (tileData == null
                || tileData.IsOwned
                || tileData.Size == 0)
            {
                resultTileRef = 0;
                return NavStatus.Failure | NavStatus.InvalidParam;
            }

            resultTileRef = 0;

            return NavmeshEx.dtnmAddTile(root
                , tileData
                , desiredTileRef
                , ref resultTileRef);
        }
예제 #11
0
        /// <summary>
        /// Creates tile data from a serialized data created by <see cref="GetData"/>.
        /// </summary>
        /// <param name="rawTileData">The serialized tile data.</param>
        /// <returns>A new tile data object, or null on error.</returns>
        public static NavmeshTileData Create(byte[] rawTileData)
        {
            if (rawTileData == null)
                return null;

            NavmeshTileData result = new NavmeshTileData();

            if (NavmeshTileEx.dtnmBuildTileDataRaw(rawTileData, rawTileData.Length, result))
                return result;

            return null;
        }
예제 #12
0
        public void Build()
        {
            if (!mBuild)
                return;

            mBuild.ResetBuild();

            // Note: The 'finally' takes care of all cleanup.

            try
            {
                EditorUtility.DisplayCancelableProgressBar("Build & Bake"
                    , "Preparing..."
                    , 0);

                // Prepare the build.

                if (!CompileInput())
                    return;

                if (!InitializeBuild())
                    return;

                // Build the tiles.
                NavmeshParams nconfig = null;
                NavmeshTileData[] tiles = null;
                bool success = true;

                if (mBuild.TileSetDefinition == null)
                {
                    if (!BuildSingleTile())
                        return;

                    tiles = new NavmeshTileData[1] { mBuild.BuildData.GetTileData(0, 0) };

                    nconfig = NavUtil.DeriveConfig(tiles[0]);
                }
                else if (BuildMultiTiled())
                {
                    success = mBuild.BuildData.GetMeshBuildData(mBuild.TileSetDefinition.BoundsMin
                        , mBuild.TileSetDefinition.TileWorldSize
                        , out nconfig, out tiles);
                }
                else
                    return;

                if (!success)
                {
                    mContext.PostError("Navigation mesh creation failed.", mBuild);
                    return;
                }

                // Bake the mesh.

                NavStatus nstatus = 
                    mBuild.BuildTarget.Load(nconfig, tiles, NMBEditorUtil.GetConfig(mBuild));

                if ((nstatus & NavStatus.Sucess) == 0)
                    mContext.PostError("Bake to target: Target reported failure.", mBuild);
                else
                    EditorUtility.SetDirty((Object)mBuild.BuildTarget);
            }
            finally
            {
                mBuild.ResetBuild();
                EditorUtility.ClearProgressBar();
            }
        }
        internal void SetWorkingData(NavmeshTileData tile, int polyCount)
        {
            if (tile == null || polyCount <= 0)
            {
                SetAsEmpty();
                return;
            }

            workingTile = tile.GetData();
            workingPolyCount = polyCount;
        }
예제 #14
0
        /// <summary>
        /// Create tile data from the provided build data.
        /// </summary>
        /// <param name="buildData">The build data.</param>
        /// <returns>A new tile data object, or null on error.</returns>
        public static NavmeshTileData Create(NavmeshTileBuildData buildData)
        {
            if (buildData == null || buildData.IsDisposed)
                return null;

            NavmeshTileData result = new NavmeshTileData();

            if (NavmeshTileEx.dtnmBuildTileData(buildData, result))
                return result;

            return null;
        }
        internal void SetWorkingData(int x, int z, NavmeshTileData tile, int polyCount)
        {
            int i = GetIndex(x, z);
            if (i == IndexError)
                return;

            unsafeItems[i].SetWorkingData(tile, polyCount);

            mIsDirty = true;
            unsafeVersion++;
        }
        internal bool GetMeshBuildData(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
                , 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;
        }
 internal bool GetMeshBuildData(Vector3 origin, float tileWorldSize
     , out NavmeshParams config
     , out NavmeshTileData[] tiles)
 {
     return GetMeshBuildData(origin, tileWorldSize, new TileZone(0, 0, Width - 1, Depth - 1)
         , out config, out tiles);
 }
예제 #18
0
        private void HandleBake()
        {
            NavmeshBuild build = Context.Build;

            NavmeshTileData[] tiles = new NavmeshTileData[1] 
            {
                build.BuildData.GetTileData(0, 0)
            };

            NavmeshParams nconfig = NavUtil.DeriveConfig(tiles[0]);

            NavStatus status = build.BuildTarget.Load(nconfig, tiles, NMBEditorUtil.GetConfig(build));

            if ((status & NavStatus.Failure) == 0)
            {
                EditorUtility.SetDirty((Object)build.BuildTarget);
                // build.BuildData.SetAsBaked();
                build.BuildData.Reset(0, 0);  // Don't need to keep the data.
            }
            else
                Logger.PostError("Bake to target: Target reported failure.", (Object)build.BuildTarget);
        }
    /// <summary>
    /// Loads a navigation mesh.
    /// </summary>
    /// <param name="config">The mesh configuration.</param>
    /// <param name="tiles">The tiles to add to the mesh.</param>
    /// <param name="buildConfig">The build information. (Optional)</param>
    /// <returns>The <see cref="NavStatus"/> flags for the operation.</returns>
    public NavStatus Load(NavmeshParams config
        , NavmeshTileData[] tiles
        , NavmeshBuildInfo buildConfig)
    {
        if (config == null || tiles == null || tiles.Length > config.maxTiles)
            return NavStatus.Failure | NavStatus.InvalidParam;

        Navmesh navmesh;
        NavStatus status = Navmesh.Create(config, out navmesh);

        if ((status & NavStatus.Sucess) == 0)
            return status;

        foreach (NavmeshTileData tile in tiles)
        {
            if (tile == null)
                continue;

            uint trash;
            status = navmesh.AddTile(tile, Navmesh.NullTile, out trash);

            if ((status & NavStatus.Sucess) == 0)
                return status | NavStatus.InvalidParam;
        }

        mDataPack = navmesh.GetSerializedMesh();

        if (mDataPack == null)
            return NavStatus.Failure;

        mBuildInfo = buildConfig;

        mVersion++;

        return NavStatus.Sucess;
    }