private void RelaxVoronoi() { // instead of random points, use the average of voronoi cell boundary points // the points from the big base triangle get added here, but removed in ComputeTriangulation() List <Vector3> pointsNew = new List <Vector3>(); foreach (VoronoiCell voronoiCell in voronoi.voronoiCells) { // if average center is within initial boundary, use that // else use the previous random center if (IsPointWithinBoundary(voronoiCell.averageCenter)) { pointsNew.Add(voronoiCell.averageCenter); } else { pointsNew.Add(voronoiCell.center); } } points = null; points = pointsNew.ToArray(); // clear the current voronoi voronoi = null; // to the triangulation again triangulation = null; triangulation = new Triangulation(max_x, max_z, Yplane, points, VERBOSE, validateAfterEveryPoint); triangulation.ComputeTriangulation(); }
// Update is called once per frame void Update() { float rt = Time.realtimeSinceStartup; if (doTriangulation) { doTriangulation = false; if (!firstTriangulationDone) { Debug.Log("Get random points"); // create random points randomPoints = new RandomPoints(max_x, max_z, Yplane); points = randomPoints.CreateRandomPoints(pointsNum, VERBOSE); Debug.Log("Compute triangulation"); triangulation = null; triangulation = new Triangulation(max_x, max_z, Yplane, points, VERBOSE, validateAfterEveryPoint); triangulation.ComputeTriangulation(); firstTriangulationDone = true; if (showTimer) { Debug.Log("(Triangulation) Timer: " + (Time.realtimeSinceStartup - rt) + " s"); } } else { Debug.LogError("Triangulation has already been computed."); } } if (doVoronoi) { doVoronoi = false; if (!firstVoronoiDone && firstTriangulationDone) { Debug.Log("Convert triangulation to Voronoi"); ConvertTriangulationToVoronoi(); firstVoronoiDone = true; int numOfValidCells = 0; foreach (VoronoiCell cell in voronoi.voronoiCells) { if (cell.isValid) { numOfValidCells++; } } Debug.Log("Number of cells = " + numOfValidCells); } else if (!firstTriangulationDone) { Debug.LogError("Triangulation must be computed before the Voronoi."); } else { Debug.LogError("First Voronoi has already been computed."); } } if (doRelaxation) { doRelaxation = false; if (firstVoronoiDone) { if (relaxTimes < 0) { relaxTimes = 0; } Debug.Log("The Voronoi will be relaxed " + relaxTimes + " time" + (relaxTimes != 1 ? "s." : ".")); while (relaxTimesCounter < relaxTimes) { totalRelaxesDone++; Debug.Log("Apply Relaxation #" + totalRelaxesDone); RelaxVoronoi(); ConvertTriangulationToVoronoi(); relaxTimesCounter++; if (showTimer) { Debug.Log("(Relaxation) Timer: " + (Time.realtimeSinceStartup - rt) + " s"); } } relaxTimesCounter = 0; } else { Debug.LogError("The first Voronoi must be computed before the diagram is relaxed."); } } if (doMesh) { doMesh = false; if (!meshesCreated && firstVoronoiDone) { Debug.Log("Generate meshes"); CreateMeshColumns(); FindCellNeighbors(); meshesCreated = true; } else if (meshesCreated) { Debug.LogError("Meshes have already been generated."); } else { Debug.LogError("The first Voronoi must be computed before the meshes are created."); } } if (doPerlin) { doPerlin = false; if (meshesCreated) { Debug.Log("Apply Perlin Noise to column heights"); ApplyPerlinToVoronoi(); } else { Debug.LogError("Meshes must be generated before noise is applied."); } } if (doRandomRipple) { doRandomRipple = false; doRandomPathing = false; if (meshesCreated) { Debug.Log("Do random ripple (may need to reset first)"); // rippleResetNeeded = true; ChooseRandomObjectToStartRipple(); } else { Debug.LogError("Meshes must be generated before ripple is applied."); } } if (doRandomPathing) { doRandomPathing = false; if (meshesCreated) { Debug.Log("Do random pathing (may need to reset first)"); // rippleResetNeeded = true; ChooseRandomObjectToStartPathing(); } else { Debug.LogError("Meshes must be generated before pathing is applied."); } } if (resetRipples) { resetRipples = false; if (meshesCreated) { Debug.Log("Reset ripple"); // reset all RippleColumn foreach (GameObject obj in meshObjects) { obj.GetComponent <RippleColumn>().RESET = true; } // rippleResetNeeded = false; } } //showTimer = false; // so timer only gets shown the first time Update() runs }