Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MapChunk
- {
- public int MapSize;
- public MapChunk(int size)
- {
- MapSize = size;
- }
- public struct MapChunkData
- {
- public float[] _mapChunk;
- public int Size { get; set; }
- public MapChunkData(MapChunk mapChunk)
- {
- _mapChunk = new float[mapChunk.MapSize];
- Size = mapChunk.MapSize;
- }
- public void Evaluate(int index)
- {
- for (int i = 0; i < Size; i++)
- {
- _mapChunk[i] = Unity.Mathematics.noise.snoise(new Vector2(index, i));
- }
- }
- }
- }
- public struct MapChunkEvaluateJob : IJobParallelFor
- {
- public NativeArray<MapChunk.MapChunkData> chunks;
- public void Execute(int index)
- {
- var chunk = chunks[index];
- chunk.Evaluate(index);
- chunks[index] = chunk;
- }
- }
- private MapChunkEvaluateJob _job;
- private NativeArray<MapChunk.MapChunkData> _mapChunkDataArray;
- List<MapChunk> mapChunks = new List<MapChunk>();
- // wouldn't it be better if there was some heightmap array that the jobs wrote to directly?
- public void InitMapGeneratorJobs()
- {
- // setup
- MapChunk.MapChunkData[] mapChunkData = new MapChunk.MapChunkData[128];
- for (int i = 0; i < mapChunkData.Length; i++)
- {
- MapChunk mp = new MapChunk(128);
- mapChunks.Add(mp);
- mapChunkData[i] = new MapChunk.MapChunkData(mapChunks[i]);
- }
- _mapChunkDataArray = new NativeArray<MapChunk.MapChunkData>(mapChunkData, Allocator.Persistent);
- _job = new MapChunkEvaluateJob
- {
- chunks = _mapChunkDataArray
- };
- }
- public void RunMapGeneratorJobs()
- {
- var jobHandle = _job.Schedule(128, 1); // can this be better?
- jobHandle.Complete();
- float[,] parallelledHeightMap = new float[128, 128];
- // can this be better?
- for (int i = 0; i < 128; i++)
- {
- for (int j = 0; j < 128; j++)
- {
- parallelledHeightMap[i, j] = _job.chunks[i]._mapChunk[j]; // this???
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment