riversong code showcase
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.Collections;
|
||||
using Unity.Jobs;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class TerrainChunkMeshGenerator : ChunkMeshGenerator
|
||||
{
|
||||
public TerrainChunkMeshGenerator(World world, GameConfig config) : base(world, config)
|
||||
{
|
||||
}
|
||||
|
||||
protected override int GetLodCount()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
protected override string GetGameObjectName(string chunkName, int2 chunkCoords, int lod)
|
||||
{
|
||||
return $"{chunkName}_Terrain";
|
||||
}
|
||||
|
||||
protected override void InitializeChunk(TerrainChunk chunk, int lod, GameObject gameObject, Mesh mesh, Renderer renderer)
|
||||
{
|
||||
gameObject.layer = GameObjectLayers.Terrain;
|
||||
|
||||
chunk.Terrain = new TerrainChunk.RenderData(mesh, renderer);
|
||||
}
|
||||
|
||||
protected override Mesh GetMesh(TerrainChunk chunk, int lod)
|
||||
{
|
||||
return chunk.Terrain.Mesh;
|
||||
}
|
||||
|
||||
protected override JobHandle ScheduleJob(ChunkGenerationJobState jobState, NativeArray<int2> chunkCoords)
|
||||
{
|
||||
return new GenerateTerrainChunkJob
|
||||
{
|
||||
WorldSize = World.Size,
|
||||
ChunkCoordsArray = chunkCoords,
|
||||
ChunkSize = Config.WorldGen.ChunkSize,
|
||||
Heightmap = World.Heightmap.GetNativeArray(),
|
||||
MeshDataArray = jobState.MeshDataArray
|
||||
}.Schedule(chunkCoords.Length, Config.WorldGen.ChunksPerThread);
|
||||
}
|
||||
|
||||
protected override void OnCompleted(int lod, List<TerrainChunk> chunks)
|
||||
{
|
||||
foreach (var chunk in chunks)
|
||||
{
|
||||
var mesh = chunk.Terrain.Mesh;
|
||||
var renderer = chunk.Terrain.Renderer;
|
||||
|
||||
var subMeshCount = mesh.subMeshCount;
|
||||
|
||||
var materials = new Material[subMeshCount];
|
||||
materials[0] = Config.Terrain.GroundMaterial;
|
||||
if (subMeshCount > 1) materials[1] = Config.Terrain.CliffMaterial;
|
||||
|
||||
renderer.sharedMaterials = materials;
|
||||
|
||||
renderer.gameObject.AddComponent<MeshCollider>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user