Example #1
0
        public unsafe bool LoadTiles(int surfaceId, Dictionary <int2, NavMeshTile> tiles)
        {
            string path = GetPath(surfaceId, typeof(NavMeshTile).Name, null);

            if (!File.Exists(path))
            {
                //Debug.LogFormat("Tiles not found id:{0}", surfaceId);
                return(false);
            }

            using (StreamBinaryReader reader = new StreamBinaryReader(path))
            {
                int tileCount = reader.ReadInt();
                for (int i = 0; i < tileCount; i++)
                {
                    int    tileSize = reader.ReadInt();
                    byte[] data     = new byte[tileSize];
                    fixed(byte *dataPtr = data)
                    {
                        reader.ReadBytes(dataPtr, tileSize);
                    }

                    NavMeshTile tile = new NavMeshTile();
                    tile.Data         = data;
                    tiles[tile.Coord] = tile;
                }
            }
            return(true);
        }
Example #2
0
        public void GetTileGeometry(AiNativeList <float3> vertices, AiNativeList <int> indices)
        {
            AiNativeList <float3> tileVerts   = new AiNativeList <float3>(2);
            AiNativeList <int>    tileIndices = new AiNativeList <int>(2);

            foreach (byte[] data in Tiles)
            {
                NavMeshTile tile = new NavMeshTile();
                tile.Data = data;
                tile.GetTileVertices(tileVerts, tileIndices);

                // Copy vertices
                int vbase = vertices.Length;
                for (int i = 0; i < tileVerts.Length; i++)
                {
                    vertices.Add(tileVerts[i]);
                }

                // Copy indices with offset applied
                for (int i = 0; i < tileIndices.Length; i++)
                {
                    indices.Add(tileIndices[i] + vbase);
                }

                tileVerts.Clear();
                tileIndices.Clear();
            }

            tileVerts.Dispose();
            tileIndices.Dispose();
        }
Example #3
0
        private unsafe int BuildTile(int2 tileCoordinate, NavMeshBuildSettings buildSettings, NavAgentSettings agentSettings,
                                     NavMeshBuildInput buildInput, long buildTimeStamp, out NavMeshTile meshTile)
        {
            meshTile = null;

            if (buildInput.AreasLength != buildInput.IndicesLength / 3)
            {
                return(-1001);
            }

            if (buildInput.VerticesLength <= 0 || buildInput.IndicesLength <= 0)
            {
                return(-1000);
            }

            DtBoundingBox tileBoundingBox = NavMeshBuildUtils.CalculateTileBoundingBox(buildSettings, tileCoordinate);

            NavMeshBuildUtils.SnapBoundingBoxToCellHeight(buildSettings, ref tileBoundingBox);

            tileBoundingBox.min.y = HeightBounds.min.y;
            tileBoundingBox.max.y = HeightBounds.max.y;

            IntPtr builder = Navigation.NavMesh.CreateBuilder();

            DtBuildSettings internalBuildSettings = new DtBuildSettings
            {
                // Tile settings
                BoundingBox  = tileBoundingBox,
                TilePosition = tileCoordinate,
                TileSize     = buildSettings.TileSize,

                // General build settings
                CellHeight           = buildSettings.CellHeight,
                CellSize             = buildSettings.CellSize,
                RegionMinArea        = buildSettings.MinRegionArea,
                RegionMergeArea      = buildSettings.RegionMergeArea,
                EdgeMaxLen           = buildSettings.MaxEdgeLen,
                EdgeMaxError         = buildSettings.MaxEdgeError,
                DetailSampleDist     = buildSettings.DetailSamplingDistance,
                DetailSampleMaxError = buildSettings.MaxDetailSamplingError,

                // Agent settings
                AgentHeight   = agentSettings.Height,
                AgentRadius   = agentSettings.Radius,
                AgentMaxClimb = agentSettings.MaxClimb,
                AgentMaxSlope = agentSettings.MaxSlope
            };

            Navigation.NavMesh.SetSettings(builder, new IntPtr(&internalBuildSettings));

            IntPtr buildResultPtr = Navigation.NavMesh.Build2(builder, buildInput.Vertices, buildInput.VerticesLength, buildInput.Indices, buildInput.IndicesLength, buildInput.Areas);

            DtGeneratedData *generatedDataPtr = (DtGeneratedData *)buildResultPtr;

            if (generatedDataPtr->Success && generatedDataPtr->NavmeshDataLength > 0)
            {
                meshTile = new NavMeshTile();

                // Copy the generated navigationMesh data
                meshTile.Data = new byte[generatedDataPtr->NavmeshDataLength + sizeof(long)];
                Marshal.Copy(generatedDataPtr->NavmeshData, meshTile.Data, 0, generatedDataPtr->NavmeshDataLength);

                // Append time stamp
                byte[] timeStamp = BitConverter.GetBytes(buildTimeStamp);
                for (int i = 0; i < timeStamp.Length; i++)
                {
                    meshTile.Data[meshTile.Data.Length - sizeof(long) + i] = timeStamp[i];
                }
            }

            int error = generatedDataPtr->Error;

            Navigation.NavMesh.DestroyBuilder(builder);

            return(error);
        }