using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DiamondSquare : MonoBehaviour {
public TerrainData TerrainData;
Mesh m;
// Length of the square
public float length;
// Num of vertices in one side
public int vertNum;
// Max height
public float height;
Vector3[] vertices;
void Start () {
m = GetComponent<MeshFilter>().mesh;
CreateTerrian();
}
void CreateTerrian () {
m.name = "Terrian";
// Num of the phase
int division = vertNum - 1;
float halfLength = length / 2;
// The length of the smallest square
float singleLength = length / division;
int vertCount = vertNum * vertNum;
// All the vertices of the terrian
vertices = new Vector3[vertCount];
Vector2[] uvs = new Vector2[vertCount];
// Index of all the triangles
int[] triVertex = new int[vertNum * vertNum * 6];
int triIndex = 0;
for (int i = 0; i < vertNum; i++) {
for (int j = 0; j < vertNum; j++) {
vertices[i * vertNum + j] = new Vector3(-halfLength + singleLength * j, 0, halfLength - singleLength * i);
uvs[i * vertNum + j] = new Vector2((float)i / division, (float)j / division);
if (i < division && j < division) {
int topLeft = i * vertNum + j;
int botLeft = (i + 1) * vertNum + j;
int topRight = i * vertNum + j + 1;
int botRight = (i + 1) * vertNum + j + 1;
// Topright tri
triVertex[triIndex++] = topLeft;
triVertex[triIndex++] = topRight;
triVertex[triIndex++] = botRight;
// Botleft tri
triVertex[triIndex++] = topLeft;
triVertex[triIndex++] = botRight;
triVertex[triIndex++] = botLeft;
}
}
}
vertices[0].y = Random.Range(-height, height);
vertices[division].y = Random.Range(-height, height);
vertices[vertCount - 1].y = Random.Range(-height, height);
vertices[vertCount - division - 1].y = Random.Range(-height, height);
int iteration = (int)Mathf.Log(division, 2);
int numSquare = 1;
int squareSize = division;
for (int i = 0; i < iteration; i++) {
int row = 0;
for (int j = 0; j < numSquare; j++) {
int column = 0;
for (int k = 0; k < numSquare; k++) {
diamondSquare(row, column, squareSize, height);
column += squareSize;
}
row += squareSize;
}
squareSize /= 2;
numSquare *= 2;
height *= 0.5f;
}
m.vertices = vertices;
m.uv = uvs;
m.triangles = triVertex;
m.RecalculateBounds();
m.RecalculateNormals();
}
void diamondSquare (int row, int column, int squareSize, float height) {
int halfSize = squareSize / 2;
int topLeft = row * vertNum + column;
int topRight = topLeft + squareSize;
int botLeft = topLeft + vertNum * squareSize;
int botRight = botLeft + squareSize;
int topMid = (topLeft + topRight) / 2;
int botMid = (botLeft + botRight) / 2;
int leftMid = (topLeft + botLeft) / 2;
int rightMid = (topRight + botRight) / 2;
int mid = (topLeft + botRight) / 2;
vertices[mid].y = (vertices[topLeft].y + vertices[topRight].y + vertices[botLeft].y + vertices[botRight].y) * 0.25f + Random.Range(-height, height);
vertices[topMid].y = (vertices[topLeft].y + vertices[topRight].y + vertices[mid].y) / 3 + Random.Range(-height, height);
vertices[botMid].y = (vertices[botLeft].y + vertices[botRight].y + vertices[mid].y) / 3 + Random.Range(-height, height);
vertices[leftMid].y = (vertices[topLeft].y + vertices[botLeft].y + vertices[mid].y) / 3 + Random.Range(-height, height);
vertices[rightMid].y = (vertices[topRight].y + vertices[botRight].y + vertices[mid].y) / 3 + Random.Range(-height, height);
}
}
This is my own diamond-square algorithm attached to a GameObject->EmptyGameObject
I just want to attach it with the Terrain type Object and make use of it. I have difficulty modify the code to suit the type Terrain given by unity. Could any one can help me with this?