- Home /
How to have graphic continuity in my chuncks ?(2d procedural / sprites)
I'm trying to learn how to procedurally create a world in 2d. After some research i did this :
I create a DataWorld.cs a DataChunk.cs and a DataSprite.cs to store all the data and separate Data from graphic creation. And i create an other script WorldGenerator.cs to actually create the world.
it looks like this :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class WorldGenerator : MonoBehaviour {
public Sprite[] sprites;
public SpriteRenderer sp;
public int chunk_size_x;
public int chunk_size_y;
public int world_size_x;
public int world_size_y;
int _chunkNum = -1;
int _scale;
int _magnitude;
float _power = 1;
int _treshold = 15;
GameObject _chunkGO;
SpriteRenderer spriteClone;
void Start ()
{
sprites = Resources.LoadAll<Sprite>("spriteTest");
_scale = (int)Random.Range(10f,50f);
_magnitude =(int) Random.Range((float)_scale-10, _scale+20);
MakeWorld(world_size_x, world_size_y, chunk_size_x, chunk_size_y);
}
public void MakeWorld(int world_size_x, int world_size_y, int chunk_size_x, int chunk_size_y)
{
DataWorld world = new DataWorld(world_size_x, world_size_y, chunk_size_x, chunk_size_y);
for(int x = 0; x < world_size_x; x++)
{
for(int y = 0; y < world_size_y; y++)
{
MakeChunk(world, chunk_size_x,chunk_size_y);
_chunkGO.transform.position = new Vector3 (world.chunks[x,y].posX, world.chunks[x,y].posY,0) ;
}
}
}
public void MakeChunk(DataWorld world, int chunk_size_x, int chunk_size_y)
{
_chunkNum++;
GameObject chunkGO = new GameObject();
chunkGO.name = "CHUNK" + " " + _chunkNum;
_chunkGO = chunkGO;
for(int x=0; x< chunk_size_x; x++)
{
for(int y = 0; y< chunk_size_y; y++)
{
if(y<PerlinNoise(x/8,0,_scale,_magnitude,_power))
{
spriteClone = (SpriteRenderer)Instantiate(sp, new Vector2(x,y), Quaternion.identity);
spriteClone.transform.parent = chunkGO.transform;
spriteClone.name = "sprite " +x + " , " +y;
world.GetAllChunks().spriteTile[x,y].spr = spriteClone;
world.GetAllChunks().spriteTile[x,y].type = DataSprite.TYPE.BLACK;
}
}
}
for(int x=0; x< chunk_size_x; x++)
{
for(int y = 0; y< chunk_size_y; y++)
{
CheckSpritePos(world.GetAllChunks(),x,y);
UpdateSpritesGraphics(world.GetAllChunks(),world.GetAllChunks().spriteTile[x,y].spr,x,y);
}
}
}
int PerlinNoise(int x,int y, float scale, float mag, float power){
float rValue;
rValue=Noise.Noise.GetNoise (((double)x) / scale, ((double)y)/ scale,0);
rValue*=mag;
if(power!=0)
{
rValue=Mathf.Pow( rValue, power);
}
return (int) rValue;
}
But the way i do it all instantiates chunks are the same. I understand why there are all the same, but i can't figure out how to make them continue each other base on the PerlinNoise function.
EDIT :
So i fixed the continuity on the axis X by adding
if(y<PerlinNois((x+chunk.posX),0,50,100,1f))
It works fine when i only have one dimension in my world ( i only add chunk along the X axis). But when i have 2 dimension let say my world size is
world_size_x =4
world_size_y =2
I can't figure out how to add chunk.posY to sample the noise corectly.
if(y<PerlinNoise((x+chunk.posX+chunk.posY),0,50,100,1f))
does not work. I did plenty of other test but none of them worked. Where and how do i have to add the chunk.posY ?
EDDIT 2
nevermind after several hour of break i finally had the right brain wave :
if(y+chunk.posY<PerlinNoise((x+chunk.posX),0,50,100,1f))
Answer by HappyMoo · Jan 14, 2014 at 02:59 PM
The Problem is, your chunks don't know where they are globally... you only feed them local x/y from 0 to chunk_size_x/y-1 ... the need to know their position and then add this position to the local one when they sample the noise field.
Also, you don't seem to use DataChunk at all - your worldgen is doing all the work.
DataChunk is created inside the Dataworld class. So when i call new DataWorld() it creates an 2dimensional array of the data chunk. The same way it creates a 2 dimensional array of DataSprite when the DataChunk class is called. $$anonymous$$aybe i am using the data the wrong way ?
There is a variable posX ine the Datachunk so they know their position in the worldspace. So what you are saying make totally sense, and it's what i'm trying to do for the last hour. But i don't know where or how to add this value in my code to makes the chunk continue. It is out of doubt simple for most people but i can't figure it out.
Ok i understood woohoo ! Thx fo the answer.
PerlinNoise(x+chunk_posX,0,_scale,_magnitude,_power) did the job
Your answer
Follow this Question
Related Questions
How to handle a MASSIVE amount of game objects? 2 Answers
Asset store categories - How to determine best fit for my asset 0 Answers
Strange renderissues with tiled sprites 1 Answer
Errors in Procedurally Generated Dungeon 0 Answers
Adjust scale of object when changing the draw mode of prefab? 0 Answers