예제 #1
0
    // Use this for initialization
    void Start()
    {
        // add 20 random big cuboids into the level.
        // The prefab has been tagged as StaticBatchingUtility Navigation in The Navigation editor
        for (int i = 0; i < 20; i++)
        {
            GameObject go = Instantiate(prefab, new Vector3(Random.Range(-25, 25), Random.Range(0, 1), Random.Range(-25, 25)), Quaternion.AngleAxis(Random.Range(-180, 180), Vector3.up));
            go.transform.parent = transform;
        }

        // Use the standard settings from the editor (I think)
        NavMeshBuildSettings settings = NavMesh.GetSettingsByID(0);

        // gather all the physics colliders which are children of this transform (or you can do this by volume)
        List <NavMeshBuildSource> results = new List <NavMeshBuildSource>();

        NavMeshBuilder.CollectSources(transform, 255, NavMeshCollectGeometry.PhysicsColliders, 0, new List <NavMeshBuildMarkup>(), results);

        // make a 100m box around the origin
        Bounds bounds = new Bounds(Vector3.zero, 100 * Vector3.one);

        // Build the actual navmesh
        NavMeshData data = NavMeshBuilder.BuildNavMeshData(settings, results, bounds, Vector3.zero, Quaternion.identity);

        NavMesh.AddNavMeshData(data);
        success = NavMeshBuilder.UpdateNavMeshData(data, settings, results, bounds);
    }
예제 #2
0
    private void OnEnable()
    {
        terrain                    = GetComponent <Terrain>();
        terrain.terrainData        = GenerateTerrain(terrain.terrainData);
        terrain.transform.position = new Vector3(-config.width / 2, 0, -config.depth / 2);

        List <NavMeshBuildSource> buildSources = new List <NavMeshBuildSource>();

        NavMeshBuilder.CollectSources(
            null,
            ~9, // TODO Custom editor so we can select layers
            NavMeshCollectGeometry.RenderMeshes,
            0,
            new List <NavMeshBuildMarkup>(),
            buildSources
            );

        NavMeshData navData = NavMeshBuilder.BuildNavMeshData(
            NavMesh.GetSettingsByID(0),
            buildSources,
            new Bounds(new Vector3(0, 0, 0), new Vector3(config.width, config.height, config.depth)),
            Vector3.zero,
            Quaternion.identity
            );

        if (navData == null)
        {
            Debug.LogError("Failed to create nav mesh data!");
        }
        else
        {
            navMeshDataInstance = NavMesh.AddNavMeshData(navData);
        }
    }
    void Awake()
    {
        nvData = new NavMeshData();
        foreach (var obj in notWalkable)
        {
            NavMeshBuildMarkup navMeshBuildMarkup = new NavMeshBuildMarkup();
            navMeshBuildMarkup.area         = NavMesh.GetAreaFromName("Not Walkable");
            navMeshBuildMarkup.overrideArea = true;
            navMeshBuildMarkup.root         = obj.transform;
            objectsNotTreated.Add(navMeshBuildMarkup);
        }
        NavMesh.AddNavMeshData(nvData);
        NavMeshBuilder.CollectSources(
            new Bounds(gameObject.transform.position, new Vector3(bounds, bounds, bounds)),
            mask.value,
            NavMeshCollectGeometry.PhysicsColliders,
            0,
            objectsNotTreated,
            watchedSources);

        status = NavMeshBuilder.UpdateNavMeshDataAsync(
            nvData,
            NavMesh.GetSettingsByID(0),
            watchedSources,
            new Bounds(gameObject.transform.position, new Vector3(bounds, bounds, bounds)));
    }
예제 #4
0
        public void BuildNavMesh(GameObject[] fromObjects = null)
        {
            RemoveData();

            var sources = new List <NavMeshBuildSource>();
            var markups = new List <NavMeshBuildMarkup>();

            var buildSettings = NavMesh.GetSettingsByID(agentTypeID);

            if (fromObjects == null)
            {
                NavMeshBuilder.CollectSources(null, layerMask, useGeometry, defaultArea, markups, sources);
            }
            else
            {
                foreach (var obj in fromObjects)
                {
                    var localSources = new List <NavMeshBuildSource>();
                    NavMeshBuilder.CollectSources(obj.transform, layerMask, useGeometry, defaultArea, markups, localSources);
                    sources.AddRange(localSources);
                }
            }

            var bounds = CalculateWorldBounds(transform, sources);

            navMeshData = NavMeshBuilder.BuildNavMeshData(buildSettings, sources, bounds, Vector3.zero, Quaternion.identity);

            AddData();
        }
예제 #5
0
    private IEnumerator CollectSourcesAsync(Action callback)
    {
        float time = Time.get_realtimeSinceStartup();

        Debug.Log((object)"Starting Navmesh Source Collecting.");
        NavMeshBuilder.CollectSources(this.Bounds, LayerMask.op_Implicit(this.LayerMask), this.NavMeshCollectGeometry, this.defaultArea, new List <NavMeshBuildMarkup>(), this.sources);
        if (Object.op_Inequality((Object)TerrainMeta.HeightMap, (Object)null))
        {
            for (float x = (float)-((Bounds) ref this.Bounds).get_extents().x; (double)x < ((Bounds) ref this.Bounds).get_extents().x; x += (float)this.CellSize)
            {
                for (float z = (float)-((Bounds) ref this.Bounds).get_extents().z; (double)z < ((Bounds) ref this.Bounds).get_extents().z; z += (float)this.CellSize)
                {
                    AsyncTerrainNavMeshBake terrainSource = new AsyncTerrainNavMeshBake(Vector3.op_Addition(((Bounds) ref this.Bounds).get_center(), new Vector3(x, 0.0f, z)), this.CellSize, this.Height, false, true);
                    yield return((object)terrainSource);

                    this.terrainBakes.Add(terrainSource);
                    NavMeshBuildSource navMeshBuildSource = terrainSource.CreateNavMeshBuildSource(true);
                    ((NavMeshBuildSource) ref navMeshBuildSource).set_area(this.defaultArea);
                    this.sources.Add(navMeshBuildSource);
                    terrainSource = (AsyncTerrainNavMeshBake)null;
                }
            }
        }
        this.AppendModifierVolumes(ref this.sources);
        float num = Time.get_realtimeSinceStartup() - time;

        if ((double)num > 0.100000001490116)
        {
            Debug.LogWarning((object)("Calling CollectSourcesAsync took " + (object)num));
        }
        if (callback != null)
        {
            callback();
        }
    }
예제 #6
0
        /// <summary>
        /// Creates the NavMeshData
        /// </summary>
        /// <param name="surface"></param>
        /// <returns></returns>
        private NavMeshData InitializeBakeData()
        {
            List <NavMeshBuildSource> buildSources = new List <NavMeshBuildSource>();
            Bounds bounds = new Bounds(primaryViewer.Transform.position, new Vector3(100f, 50f, 100f));

            NavMeshBuilder.CollectSources(transform, navMeshSurface.layerMask, NavMeshCollectGeometry.PhysicsColliders, 0, new List <NavMeshBuildMarkup>(), buildSources);

            return(NavMeshBuilder.BuildNavMeshData(navMeshSurface.GetBuildSettings(), buildSources, bounds, navMeshSurface.transform.position, navMeshSurface.transform.rotation));
        }
예제 #7
0
    // Token: 0x06000B4C RID: 2892 RVA: 0x000516E0 File Offset: 0x0004F8E0
    private void BuildTile(Pathfinding.NavMeshTile tile)
    {
        DateTime now = DateTime.Now;
        List <NavMeshBuildSource> list    = new List <NavMeshBuildSource>();
        List <NavMeshBuildMarkup> markups = new List <NavMeshBuildMarkup>();

        Pathfinding.AgentType     z        = (Pathfinding.AgentType)tile.m_tile.z;
        Pathfinding.AgentSettings settings = this.GetSettings(z);
        Bounds includedWorldBounds         = new Bounds(tile.m_center, new Vector3(this.m_tileSize, 6000f, this.m_tileSize));
        Bounds localBounds = new Bounds(Vector3.zero, new Vector3(this.m_tileSize, 6000f, this.m_tileSize));
        int    defaultArea = settings.m_canWalk ? 0 : 1;

        NavMeshBuilder.CollectSources(includedWorldBounds, this.m_layers.value, NavMeshCollectGeometry.PhysicsColliders, defaultArea, markups, list);
        if (settings.m_avoidWater)
        {
            List <NavMeshBuildSource> list2 = new List <NavMeshBuildSource>();
            NavMeshBuilder.CollectSources(includedWorldBounds, this.m_waterLayers.value, NavMeshCollectGeometry.PhysicsColliders, 1, markups, list2);
            using (List <NavMeshBuildSource> .Enumerator enumerator = list2.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    NavMeshBuildSource item = enumerator.Current;
                    item.transform *= Matrix4x4.Translate(Vector3.down * 0.2f);
                    list.Add(item);
                }
                goto IL_1AE;
            }
        }
        if (settings.m_canSwim)
        {
            List <NavMeshBuildSource> list3 = new List <NavMeshBuildSource>();
            NavMeshBuilder.CollectSources(includedWorldBounds, this.m_waterLayers.value, NavMeshCollectGeometry.PhysicsColliders, 3, markups, list3);
            if (settings.m_swimDepth != 0f)
            {
                using (List <NavMeshBuildSource> .Enumerator enumerator = list3.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        NavMeshBuildSource item2 = enumerator.Current;
                        item2.transform *= Matrix4x4.Translate(Vector3.down * settings.m_swimDepth);
                        list.Add(item2);
                    }
                    goto IL_1AE;
                }
            }
            list.AddRange(list3);
        }
IL_1AE:
        if (tile.m_data == null)
        {
            tile.m_data          = new NavMeshData();
            tile.m_data.position = tile.m_center;
        }
        this.m_buildOperation = NavMeshBuilder.UpdateNavMeshDataAsync(tile.m_data, settings.m_build, list, localBounds);
        this.m_buildTile      = tile;
    }
예제 #8
0
        private List <NavMeshBuildSource> CollectMesh()
        {
            List <NavMeshBuildSource> sources = new List <NavMeshBuildSource>();
            List <NavMeshBuildMarkup> markups = new List <NavMeshBuildMarkup>();

            NavMeshBuilder.CollectSources(bounds, collectLayers.value, NavMeshCollectGeometry.RenderMeshes,
                                          NavMesh.GetAreaFromName("BakeLink"), markups, sources);

            return(sources);
        }
예제 #9
0
    List <NavMeshBuildSource> GetSource()
    {
        var sources = new List <NavMeshBuildSource>();

        NavMeshBuilder.CollectSources(null, LayerMask, UseGeometry, 0, new List <NavMeshBuildMarkup>(), sources);

        sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshAgent>() != null));

        return(sources);
    }
예제 #10
0
    // Use this for initialization
    void Start()
    {
        List <NavMeshBuildMarkup> markups = new List <NavMeshBuildMarkup>();
        List <NavMeshBuildSource> builds  = new List <NavMeshBuildSource>();

        NavMeshBuilder.CollectSources(transform, LayerMask.NameToLayer("Default"), NavMeshCollectGeometry.RenderMeshes, 0, markups, builds);
        NavMeshBuildSettings buildSettings = new NavMeshBuildSettings();
        Bounds localBounds = new Bounds();

        localBounds.size = new Vector3(100f, 100f, 100f);

        NavMeshBuilder.BuildNavMeshData(buildSettings, builds, localBounds, Vector3.zero, Quaternion.identity);
    }
예제 #11
0
    // create the NavMesh at run time after map is generated
    void bakeNavMesh()
    {
        // lots of BS about the scene that will need editing at some point I'm sure
        Bounds b = new Bounds(mapCenter, mapCenter + new Vector3(0f, 10f, 0f));
        List <NavMeshBuildSource> sources = new List <NavMeshBuildSource>();
        List <NavMeshBuildMarkup> markups = new List <NavMeshBuildMarkup>();

        NavMeshBuilder.CollectSources(b, 0, NavMeshCollectGeometry.RenderMeshes, 0, markups, sources);
        NavMeshBuildSettings settings = NavMesh.CreateSettings();

        // here's the actual important line
        NavMeshBuilder.BuildNavMeshData(settings, sources, b, mapCenter, Quaternion.identity);
    }
예제 #12
0
    public void GeanerateNavMeshData()
    {
        List <NavMeshBuildSource> sourceList = new List <NavMeshBuildSource>();
        List <NavMeshBuildMarkup> markups    = new List <NavMeshBuildMarkup>();

        NavMeshBuilder.CollectSources(new Bounds(Vector3.zero, Vector3.one * 1000), 0 | (1 << 9), NavMeshCollectGeometry.RenderMeshes, 0, markups, sourceList);
        NavMeshBuildSettings nmbs = NavMesh.CreateSettings();

        nmbs.agentTypeID = Spawner.nma[0].agentTypeID;
        NavMeshData nmd = NavMeshBuilder.BuildNavMeshData(nmbs, sourceList, new Bounds(transform.position, Vector3.one * 1000), Vector3.zero, Quaternion.identity);

        currentNavMeshs.Add(NavMesh.AddNavMeshData(nmd));
    }
예제 #13
0
    public void generateNavMesh()
    {
        //https://community.gamedev.tv/t/modify-navmesh-dynamically/25849/3
        List <NavMeshBuildSource> buildSources = new List <NavMeshBuildSource>();

        NavMeshBuilder.CollectSources(transform, navMeshLayers, NavMeshCollectGeometry.RenderMeshes, 0, new List <NavMeshBuildMarkup>(), buildSources);

        NavMeshData navData = NavMeshBuilder.BuildNavMeshData(navSettings, buildSources, new Bounds(Vector3.zero, new Vector3(10000, 10000, 10000)), Vector3.down,
                                                              Quaternion.Euler(Vector3.up));

        navMeshDataInstance = NavMesh.AddNavMeshData(navData);
        //Debug.Log(buildSources.Count);
        //Debug.Log("Map Built");
    }
예제 #14
0
    List <NavMeshBuildSource> GetBuildSources(LayerMask mask)
    {
        List <NavMeshBuildSource> sources = new List <NavMeshBuildSource>();

        NavMeshBuilder.CollectSources(
            new Bounds(BoundsCenter, BoundsSize),
            mask,
            NavMeshCollectGeometry.PhysicsColliders,
            0,
            new List <NavMeshBuildMarkup>(),
            sources);
        Debug.Log("Sources found: " + sources.Count.ToString());
        return(sources);
    }
예제 #15
0
    public void RebuildNavMesh()
    {
        NavMeshBuilder.CollectSources(
            new Bounds(gameObject.transform.position, new Vector3(bounds, bounds, bounds)),
            mask.value, // LayerMask.GetMask("MovingNavMesh"),
            NavMeshCollectGeometry.PhysicsColliders,
            0,
            objectsNotTreated,
            watchedSources);

        status = NavMeshBuilder.UpdateNavMeshDataAsync(
            nvData,
            NavMesh.GetSettingsByID(0),
            watchedSources,
            new Bounds(gameObject.transform.position, new Vector3(bounds, bounds, bounds)));
    }
예제 #16
0
    public static void BuildNavMesh(Transform root, Bounds bounds)
    {
        // Use the standard settings from the editor (I think)
        NavMeshBuildSettings settings = NavMesh.GetSettingsByID(0);

        // gather all the physics colliders which are children of this transform (or you can do this by volume)
        List <NavMeshBuildSource> results = new List <NavMeshBuildSource>();

        NavMeshBuilder.CollectSources(root, 255, NavMeshCollectGeometry.RenderMeshes, 0, new List <NavMeshBuildMarkup>(), results);

        // Build the actual navmesh
        NavMeshData data = NavMeshBuilder.BuildNavMeshData(settings, results, bounds, Vector3.zero, Quaternion.identity);

        instances.Add(NavMesh.AddNavMeshData(data));

        //success = NavMeshBuilder.UpdateNavMeshData(data, settings, results, bounds);
    }
예제 #17
0
    // Start is called before the first frame update
    void Start()
    {
        mg = GetComponent <MapGenerator>();
        mg.Generate();
        List <NavMeshBuildSource> sourceList = new List <NavMeshBuildSource>();
        List <NavMeshBuildMarkup> markups    = new List <NavMeshBuildMarkup>();

        NavMeshBuilder.CollectSources(new Bounds(Vector3.zero, Vector3.one * 1000), 0 | (1 << 9), NavMeshCollectGeometry.RenderMeshes, 0, markups, sourceList);

        NavMeshData nmd = NavMeshBuilder.BuildNavMeshData(NavMesh.CreateSettings(), sourceList, new Bounds(transform.position, Vector3.one * 1000), Vector3.zero, Quaternion.identity);

        if (!nmd)
        {
            print("OH NO"); return;
        }
        NavMesh.AddNavMeshData(nmd);
    }
예제 #18
0
        List <NavMeshBuildSource> CollectSources()
        {
            var sources = new List <NavMeshBuildSource>();
            var markups = new List <NavMeshBuildMarkup>();

            NavMeshBuilder.CollectSources(transform, m_LayerMask, NavMeshCollectGeometry.RenderMeshes, m_DefaultArea, markups, sources);

            if (m_IgnoreNavMeshAgent)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshAgent>() != null));
            }

            if (m_IgnoreNavMeshObstacle)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshObstacle>() != null));
            }

            return(sources);
        }
예제 #19
0
    void InitializeBuildSetting()
    {
        //build setting
        buildSettings             = NavMesh.GetSettingsByID(0);
        buildSettings.agentClimb  = 0.4f;
        buildSettings.agentSlope  = 45;
        buildSettings.agentHeight = 2;
        buildSettings.agentRadius = 0.5f;

        //set build source
        Bounds planetBound = new Bounds(planet.position, planet.localScale * 2);

        NavMeshBuilder.CollectSources(planetBound
                                      , navMeshLayer
                                      , new NavMeshCollectGeometry()
                                      , 0
                                      , new List <NavMeshBuildMarkup>()
                                      , buildSources);
    }
예제 #20
0
    private IEnumerator CollectSourcesAsync(Action callback)
    {
        MonumentNavMesh monumentNavMesh = null;
        float           single          = UnityEngine.Time.realtimeSinceStartup;

        UnityEngine.Debug.Log("Starting Navmesh Source Collecting.");
        List <NavMeshBuildMarkup> navMeshBuildMarkups = new List <NavMeshBuildMarkup>();

        NavMeshBuilder.CollectSources(monumentNavMesh.Bounds, monumentNavMesh.LayerMask, monumentNavMesh.NavMeshCollectGeometry, monumentNavMesh.defaultArea, navMeshBuildMarkups, monumentNavMesh.sources);
        if (TerrainMeta.HeightMap != null)
        {
            for (float i = -monumentNavMesh.Bounds.extents.x; i < monumentNavMesh.Bounds.extents.x; i += (float)monumentNavMesh.CellSize)
            {
                for (float j = -monumentNavMesh.Bounds.extents.z; j < monumentNavMesh.Bounds.extents.z; j += (float)monumentNavMesh.CellSize)
                {
                    AsyncTerrainNavMeshBake asyncTerrainNavMeshBake = new AsyncTerrainNavMeshBake(monumentNavMesh.Bounds.center + new Vector3(i, 0f, j), monumentNavMesh.CellSize, monumentNavMesh.Height, false, true);
                    yield return(asyncTerrainNavMeshBake);

                    monumentNavMesh.terrainBakes.Add(asyncTerrainNavMeshBake);
                    NavMeshBuildSource navMeshBuildSource = asyncTerrainNavMeshBake.CreateNavMeshBuildSource(true);
                    navMeshBuildSource.area = monumentNavMesh.defaultArea;
                    monumentNavMesh.sources.Add(navMeshBuildSource);
                    asyncTerrainNavMeshBake = null;
                }
            }
        }
        monumentNavMesh.AppendModifierVolumes(ref monumentNavMesh.sources);
        float single1 = UnityEngine.Time.realtimeSinceStartup - single;

        if (single1 > 0.1f)
        {
            UnityEngine.Debug.LogWarning(string.Concat("Calling CollectSourcesAsync took ", single1));
        }
        if (callback != null)
        {
            callback();
        }
    }
예제 #21
0
    public void GenerateGrid(Vector3 minCorner, Vector3 maxCorner)
    {
        _worldBounds.min = minCorner;
        _worldBounds.max = maxCorner;

        // Find all nav mesh object sources in the scene
        NavMeshBuilder.CollectSources(_worldBounds, _obstacleMask, NavMeshCollectGeometry.PhysicsColliders, 1, _navMeshMarkups, _navMeshBuildSources);
        for (int i = 0; i < _pathFindObjects.Count; ++i)
        {
            _navMeshBuildSources.Add(_pathFindObjects[i].Source);
        }

        // Add a source to represent the 'ground' plane
        // NavMeshBuildSource groundSource = default(NavMeshBuildSource);
        // groundSource.area = 0;
        // groundSource.shape = NavMeshBuildSourceShape.Box;
        // groundSource.size = _worldBounds.size.WithY(0.1f);
        // groundSource.transform = Matrix4x4.identity * Matrix4x4.Translate(_worldBounds.center.WithY(0));
        // _navMeshBuildSources.Add(groundSource);

        // Build settings for default agent
        NavMeshBuildSettings buildSettings = NavMesh.GetSettingsByID(0);

        // Create or update the nav mesh
        if (_navMesh == null)
        {
            _navMesh = NavMeshBuilder.BuildNavMeshData(buildSettings, _navMeshBuildSources, _worldBounds, Vector3.zero, Quaternion.identity);
            NavMesh.AddNavMeshData(_navMesh);
        }
        else
        {
            NavMeshBuilder.UpdateNavMeshData(_navMesh, buildSettings, _navMeshBuildSources, _worldBounds);
        }

        _isBuilt = true;
    }
예제 #22
0
        List <NavMeshBuildSource> CollectSources()
        {
            var sources = new List <NavMeshBuildSource>();
            var markups = new List <NavMeshBuildMarkup>();

            List <NavMeshModifier> modifiers;

            if (m_CollectObjects == CollectObjects.Children)
            {
                modifiers = new List <NavMeshModifier>(GetComponentsInChildren <NavMeshModifier>());
                modifiers.RemoveAll(x => !x.isActiveAndEnabled);
            }
            else
            {
                modifiers = NavMeshModifier.activeModifiers;
            }

            foreach (var m in modifiers)
            {
                if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
                {
                    continue;
                }
                if (!m.AffectsAgentType(m_AgentTypeID))
                {
                    continue;
                }
                var markup = new NavMeshBuildMarkup();
                markup.root            = m.transform;
                markup.overrideArea    = m.overrideArea;
                markup.area            = m.area;
                markup.ignoreFromBuild = m.ignoreFromBuild;
                markups.Add(markup);
            }

            if (m_CollectObjects == CollectObjects.All)
            {
                NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
            }
            else if (m_CollectObjects == CollectObjects.Children)
            {
                NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
            }
            else if (m_CollectObjects == CollectObjects.Volume)
            {
                Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
                NavMeshBuilder.CollectSources(worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
            }

            if (m_IgnoreNavMeshAgent)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshAgent>() != null));
            }

            if (m_IgnoreNavMeshObstacle)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshObstacle>() != null));
            }

            AppendModifierVolumes(ref sources);

            return(sources);
        }