- Home /
Problem is not reproducible or outdated
CoherentNoise still coherent through diferent terrains
Hi,
Well I had downloaded old version of CoherentNoise via SVN and I'm trying to make something like that. But I don't know where I can start... Can somebody give me, some tips? That is the code that I have so far:
public void GenerateHeights(Terrain terrain)
{
var Pink = seed;
var n = new CoherentNoise.Generation.Fractal.PinkNoise(Pink);
float[,] heights = new float[terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight];
for (int i = 0; i < terrain.terrainData.heightmapWidth; i++)
{
for (int k = 0; k < terrain.terrainData.heightmapHeight; k++)
{
var x = n.GetValue((((float)terrain.terrainData.heightmapWidth * (float)lastChunkCharged + (float)i) / (float)terrain.terrainData.heightmapWidth) * tileSize, (((float)terrain.terrainData.heightmapHeight * (float)lastChunkCharged + (float)k) / (float)terrain.terrainData.heightmapHeight) * tileSize, 0);
heights[i, k] = x/5.0f;
}
}
terrain.terrainData.SetHeights(0,0, heights);
}
and my infinite terrain generation
void Start() {
Terrain linkedTerrain = gameObject.GetComponent<Terrain>();
_terrainGrid[0,0] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
_terrainGrid[0,1] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
_terrainGrid[0,2] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
_terrainGrid[1,0] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
_terrainGrid[1,1] = linkedTerrain;
_terrainGrid[1,2] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
_terrainGrid[2,0] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
_terrainGrid[2,1] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
_terrainGrid[2,2] = Terrain.CreateTerrainGameObject(linkedTerrain.terrainData).GetComponent<Terrain>();
UpdateTerrainPositionsAndNeighbors();
}
private void UpdateTerrainPositionsAndNeighbors()
{
_terrainGrid[0,0].transform.position = new Vector3(
_terrainGrid[1,1].transform.position.x - _terrainGrid[1,1].terrainData.size.x,
_terrainGrid[1,1].transform.position.y,
_terrainGrid[1,1].transform.position.z + _terrainGrid[1,1].terrainData.size.z);
_terrainGrid[0,1].transform.position = new Vector3(
_terrainGrid[1,1].transform.position.x - _terrainGrid[1,1].terrainData.size.x,
_terrainGrid[1,1].transform.position.y,
_terrainGrid[1,1].transform.position.z);
_terrainGrid[0,2].transform.position = new Vector3(
_terrainGrid[1,1].transform.position.x - _terrainGrid[1,1].terrainData.size.x,
_terrainGrid[1,1].transform.position.y,
_terrainGrid[1,1].transform.position.z - _terrainGrid[1,1].terrainData.size.z);
_terrainGrid[1,0].transform.position = new Vector3(
_terrainGrid[1,1].transform.position.x,
_terrainGrid[1,1].transform.position.y,
_terrainGrid[1,1].transform.position.z + _terrainGrid[1,1].terrainData.size.z);
_terrainGrid[1,2].transform.position = new Vector3(
_terrainGrid[1,1].transform.position.x,
_terrainGrid[1,1].transform.position.y,
_terrainGrid[1,1].transform.position.z - _terrainGrid[1,1].terrainData.size.z);
_terrainGrid[2,0].transform.position = new Vector3(
_terrainGrid[1,1].transform.position.x + _terrainGrid[1,1].terrainData.size.x,
_terrainGrid[1,1].transform.position.y,
_terrainGrid[1,1].transform.position.z + _terrainGrid[1,1].terrainData.size.z);
_terrainGrid[2,1].transform.position = new Vector3(
_terrainGrid[1,1].transform.position.x + _terrainGrid[1,1].terrainData.size.x,
_terrainGrid[1,1].transform.position.y,
_terrainGrid[1,1].transform.position.z);
_terrainGrid[2,2].transform.position = new Vector3(
_terrainGrid[1,1].transform.position.x + _terrainGrid[1,1].terrainData.size.x,
_terrainGrid[1,1].transform.position.y,
_terrainGrid[1,1].transform.position.z - _terrainGrid[1,1].terrainData.size.z);
for (int x = 0; x < 3; x++)
{
for (int z = 0; z < 3; z++)
{
GenerateHeights(_terrainGrid[x,z]);
lastChunkCharged += 1;
}
}
_terrainGrid[0,0].SetNeighbors( null, null, _terrainGrid[1,0], _terrainGrid[0,1]);
_terrainGrid[0,1].SetNeighbors( null, _terrainGrid[0,0], _terrainGrid[1,1], _terrainGrid[0,2]);
_terrainGrid[0,2].SetNeighbors( null, _terrainGrid[0,1], _terrainGrid[1,2], null);
_terrainGrid[1,0].SetNeighbors(_terrainGrid[0,0], null, _terrainGrid[2,0], _terrainGrid[1,1]);
_terrainGrid[1,1].SetNeighbors(_terrainGrid[0,1], _terrainGrid[1,0], _terrainGrid[2,1], _terrainGrid[1,2]);
_terrainGrid[1,2].SetNeighbors(_terrainGrid[0,2], _terrainGrid[1,1], _terrainGrid[2,2], null);
_terrainGrid[2,0].SetNeighbors(_terrainGrid[1,0], null, null, _terrainGrid[2,1]);
_terrainGrid[2,1].SetNeighbors(_terrainGrid[1,1], _terrainGrid[2,0], null, _terrainGrid[2,2]);
_terrainGrid[2,2].SetNeighbors(_terrainGrid[1,2], _terrainGrid[2,1], null, null);
}
void UpdatePlayerAndTerrain() {
Vector3 playerPosition = new Vector3(PlayerObject.transform.position.x, PlayerObject.transform.position.y, PlayerObject.transform.position.z);
Terrain playerTerrain = null;
int xOffset = 0;
int yOffset = 0;
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
if ((playerPosition.x >= _terrainGrid[x,y].transform.position.x) &&
(playerPosition.x <= (_terrainGrid[x,y].transform.position.x + _terrainGrid[x,y].terrainData.size.x)) &&
(playerPosition.z >= _terrainGrid[x,y].transform.position.z) &&
(playerPosition.z <= (_terrainGrid[x,y].transform.position.z + _terrainGrid[x,y].terrainData.size.z)))
{
playerTerrain = _terrainGrid[x,y];
xOffset = 1 - x;
yOffset = 1 - y;
break;
}
}
if (playerTerrain != null)
break;
}
if (playerTerrain != _terrainGrid[1,1])
{
Terrain[,] newTerrainGrid = new Terrain[3,3];
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
{
int newX = x + xOffset;
if (newX < 0)
newX = 2;
else if (newX > 2)
newX = 0;
int newY = y + yOffset;
if (newY < 0)
newY = 2;
else if (newY > 2)
newY = 0;
newTerrainGrid[newX, newY] = _terrainGrid[x,y];
}
_terrainGrid = newTerrainGrid;
UpdateTerrainPositionsAndNeighbors();
}
}
// Update is called once per frame
void Update ()
{
UpdatePlayerAndTerrain();
}
I'm thinking to make a new function in .cs files called getLastValue and it returns the last pixel of each edge of the terrain, and share that information in a Vector4, that have arrays of each side of the squared terrain grid. And the next terrain, will take all that values for make a continous terrain. But I don't know if it will take to much RAM... So what are you advices?
Thanks in advance.
There you can see the problem I have:
Follow this Question
Related Questions
Tile generation problem 1 Answer
Add perlin noise to blocky terrain 0 Answers
2d Top-Down Perlin noise map using Mathf.PerlinNoise 3 Answers
How to make a game with lots of objects?!! 1 Answer
Is it possible to split a terrain/plane at runtime ? 2 Answers