- Home /
Perlin Noise world generator, instantiating smaller tiles together.
I'm referring to this YouTube video of Arend Peter Teaches: Coding Perlin Noise in Unity
In the tutorial, he uses 100x100 tile to generate the world, but I would like to use a real Terraria tile which is 16x16, if I just fit in the 16x16 .png tiles to the prefab, the world generates the prefabs with lots of gaps in between.
I'm attaching the 2 scripts used in the video:
using UnityEngine;
using System.Collections;
public class Generator : MonoBehaviour {
public GameObject dirtPrefab;
public GameObject grassPrefab;
int minX = -160; // affects -x length of world generation
int maxX = 160; // affects x length of world generation
int minY = -10; // affects -y height of world generation
int maxY = 10; // affects y height of world generation
PerlinNoise noise;
void Start ()
{
noise = new PerlinNoise(Random.Range(1000000,10000000));
Regenerate();
}
private void Regenerate()
{
float width = dirtPrefab.transform.lossyScale.x;
float height = dirtPrefab.transform.lossyScale.y;
for (int i = minX; i < maxX; i++)
{
//columns (x values
int columnHeight = 2 + noise.getNoise(i - minX, maxY - minY - 2);
for(int j = minY; j < minY + columnHeight; j++)
{
//rows (y values)
GameObject block = (j == minY + columnHeight - 1)?grassPrefab:dirtPrefab;
Instantiate(block, new Vector2(i * width, j * height), Quaternion.identity);
}
}
}
}
and
using UnityEngine;
using System.Collections;
public class PerlinNoise {
long seed;
public PerlinNoise(long seed)
{
this.seed = seed;
}
private int random(long x, int range)
{
return (int)(((x+seed)^5) % range);
}
public int getNoise(int x, int range)
{
int chunkSize = 16; //affects surface smoothness
float noise = 0; //affects height of generation
range /= 1;
while(chunkSize > 5)
{
int chunkIndex = x / chunkSize;
float prog = (x % chunkSize) / (chunkSize * 1f);
float left_random = random(chunkIndex, range);
float right_random = random(chunkIndex + 1, range);
noise += (1-prog) * left_random + prog * right_random;
chunkSize /= 2;
range /= 2;
range = Mathf.Max(1,range);
}
return (int)Mathf.Round(noise);
}
}
What should I be looking to change within the codes?
And a brief explanation as to why would be very much appreciated!
Thank you in advance.
Answer by brahini98 · Dec 31, 2017 at 04:58 PM
como hago si quiero que genere piedra debajo de la tierra ,amigo quiero añadirle otra capa debajo de piedra, q podria hacer
Answer by Zodiaq · Apr 04, 2018 at 09:15 PM
I know this is old but i answer just in case someone checks. i++ and j++ are the steps for the next tile, it adds 1 every time which is the size of the tile used in the video(100x100). Yours is 16x16 means 0.16 (float) each, same for height and width so it becomes like this:
private void Regenerate()
{
float width = 0.16f;
float height = 0.16f;
for (float i = minX; i < maxX; i += 0.16f)
{
//columns (x values
int columnHeight = noise.getNoise((int)i - minX, maxY - minY);
for (float j = minY; j < minY + columnHeight; j +=0.16f)
{
//rows (y values)
GameObject block = (j == minY + columnHeight - 1) ? grassPrefab : dirtPrefab;
Instantiate(block, new Vector2(i * width, j * height), Quaternion.identity);
}
}
}