Advertisement
JontePonte

voxel grid generator

Jul 4th, 2025
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using Unity.Jobs;
  4. using Unity.Collections;
  5. using Unity.Mathematics;
  6. using Unity.Entities;
  7. using Voxels;
  8. using TerrainGeneration.TerrainMasks;
  9. using Unity.VisualScripting;
  10. using System;
  11. using UnityEditor.PackageManager;
  12.  
  13. namespace TerrainGeneration
  14. {
  15.     public static class VoxelGridGenerator
  16.     {
  17.         private static BlobAssetReference<TerrainMaskList> terrainMaskList;
  18.  
  19.         /// <summary>
  20.         /// Generates a voxel grid based on masks2D and masks3D
  21.         /// </summary>
  22.         public static NativeVoxelGrid GenerateGrid(Vector3Int gridSize, Vector3Int position, float surfaceLevel, int stepSize)
  23.         {
  24.             NativeVoxelGrid voxelGrid = new(gridSize.x, gridSize.y, gridSize.z, Allocator.TempJob);
  25.  
  26.             if (!terrainMaskList.IsCreated) throw new ArgumentException("TerrainMaskList has not been created. You should call GenerateJobTerrainMasks() before GenerateGrid().");
  27.  
  28.             VoxelGridJob voxelGridJob = new()
  29.             {
  30.                 voxelGrid = voxelGrid,
  31.                 terrainMasks = terrainMaskList,
  32.                 positionOffset = new float3(position.x, position.y, position.z),
  33.                 gridSize = new int3(gridSize.x, gridSize.y, gridSize.z),
  34.                 surfaceLevel = surfaceLevel,
  35.                 stepSize = stepSize
  36.             };
  37.  
  38.             JobHandle handle = voxelGridJob.Schedule(gridSize.x * gridSize.z, 16);
  39.             handle.Complete();
  40.  
  41.             return voxelGrid;
  42.         }
  43.  
  44.         /// <summary>
  45.         /// The masks modified to work in jobs
  46.         /// </summary>
  47.         public static void GenerateJobTerrainMasks(List<TerrainMask2D> masks2D, List<TerrainMask3D> masks3D)
  48.         {
  49.             var builder = new BlobBuilder(Allocator.Persistent);
  50.             ref TerrainMaskList terrainMaskListRoot = ref builder.ConstructRoot<TerrainMaskList>();
  51.  
  52.             int count = masks2D.Count + masks3D.Count;
  53.             BlobBuilderArray<JobTerrainMask> arrayBuilder = builder.Allocate(
  54.                 ref terrainMaskListRoot.terrainMasks,
  55.                 count
  56.             );
  57.  
  58.             // Fill 2D masks
  59.             for (int i = 0; i < masks2D.Count; i++)
  60.             {
  61.                 masks2D[i].ToJobMask(ref arrayBuilder[i], ref builder);
  62.             }
  63.  
  64.             // Fill 3D masks
  65.             for (int i = 0; i < masks3D.Count; i++)
  66.             {
  67.                 masks3D[i].ToJobMask(ref arrayBuilder[i + masks2D.Count], ref builder);
  68.             }
  69.  
  70.             var result = builder.CreateBlobAssetReference<TerrainMaskList>(Allocator.Persistent);
  71.             builder.Dispose();
  72.  
  73.             terrainMaskList = result;
  74.         }
  75.  
  76.         public static void Dispose()
  77.         {
  78.             if (terrainMaskList.IsCreated) terrainMaskList.Dispose();
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement