Example #1
0
    public void GenerateMap()
    {
        float[,] noiseMap = Noise.GenNoiseMap(mapChunkSize, mapChunkSize, seed, noiseScale, octaves, persistance, lacunarity, offset);

        Color[] colormap = new Color[mapChunkSize * mapChunkSize];
        for (int y = 0; y < mapChunkSize; y++)
        {
            for (int x = 0; x < mapChunkSize; x++)
            {
                float currentHeight = noiseMap [x, y];
                for (int i = 0; i < regions.Length; i++)
                {
                    if (currentHeight <= regions [i].height)
                    {
                        colormap [y * mapChunkSize + x] = regions [i].color;
                        break;
                    }
                }
            }

            MapDisplay display = FindObjectOfType <MapDisplay> ();
            if (drawMode == DrawMode.NoiseMap)
            {
                display.DrawTexture(TextureGen.TextureFromHeightMap(noiseMap));
            }
            else if (drawMode == DrawMode.ColorMap)
            {
                display.DrawTexture(TextureGen.TextureFromColorMap(colormap, mapChunkSize, mapChunkSize));
            }
            else if (drawMode == DrawMode.Mesh)
            {
                display.DrawMesh(MeshGen.GenerateTerrainMesh(noiseMap, meshHeightMultiplier, MeshHeightCurve, levelOfDetail), TextureGen.TextureFromColorMap(colormap, mapChunkSize, mapChunkSize));
            }
        }
    }
    public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMulti, AnimationCurve heightCurve, Vector2 chunkCoords, TerrainType[] regions)
    {
        int       width       = heightMap.GetLength(0);
        int       height      = heightMap.GetLength(1);
        float     topLeftX    = (width - 1) / -2f;
        float     topLeftZ    = (height - 1) / 2f;
        Texture2D meshTexture = TextureGen.TextureFromColorMap(ColorGen.GenerateColorMapFromHeightMap(heightMap, regions), width, height);

        Color[]  colorMap        = ColorGen.GenerateColorMapFromHeightMap(heightMap, regions);
        MeshData currentMeshData = new MeshData(width, height, chunkCoords, meshTexture, colorMap);
        int      vertexIndex     = 0;

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                currentMeshData.vertices[vertexIndex] = new Vector3(topLeftX + x,
                                                                    heightCurve.Evaluate(heightMap[x, y]) * heightMulti, topLeftZ - y);
                currentMeshData.uvs[vertexIndex] = new Vector2(x / (float)width, y / (float)height);

                if (x < width - 1 && y < height - 1)
                {
                    currentMeshData.AddTriangle(vertexIndex, vertexIndex + width + 1, vertexIndex + width);
                    currentMeshData.AddTriangle(vertexIndex + width + 1, vertexIndex, vertexIndex + 1);
                }

                vertexIndex++;
            }
        }

        return(currentMeshData);
    }
Example #3
0
        void OnMapDataRecieved(MapData mapData)
        {
            this.mapData    = mapData;
            mapDataRecieved = true;
            Texture2D texture = TextureGen.TextureFromColorMap(mapData.colorMap, MapGen.mapChunkSize, MapGen.mapChunkSize);

            meshRenderer.material.mainTexture = texture;
            UpdateTerrainChunk();
        }
Example #4
0
    public void DrawMapInEditor()
    {
        MapData    mapData = GenerateMapData(Vector2.zero);
        MapDisplay display = FindObjectOfType <MapDisplay>();

        if (drawMode == DrawMode.NoiseMap)
        {
            display.DrawTexture(TextureGen.TextureFromHeightMap(mapData.heightMap));
        }
        else if (drawMode == DrawMode.ColorMap)
        {
            display.DrawTexture(TextureGen.TextureFromColorMap(mapData.colorMap, mapChunkSize, mapChunkSize));
        }
        else if (drawMode == DrawMode.Mesh)
        {
            display.DrawMesh(MeshGen.GenerateMesh(mapData.heightMap, meshHeightMultiplier, meshHeightCurve, editorPreviewLOD), TextureGen.TextureFromColorMap(mapData.colorMap, mapChunkSize, mapChunkSize));
        }
    }
Example #5
0
    public void DrawMapInEditor()
    {
        MapData mapdata = GenThatMapData(Vector2.zero);
        Map     map     = FindObjectOfType <Map>();

        if (drawMode == DrawMode.NoiseMap)
        {
            map.DrawTexture(TextureGen.TextureFromHeightMap(mapdata.heightMap));
        }
        else if (drawMode == DrawMode.ColorMap)
        {
            map.DrawTexture(TextureGen.TextureFromColorMap(mapdata.colorMap, mapChunkSize, mapChunkSize));
        }
        else if (drawMode == DrawMode.DrawMesh)
        {
            map.DrawMesh(MeshGen.GennerateTerrainMesh(mapdata.heightMap, meshHeight, meshHeightCurve, editorlevelOfDetail), TextureGen.TextureFromColorMap(mapdata.colorMap, mapChunkSize, mapChunkSize));
        }
        else if (drawMode == DrawMode.FallOffMap)
        {
            map.DrawTexture(TextureGen.TextureFromHeightMap(FalloffMapGenerator.GenerateFalloffMapp(mapChunkSize)));
        }
    }
Example #6
0
    void OnScene(SceneView scene)
    {
        if (stampMode == StampMode.Nothing)
        {
            return;
        }

        HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
        Event e = Event.current;

        terrainGenerator = gameObject.GetComponent <TerrainGenerator>();
        if (e.type == EventType.MouseDown && e.button == 0)
        {
            Vector3 mousePos = e.mousePosition;
            float   ppp      = EditorGUIUtility.pixelsPerPoint;
            mousePos.y  = scene.camera.pixelHeight - mousePos.y * ppp;
            mousePos.x *= ppp;

            Ray        ray = scene.camera.ScreenPointToRay(mousePos);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                Mesh              mesh             = hit.collider.gameObject.GetComponent <MeshFilter>().sharedMesh;
                MeshRenderer      meshRenderer     = hit.collider.gameObject.GetComponent <MeshRenderer>();
                MeshFields        meshFields       = hit.collider.gameObject.GetComponent <MeshFields>();
                MeshCollider      meshCollider     = hit.collider.gameObject.GetComponent <MeshCollider>();
                Transform         trans            = hit.collider.gameObject.transform;
                Vector3[]         verts            = mesh.vertices;
                List <VertexData> verticesToChange = new List <VertexData>();
                float             nearestDistance  = Mathf.Infinity;
                for (int i = 0; i < verts.Length; i++)
                {
                    Vector3 worldPt = trans.TransformPoint(verts[i]);
                    float   dist    = Vector2.Distance(new Vector2(worldPt.x, worldPt.z),
                                                       new Vector2(hit.point.x, hit.point.z));
                    if (dist < radius)
                    {
                        if (stampMode == StampMode.Mountain)
                        {
                            verts[i].y += (radius - dist) * heightMultiplier + Random.Range(0, persistance);
                            meshFields.MeshData.colorMap[i] = checkColorFromHeight(verts[i].y, terrainGenerator.regions,
                                                                                   terrainGenerator.meshHeightCurve, terrainGenerator.meshHeightMultiplier);
                        }

                        if (stampMode == StampMode.Flatten)
                        {
                            bool nearestToCursor = false;
                            if (dist < nearestDistance)
                            {
                                nearestDistance = dist;
                                nearestToCursor = true;
                            }

                            VertexData vertex = new VertexData(i, verts[i], nearestToCursor);
                            verticesToChange.Add(vertex);
                        }

                        if (stampMode == StampMode.Paint)
                        {
                            meshFields.MeshData.colorMap[i] = paintColor;
                        }
                    }
                }

                if (stampMode == StampMode.Flatten)
                {
                    float referenceY = 0f;
                    foreach (VertexData vertexData in verticesToChange.Where(vertexData => vertexData.closestToCursor))
                    {
                        referenceY = vertexData.position.y;
                    }

                    foreach (VertexData vertexData in verticesToChange)
                    {
                        float dif = referenceY - verts[vertexData.index].y;
                        if (Math.Abs(dif) < flattenStepSize)
                        {
                            verts[vertexData.index].y = referenceY;
                        }
                        else if (dif < 0)
                        {
                            verts[vertexData.index].y -= flattenStepSize;
                        }
                        else if (dif > 0)
                        {
                            verts[vertexData.index].y += flattenStepSize;
                        }

                        meshFields.MeshData.colorMap[vertexData.index] = checkColorFromHeight(verts[vertexData.index].y,
                                                                                              terrainGenerator.regions,
                                                                                              terrainGenerator.meshHeightCurve, terrainGenerator.meshHeightMultiplier);
                    }
                }

                mesh.vertices = verts;
                mesh.RecalculateBounds();
                mesh.RecalculateNormals();
                meshCollider.sharedMesh = mesh;
                Texture2D meshTexture = TextureGen.TextureFromColorMap(meshFields.MeshData.colorMap,
                                                                       meshFields.MeshData.dims.x, (int)mesh.bounds.size.z);
                meshRenderer.sharedMaterial.mainTexture = meshTexture;
            }

            e.Use();
        }

        HandleUtility.Repaint();
    }
Example #7
0
    public void GenerateMap()
    {
        numOfTrees   = 0;
        numOfStones  = 0;
        parentObject = GameObject.FindGameObjectWithTag("Objects").transform;

        //SceneManager.LoadScene("LevelGenScrene");
        //EditorSceneManager.LoadScene("LevelGenScrene");
        GameObject cleanup;

        while (cleanup = GameObject.FindWithTag("Tree"))
        {
            Object.DestroyImmediate(cleanup);
        }
        while (cleanup = GameObject.FindWithTag("Stone"))
        {
            Object.DestroyImmediate(cleanup);
        }
        while (cleanup = GameObject.FindWithTag("Bush"))
        {
            Object.DestroyImmediate(cleanup);
        }
        while (cleanup = GameObject.FindWithTag("Boulder"))
        {
            Object.DestroyImmediate(cleanup);
        }
        while (cleanup = GameObject.FindWithTag("Grass"))
        {
            Object.DestroyImmediate(cleanup);
        }

        float a = Random.Range(5f, 20f);
        float b = Random.Range(0f, 20f);
        float c = Random.Range(5f, 20f);
        float d = Random.Range(0f, 20f);

        float[,] noiseMap = NoiseMapGen.GenerateNoiseMap(mapWidth, mapHeight, a, b, c, d);
        MeshData meshData = MeshGen.GenerateTerrainMesh(noiseMap, meshHeightMulitplier, heightCurve);

        triangles = meshData.triangles;

        Color[] colorMap = new Color[mapWidth * mapHeight];

        for (int y = 0; y < mapHeight; y++)
        {
            for (int x = 0; x < mapWidth; x++)
            {
                float currentHeight = Mathf.InverseLerp(0, 20, noiseMap[x, y]);
                for (int i = 0; i < regions.Length; i++)
                {
                    if (currentHeight <= regions[i].height)
                    {
                        //New code to try and get lerp from one zone to another
                        //Skipping earth into grass transition
                        if (regions[i].name.Equals("Earth"))
                        {
                            float tempMaxHeightOfRegion = regions[i].height;
                            float tempMinHeightOfRegion = regions[i - 1].height;
                            float tempMidHeightOfRegion = regions[i - 1].height + ((regions[i].height - regions[i - 1].height) / 2);

                            if (currentHeight > tempMidHeightOfRegion)
                            {
                                colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i + 1].color, Mathf.InverseLerp(tempMidHeightOfRegion, tempMaxHeightOfRegion, currentHeight));
                            }
                            else
                            {
                                colorMap[y * mapWidth + x] = regions[i].color;
                            }
                        }
                        else if (i > 0 && i < regions.Length - 1)
                        {
                            float tempMaxHeightOfRegion = regions[i].height;
                            float tempMinHeightOfRegion = regions[i - 1].height;
                            float tempMidHeightOfRegion = regions[i - 1].height + ((regions[i].height - regions[i - 1].height) / 2);

                            /*if (currentHeight >= tempMinHeightOfRegion)
                             *  colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i + 1].color, /*(float) noiseMap[x, y]/10 Mathf.InverseLerp(tempMidHeightOfRegion, tempMaxHeightOfRegion, currentHeight));
                             * else if (currentHeight < tempMinHeightOfRegion)
                             * {
                             *  colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i - 1].color, /*(float) noiseMap[x, y]/10 Mathf.InverseLerp(tempMinHeightOfRegion, tempMidHeightOfRegion, currentHeight));
                             * }*/
                            //colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i + 1].color, /*(float) noiseMap[x, y]/10*/ Mathf.InverseLerp(tempMinHeightOfRegion, tempMaxHeightOfRegion, currentHeight) - 0.1f);
                            if (currentHeight > tempMidHeightOfRegion)
                            {
                                colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i + 1].color, Mathf.InverseLerp(tempMidHeightOfRegion, tempMaxHeightOfRegion, currentHeight));
                            }
                            else
                            {
                                colorMap[y * mapWidth + x] = regions[i].color;
                            }
                        }
                        else if (i == 0)
                        {
                            /*float tempMaxHeightOfRegion = regions[i].height;
                             * float tempMinHeightOfRegion = 0;
                             * float tempMidHeightOfRegion = 0 + ((regions[i].height - 0) / 2);
                             * if(currentHeight > tempMidHeightOfRegion)
                             *  colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i + 1].color, /*(float) noiseMap[x, y]/10 Mathf.InverseLerp(tempMidHeightOfRegion, tempMaxHeightOfRegion, currentHeight));
                             * else*/
                            colorMap[y * mapWidth + x] = regions[i].color;
                        }
                        else
                        {
                            /*float tempMaxHeightOfRegion = regions[i].height;
                             * float tempMinHeightOfRegion = regions[i - 1].height;
                             * float tempMidHeightOfRegion = regions[i - 1].height + ((regions[i].height - regions[i - 1].height) / 2);
                             * colorMap[y * mapWidth + x] = Color.Lerp(regions[i].color, regions[i - 1].color, /*(float) noiseMap[x, y]/10 Mathf.InverseLerp(tempMinHeightOfRegion, tempMaxHeightOfRegion, currentHeight));
                             */
                            colorMap[y * mapWidth + x] = regions[i].color;
                        }
                        //OG code for coolour depending on height
                        //colorMap[y * mapWidth + x] = regions[i].color;
                        //place Gameobjects
                        //trees/bushes on grassy area
                        if (regions[i].name.Equals("Grass"))
                        {
                            if (Random.Range(0f, 1f) > 0.9f)
                            {
                                Vector3 treePos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5));
                                currentInstance = Instantiate(trees[(int)Random.Range(0, trees.Length)], treePos, Quaternion.identity);
                                numOfTrees++;
                                currentInstance.transform.parent = parentObject;
                            }
                            else if (Random.Range(0f, 1f) > 0.8f)
                            {
                                Vector3 bushPos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5));
                                currentInstance = Instantiate(bushes[(int)Random.Range(0, bushes.Length)], bushPos, Quaternion.identity);
                                currentInstance.transform.parent = parentObject;
                            }
                            if (Random.Range(0f, 1f) > 0.75f)
                            {
                                Vector3 grassPos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5));
                                currentInstance = Instantiate(grasses[(int)Random.Range(0, grasses.Length)], grassPos, Quaternion.identity);
                                currentInstance.transform.parent = parentObject;
                            }
                        }
                        //Stones on Sand
                        else if (regions[i].name.Equals("Sand"))
                        {
                            if (Random.Range(0f, 1f) > 0.9f)
                            {
                                Vector3 stonePos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5));
                                currentInstance = Instantiate(stones[(int)Random.Range(0, stones.Length)], stonePos, Quaternion.identity);
                                numOfStones++;
                                currentInstance.transform.parent = parentObject;
                            }
                        }
                        else if (regions[i].name.Equals("Earth"))
                        {
                            if (Random.Range(0f, 1f) > 0.96f)
                            {
                                Vector3 treePos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5));
                                currentInstance = Instantiate(beachTrees[(int)Random.Range(0, beachTrees.Length)], treePos + new Vector3(0, 3.5f, 0), Quaternion.Euler(-90, 0, 0));
                                currentInstance.transform.parent = parentObject;
                            }
                            else if (Random.Range(0f, 1f) > 0.9f)
                            {
                                Vector3 stonePos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5));
                                currentInstance = Instantiate(stones[(int)Random.Range(0, stones.Length)], stonePos, Quaternion.identity);
                                currentInstance.transform.parent = parentObject;
                            }
                            else if (Random.Range(0f, 1f) > 0.90f)
                            {
                                Vector3 grassPos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5));
                                currentInstance = Instantiate(deadGrass, grassPos + new Vector3(0, 0.3f, 0), Quaternion.identity);
                                currentInstance.transform.parent = parentObject;
                            }
                        }
                        else if (regions[i].name.Equals("Mountain"))
                        {
                            if (Random.Range(0f, 1f) > 0.95f)
                            {
                                Vector3 boulderPos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5));
                                currentInstance = Instantiate(bouldersMountain[(int)Random.Range(0, bouldersMountain.Length)], boulderPos, Quaternion.identity);
                                currentInstance.transform.localScale = new Vector3(Random.Range(8, 10), Random.Range(8, 10), Random.Range(8, 10));
                                currentInstance.transform.parent     = parentObject;
                            }
                            else if (Random.Range(0f, 1f) > 0.99f)
                            {
                                float   temp       = Random.Range(5, 10);
                                Vector3 boulderPos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5));
                                currentInstance = Instantiate(treesMountain[(int)Random.Range(0, treesMountain.Length)], boulderPos, Quaternion.identity);
                                currentInstance.transform.localScale = new Vector3(temp, temp, temp);
                                currentInstance.transform.parent     = parentObject;
                            }
                        }
                        else if (regions[i].name.Equals("Snow"))
                        {
                            if (Random.Range(0f, 1f) > 0.9f)
                            {
                                Vector3 boulderPos = Vector3.Scale(meshData.vertices[y * mapWidth + x], new Vector3(5, 1, 5));
                                currentInstance = Instantiate(boulders[(int)Random.Range(0, boulders.Length)], boulderPos, Quaternion.identity);
                                currentInstance.transform.parent = parentObject;
                            }
                        }
                        break;
                    }
                }
            }
        }
        //Force conditions
        if (numOfTrees < minNumOfTree || numOfStones < minNumOfStones)
        {
            Debug.Log("Not Enough");
            GenerateMap();
        }
        else
        {
            Debug.Log("Enough");
            MapDisplay display = FindObjectOfType <MapDisplay>();
            //display.DrawTexture(TextureGen.TextureFromColorMap(colorMap, mapWidth, mapHeight));

            //Select render type just 2d colour or 3d mesh
            display.DrawMesh(meshData, TextureGen.TextureFromColorMap(colorMap, mapWidth, mapHeight));

            //Texture try
            //setTextureTriangles(meshData);
            //display.DrawMeshWithSubMesh(meshData/*, water.ToArray(), sand.ToArray(), earth.ToArray(), forest.ToArray(), mountain.ToArray(), snow.ToArray()*/);
        }
    }
Example #8
0
    private void Start()
    {
        Map     map     = FindObjectOfType <Map>();
        MapData mapdata = GenThatMapData(Vector2.zero);

        if (drawMode == DrawMode.DrawMesh)
        {
            map.DrawMesh(MeshGen.GennerateTerrainMesh(mapdata.heightMap, meshHeight, meshHeightCurve, editorlevelOfDetail), TextureGen.TextureFromColorMap(mapdata.colorMap, mapChunkSize, mapChunkSize));
        }
    }