78 lines
2.5 KiB
C#
78 lines
2.5 KiB
C#
using Unity.Collections;
|
|
using Unity.Jobs;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
using Random = Unity.Mathematics.Random;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
public class GrassChunkMeshGenerator : GrassChunkMeshGeneratorBase<GrassTileMask>
|
|
{
|
|
public GrassChunkMeshGenerator(World world, GameConfig config) : base(world, config)
|
|
{
|
|
}
|
|
|
|
protected override int GetLodCount()
|
|
{
|
|
return Config.Terrain.GrassLODs.Length;
|
|
}
|
|
|
|
protected override string GetGameObjectName(string chunkName, int2 chunkCoords, int lod)
|
|
{
|
|
return $"{chunkName}_Grass_LOD{lod}";
|
|
}
|
|
|
|
protected override void InitializeChunk(TerrainChunk chunk, int lod, GameObject gameObject, Mesh mesh, Renderer renderer)
|
|
{
|
|
base.InitializeChunk(chunk, lod, gameObject, mesh, renderer);
|
|
|
|
renderer.shadowCastingMode = ShadowCastingMode.Off;
|
|
|
|
chunk.GrassLODs[lod] = new TerrainChunk.RenderData(mesh, renderer);
|
|
}
|
|
|
|
protected override Mesh GetMesh(TerrainChunk chunk, int lod)
|
|
{
|
|
return chunk.GrassLODs[lod].Mesh;
|
|
}
|
|
|
|
protected override JobHandle ScheduleJob(ChunkGenerationJobState jobState, NativeArray<int2> chunkCoords, NativeArray<Random> randomArray)
|
|
{
|
|
var job = new GenerateGrassChunkJob<GrassTileMask>();
|
|
PrepareJob(jobState, chunkCoords, randomArray, jobState.Lod, ref job);
|
|
return job.Schedule(chunkCoords.Length, Config.WorldGen.ChunksPerThread);
|
|
}
|
|
|
|
protected override Renderer GetRenderer(TerrainChunk chunk, int lod)
|
|
{
|
|
return chunk.GrassLODs[lod].Renderer;
|
|
}
|
|
|
|
protected override Material GetMaterial(int lod)
|
|
{
|
|
return Config.Terrain.GrassMaterial;
|
|
}
|
|
|
|
protected override GameConfig.WorldGenConfig.GrassConfig GetGrassGenerationConfig()
|
|
{
|
|
return Config.WorldGen.Grass;
|
|
}
|
|
|
|
protected override GrassTileMask GetMask()
|
|
{
|
|
var heightmap = World.Heightmap.GetNativeArray();
|
|
var fertility = World.Fertility.GetNativeArray();
|
|
return new GrassTileMask
|
|
{
|
|
Heightmap = heightmap,
|
|
Fertility = fertility
|
|
};
|
|
}
|
|
|
|
protected override int GetDensityForLOD(int lod)
|
|
{
|
|
return Config.Terrain.GrassLODs[lod].Density;
|
|
}
|
|
}
|
|
} |