Exemplo n.º 1
0
    public List <Mesh> GenerateIslandMesh(MapRegion region, IsleInfo info, float squareSize, float depth, TerrainVerticesDatabase vertDatabase)
    {
        squareGrid = new SquareGrid(region, squareSize);
        vertices.Clear();
        triangles.Clear();
        coords.Clear();

        meshDetail = new IsleMeshDetail();

        for (int x = 0; x < squareGrid.squares.GetLength(0); x++)
        {
            for (int y = 0; y < squareGrid.squares.GetLength(1); y++)
            {
                TriangulateSquare(squareGrid.squares[x, y]);
            }
        }

        vertDatabase.AddVertices(vertices, coords, transform.position + info.offset, info.id);

        Mesh surfaceMesh = new Mesh();

        surfaceMesh.vertices  = vertices.ToArray();
        surfaceMesh.triangles = triangles.ToArray();
        surfaceMesh.RecalculateNormals();

        Mesh undersideMesh = new Mesh();

        triangles.Reverse();
        undersideMesh.vertices  = vertices.ToArray();
        undersideMesh.triangles = triangles.ToArray();
        undersideMesh.RecalculateNormals();

        List <Mesh> meshList = new List <Mesh> ();

        meshList.Add(surfaceMesh);
        meshList.Add(CreateWallMesh(depth));
        meshList.Add(undersideMesh);

        info.surfaceMeshDetail = meshDetail;
        return(meshList);
    }
Exemplo n.º 2
0
    public List <Mesh> GenerateMesh(MapRegion region, IsleInfo info, float squareSize, float depth)
    {
        squareGrid = new SquareGrid(region, squareSize);
        vertices.Clear();
        triangles.Clear();

        meshRegion = new MeshRegion();

        for (int x = 0; x < squareGrid.squares.GetLength(0); x++)
        {
            for (int y = 0; y < squareGrid.squares.GetLength(1); y++)
            {
                TriangulateSquare(squareGrid.squares[x, y]);
            }
        }

        List <Mesh> meshList = new List <Mesh> ();

        Mesh surfaceMesh = new Mesh();

        surfaceMesh.vertices  = vertices.ToArray();
        surfaceMesh.triangles = triangles.ToArray();
        surfaceMesh.RecalculateNormals();

        Mesh undersideMesh = new Mesh();

        triangles.Reverse();
        undersideMesh.vertices  = vertices.ToArray();
        undersideMesh.triangles = triangles.ToArray();
        undersideMesh.RecalculateNormals();

        meshList.Add(surfaceMesh);
        meshList.Add(CreateWallMesh(depth));
        meshList.Add(undersideMesh);

        info.surfaceMeshRegion = meshRegion;
        return(meshList);
    }
    void SeparateIslands(List <MapRegion> islandRegions)
    {
        // Based on regions, create separate child GameObject for each island

        // Destroy all the previous islands
        islands.Clear();
        var childList = transform.Cast <Transform> ().ToList();

        foreach (Transform island in childList)
        {
#if UNITY_EDITOR
            ////////////////////////////////////////////////////////////  for debugging, only for in editor
            if (!Application.isPlaying)                             ////
            {
                UnityEditor.EditorApplication.delayCall += () =>    ////
                {                                                   ////
                    if (island)                                     ////
                    {
                        DestroyImmediate(island.gameObject);        ////
                    }                                               ////
                };                                                  ////
            }
            else                                                    ////
            ////////////////////////////////////////////////////////////
            {
#endif
            Destroy(island.gameObject);
#if UNITY_EDITOR
            ////////////////////////////////////////////////////////////
        }                                                           ////
        ////////////////////////////////////////////////////////////
#endif
        }

        int islandCount = 0;
        foreach (MapRegion region in islandRegions)
        {
            IsleInfo isle = new IsleInfo();
            isle.id = islandCount;

            // Create each isle game object
            isle.gameObject = new GameObject("Island " + isle.id);
            isle.gameObject.transform.parent        = transform;
            isle.gameObject.transform.localRotation = Quaternion.identity;
            isle.offset = region.GetCentre() * islandData.tileSize;
            isle.gameObject.transform.localPosition = isle.offset;

            // Child game object of isle to store surface
            GameObject surface = AddChildMesh("Surface", isle.gameObject.transform, withCollider);
            // Child game object of isle to store wall
            GameObject wall = AddChildMesh("Wall", isle.gameObject.transform);
            // Child game object of isle to store underside
            GameObject underside = AddChildMesh("Underside", isle.gameObject.transform);
            underside.transform.position += Vector3.up * -islandData.depth;

            List <Mesh> meshes = meshGen.GenerateIslandMesh(region, isle, islandData.tileSize, islandData.depth, vertDatabase);

            // Mesh for surface
            surface.GetComponent <MeshFilter> ().mesh       = meshes[0];
            surface.GetComponent <MeshRenderer> ().material = islandData.grassMaterial;

            // Mesh for wall
            wall.GetComponent <MeshFilter> ().mesh       = meshes[1];
            wall.GetComponent <MeshRenderer> ().material = islandData.wallMaterial;

            // Mesh for underside
            underside.GetComponent <MeshFilter> ().mesh       = meshes[2];
            underside.GetComponent <MeshRenderer> ().material = islandData.dirtMaterial;

            islands.Add(isle);
            islandCount++;
        }
    }