- Home /
My 3D infinite terrain generator is acting weirdly, Ive spent weeks debugging and I cant find the problem.
I Get weird meshing of the terrain, I feel like it lies in Terrain_Generation.cs but I cant pinpoint the problem in it.
Terrain_Generation.cs
terr is a prefab containing a terrain object and a few special things for my project that are visual
using UnityEngine;
public class Terrain_Generation : MonoBehaviour
{
public int depth = 200; //height from above
public int width = 512; //make a int named width and set it to a default of 256
public int height = 512; //make int named height and set it to a default of 256 [Length of terrain]
public float scale = 0.01f;
public float offsetX = 0;
public float offsetY = 0;
public float power = 3;
public float seed = 0;
[Header("Terrain Manager Interface")]
public bool redraw = false;
Terrain terr;
TerrainCollider tercol;
public void Start()
{
//seed = Random.Range(0, 100000);
//terr = GetComponent<Terrain>();
//tercol = GetComponent<TerrainCollider> ();
offsetX = (int)transform.position.x;
offsetY = (int)transform.position.y;
}
public void Update() {
// if (redraw==true) {
// TerrainData terrr = new TerrainData ();
// terrr.size = new Vector3 (width, depth, height);
//
//
// terrr = GenerateTerrain (terrr);
//
// terr.terrainData = terrr;
// tercol.terrainData = terrr;
//
// }
// redraw = false;
}
public TerrainData GenerateTerrain(TerrainData terrData)
{
terrData.heightmapResolution = width;
terrData.size = new Vector3(width, depth, height);
terrData.SetHeights(0, 0, GenerateHeights());
return terrData;
}
float[,] GenerateHeights()
{
offsetX = (int)transform.position.x + 1;
offsetY = (int)transform.position.y + 1;
float[,] heights = new float[width, height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
heights[x, y] = CalculateHeight(x + offsetX, y + offsetY); //generate some perlin noise value
}
}
return heights;
}
float CalculateHeight(float x, float y)
{
float xCord = (float)(x)*scale;
float yCord = (float)(y)*scale;
xCord += seed;
yCord += seed;
return Mathf.Pow(Mathf.PerlinNoise(xCord, yCord), power);//+Mathf.Pow(Mathf.PerlinNoise(xCord, yCord), 10)*5;
//return Mathf.PerlinNoise(xCord, yCord) * (float)depth;
}
}
Terrain_Manager.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TerrainManager : MonoBehaviour {
public GameObject terr;
public int renderDistance = 3; // should be odd;
public float seed;
public GameObject player;
// Use this for initialization
void Start () {
seed = Random.Range(-1000, 1000);
reloadChunks ();
}
// Update is called once per frame
void Update () {
}
void reloadChunks () {
for(int i = transform.childCount-1; i >= 0; i--)
{
Transform child = transform.GetChild(i);
Destroy (child.gameObject);
}
for (int z = 0; z < renderDistance; z++) {
for (int x = 0; x < renderDistance; x++) {
Vector3 terpos = new Vector3 (x*512, 0, z*512);
Transform trans = transform;
trans.position = terpos;
GameObject t = Instantiate(terr, trans) as GameObject;
Terrain_Generation ter = GetComponent<Terrain_Generation>() as Terrain_Generation;
//ter.offsetX = terpos.x;
//ter.offsetY = terpos.z;
ter.seed = seed;
//ter.redraw = true;
TerrainData terdata = new TerrainData();
TerrainData terrrdata = ter.GenerateTerrain (terdata);
t.GetComponent<Terrain>().terrainData = terrrdata;
t.GetComponent<TerrainCollider> ().terrainData = terrrdata;
transform.position = new Vector3 (0, 0, 0);
}
}
}
}
There is a terrain manager object with the components:
TerrainManager.cs
Terrain_Generation.cs
PS:
Sorry for messy code!
So... your terrain has 'weird meshing'? You're just assu$$anonymous$$g people here will know what that means, without further explanation or screenshots?
Your answer
Follow this Question
Related Questions
terrainData.heightmapTexture float value range 2 Answers
Something wrong with how i handle terrain generation 1 Answer
Applying a Large Texture Map to a Terrain 1 Answer
Is there a way to make Unity terrain react and "dig" away from certain objects? 0 Answers
Blend edges between multi terrains 0 Answers