Exemple #1
0
    void GenerateInOneFrame()
    {
        Texture2D texture = new Texture2D(width, height);

        GetComponent <Renderer>().material.mainTexture = texture;

        ModuleBase     combinedTerrain = TerrainPresets.GetPreset(preset);
        SMappingRanges ranges          = new SMappingRanges();


        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                double p = (double)x / (double)width;
                double q = (double)y / (double)height;
                double nx, ny = 0.0;
                nx = ranges.mapx0 + p * (ranges.mapx1 - ranges.mapx0);
                ny = ranges.mapy0 + q * (ranges.mapy1 - ranges.mapy0);

                double val = combinedTerrain.Get(nx * 3, ny * 3);
                if (val == 0)
                {
                    texture.SetPixel(x, y, Color.black);
                }
                else
                {
                    texture.SetPixel(x, y, Color.white);
                }
            }
        }

        texture.Apply();
    }
	IEnumerator GenerateStream(){
		Texture2D texture = new Texture2D(width,height);
		GetComponent<Renderer>().material.mainTexture = texture;

		ModuleBase combinedTerrain = TerrainPresets.GetPreset(preset);
		SMappingRanges ranges = new SMappingRanges();

		yield return null;

		for (int x = 0; x < width; x++){
			yield return null;

			for(int y = 0; y < height; y++){
				double p = (double)x / (double)width;
				double q = (double)y / (double)height;
				double nx, ny = 0.0;
				nx = ranges.mapx0 + p * (ranges.mapx1 - ranges.mapx0);
				ny = ranges.mapy0 + q * (ranges.mapy1 - ranges.mapy0);

				float val = (float) combinedTerrain.Get(nx * scale, ny * scale);
				texture.SetPixel(x,y,new Color(val,val,val));
			}
			texture.Apply();
		}		

	}
    protected void GenerateInOneFrame(ModuleBase moduleBase)
    {
        Texture2D texture = new Texture2D(width, height);

        GetComponent <Renderer>().material.mainTexture = texture;
        SMappingRanges ranges = new SMappingRanges();


        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                double p = (double)x / (double)width;
                double q = (double)y / (double)height;
                double nx, ny = 0.0;
                nx = ranges.mapx0 + p * (ranges.mapx1 - ranges.mapx0);
                ny = ranges.mapy0 + q * (ranges.mapy1 - ranges.mapy0);

                float val = (float)moduleBase.Get(nx * scale, ny * scale);
                texture.SetPixel(x, y, new Color(val, val, val));
            }
        }

        texture.Apply();
    }
Exemple #4
0
    public Texture2D getTexture()
    {
        Fractal fractal = new Fractal(fractalType, basisType, interpType, octaves, frequency, null);

        fractal.SetLacunarity(lacunarity);
        fractal.seed = seed;

        // Create new texture
        heightmap = new Texture2D(texture_width, texture_width);

        SMappingRanges ranges = new SMappingRanges();

        ranges.loopx0 = 1.0;
        ranges.loopy0 = 1.0;
        ranges.loopz0 = 1.0;

        // Fill heightmap
        for (var i = 0; i < (texture_width * texture_width); i++)
        {
            int x = i / texture_width;
            int y = i % texture_width;

            float noise_x = ((float)x / (float)texture_width) * noise_scale;
            float noise_y = ((float)y / (float)texture_width) * noise_scale;

            double nx = ranges.mapx0 + noise_x * (ranges.mapx1 - ranges.mapx0);
            double ny = ranges.mapy0 + noise_y * (ranges.mapy1 - ranges.mapy0);

            float sample = (float)fractal.Get(nx, ny);

            if (sample >= sea_level)
            {
                //sample = sample;
            }
            else
            {
                //sample  -= 0.2f;
            }

            Color col = new Color(sample, sample, sample);

            heightmap.SetPixel(x, y, col);
        }

        heightmap.Apply();

        return(heightmap);
    }
Exemple #5
0
        public static float[,] GenerateNoiseMap(System.Random prng, int mapWidth, int mapHeight, AccidentalNoiseSettings settings)
        {
            ModuleBase     moduleBase = settings.GetFractal(prng.Next(int.MinValue, int.MaxValue));
            SMappingRanges ranges     = new SMappingRanges();

            float[,] noiseMap = new float[mapWidth, mapHeight];
            for (int x = 0; x < mapWidth; x++)
            {
                for (int y = 0; y < mapHeight; y++)
                {
                    double p = x / (double)mapWidth;
                    double q = y / (double)mapHeight;

                    double nx = ranges.mapx0 + p * (ranges.mapx1 - ranges.mapx0);
                    double ny = ranges.mapy0 + q * (ranges.mapy1 - ranges.mapy0);

                    noiseMap[x, y] = (float)moduleBase.Get(nx * settings.scale, ny * settings.scale);
                }
            }

            return(noiseMap);
        }
Exemple #6
0
    IEnumerator GenerateStream()
    {
        float scale = duplicateObj.transform.localScale.x;

        ModuleBase     combinedTerrain = TerrainPresets.GetPreset(preset);
        SMappingRanges ranges          = new SMappingRanges();


        Camera.main.transform.position = new Vector3(width / 2.0f * scale, height / 2.0f * scale, -15.0f);


        yield return(null);

        for (int x = 0; x < width; x++)
        {
            yield return(null);

            for (int y = 0; y < height; y++)
            {
                double p = (double)x / (double)width;
                double q = (double)y / (double)height;
                double nx, ny = 0.0;
                nx = ranges.mapx0 + p * (ranges.mapx1 - ranges.mapx0);
                ny = ranges.mapy0 + q * (ranges.mapy1 - ranges.mapy0);

                double val = combinedTerrain.Get(nx * 3, ny * 3);

                if (val > threshold)
                {
                    Vector3    pos = new Vector3(x, height - y, 0) * scale;
                    GameObject g   = GameObject.Instantiate(duplicateObj, pos, Quaternion.identity) as GameObject;
                    g.name = "cube-" + x + "-" + y;
                }
            }
        }
    }
    public void Generate(int mapSeed, int newSize = 3)
    {
        fogTexture2D   = new Texture2D(fogTexture.width, fogTexture.height);
        blindTexture2D = new Texture2D(fogTexture.width, fogTexture.height);

        mainMapGenerateInfo.seed = (uint)mapSeed;
        Terrain t = Terrain.activeTerrain;

        int newTerrainSize = (int)(newSize * 128 * 1.5f);

        t.terrainData.SetDetailResolution(newTerrainSize, 32);
        t.terrainData.alphamapResolution  = newTerrainSize;
        t.terrainData.heightmapResolution = newTerrainSize;
        t.terrainData.size        = new Vector3(100 * newSize, 100, 100 * newSize);
        mainMapGenerateInfo.scale = newSize / 2.0f;

        BlindPlane.transform.localScale = new Vector3(10 * newSize, 0, 10 * newSize);

        // Debug.Log("Terrain Generate: size:" + newSize + " " + t.terrainData.size);

        int sizeX = t.terrainData.alphamapWidth;
        int sizeY = t.terrainData.alphamapHeight;

        if (t.terrainData.detailHeight != t.terrainData.alphamapWidth)
        {
            Debug.Log("detailHeight and alphamapWidth must be equal :" + t.terrainData.alphamapWidth + " " + t.terrainData.alphamapHeight);
        }
        mapData = new float[sizeX, sizeY];

        ModuleBase moduleBase = GetFractal();

        mapTexture = new Texture2D(sizeX, sizeY);
        SMappingRanges ranges = new SMappingRanges();

        float[,,] map        = new float[sizeX, sizeY, layersCount];
        int[][,] grassLayers = new int[][, ] {
            new int[sizeX, sizeY], new int[sizeX, sizeY], new int[sizeX, sizeY], new int[sizeX, sizeY]
        };
        for (int y = 0; y < sizeX; y++)
        {
            for (int x = 0; x < sizeY; x++)
            {
                double p = (double)x / (double)sizeX;
                double q = (double)y / (double)sizeY;
                double nx, ny = 0.0;
                nx = ranges.mapx0 + p * (ranges.mapx1 - ranges.mapx0);
                ny = ranges.mapy0 + q * (ranges.mapy1 - ranges.mapy0);

                float val = (float)moduleBase.Get(nx * mainMapGenerateInfo.scale, ny * mainMapGenerateInfo.scale);

                float textureScale = (val + 1.0f);
                mapData[x, y] = textureScale;

                if (textureScale > 0.89f)
                {
                    mapTexture.SetPixel(y, x, ParseHEX("#005C01"));
                }
                else if (textureScale > 0.7f)
                {
                    mapTexture.SetPixel(y, x, ParseHEX("#007501"));
                }
                else if (textureScale < 0.2f)
                {
                    mapTexture.SetPixel(y, x, ParseHEX("#3F8541"));
                }
                else if (textureScale < 0.1f)
                {
                    mapTexture.SetPixel(y, x, ParseHEX("#5F8560"));
                }
                else
                {
                    mapTexture.SetPixel(y, x, ParseHEX("#008501"));
                }
                // mapTexture.SetPixel(x, y, new Color(val, val, val));

                float grassValue = 0.0f;
                if (textureScale > 0.75)
                {
                    grassValue = KeepPositive((textureScale - 0.75f) * 4.0f);
                }
                map[x, y, 1] = grassValue;

                float stoneValue = 0.0f;
                if (textureScale < 0.25)
                {
                    stoneValue = 1.0f - KeepPositive(textureScale * 4.0f);
                }
                map[x, y, 3] = stoneValue;

                map[x, y, 0] = KeepPositive(textureScale + 0.2f) - grassValue - stoneValue / 2.0f;
                map[x, y, 2] = KeepPositive(1.0f - textureScale - 0.2f) - grassValue - stoneValue / 2.0f;

                if (textureScale > 0.9f)
                {
                    grassLayers[3][x, y] = 1;
                }
                if (textureScale > 0.7f)
                {
                    grassLayers[2][x, y] = 2;
                }
                else if (textureScale > 0.5f)
                {
                    grassLayers[1][x, y] = 2;
                }
                else if (textureScale > 0.15f)
                {
                    grassLayers[1][x, y] = 1;
                }
            }
        }
        mapTexture.Apply();
        t.terrainData.SetAlphamaps(0, 0, map);
        for (int i = 0; i < grassLayers.Length; i++)
        {
            t.terrainData.SetDetailLayer(0, 0, i, grassLayers[i]);
        }
    }