72 lines
2.8 KiB
C#
72 lines
2.8 KiB
C#
using System.Collections.Generic;
|
|
using Unity.Collections;
|
|
using Unity.Jobs;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using Random = Unity.Mathematics.Random;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
public abstract class GrassChunkMeshGeneratorBase<T> : ChunkMeshGenerator where T : unmanaged, IGrassTileMask
|
|
{
|
|
protected GrassChunkMeshGeneratorBase(World world, GameConfig config) : base(world, config)
|
|
{
|
|
}
|
|
|
|
protected override void InitializeChunk(TerrainChunk chunk, int lod, GameObject gameObject, Mesh mesh, Renderer renderer)
|
|
{
|
|
renderer.enabled = false;
|
|
}
|
|
|
|
protected override void OnCompleted(int lod, List<TerrainChunk> chunks)
|
|
{
|
|
foreach (var chunk in chunks)
|
|
{
|
|
var renderer = GetRenderer(chunk, lod);
|
|
renderer.sharedMaterial = GetMaterial(lod);
|
|
}
|
|
}
|
|
|
|
protected abstract Renderer GetRenderer(TerrainChunk chunk, int lod);
|
|
|
|
protected abstract Material GetMaterial(int lod);
|
|
|
|
protected override JobHandle ScheduleJob(ChunkGenerationJobState jobState, NativeArray<int2> chunkCoords)
|
|
{
|
|
var randomArray = new NativeArray<Random>(chunkCoords.Length, Allocator.Persistent);
|
|
for (var i = 0; i < randomArray.Length; i++) randomArray[i] = Random.CreateFromIndex((uint)(World.Seed + i));
|
|
|
|
var jobHandle = ScheduleJob(jobState, chunkCoords, randomArray);
|
|
|
|
randomArray.Dispose(jobHandle);
|
|
|
|
return jobHandle;
|
|
}
|
|
|
|
protected abstract JobHandle ScheduleJob(ChunkGenerationJobState jobState, NativeArray<int2> chunkCoords, NativeArray<Random> randomArray);
|
|
|
|
protected void PrepareJob(ChunkGenerationJobState jobState, NativeArray<int2> chunkCoords, NativeArray<Random> randomArray, int lod, ref GenerateGrassChunkJob<T> job)
|
|
{
|
|
var grassGenerationConfig = GetGrassGenerationConfig();
|
|
|
|
job.WorldSize = World.Size;
|
|
job.ChunkCoordsArray = chunkCoords;
|
|
job.ChunkSize = Config.WorldGen.ChunkSize;
|
|
job.Mask = GetMask();
|
|
job.Heightmap = World.Heightmap.GetNativeArray();
|
|
job.MeshDataArray = jobState.MeshDataArray;
|
|
job.RandomArray = randomArray;
|
|
job.Density = GetDensityForLOD(lod);
|
|
job.NoiseScale = grassGenerationConfig.NoiseScale;
|
|
job.DiscardThreshold = grassGenerationConfig.NoiseDiscardThreshold;
|
|
job.BladeWidth = grassGenerationConfig.BladeWidth;
|
|
job.BladeHeightRange = grassGenerationConfig.BladeHeightRange;
|
|
}
|
|
|
|
protected abstract GameConfig.WorldGenConfig.GrassConfig GetGrassGenerationConfig();
|
|
|
|
protected abstract T GetMask();
|
|
|
|
protected abstract int GetDensityForLOD(int lod);
|
|
}
|
|
} |