Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

Square Dimond Algorythem Problems

$
0
0
hey so ime building a procedural terrain gen using the square diamond method. and ime having some problems mainly the points generated arnt very random and seem to give weird exponential curves ![alt text][1] [1]: /storage/temp/21362-untitled.png so has anyone else encountered this or know what mistake ive made var RandomRange : float; var Ground : Transform; var HStore : HeightStorage; var Iteration : int; var CurentDimention : int; function Start () { var clone = Instantiate (Ground, Vector3(0, 0, 0), Quaternion.identity); HStore = clone.GetComponent ("HeightStorage"); HStore.Height = Random.Range(0,RandomRange); clone.parent = transform; clone = Instantiate (Ground, Vector3(1, 0, 0), Quaternion.identity); HStore = clone.GetComponent ("HeightStorage"); HStore.Height = Random.Range(0,RandomRange); clone.parent = transform; clone = Instantiate (Ground, Vector3(1, 0, 1), Quaternion.identity); HStore = clone.GetComponent ("HeightStorage"); HStore.Height = Random.Range(0,RandomRange); clone.parent = transform; clone = Instantiate (Ground, Vector3(0, 0, 1), Quaternion.identity); HStore = clone.GetComponent ("HeightStorage"); HStore.Height = Random.Range(0,RandomRange); clone.parent = transform; CurentDimention = 3; Subdivied(); } function Subdivied (){ var GroundBlocks : Component[]; GroundBlocks = GetComponentsInChildren (Transform); for (var Block : Transform in GroundBlocks) { Block.position.x = Block.position.x*2; Block.position.z = Block.position.z*2; } //dimond step for (var x : int = 1;x < CurentDimention; x += 2) { for (var z : int = 1;z < CurentDimention; z += 2) { var clone = Instantiate (Ground, Vector3(x, 0, z), Quaternion.identity); clone.parent = transform; var Sum : float; var hit : RaycastHit; if (Physics.Raycast (clone.position, Vector3(1,0,1), hit)){ HStore = hit.transform.GetComponent ("HeightStorage"); Sum += HStore.Height; } if (Physics.Raycast (clone.position, Vector3(1,0,-1), hit)){ HStore = hit.transform.GetComponent ("HeightStorage"); Sum += HStore.Height; } if (Physics.Raycast (clone.position, Vector3(-1,0,1), hit)){ HStore = hit.transform.GetComponent ("HeightStorage"); Sum += HStore.Height; } if (Physics.Raycast (clone.position, Vector3(-1,0,-1), hit)){ HStore = hit.transform.GetComponent ("HeightStorage"); Sum += HStore.Height; } HStore = clone.GetComponent ("HeightStorage"); HStore.Height = (Sum/4)+(Random.Range(-RandomRange,RandomRange)/Mathf.Pow(2,Iteration)); } } //Square step for (x = 1;x < CurentDimention; x += 2) { for (z = 0;z < CurentDimention+1; z += 2) { clone = Instantiate (Ground, Vector3(x, 0, z), Quaternion.identity); clone.parent = transform; if (Physics.Raycast (clone.position, Vector3(0,0,1), hit)){ HStore = hit.transform.GetComponent ("HeightStorage"); Sum += HStore.Height; } if (Physics.Raycast (clone.position, Vector3(0,0,-1), hit)){ HStore = hit.transform.GetComponent ("HeightStorage"); Sum += HStore.Height; } if (Physics.Raycast (clone.position, Vector3(-1,0,0), hit)){ HStore = hit.transform.GetComponent ("HeightStorage"); Sum += HStore.Height; } if (Physics.Raycast (clone.position, Vector3(1,0,0), hit)){ HStore = hit.transform.GetComponent ("HeightStorage"); Sum += HStore.Height; } HStore = clone.GetComponent ("HeightStorage"); HStore.Height = (Sum/4)+(Random.Range(-RandomRange,RandomRange)/Mathf.Pow(2,Iteration)); } } //------------------------ for (x = 0;x < CurentDimention+1; x += 2) { for (z = 1;z < CurentDimention; z += 2) { clone = Instantiate (Ground, Vector3(x, 0, z), Quaternion.identity); clone.parent = transform; if (Physics.Raycast (clone.position, Vector3(0,0,1), hit)){ HStore = hit.transform.GetComponent ("HeightStorage"); Sum += HStore.Height; } if (Physics.Raycast (clone.position, Vector3(0,0,-1), hit)){ HStore = hit.transform.GetComponent ("HeightStorage"); Sum += HStore.Height; } if (Physics.Raycast (clone.position, Vector3(-1,0,0), hit)){ HStore = hit.transform.GetComponent ("HeightStorage"); Sum += HStore.Height; } if (Physics.Raycast (clone.position, Vector3(1,0,0), hit)){ HStore = hit.transform.GetComponent ("HeightStorage"); Sum += HStore.Height; } HStore = clone.GetComponent ("HeightStorage"); HStore.Height = (Sum/4)+(Random.Range(-RandomRange,RandomRange)/Mathf.Pow(2,Iteration)); } } //------------------------ CurentDimention += Mathf.Pow(2,Iteration); Iteration +=1; } function Update () { if (Input.GetKeyDown (KeyCode.Space)){ Subdivied(); } if (Input.GetKeyDown (KeyCode.Return)){ //output terrain var GroundBlocks : Component[]; GroundBlocks = GetComponentsInChildren (Transform); for (var Block : Transform in GroundBlocks) { HStore = Block.GetComponent ("HeightStorage"); if (HStore){ Block.position.y = HStore.Height; } } } } HeightStorage is just another script that contains the height of each block before it is moved into position

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>