Exemplo n.º 1
0
    public void RebakeMesh()
    {
        area = new Bounds();

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

        foreach (Transform go in transform)
        {
            var src = new NavMeshBuildSource();
            src.transform    = go.localToWorldMatrix;
            src.shape        = NavMeshBuildSourceShape.Mesh;
            src.sourceObject = go.GetComponent <MeshFilter>().mesh;
            sources.Add(src);

            area.Encapsulate(go.GetComponent <Renderer>().bounds);
        }
        area.Expand(1);

        List <NavMeshBuildSettings> configs = new List <NavMeshBuildSettings>();

        foreach (var agent in settings)
        {
            var s = NavMesh.CreateSettings();
            s.agentClimb  = agent.climbAngle;
            s.agentRadius = agent.radius;
            s.agentSlope  = agent.jump;
            s.agentTypeID = 1;
            configs.Add(s);
        }

        NavMeshBuilder.UpdateNavMeshData(data, configs[0], sources, area);
    }
        void ResetNavMeshSurface(RecastGraph recast, ref NavMeshSurface surface)
        {
            #region set agent
            var agentTypeID = int.MinValue;
            var count       = NavMesh.GetSettingsCount();
            for (var i = 0; i < count; i++)
            {
                var settings = NavMesh.GetSettingsByIndex(i);
                if (Mathf.Abs(settings.agentRadius - recast.characterRadius) > Mathf.Epsilon)
                {
                    continue;
                }
                if (Mathf.Abs(settings.agentHeight - recast.walkableHeight) > Mathf.Epsilon)
                {
                    continue;
                }
                if (Mathf.Abs(settings.agentClimb - recast.walkableClimb) > Mathf.Epsilon)
                {
                    continue;
                }
                if (Mathf.Abs(settings.agentSlope - recast.maxSlope) > Mathf.Epsilon)
                {
                    continue;
                }
                agentTypeID = settings.agentTypeID;
            }
            if (agentTypeID == int.MinValue)
            {
                var settings = NavMesh.CreateSettings();
                settings.agentRadius = recast.characterRadius;
                settings.agentHeight = recast.walkableHeight;
                settings.agentClimb  = recast.walkableClimb;
                settings.agentSlope  = recast.maxSlope;
                agentTypeID          = settings.agentTypeID;
                Debug.LogWarning("Creating new agent setting");
            }
            surface.agentTypeID = agentTypeID;
            #endregion

            surface.collectObjects = CollectObjects.Volume;
            surface.size           = recast.forcedBoundsSize;
            surface.center         = recast.forcedBoundsCenter;
            surface.layerMask      = recast.mask;
            surface.useGeometry    = NavMeshCollectGeometry.RenderMeshes;
            if (recast.rasterizeColliders)
            {
                surface.useGeometry = NavMeshCollectGeometry.PhysicsColliders;
            }

            #region TODO: set area
            // RecastMeshObj?
            #endregion

            surface.overrideVoxelSize = true;
            surface.voxelSize         = recast.cellSize;
            surface.overrideTileSize  = true;
            //surface.tileSize = Mathf.FloorToInt(recast.editorTileSize / surface.voxelSize);
            surface.tileSize = recast.editorTileSize;
        }
Exemplo n.º 3
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);
    }
Exemplo n.º 4
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));
    }
Exemplo n.º 5
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);
    }
Exemplo n.º 6
0
 // Token: 0x0600117B RID: 4475 RVA: 0x00078C3C File Offset: 0x00076E3C
 public AgentSettings(Pathfinding.AgentType type)
 {
     this.m_agentType = type;
     this.m_build     = NavMesh.CreateSettings();
 }
Exemplo n.º 7
0
        public void LoadMap(IMapData mapData, bool generateNavMesh)
        {
            var meshFilter = GetComponent <MeshFilter>();

            if (meshFilter == null)
            {
                throw new Exception("MeshFilter on map not found.");
            }

            if (meshFilter.mesh == null)
            {
                meshFilter.mesh = new Mesh();
            }

            var fowMeshFilter = FogOfWar.GetComponent <MeshFilter>();

            if (fowMeshFilter.mesh == null)
            {
                fowMeshFilter.mesh = new Mesh();
            }

            var fowVertices = new List <Vector3>();
            var mapVertices = new List <Vector3>();
            var mapUVs      = new List <Vector2>();
            var fowUVs      = new List <Vector2>();
            var i           = 0;
            var mapIndices  = new int[(mapData.Width - 1) * (mapData.Length - 1) * 4];

            for (int x = 0; x < mapData.Width; x++)
            {
                for (int y = 0; y < mapData.Length; y++)
                {
                    fowVertices.Add(GameUtils.GetPosition(new Vector2(x, y), mapData));
                    mapVertices.Add(GameUtils.GetPosition(new Vector2(x, y), mapData));
                    mapUVs.Add(new Vector2(x, y) / 4);
                    fowUVs.Add(new Vector2((float)x / mapData.Width, (float)y / mapData.Length));

                    if (y == mapData.Length - 1 || x == mapData.Width - 1)
                    {
                        continue;
                    }

                    mapIndices[i++] = x * mapData.Length + y;
                    mapIndices[i++] = x * mapData.Length + y + 1;
                    mapIndices[i++] = (x + 1) * mapData.Length + y + 1;
                    mapIndices[i++] = (x + 1) * mapData.Length + y;
                }
            }

            ApplyMesh(meshFilter, mapVertices, mapIndices, mapUVs);
            ApplyMesh(fowMeshFilter, fowVertices, mapIndices, fowUVs);

            var mapCollider = GetComponent <MeshCollider>();

            if (mapCollider != null)
            {
                mapCollider.sharedMesh = meshFilter.mesh;
            }

            var fowCollider = FogOfWar.GetComponent <MeshCollider>();

            if (fowCollider != null)
            {
                fowCollider.sharedMesh = fowMeshFilter.mesh;
            }

            gameObject.isStatic = true;

            for (int x = 0; x < mapData.Width; x++)
            {
                for (int y = 0; y < mapData.Length; y++)
                {
                    var        pos  = GameUtils.GetPosition(new Vector2(x + 0.5f, y + 0.5f), mapData);
                    GameObject inst = null;
                    switch (mapData.GetMapObjectAt(x, y))
                    {
                    case MapObject.Tree:
                        inst = Instantiate(TreePrefab);
                        break;

                    case MapObject.Crystal:
                        inst = Instantiate(CrystalPrefab);
                        break;
                    }

                    if (inst == null)
                    {
                        continue;
                    }

                    inst.transform.parent        = ObjectsContainer.transform;
                    inst.transform.localPosition = pos;
                }
            }

            if (generateNavMesh)
            {
                var settings = NavMesh.CreateSettings();
                settings.agentTypeID = AgentTypeID;
                settings.agentRadius = 0;
                settings.agentSlope  = 60;

                var source = new NavMeshBuildSource();
                source.shape        = NavMeshBuildSourceShape.Mesh;
                source.sourceObject = meshFilter.mesh;
                source.transform    = transform.localToWorldMatrix;

                var navMeshData = NavMeshBuilder.BuildNavMeshData(settings, new List <NavMeshBuildSource> {
                    source
                }, new Bounds(new Vector3((float)mapData.Width / 2, 0, (float)mapData.Length / 2), new Vector3(mapData.Width, 2, mapData.Length)), transform.position, transform.rotation);

                NavMesh.RemoveAllNavMeshData();
                NavMesh.AddNavMeshData(navMeshData);
            }
            mMapData = mapData;
            FogOfWarTexture.width                  = mapData.Width;
            FogOfWarTexture.height                 = mapData.Length;
            FogOfWarCamera.orthographicSize        = Mathf.Max(mapData.Width - 1, mapData.Length - 1) / 2f;
            FogOfWarCamera.transform.localPosition = new Vector3((mapData.Width - 1) / 2f, 10, (mapData.Length - 1) / 2f);
        }