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 /
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.

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

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

Using noise to generate Cube World like terrain 0 Answers

Generating in chunks 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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