public MeshGenData generateTerrain() { float startX = (res - 1) / -2.0f; float startZ = (res - 1) / 2.0f; MeshGenData md = new MeshGenData(res * lod, res * lod); int ind = 0; for (int y = 0; y < res; y += 1) { for (int x = 0; x < res; x += 1) { md.vertices[ind] = new Vector3(startX + x, hCurve.Evaluate(noise.skin.GetPixel(x, y).grayscale) * multiplier, startZ - y); md.uvs[ind] = new Vector2(x / (float)res, y / (float)res); if (x < res - 1 && y < res - 1) { md.AddTriangle(ind, ind + res, ind + res - 1); md.AddTriangle(ind + res, ind, ind + 1); } ind++; } } return(md); }
public void generateNoise() { if (noise != null) { noise = null; } switch (type) { case NoiseType.BasicPerlin: noise = new Perlin(); Perlin pNoise = noise as Perlin; pNoise.frequency = freq; pNoise.octaves = octave; pNoise.lacunarity = lacunarity; pNoise.persistence = persistence; pNoise.dimensions = dimension; break; case NoiseType.DiamondSquare: noise = new DiamondSquare(); break; } noise.obj = this.transform; noise.color = color; if (noise.skin == null) { noise.skin = new Texture2D(res, res, TextureFormat.RGB24, true); noise.skin.name = "Procedurally Generated Noise"; noise.skin.wrapMode = TextureWrapMode.Clamp; noise.skin.filterMode = FilterMode.Bilinear; noise.skin.anisoLevel = 9; //skinObj.GetComponent<MeshRenderer>().material.mainTexture = noise.skin; skinObj.GetComponent <Image>().material.mainTexture = noise.skin; noise.skin.Apply(); } noise.FillColor(); noise.WriteToFile(); MeshGenData mgd = generateTerrain(); Texture2D mt = generateTexture(); Mesh terrain = mgd.MakeMesh(); mf.sharedMesh = terrain; mr.sharedMaterial.mainTexture = mt; }