public static Mesh Generate(ref Datas datas, Vector3 pos)
    {
        Mesh mesh = HexMeshGenerator.GenerateMesh(ref datas.tVars);

        AddNoise(ref datas.tVars, ref datas.nVars, mesh, pos);

        return(mesh);
    }
Exemple #2
0
    public static MeshData Generate(Datas datas, Vector3 pos)
    {
        MeshData mesh = HexMeshGenerator.GenerateMesh(datas.tVars.Size, datas.tVars.Scale);

        AddNoise(ref datas.tVars, ref datas.nVars, ref mesh, pos);

        return(mesh);
    }
    void Awake()
    {
        generator = new HexMeshGenerator(18, 1);

        map = ScriptableObject.CreateInstance <Map>();
        map.New(10);
        map.GetCell(1, 0).state   = MapCell.State.Full;
        map.GetCell(6, 2).state   = MapCell.State.Full;
        map.GetCell(7, 2).state   = MapCell.State.Excavated;
        map.GetCell(7, 1).state   = MapCell.State.Full;
        map.GetCell(8, 1).state   = MapCell.State.Full;
        map.GetCell(10, -2).state = MapCell.State.Full;

        RedrawMeshes();
    }
Exemple #4
0
    public static GameObject GenerateHexObject(Vector3 size, Material mat)
    {
        GameObject obj = new GameObject();

        obj.transform.localScale = size;

        MeshFilter mf = obj.AddComponent <MeshFilter>();

        mf.mesh = HexMeshGenerator.GenerateMesh(size);

        MeshRenderer mr = obj.AddComponent <MeshRenderer>();

        mr.material = mat;

        return(obj);
    }
    void RecreateMapPatchMesh(Vector2Int c)
    {
        if (meshGenerator == null)
        {
            meshGenerator = new HexMeshGenerator(meshPatchSize, meshSubdivsShift);
        }

        int idx = (c.y - patchesY0) * patchesW + (c.x - patchesX0);

        DestroyImmediate(mapPatchObjs[idx].gameObject);

        Transform patch = CreatePatch(c.y, c.x);

        if (patch != null)
        {
            patch.SetParent(patchesRoot, false);
            Vector2 pos = (c.x * HexMeshGenerator.rhex + c.y * HexMeshGenerator.rhey) * meshPatchSize;
            patch.localPosition = new Vector3(pos.x, 0, pos.y);
            mapPatchObjs[idx]   = patch;
        }
    }
    public void DrawWithBrush()
    {
        if (currentMousePos == null)
        {
            return;
        }

        HexXY coords = currentMousePos.Value.Coords;
        HashSet <Vector2Int> patchesChanged      = new HashSet <Vector2Int>();
        List <Vector2Int>    patchIndicesForCell = new List <Vector2Int>();

        for (int i = -brushSize + 1; i < brushSize; i++)
        {
            for (int j = -brushSize + 1; j < brushSize; j++)
            {
                if (HexXY.Dist(new HexXY(i, j), new HexXY(0, 0)) > brushSize - 1)
                {
                    continue;
                }

                HexXY   c    = coords + new HexXY(i, j);
                MapCell cell = map.GetCell(c);
                if (cell == map.externalCell)
                {
                    continue;
                }

                bool changed = false;
                switch (brushMode)
                {
                case BrushMode.Excavate:
                    if (cell.state != MapCell.State.Excavated)
                    {
                        changed    = true;
                        cell.state = MapCell.State.Excavated;
                    }
                    break;

                case BrushMode.Fill:
                    if (cell.state != MapCell.State.Full)
                    {
                        changed    = true;
                        cell.state = MapCell.State.Full;
                    }
                    break;
                }

                if (changed)
                {
                    HexMeshGenerator.ListPatchIndicesForCell(meshPatchSize, c, patchIndicesForCell);
                    foreach (Vector2Int pi in patchIndicesForCell)
                    {
                        patchesChanged.Add(pi);
                    }
                }
            }
        }

        if (patchesChanged.Count > 0)
        {
            //Fix outermost wall issues (when using ListPatchIndicesForCell it may happen that we don't redraw outermost walls)
            //We do this by adding cells just outside the brush if anything is changed
            for (int i = -brushSize; i < brushSize + 1; i++)
            {
                for (int j = -brushSize; j < brushSize + 1; j++)
                {
                    if (HexXY.Dist(new HexXY(i, j), new HexXY(0, 0)) == brushSize)
                    {
                        HexXY c = coords + new HexXY(i, j);
                        HexMeshGenerator.ListPatchIndicesForCell(meshPatchSize, c, patchIndicesForCell);
                        foreach (Vector2Int pi in patchIndicesForCell)
                        {
                            patchesChanged.Add(pi);
                        }
                    }
                }
            }
        }

        foreach (Vector2Int c in patchesChanged)
        {
            RecreateMapPatchMesh(c);
        }

        if (patchesChanged.Count > 0)
        {
            EditorUtility.SetDirty(map);
        }

        EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
    }
    public void CreateMapMeshes()
    {
        meshGenerator = new HexMeshGenerator(meshPatchSize, meshSubdivsShift);

        //DEBUG some walls
        foreach (var cell in map.AllCells())
        {
            if (HexXY.Dist(new HexXY(map.size / 2, map.size / 2), cell.Coords) < map.size / 3)
            {
                cell.Cell.state = MapCell.State.Excavated;
            }
        }
        //-------------

        int s = (map.size - 1) / meshPatchSize + 1;

        patchesX0 = -2 * s;
        patchesY0 = -1;
        patchesW  = 3 * s;
        patchesH  = 2 * s + 1;

        mapPatchObjs = new Transform[patchesH * patchesW];

        if (patchesRoot != null)
        {
            DestroyImmediate(patchesRoot.gameObject);
        }


        patchesRoot            = new GameObject(mapPatchesName).transform;
        patchesRoot.localScale = new Vector3(mapScale, mapScale, mapScale);
        patchesRoot.parent     = this.transform;

        for (int i = 0; i < patchesH; i++)
        {
            for (int j = 0; j < patchesW; j++)
            {
                Transform patch = CreatePatch(patchesY0 + i, patchesX0 + j);
                if (patch != null)
                {
                    patch.SetParent(patchesRoot, false);
                    Vector2 pos = ((patchesX0 + j) * HexMeshGenerator.rhex + (patchesY0 + i) * HexMeshGenerator.rhey) * meshPatchSize;
                    patch.localPosition            = new Vector3(pos.x, 0, pos.y);
                    mapPatchObjs[i * patchesW + j] = patch;
                }
            }
        }

        //DEBUG patches bounds checking...
        //List<Vector2Int> patchIdxs = new List<Vector2Int>();
        //int minX = int.MaxValue, minY = int.MaxValue, maxX = int.MinValue, maxY = int.MinValue;
        //foreach(var cell in map.AllCells())
        //{
        //    HexMeshGenerator.ListPatchIndicesForCell(meshPatchSize, cell.Coords, patchIdxs);

        //    foreach(Vector2Int idx in patchIdxs)
        //    {
        //        minX = Mathf.Min(idx.x, minX);
        //        maxX = Mathf.Max(idx.x, maxX);
        //        minY = Mathf.Min(idx.y, minY);
        //        maxY = Mathf.Max(idx.y, maxY);
        //    }
        //    //Debug.Log("Coords: " + cell.Coords.ToString());
        //    //Debug.Log(string.Join(" ", patchIdxs.Select(x => x.ToString()).ToArray()));
        //}

        //Debug.Log(map.size);

        //Debug.Log(string.Format("{0},{1},{2},{3}", minX, minY, maxX, maxY));

        //if (minX < patchesX0 || maxX >= patchesX0 + patchesW || minY < patchesY0 || maxY >= patchesY0 + patchesH)
        //    Debug.Log(string.Format("Wrong! {0} {1} {2} {3}", patchesX0, patchesY0, patchesX0 + patchesW - 1, patchesY0 + patchesH - 1));
    }