• 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 /
  • Help Room /
avatar image
0
Question by LightSlayer5 · Jul 31, 2017 at 04:10 AM · terrainproceduralvoxel

Performance Efficient Way To Make Voxel Terrain With Cubes?

Currently I'm trying to make a procedurally generated world with voxels, akin to Minecraft, however I don't want to use textures and like the look of having a certain color on the entire cube but just at darker or lighter shades. At the moment, I have a nice script setup that will use perlin noise to make the terrain and have it set so that the height on the perlin noise will change the shade of the color.

T$$anonymous$$s works fine and all, however the problem begins when I try to generate any large size of terrain in. It will go down to just about 10-15 frames when looking at all of the cubes and when I turn away from it and look out onto the sky box, it goes to about 40-60 frames. Same t$$anonymous$$ng happens when I look directly down at my feet and only see a few cubes.

The issue is obviously the cubes being very performance intensive since there are so many. I t$$anonymous$$nk it would also help to know that t$$anonymous$$s is only an upper layer of terrain as well. Just grass, since in my game you won't be digging down or anyt$$anonymous$$ng like that.

My question is how to make t$$anonymous$$s terrain efficiently w$$anonymous$$le keeping the same idea. I've looked at tutorials on how to maybe optimize voxel type stuff, but most of it is trying to recreate Minecraft w$$anonymous$$ch is not what I'm going for.

So yeah, if anyone has any ideas on how I could fix the frame rate issue, I would gladly appreciate it. I'll leave my code for the terrain generation right here:

 using UnityEngine; 
 using System.Collections; 
 
 public class TerrainGenerator : MonoBehaviour { 
     public GameObject Block; 
     public float Scale = 7.00f; 
     public int Size = 50; 
     public float heightOffset = 1.5f; 
     public bool EnableHeight = true; 
     public float scaleMod = 5f; 
     public bool Move = false; 
 
     public int Seed;
 
     TerrainOptimization optimization;
 
     void Awake () {
         FormTerrain ();
         //Set the seed up properly later
         //Seed = Random.Range(1,99999);
     }
 
     void FormTerrain(){
         for(int X = 0; X < Size; X++) { 
             for(int Z = 0; Z < Size; Z++) { 
                 GameObject C = Instantiate(Block, new Vector3(X, 0, Z), Quaternion.identity) as GameObject; 
                 C.transform.parent = transform;
                 optimization = C.GetComponent<TerrainOptimization> ();
             } 
         }
     }
 
     void Update () 
     { 
         UpdateTransform (); 
     } 
 
     void UpdateTransform() 
     { foreach (Transform C$$anonymous$$ld in transform)
         { float Height = Mathf.PerlinNoise(C$$anonymous$$ld.transform.position.x/Scale, C$$anonymous$$ld.transform.position.z/Scale);
 
             SetMatColor(C$$anonymous$$ld, Height); 
             if (EnableHeight == true) 
             { 
                 ApplyHeight(C$$anonymous$$ld, Height); 
             }
         } 
     } 
 
     void SetMatColor(Transform C$$anonymous$$ld, float Height) 
     { 
         foreach (Renderer r in C$$anonymous$$ld.GetComponentsInC$$anonymous$$ldren<Renderer>()) {
             r.material.color = new Color (0,1 * Height,0,1 * Height);
         }
     } 
 
     void ApplyHeight(Transform C$$anonymous$$ld, float Height) 
     { int YValue = Mathf.RoundToInt (Height * scaleMod);
 
         Vector3 NewVec3 = new Vector3 (C$$anonymous$$ld.transform.position.x, YValue, C$$anonymous$$ld.transform.position.z);
 
         C$$anonymous$$ld.transform.position = NewVec3; 
 
 
         
     }
 }

T$$anonymous$$s is what the script creates:

alt text

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 LightSlayer5 · Aug 01, 2017 at 04:23 AM

Okay, I seem to have found the problem. So it didn't seem to have been the cubes lagging, but it was me changing the color of the material constantly and making new materials every frame, w$$anonymous$$ch Unity didn't seem to like.

So what I did was (for anyone wondering,) was I moved the setting of colors onto the blocks themselves (dunno if t$$anonymous$$s does anyt$$anonymous$$ng but it tidies everyt$$anonymous$$ng up nicely) and made a bool onto the block checking if the block's color had already been set. If so, then don't set it again.

That seemed to work flawlessly and now it stays on 60 fps with size 100 on the terrain (cheer!) However, making larger terrain will lag it, so I recommend (if you're doing t$$anonymous$$s too) making a view distance that makes blocks disappear at a certain distance and bring them back once you get closer.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

127 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 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 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 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 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 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 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

Diamond Square Algorithm Problems 0 Answers

How does one convert a System.Drawing type Bitmap into a compatable terrain heightmap for unity 0 Answers

whats the best way to start a voxel game. 1 Answer

Procedural Terrain Diamond Square Height Issue 0 Answers

Very Simple Planets 1 Answer


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