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 AndreaBrag · Mar 14, 2015 at 11:43 AM · procedural-generationthreadprocedural-terrain

Proper use of threads with procedural generation

Hi everybody! I have been working on a simple chunk based terrain generation system, with voxel octrees. The overall method works pretty well, but testing this system with large amounts of chunks completely freezes the game/main thread until the generation process is finished. Looking for a solution, I read about threads and coroutines and I implemented a simple thread class (based on this template) to call the "generate" function from it. The only advantage I had was a faster generation, with full CPU usage, but the game still freezes until the chunks are completely loaded. Am I doing something wrong? Here some parts of the code:

 public class ChunkThread : ThreadedJob {
  
     public Chunk chunk;
  
     protected override void ThreadFunction()
     {
         chunk.root = chunk.root.BuildOctree (chunk.pos, Chunk.size, -1);
     }
  
     protected override void OnFinished()
     {
         chunk.RenderChunk ();
     }
 }

This is the simple Thread class I use to call the Build function, and the Render function when finished. This piece of code below is the start\update method for every Chunk gameObject (I kept only the relevant part), that simply creates the Thread instance and calls Start() pressing the mouse button.

 void Start () {
         t = new ChunkThread ();
         t.chunk = this;
     }
  
     void Update () {
         if (Input.GetButtonDown ("Fire1")) {
             t.Start ();
         }

Now, coming to the questions, what should I do to generate the terrain while still being able to play? Is there something wrong in my thread? Should I use a thread pool, if so how?

I used this template to build my thread class (it should be linked in the question). Anyway to avoid any doubt this is the ThreadedJob class

  public class ThreadedJob
  {
    private bool m_IsDone = false;
    private object m_Handle = new object();
    private System.Threading.Thread m_Thread = null;
    public bool IsDone
    {
      get
      {
        bool tmp;
        lock (m_Handle)
        {
           tmp = m_IsDone;
        }
        return tmp;
      }
      set
      {
        lock (m_Handle)
        {
           m_IsDone = value;
        }
      }
    }
    public virtual void Start()
    {
      m_Thread = new System.Threading.Thread(Run);
      m_Thread.Start();
    }
    public virtual void Abort()
    {
      m_Thread.Abort();
    }
    protected virtual void ThreadFunction() { }
    protected virtual void OnFinished() { }
    public virtual bool Update()
    {
      if (IsDone)
      {
        OnFinished();
        return true;
      }
      return false;
    }
    private void Run()
    {
      ThreadFunction();
      IsDone = true;
    }
  }

Comment
Add comment · Show 1
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 Glurth · Mar 14, 2015 at 02:09 PM 0
Share

I'd like to help, but I'm totally unfamiliar with the ThreadedJob base class your using. I couldn't even find docs on it, did you create it yourself? Is it possible the issue lies with this class (and if so, can you post that code too)?

However, I can say it LOO$$anonymous$$s like you are doing it right, (I'm assu$$anonymous$$g ThreadedJob.Start() actually launches the thread with the code in the function "ThreadFunction").

You should NOT need a thread pool for this, at least to get started.

Remember, you may NOT call any unity API functions in your thread! (not that you should need it to generate an octtree).

Here is a sample that I whipped on on how to launch a thread without the ThreadedJob class, just using the basic Thread class. Perhaps using something based on this will give you better results? (this sample calls the thread every update cycle, if it's not already running- you'll obviously want to change that to work on a mouse button press or something)

 using UnityEngine;
 using System.Collections;
 using System.Threading;
 
 public class ThreadedCube : $$anonymous$$onoBehaviour {
 
 
     static Thread testThread;
     static public string some_value="0";
     // Use this for initialization
     void Start () {
         some_value = "0";
         testThread = new Thread(ThreadFunc);
     }
     
     // Update is called once per frame
     void Update () {
         Debug.Log(some_value.ToString());
         if (!testThread.IsAlive)
         {
             testThread = new Thread(ThreadFunc);
             testThread.Start("test");
         }
     }
 
     static void ThreadFunc(object textObject)
     {
         some_value = (string)textObject;
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Glurth · Mar 14, 2015 at 03:15 PM

oops! You merged that link so smoothly, I totally missed it, sorry bout that.

That class looks ok to me, and looks like you are using it correctly. Hmmm, the code obviously being called, that's why the program hangs, but why is it hanging if in another thread...

Ah! here is a possibility: You may need to insert some Thread.Sleep(0); calls into your OctTree generator function. This allows the main thread some time to run it's processes, before returning to yours.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Mesh generation issue (C#) 1 Answer

Unexpected period length with Unity Mathematics pnoise method 0 Answers

Is it possible to randomly generate terrain around pre-existing mesh? 1 Answer

procedurally generated map 0 Answers

How to add erosion to terrain on runtime 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