Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. Dismiss Notice

Terrain Generating and multithreading

Discussion in 'Scripting' started by MMOInteractiveRep, Jan 12, 2017.

  1. MMOInteractiveRep

    MMOInteractiveRep

    Joined:
    Apr 7, 2015
    Posts:
    88
    Got a question. I am working on a Voxel Engine for a game I'm working on. I have the voxel engine running good. I've created a Visual Node editor for generating the voxel terrain and I just over night multithreaded everything I possibly could. It's running pretty smoothly. I am now adding new ways of generating the terrain and wanted to add in Height map support. I then remembered I can't call any Unity API methods from threads other then the main thread. So I am now unable to call the get pixel method on the height map. What is the best way to add in Height map support with out using Unity's built in methods?
     
  2. MMOInteractiveRep

    MMOInteractiveRep

    Joined:
    Apr 7, 2015
    Posts:
    88
    I came up with the following which seems to work but is it the best way or is there a better way that I'm over looking?

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using UnityEngine;
    6.  
    7. namespace LibNoise.Generator
    8. {
    9.     class Heightmap : ModuleBase
    10.     {
    11.         private Texture2D heightmap;
    12.  
    13.         Dictionary<Vector2, Color> pixels = new Dictionary<Vector2, Color>();
    14.         private float width;
    15.         private float height;
    16.         private Vector2 Scale;
    17.  
    18.         public Heightmap(Texture2D h)
    19.             : base(0)
    20.         {
    21.             if (h != null)
    22.             {
    23.                 Scale = new Vector2(500, 500);
    24.                 heightmap = h;
    25.  
    26.                 for (int x = 0; x < heightmap.width; x++)
    27.                 {
    28.                     for (int z = 0; z < heightmap.height; z++)
    29.                     {
    30.                         pixels.Add(new Vector2(x, z), heightmap.GetPixel(x, z));
    31.                     }
    32.                 }
    33.  
    34.                 width = heightmap.width;
    35.                 height = heightmap.height;
    36.             }
    37.         }
    38.  
    39.         public override double GetValue(double x, double y, double z)
    40.         {
    41.             int _x = Mathf.FloorToInt((float)x / Scale.x * width);
    42.             int _z = Mathf.FloorToInt((float)z / Scale.y * height);
    43.  
    44.             Vector2 pos = new Vector2(_x, _z);
    45.  
    46.             if (pixels.ContainsKey(pos))
    47.                 return pixels[pos].grayscale;
    48.  
    49.             return 0;
    50.         }
    51.     }
    52. }
     
unityunity