示例#1
0
        static void LoadConfig()
        {
            if (!ConfigLoaded)
            {
                // Standard config
                IntPtr ptr = Recast.DefaultConfig(Application.dataPath + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + "Recast.log");
                config = (Config)Marshal.PtrToStructure(ptr, typeof(Config));

                ecfg.AgentHeight     = config.walkableHeight * config.ch;
                ecfg.AgentMaxClimb   = config.walkableClimb * config.ch;
                ecfg.AgentRadius     = config.walkableRadius * config.ch;
                EdgeLen              = config.maxEdgeLen * config.cs;
                RegionMinSize        = (float)Math.Sqrt(config.minRegionArea);
                RegionMergeSize      = (float)Math.Sqrt(config.mergeRegionArea);
                DetailSampleDist     = config.detailSampleDist / config.cs;
                DetailSampleMaxError = config.detailSampleMaxError / config.ch;
                ConfigLoaded         = true;
            }
        }
示例#2
0
        void BakeSingleTile()
        {
            UpdateProgress(0f);

            int nverts = 0;

            float[] verts = null;
            int     ntris = 0;

            int[] tris = null;
            ProcessTerrain(ref verts, ref nverts, ref tris, ref ntris);

            UpdateProgress(0.25f);

            config.walkableHeight       = (int)Math.Ceiling(ecfg.AgentHeight / config.ch);
            config.walkableClimb        = (int)Math.Floor(ecfg.AgentMaxClimb / config.ch);
            config.walkableRadius       = (int)Math.Ceiling(ecfg.AgentRadius / config.cs);
            config.maxEdgeLen           = (int)(EdgeLen / config.cs);
            config.minRegionArea        = (int)Math.Pow(RegionMinSize, 2);
            config.mergeRegionArea      = (int)Math.Pow(RegionMergeSize, 2);
            config.detailSampleDist     = DetailSampleDist < 0.9f ? 0 : config.cs * DetailSampleDist;
            config.detailSampleMaxError = DetailSampleMaxError * config.ch;

            // Generate Recast
            Recast.handleBuild(ref config, verts, nverts, tris, ntris);

            // Fetch navmeshes
            IntPtr         polyPtr   = Recast.getPolyMesh();
            IntPtr         detailPtr = Recast.getPolyMeshDetail();
            PolyMesh       mesh      = (PolyMesh)Marshal.PtrToStructure(polyPtr, typeof(PolyMesh));
            PolyMeshDetail detail    = (PolyMeshDetail)Marshal.PtrToStructure(detailPtr, typeof(PolyMeshDetail));

            UpdateProgress(0.5f);

            // Create asset
            PolyMeshAsset asset = CustomAssetUtility.CreateAssetWithoutSaving <PolyMeshAsset>();

            // Save config
            asset.config = config;

            // Set poly data
            asset.PolyMesh.nverts   = mesh.nverts;
            asset.PolyMesh.npolys   = mesh.npolys;
            asset.PolyMesh.maxpolys = mesh.maxpolys;
            asset.PolyMesh.nvp      = mesh.nvp;
            CopyArray <float>(mesh.bmin, ref asset.PolyMesh.bmin, 3);
            CopyArray <float>(mesh.bmax, ref asset.PolyMesh.bmax, 3);
            asset.PolyMesh.cs         = mesh.cs;
            asset.PolyMesh.ch         = mesh.ch;
            asset.PolyMesh.borderSize = mesh.borderSize;

            asset.PolyMesh.verts = new ushort[3 * mesh.nverts];
            CopyArray(mesh.verts, asset.PolyMesh.verts, 3 * mesh.nverts);

            asset.PolyMesh.polys = new ushort[mesh.maxpolys * 2 * mesh.nvp];
            CopyArray(mesh.polys, asset.PolyMesh.polys, mesh.maxpolys * 2 * mesh.nvp);

            asset.PolyMesh.regs = new ushort[mesh.maxpolys];
            CopyArray(mesh.regs, asset.PolyMesh.regs, mesh.maxpolys);

            asset.PolyMesh.flags = new ushort[mesh.npolys];
            CopyArray(mesh.flags, asset.PolyMesh.flags, mesh.npolys);

            asset.PolyMesh.areas = new byte[mesh.maxpolys];
            CopyArray(mesh.areas, asset.PolyMesh.areas, mesh.maxpolys);

            // Set detail data
            asset.PolyDetailMesh.nmeshes = detail.nmeshes;
            asset.PolyDetailMesh.nverts  = detail.nverts;
            asset.PolyDetailMesh.ntris   = detail.ntris;

            asset.PolyDetailMesh.meshes = new uint[4 * detail.nmeshes];
            CopyArray(detail.meshes, asset.PolyDetailMesh.meshes, 4 * detail.nmeshes);

            asset.PolyDetailMesh.verts = new float[3 * detail.nverts];
            CopyArray(detail.verts, asset.PolyDetailMesh.verts, 3 * detail.nverts);

            asset.PolyDetailMesh.tris = new byte[4 * detail.ntris];
            CopyArray(detail.tris, asset.PolyDetailMesh.tris, 4 * detail.ntris);

            // Save asset again
            UpdateProgress(0.75f);
            CustomAssetUtility.SaveAsset <PolyMeshAsset>(asset);

            // Close window
            UpdateProgress(0f);
            EditorUtility.ClearProgressBar();
        }