• 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 /
avatar image
0
Question by boxing_rex · Feb 08, 2014 at 03:45 PM · c#terrainvoxel

How to make a voxel terrain generate all around the start point

I have a voxel terrain generator, the problem is that it only generates in the positive x + Z position. Is there a way to also make it generate in the negative X and Z directions ? Here is the code.

 using UnityEngine;
 using System.Collections;
  
 public class World : MonoBehaviour
 {
   
     public GameObject chunk;
     public Chunk[,,] chunks;  //Changed from public GameObject[,,] chunks;
     public int chunkSize = 16;
     public byte[,,] data;
     public int worldX = 16;
     public int worldY = 16;
     public int worldZ = 16;
   
     // Use this for initialization
     void Start ()
     {
   
         data = new byte[worldX, worldY, worldZ];
    
         for (int x=0; x<worldX; x++) {
             for (int z=0; z<worldZ; z++) {
                 int stone = PerlinNoise (x, 0, z, 10, 3, 1.2f);    //PerlinNoise (int x, int y, int z, float scale, float height, float power)
                 stone += PerlinNoise (x, 300, z, 200, 4, 0) + 50;
                 int dirt = PerlinNoise (x, 1000, z, 50, 20, 0) + 1;
      
                 for (int y=0; y<worldY; y++) {
                     if (y <= stone) {
                         data [x, y, z] = 1;
                     } else if (y <= dirt + stone) {
                         data [x, y, z] = 2;
                     }
       
                 }
             }
         }
    
    
         chunks = new Chunk[Mathf.FloorToInt (worldX / chunkSize), Mathf.FloorToInt (worldY / chunkSize), Mathf.FloorToInt (worldZ / chunkSize)];
         
    
     }
     
     public void GenColumn(int x, int z){
         for (int y=0; y<chunks.GetLength(1); y++) {
       
                     //Create a temporary Gameobject for the new chunk instead of using chunks[x,y,z]
                     GameObject newChunk = Instantiate (chunk, new Vector3 (x * chunkSize - 0.5f,
  y * chunkSize + 0.5f, z * chunkSize - 0.5f), new Quaternion (0, 0, 0, 0)) as GameObject;
       
                     chunks [x, y, z] = newChunk.GetComponent ("Chunk") as Chunk;
                     chunks [x, y, z].worldGO = gameObject;
                     chunks [x, y, z].chunkSize = chunkSize;
                     chunks [x, y, z].chunkX = x * chunkSize;
                     chunks [x, y, z].chunkY = y * chunkSize;
                     chunks [x, y, z].chunkZ = z * chunkSize;
       
                 
             }
     }
     
     public void UnloadColumn(int x, int z){
         for (int y=0; y<chunks.GetLength(1); y++) {
             Object.Destroy(chunks [x, y, z].gameObject);
             
         }
     }
   
     int PerlinNoise (int x, int y, int z, float scale, float height, float power)
     {
         float rValue;
         rValue = Noise.GetNoise (((double)x) / scale, ((double)y) / scale, ((double)z) / scale);
         rValue *= height;
    
         if (power != 0) {
             rValue = Mathf.Pow (rValue, power);
         }
    
         return (int)rValue;
     }
   
   
     // Update is called once per frame
     void Update ()
     {
   
     }
   
     public byte Block (int x, int y, int z)
     {
    
         if (x >= worldX || x < 0 || y >= worldY || y < 0 || z >= worldZ || z < 0) {
             return (byte)1;
         }
    
         return data [x, y, z];
     }
 }
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

Answer by Hoeloe · Feb 09, 2014 at 01:32 PM

First off, I want to say that this is a horrible way of doing a voxel engine. Yes, this is how Minecraft does it, but having extensively looked at the Minecraft source code, a lot of it is, to be frank, terrible. You may want to look up how actual voxel engines are done, specifically, sparse voxel octrees.

Secondly, this is really, REALLY simple to solve. Currently, you're calling Instantiate here:

 Instantiate (chunk, new Vector3 (x * chunkSize - 0.5f,
 y * chunkSize + 0.5f, z * chunkSize - 0.5f), new Quaternion (0, 0, 0, 0)) as GameObject;

Now, just this first vector is what you need to focus on.

Currently, you're spawning it at some integer x coordinate, multiplied by the chunk size, and then for some reason you're subtracting 0.5 from it.

Using some example values, say we have an x coordinate of 3, and a chunk size of 10, this would give a final x coordinate of 29.5. Not exactly what you're looking for I think.

My guess is that you were trying to use the x coordinate as the centre point of the chunk, in which case, you need some brackets:

 x * (chunkSize - 0.5f)

Remember your order of operations:

  • Brackets

  • Indices

  • Division

  • Multiplication

  • Addition

  • Subtraction

Without brackets, the multiplication is done first, with the subtraction done afterwards.

Now if this wasn't what you wanted, then let me know.

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Distribute terrain in zones 3 Answers

Trees not being placed correctly by script on unity terrain. 2 Answers

Multiple Cars not working 1 Answer

How to destroy terrain using Sebastian Lague's Marching Cubes implementation 1 Answer

How would you go about shaping terrain with scripting? (C#) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges