- Home /
Diamond Square Algorithm
Hello,
So I've been making a random terrain generator however the terrain it is producing is not excellent. I would like it to appear more random and realistic. Here is my code and a screenshot of what I'm getting.
#pragma strict
var width : int;
var height : int;
var desiredHeight : float;
var step: int;
var minNoise : float;
var maxNoise : float;
function Start () {
var terrainDat = GetComponent(Terrain).terrainData;
width = terrainDat.heightmapWidth;
height = terrainDat.heightmapHeight;
var xAnchor : int = 0;
var yAnchor : int = 0;
var heights = terrainDat.GetHeights(0,0,width,height);
width = width-1;
height = height-1;
step = width;
//corners
heights[xAnchor,yAnchor] = desiredHeight;
terrainDat.SetHeights(0,0,heights);
heights[xAnchor,height] = desiredHeight;
terrainDat.SetHeights(0,0,heights);
heights[width,yAnchor] = desiredHeight;
terrainDat.SetHeights(0,0,heights);
heights[width,height] = desiredHeight;
terrainDat.SetHeights(0,0,heights);
for(var p = 0; p<=1400; p++){
//print(width);
//diamond
heights[xAnchor+width/2,yAnchor+height/2] = (((terrainDat.GetHeight(xAnchor,yAnchor)+terrainDat.GetHeight(width,yAnchor)+terrainDat.GetHeight(xAnchor,height)+terrainDat.GetHeight(width,height))/4.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
terrainDat.SetHeights(0,0,heights);
//square
heights[xAnchor+width/2,yAnchor] = (((terrainDat.GetHeight(xAnchor,yAnchor)+terrainDat.GetHeight(width,yAnchor))/2.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
terrainDat.SetHeights(0,0,heights);
heights[xAnchor,yAnchor+height/2] = (((terrainDat.GetHeight(xAnchor,yAnchor)+terrainDat.GetHeight(xAnchor,height))/2.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
terrainDat.SetHeights(0,0,heights);
heights[xAnchor+width/2,height] = (((terrainDat.GetHeight(width,height)+terrainDat.GetHeight(width,yAnchor))/2.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
terrainDat.SetHeights(0,0,heights);
heights[width,yAnchor+height/2] = (((terrainDat.GetHeight(width,height)+terrainDat.GetHeight(xAnchor,height))/2.0)-this.gameObject.transform.position.x)/(terrainDat.size.x*10)+ Random.Range(minNoise,maxNoise);
terrainDat.SetHeights(0,0,heights);
//check next
if(xAnchor+width >= 64 && yAnchor+height >=64){
//print("y");
width = width/2;
height = height/2;
step = width;
xAnchor=0;
yAnchor=0;
}
else{
//print("n");
if(xAnchor+width >=64){
yAnchor = yAnchor + height;
xAnchor = 0;
}
else{xAnchor = xAnchor + width;}
}
}
}
And do you have a question? A specific problem? A technical challenge? Anything related to Unity Answers at all?
0 Yeah I suppose my question is what part of the algorithm am I getting wrong. It seems to do what others do? Just not as well
The thing is, Diamond square is just a generic terrain algorithm, and will plug right into the way Unity handles terrain. $$anonymous$$aking it work differently has nothing to do with Unity. The best place to look for answers is generic DiamondSquare resources.
$$anonymous$$anaged to get it looking much better. $$anonymous$$y issue was the averages were being taken at the same place every time. Thanks for answers guys
Answer by Greenie · Jan 12, 2015 at 01:04 PM
Well yes how can I change my code to make it more realistic?
That is not really within the scope of Unity Answers as we focus on specific technical questions. However it does seem like you have slightly misunderstood the concept for the Diamond Square algorithm. Read a little more about it here: http://www.paulboxley.com/blog/2011/03/terrain-generation-mark-one#
It looks like you have set the height on a terrain which has many more vertices than what your algorithm calculated on.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180218im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to procedural generation 0 Answers
2D Procedural Terrain Generation 1 Answer
Procedural Terrain Generation on a Map the Scale of the Milky Way 2 Answers
World Generation with Interesting Terrain 0 Answers
3d Perlin Noise? 3 Answers