Advertisement
Guest User

Untitled

a guest
Aug 15th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using UnityEngine;
  2. using Unity.Jobs;
  3. using Unity.Collections;
  4. using Unity.Burst;
  5.  
  6. namespace Voxels {
  7.     [BurstCompile]
  8.     public struct HeightGenJob : IJobParallelFor {
  9.         public int SizeX;
  10.         public int SizeY;
  11.         public int OffsetX;
  12.         public int OffsetY;
  13.         public int Seed;
  14.         public float BaseScale;
  15.  
  16.         [WriteOnly]
  17.         public NativeArray<byte> Height;
  18.  
  19.         public void Execute(int index) {
  20.             var x = OffsetX + ( index / SizeX);
  21.             var y = OffsetY + (index % SizeY);
  22.  
  23.             var noise =  Mathf.PerlinNoise(x / (float) 32 + Seed, y / (float) 32 + Seed) * BaseScale * 0.5f;
  24.             noise += Mathf.PerlinNoise(x / (float)64 + Seed - 3, y / (float)64 + Seed - 3) * BaseScale;
  25.             noise += Mathf.PerlinNoise(x / (float)128 + Seed + 5, y / (float)128 + Seed + 5) * BaseScale * 2;
  26.             noise += Mathf.PerlinNoise(x / (float)16 + Seed - 4, y / (float)16 + Seed - 8) * BaseScale * 0.25f;
  27.             Height[index] = System.Convert.ToByte(noise);
  28.         }
  29.     }
  30.  
  31.     [BurstCompile]
  32.     public struct ChunkGenJob  : IJobParallelFor {
  33.         public int SizeH;
  34.         public int SizeY;
  35.         public int Seed;
  36.         public int SeaLevel;
  37.         public Unity.Mathematics.Random Random;
  38.  
  39.         [ReadOnly]
  40.         public NativeArray<byte> HeightMap;
  41.  
  42.         [WriteOnly]
  43.         public NativeArray<BlockData> Blocks;
  44.         public void Execute(int index) {
  45.             var dh = SizeH * SizeH;
  46.             var y = index / dh;
  47.             var h = index % dh;
  48.             var x = h % SizeH;
  49.             var z = h / SizeH;
  50.             var height = HeightMap[(x) * SizeH + z];
  51.             var stoneHeight = (int)(height * 0.8f);
  52.  
  53.             var rnd8pct = Random.NextUInt(0, 12) < 1 ? true : false;
  54.             var rnd3pct = Random.NextUInt(0, 30) < 1 ? true : false;
  55.  
  56.             if ( y == 0 ) {
  57.                 Blocks[y * dh + z * SizeH + x] = new BlockData(BlockType.Bedrock, 0);
  58.                 return;
  59.             }
  60.             if ( y < stoneHeight ) {
  61.                 Blocks[y * dh + z * SizeH + x] = new BlockData(BlockType.Stone, 0);
  62.             } else if ( y < height ){
  63.                 Blocks[y * dh + z * SizeH + x] = new BlockData(BlockType.Dirt, 0);
  64.                 return;
  65.             } else if ( y == height ) {
  66.                 if ( y > SeaLevel ) {
  67.                     Blocks[y * dh + z * SizeH + x] = new BlockData(BlockType.Grass, 0);
  68.                 } else {
  69.                     Blocks[y * dh + z * SizeH + x] = new BlockData(BlockType.Sand, 0);
  70.                 }
  71.                
  72.             } else if ( y > height && y < SeaLevel ) {
  73.                 Blocks[y * dh + z * SizeH + x] = new BlockData() {
  74.                     Type = BlockType.WaterStill,
  75.                     SunLevel = (byte) (225 - (SeaLevel - y) * 10),
  76.                     AddColor = 65535,
  77.                 };
  78.             } else if (y == height + 1 && rnd8pct && y > SeaLevel ) {
  79.                 Blocks[y * dh + z * SizeH + x] = new BlockData() {
  80.                     Type = BlockType.Weed,
  81.                     SunLevel = 255,
  82.                     AddColor = 65535,
  83.                 };
  84.             } else if ( y == height + 1 && rnd3pct && y > SeaLevel ) {
  85.                 Blocks[y * dh + z * SizeH + x] = new BlockData() {
  86.                     Type = BlockType.Shrub,
  87.                     Subtype  = (byte) Random.NextInt(0, 3),
  88.                     SunLevel = 255,
  89.                     AddColor = 65535,
  90.                 };
  91.             } else if ( y > height  || (!rnd8pct && y > height)) {
  92.                 Blocks[y * dh + z * SizeH + x] = new BlockData() {
  93.                     SunLevel = 255
  94.                 };
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement