Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Eldh · Jan 14, 2014 at 12:59 PM · 2dspriteproceduralroguelike

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))
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eldh · Jan 14, 2014 at 03:20 PM 0
Share

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.

avatar image Eldh · Jan 14, 2014 at 03:48 PM 0
Share

Ok i understood woohoo ! Thx fo the answer.

PerlinNoise(x+chunk_posX,0,_scale,_magnitude,_power) did the job

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges