public void spawnTerrain(float _size, int _iterations, float _amp, float _roughness) { iTerrain = new RandomTerrain(_size, _iterations, _amp, _roughness); iTerrain.setVisualDebug(iVisualDebug); iTerrain.spawn(); iTerrainObject = new GameObject("iTerrain"); iTerrainObject.transform.parent = iSelf.transform; iTerrainObject.AddComponent <MeshFilter> (); iTerrainObject.AddComponent <MeshRenderer> (); iTerrainObject.AddComponent <CustomRender> (); workingMesh = new Mesh(); iTerrainObject.GetComponent <MeshFilter> ().mesh = workingMesh; workingMesh.vertices = iTerrain.getVertices(); workingMesh.triangles = iTerrain.getTriangles(); workingMesh.RecalculateNormals(); iTerrainObject.GetComponent <CustomRender> ().CreateLinesFromMesh(); iTerrainObject.GetComponent <Renderer> ().material = iMaterial01; iTerrainObject.GetComponent <CustomRender> ().passColor(iLineColor); iTerrainObject.SetActive(true); }
public void createDelauney(int numberOfVertices, float dimensions, RandomTerrain terrainWrapper) { // int numberOfVertices = 10; triangleMethods = new Triangle (); verticeData = new Vector3[numberOfVertices + 1]; // first 3 verices = 1 triangle. Each next vertice can generate no more than 2 additional triangles. This may be untrue triangleData = new int[3 * numberOfVertices * 2 ]; // Populate the array with -1's for (int i=0; i<triangleData.Length; i++) triangleData [i] = numberOfVertices; float x, y, z; for (int i=0; i<3; i++) { x = Random.value * dimensions; // y = Random.value * 50.0f; z = Random.value * dimensions; // Debug.Log (x +" "+z); y= terrainWrapper.getHeight(x,z); verticeData [i] = new Vector3 (x, y, z); } // // verticeData [0] = new Vector3 (0.0f, 0.0f, 0.0f); // verticeData [1] = new Vector3 (100.0f, 0.0f, 0.0f); // verticeData [2] = new Vector3 (0.0f, 0.0f, 100.0f); // Set up the initial triangle for delauney addTriangle (0, 1, 2, 0); // triangleMethods.getConnectedTrianglesFor (0); // Now start adding points, and add triangles as we go int n = 3; triangleIndex = 1;//3 flipStack = new ArrayList (); while (n<numberOfVertices) { // yield return new WaitForSeconds (0.05f); // Create a vertice x = Random.value * dimensions; // y = Random.value * 50.0f; z = Random.value * dimensions; y= terrainWrapper.getHeight(x,z); verticeData [n] = new Vector3 (x, y, z); // debugObject.GetComponent <VisualDebug>().addDebugPoint (newVertices[n], new Color (1.0f,1.0f,1.0f)); // Create point for our new vertice Point newPoint = new Point (n, triangleData, verticeData, n); // Check if the point is in the mesh. If it's field of view is smaller than 180°, it is outside. If it is larger than 180°, it is inside. // Next, we could reject a point if it's fov is too close to 180, which'd mean it was very close to the edge. if (newPoint.isInConvexMesh ()) { // Point is in the existing mesh. // We'll need to find the triangle it is in, delete that and split it into 3 new ones. Debug.Log ("Point " + n + "is in the mesh"); // GameObject.Find ("VisualDebug").GetComponent <VisualDebug> ().addDebugPoint (verticeData [n], new Color (1.0f, 1.0f, 1.0f), 1.0f); // debugObject.GetComponent <VisualDebug> ().addDebugPoint (newVertices [n], new Color (1.0f, 1.0f, 1.0f), 1.0f); for (int ti=0; ti<triangleIndex; ti ++) { int ti0 = triangleData [ti *3 + 0]; int ti1 = triangleData [ti *3 + 1]; int ti2 = triangleData [ti *3 + 2]; Triangle testTriangle = new Triangle (ti0, ti1, ti2, verticeData, triangleData); // int[] connected = testTriangle.getConnectedTriangles (); if (testTriangle.pointWithinBounds (n)) { Debug.Log ("Point is in this triangle: " + ti ); // debugObject.GetComponent <VisualDebug> ().addDebugPoint (newVertices [ti0], new Color (1.0f, 1.0f, 1.0f), 3.0f); // debugObject.GetComponent <VisualDebug> ().addDebugPoint (newVertices [ti1], new Color (1.0f, 1.0f, 1.0f), 2.0f); // debugObject.GetComponent <VisualDebug> ().addDebugPoint (newVertices [ti2], new Color (1.0f, 1.0f, 1.0f), 1.0f); // We found the triangle our point is in. Now we need to delete that triangle and create 3 new ones. addTriangle (ti0, ti1, n, ti); // replace the triangle the new point is in addToFlipStack( ti); addTriangle (ti1, ti2, n, triangleIndex); // add a triangle addToFlipStack(triangleIndex); triangleIndex ++; addTriangle (ti2, ti0, n, triangleIndex); // add another triangle addToFlipStack(triangleIndex); triangleIndex ++; break; // we found our triangle and can break the loop } else { // Debug.Log ("Point is not in this triangle: " + ti ); } } } else { // Point is not in existing mesh. Which means we'll add triangles to connect it to all the vertices it can 'see'. int current, next, end; // Find out what 'visible' point is most anticlockwise, we'll start there. current = newPoint.getVisibleMostBackward (); // Debug.Log ("Start extreme back: " + current); // Find out what 'visible' point is most clockwise, we'll stop there. end = newPoint.getVisibleMostForward (); // Debug.Log ("End extreme forward: " + end); // Create our departure point. This point is on the edge by definition, since it is the outer one 'visible' to our new point. while (current != end) { Point currentPoint = new Point (current, triangleData, verticeData, n); next = currentPoint.getVisibleMostForward (); // Debug.Log ("next: " + next); addTriangle (current, next, n, triangleIndex); addToFlipStack(triangleIndex); triangleIndex ++; current = next; } } n++; // now flip all traingles untill it's a perfect delauney. Note that this is very brute force. It would be better to establish which triangles have been affected and keep recursively flipping just those. // while ( flipAllTriangles()); // flipAllTriangles(); if (flipStack.Count > 0) { while (flipFlipStack ()) ; } // delauneyTest (0, 1); } }
public void createDelauney(int numberOfVertices, float dimensions, RandomTerrain terrainWrapper) { // int numberOfVertices = 10; triangleMethods = new Triangle(); verticeData = new Vector3[numberOfVertices + 1]; // first 3 verices = 1 triangle. Each next vertice can generate no more than 2 additional triangles. This may be untrue triangleData = new int[3 * numberOfVertices * 2]; // Populate the array with -1's for (int i = 0; i < triangleData.Length; i++) { triangleData [i] = numberOfVertices; } float x, y, z; for (int i = 0; i < 3; i++) { x = Random.value * dimensions; // y = Random.value * 50.0f; z = Random.value * dimensions; // Debug.Log (x +" "+z); y = terrainWrapper.getHeight(x, z); verticeData [i] = new Vector3(x, y, z); } // // verticeData [0] = new Vector3 (0.0f, 0.0f, 0.0f); // verticeData [1] = new Vector3 (100.0f, 0.0f, 0.0f); // verticeData [2] = new Vector3 (0.0f, 0.0f, 100.0f); // Set up the initial triangle for delauney addTriangle(0, 1, 2, 0); // triangleMethods.getConnectedTrianglesFor (0); // Now start adding points, and add triangles as we go int n = 3; triangleIndex = 1; //3 flipStack = new ArrayList(); while (n < numberOfVertices) { // yield return new WaitForSeconds (0.05f); // Create a vertice x = Random.value * dimensions; // y = Random.value * 50.0f; z = Random.value * dimensions; y = terrainWrapper.getHeight(x, z); verticeData [n] = new Vector3(x, y, z); // debugObject.GetComponent <VisualDebug>().addDebugPoint (newVertices[n], new Color (1.0f,1.0f,1.0f)); // Create point for our new vertice Point newPoint = new Point(n, triangleData, verticeData, n); // Check if the point is in the mesh. If it's field of view is smaller than 180°, it is outside. If it is larger than 180°, it is inside. // Next, we could reject a point if it's fov is too close to 180, which'd mean it was very close to the edge. if (newPoint.isInConvexMesh()) { // Point is in the existing mesh. // We'll need to find the triangle it is in, delete that and split it into 3 new ones. Debug.Log("Point " + n + "is in the mesh"); // GameObject.Find ("VisualDebug").GetComponent <VisualDebug> ().addDebugPoint (verticeData [n], new Color (1.0f, 1.0f, 1.0f), 1.0f); // debugObject.GetComponent <VisualDebug> ().addDebugPoint (newVertices [n], new Color (1.0f, 1.0f, 1.0f), 1.0f); for (int ti = 0; ti < triangleIndex; ti++) { int ti0 = triangleData [ti * 3 + 0]; int ti1 = triangleData [ti * 3 + 1]; int ti2 = triangleData [ti * 3 + 2]; Triangle testTriangle = new Triangle(ti0, ti1, ti2, verticeData, triangleData); // int[] connected = testTriangle.getConnectedTriangles (); if (testTriangle.pointWithinBounds(n)) { Debug.Log("Point is in this triangle: " + ti); // debugObject.GetComponent <VisualDebug> ().addDebugPoint (newVertices [ti0], new Color (1.0f, 1.0f, 1.0f), 3.0f); // debugObject.GetComponent <VisualDebug> ().addDebugPoint (newVertices [ti1], new Color (1.0f, 1.0f, 1.0f), 2.0f); // debugObject.GetComponent <VisualDebug> ().addDebugPoint (newVertices [ti2], new Color (1.0f, 1.0f, 1.0f), 1.0f); // We found the triangle our point is in. Now we need to delete that triangle and create 3 new ones. addTriangle(ti0, ti1, n, ti); // replace the triangle the new point is in addToFlipStack(ti); addTriangle(ti1, ti2, n, triangleIndex); // add a triangle addToFlipStack(triangleIndex); triangleIndex++; addTriangle(ti2, ti0, n, triangleIndex); // add another triangle addToFlipStack(triangleIndex); triangleIndex++; break; // we found our triangle and can break the loop } else { // Debug.Log ("Point is not in this triangle: " + ti ); } } } else { // Point is not in existing mesh. Which means we'll add triangles to connect it to all the vertices it can 'see'. int current, next, end; // Find out what 'visible' point is most anticlockwise, we'll start there. current = newPoint.getVisibleMostBackward(); // Debug.Log ("Start extreme back: " + current); // Find out what 'visible' point is most clockwise, we'll stop there. end = newPoint.getVisibleMostForward(); // Debug.Log ("End extreme forward: " + end); // Create our departure point. This point is on the edge by definition, since it is the outer one 'visible' to our new point. while (current != end) { Point currentPoint = new Point(current, triangleData, verticeData, n); next = currentPoint.getVisibleMostForward(); // Debug.Log ("next: " + next); addTriangle(current, next, n, triangleIndex); addToFlipStack(triangleIndex); triangleIndex++; current = next; } } n++; // now flip all traingles untill it's a perfect delauney. Note that this is very brute force. It would be better to establish which triangles have been affected and keep recursively flipping just those. // while ( flipAllTriangles()); // flipAllTriangles(); if (flipStack.Count > 0) { while (flipFlipStack()) { ; } } // delauneyTest (0, 1); } }
public TerrainGenerator(int patchSize) { //initialize(64,3); ds = new DiamondSquare(this, patchSize); rt = new RandomTerrain(this); this.patchSize = patchSize; try { GUIterrainPlannerMenu tpMenu = GameObject.Find("TerrainPlanner").GetComponent<GUIterrainPlannerMenu>(); pm = tpMenu.patch.pm; gtp = tpMenu.patch; extraPatchCount = tpMenu.patch.extraPatchCount; } catch (Exception e) { Debug.Log("TerrainPlanner not found"); //pm = new PatchManager(patchSize); gtp = new GUIterrainPatch(patchSize); gtp.SetDefaultPatch(DefaultTerrain.valleys); pm = gtp.pm; extraPatchCount = 0; } }