// Start is called before the first frame update void Start() { procMesh = new Mesh(); string a = "Heightmaps/HM_" + Vars.fileIndex; hMap = (Texture2D)Resources.Load(a); imgRes = hMap.width; heights = new float[imgRes * imgRes]; //Bottom left section of the map, other sections are similar for (int i = 0; i < imgRes; i++) { for (int j = 0; j < imgRes; j++) { //Copy heightmap to heigh array float height = hMap.GetPixel(i, j).grayscale; heights[i * imgRes + j] = height; } } //Create our terrain GO and add relevant components GameObject plane = new GameObject("ProcPlane"); //Create GO and add necessary components plane.AddComponent <MeshFilter>(); plane.AddComponent <MeshRenderer>(); plane.GetComponent <MeshRenderer>().material = mountain_Material; updateMesh(heights); Vars.heightMap = hMap; simErosion = new SimErosion(heights, imgRes); }
// Update is called once per frame void Update() { if (Vars.reset) { for (int i = 0; i < imgRes; i++) { for (int j = 0; j < imgRes; j++) { //Copy heightmap to heigh array float height = Vars.heightMap.GetPixel(i, j).grayscale; heights[i * imgRes + j] = height; } } updateMesh(heights); simErosion = new SimErosion(heights, imgRes); Vars.reset = false; Vars.pause = true; Vars.currentDroplets = 0; updatedDroplets = 0; } float[] map = Vars.view ? simErosion.getUpdatedHeights() : heights; if (!Vars.pause) { if (updatedDroplets + 100 > Vars.dropletsPerUpdate) { simErosion.Update(updatedDroplets - Vars.dropletsPerUpdate, true); updateMesh(map); updatedDroplets = 0; } else if (Vars.currentDroplets + 100 > Vars.totalDroplets) { simErosion.Update(Vars.currentDroplets - Vars.totalDroplets, true); updateMesh(map); updatedDroplets = 0; } else { simErosion.Update(); updatedDroplets += 100; } } else { updateMesh(map); } }