Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • 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.

This 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 thing 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 think it would also help to know that this is only an upper layer of terrain as well. Just grass, since in my game you won't be digging down or anything like that.

My question is how to make this terrain efficiently while 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 which 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 Child in transform)
         { float Height = Mathf.PerlinNoise(Child.transform.position.x/Scale, Child.transform.position.z/Scale);
 
             SetMatColor(Child, Height); 
             if (EnableHeight == true) 
             { 
                 ApplyHeight(Child, Height); 
             }
         } 
     } 
 
     void SetMatColor(Transform Child, float Height) 
     { 
         foreach (Renderer r in Child.GetComponentsInChildren<Renderer>()) {
             r.material.color = new Color (0,1 * Height,0,1 * Height);
         }
     } 
 
     void ApplyHeight(Transform Child, float Height) 
     { int YValue = Mathf.RoundToInt (Height * scaleMod);
 
         Vector3 NewVec3 = new Vector3 (Child.transform.position.x, YValue, Child.transform.position.z);
 
         Child.transform.position = NewVec3; 
 
 
         
     }
 }

This 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, which 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 this does anything but it tidies everything 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 this 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.

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

How to spawn in loop like snail but in square 0 Answers

Border around procedural height map 1 Answer

How to generate endless 2d terrain 0 Answers

How does many terrains on scene affect performance? 1 Answer

Terrain DetailPrototype added through script not bending in wind 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