Skip to content

Commit

Permalink
Add voxel greedy meshing
Browse files Browse the repository at this point in the history
  • Loading branch information
jeickhoff committed Jun 9, 2023
1 parent bbb630c commit 513aa69
Show file tree
Hide file tree
Showing 16 changed files with 766 additions and 496 deletions.
22 changes: 0 additions & 22 deletions Assets/Prefabs/TerrainArea.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ GameObject:
- component: {fileID: 7020423471914046309}
- component: {fileID: 5368163881853221015}
- component: {fileID: 6978896562413794302}
- component: {fileID: 4998810751757002297}
m_Layer: 0
m_Name: TerrainArea
m_TagString: Untagged
Expand Down Expand Up @@ -95,24 +94,3 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
ComponentOverrides: []
--- !u!65 &4998810751757002297
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3194879136077857405}
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 64
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_IsTrigger: 1
m_ProvidesContacts: 0
m_Enabled: 0
serializedVersion: 3
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
121 changes: 0 additions & 121 deletions Assets/Prefabs/TerrainFace.prefab

This file was deleted.

7 changes: 0 additions & 7 deletions Assets/Prefabs/TerrainFace.prefab.meta

This file was deleted.

9 changes: 4 additions & 5 deletions Assets/Scenes/SampleScene/SampleSubScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,14 @@ MonoBehaviour:
m_EditorClassIdentifier:
TerrainArea: {fileID: 3194879136077857405, guid: fbe285af02efd794da108f614fa73983,
type: 3}
TerrainFace: {fileID: 8123702849073069357, guid: 1648581b08e7ca045bef1afd2ed74b03,
type: 3}
TerrainMaterial: {fileID: 2100000, guid: 2e01ef40587306e4c92d3d61aa003fa9, type: 2}
seed: 43
initialAreas:
x: 5
x: 10
y: 1
z: 5
z: 10
maxChunkSpawnsPerTick: 25
blocksPerChunkSide: 4
blocksPerAreaSide: 4
YBounds:
x: 0
y: 8
Expand Down
25 changes: 13 additions & 12 deletions Assets/Scripts/Player/PlayerMovementSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Unity.Entities.Content;
using Unity.NetCode;
using Unity.Physics.Systems;
using Unity.Transforms;
using UnityEngine;


Expand All @@ -30,7 +31,7 @@ partial struct PlayerMovementSystem : ISystem

private BufferLookup<TerrainBlocks> _bufferLookup;
private NativeArray<Entity> terrainAreasEntities;
private NativeArray<TerrainArea> terrainAreas;
private NativeArray<LocalTransform> terrainAreaTransforms;
private int blocksPerChunkSide;

public void OnCreate(ref SystemState state)
Expand Down Expand Up @@ -58,10 +59,10 @@ public void OnUpdate(ref SystemState state)
var networkTime = SystemAPI.GetSingleton<NetworkTime>();

_bufferLookup.Update(ref state);
var terrainAreasQuery = SystemAPI.QueryBuilder().WithAll<TerrainArea>().Build();
var terrainAreasQuery = SystemAPI.QueryBuilder().WithAll<TerrainArea, LocalTransform>().Build();
terrainAreasEntities = terrainAreasQuery.ToEntityArray(state.WorldUpdateAllocator);
terrainAreas = terrainAreasQuery.ToComponentDataArray<TerrainArea>(state.WorldUpdateAllocator);
blocksPerChunkSide = SystemAPI.GetSingleton<TerrainSpawner>().blocksPerChunkSide;
terrainAreaTransforms = terrainAreasQuery.ToComponentDataArray<LocalTransform>(state.WorldUpdateAllocator);
blocksPerChunkSide = SystemAPI.GetSingleton<TerrainSpawner>().blocksPerSide;

foreach (var player in SystemAPI.Query<PlayerAspect>().WithAll<Simulate>())
{
Expand Down Expand Up @@ -200,12 +201,12 @@ private PlayerUtilities.PlayerSupportState CheckPlayerSupported(RigidTransform t

private bool IsBlockAtPosition(float3 pos)
{
if (GetTerrainAreaByPosition(pos, out Entity containingArea, out int3 containingAreaLocation))
if (GetTerrainAreaByPosition(pos, out Entity containingArea, out float3 containingAreaLocation))
{
var terrainBuffer = _bufferLookup[containingArea];
int localx = (int)math.floor(pos.x) - containingAreaLocation.x;
int localy = (int)math.floor(pos.y) - containingAreaLocation.y;
int localz = (int)math.floor(pos.z) - containingAreaLocation.z;
int localx = (int)math.floor(pos.x - containingAreaLocation.x);
int localy = (int)math.floor(pos.y - containingAreaLocation.y);
int localz = (int)math.floor(pos.z - containingAreaLocation.z);
int index = localx + localy * blocksPerChunkSide + localz * blocksPerChunkSide * blocksPerChunkSide;
int block = terrainBuffer[index].Value;
if (block != -1)
Expand Down Expand Up @@ -234,12 +235,12 @@ private bool IsBlockAtPosition(float3 pos)
return false;
}

private bool GetTerrainAreaByPosition(float3 pos, out Entity containingArea, out int3 containingAreaLocation)
private bool GetTerrainAreaByPosition(float3 pos, out Entity containingArea, out float3 containingAreaLocation)
{
for (int i = 0; i < terrainAreas.Length; i++ )
for (int i = 0; i < terrainAreaTransforms.Length; i++ )
{
var terrainArea = terrainAreas[i];
int3 loc = terrainArea.location;
var terrainArea = terrainAreaTransforms[i];
float3 loc = terrainArea.Position;
if (pos.x >= loc.x && pos.x < loc.x + blocksPerChunkSide &&
pos.y >= loc.y && pos.y < loc.y + blocksPerChunkSide &&
pos.z >= loc.z && pos.z < loc.z + blocksPerChunkSide)
Expand Down
61 changes: 35 additions & 26 deletions Assets/Scripts/Rendering/DebugRenderSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,48 @@
[UpdateInGroup(typeof(PresentationSystemGroup))]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
[BurstCompile]
public partial class DebugRenderSystem : SystemBase
public partial struct DebugRenderSystem : ISystem
{
protected override void OnCreate()
private EntityQuery _terrainSpawnerQuery;
[BurstCompile]
public void OnCreate(ref SystemState state)
{
//Enabled = false;
state.RequireForUpdate<TerrainSpawner>();
state.RequireForUpdate<TerrainArea>();
_terrainSpawnerQuery = state.GetEntityQuery(ComponentType.ReadOnly<TerrainSpawner>());
}

[BurstCompile]
protected override void OnUpdate()
public void OnUpdate(ref SystemState state)
{
TerrainSpawner terrainSpawner = _terrainSpawnerQuery.GetSingleton<TerrainSpawner>();

new DebugDrawTerrain{blocksPerSide = terrainSpawner.blocksPerSide}.ScheduleParallel();
}
}

[BurstCompile]
public partial struct DebugDrawTerrain : IJobEntity
{
public int blocksPerSide;
public void Execute(in TerrainArea terrainChunk, in LocalTransform t)
{
var terrainSpawner = SystemAPI.GetSingleton<TerrainSpawner>();
foreach (var terrainArea in SystemAPI.Query<RefRO<TerrainArea>>())
{
int d = terrainSpawner.blocksPerChunkSide;
//terrainArea.ValueRO.location
float3 baseLocation = new float3(
terrainArea.ValueRO.location.x,
terrainArea.ValueRO.location.y,
terrainArea.ValueRO.location.z);
Debug.DrawLine(baseLocation, baseLocation + new float3(d,0,0));
Debug.DrawLine(baseLocation, baseLocation + new float3(0,d,0));
Debug.DrawLine(baseLocation, baseLocation + new float3(0,0,d));
Debug.DrawLine(baseLocation + new float3(d,d,0), baseLocation + new float3(d,0,0));
Debug.DrawLine(baseLocation + new float3(d,d,0), baseLocation + new float3(0,d,0));
Debug.DrawLine(baseLocation + new float3(d,d,0), baseLocation + new float3(d,d,d));
Debug.DrawLine(baseLocation + new float3(0,d,d), baseLocation + new float3(0,d,0));
Debug.DrawLine(baseLocation + new float3(0,d,d), baseLocation + new float3(0,0,d));
Debug.DrawLine(baseLocation + new float3(0,d,d), baseLocation + new float3(d,d,d));
Debug.DrawLine(baseLocation + new float3(d,0,d), baseLocation + new float3(d,0,0));
Debug.DrawLine(baseLocation + new float3(d,0,d), baseLocation + new float3(d,d,d));
Debug.DrawLine(baseLocation + new float3(d,0,d), baseLocation + new float3(0,0,d));
}

var baseLocation = t.Position;
var d = blocksPerSide;
// Draw a bounding box
Debug.DrawLine(baseLocation, baseLocation + new float3(d,0,0));
Debug.DrawLine(baseLocation, baseLocation + new float3(0,d,0));
Debug.DrawLine(baseLocation, baseLocation + new float3(0,0,d));
Debug.DrawLine(baseLocation + new float3(d,d,0), baseLocation + new float3(d,0,0));
Debug.DrawLine(baseLocation + new float3(d,d,0), baseLocation + new float3(0,d,0));
Debug.DrawLine(baseLocation + new float3(d,d,0), baseLocation + new float3(d,d,d));
Debug.DrawLine(baseLocation + new float3(0,d,d), baseLocation + new float3(0,d,0));
Debug.DrawLine(baseLocation + new float3(0,d,d), baseLocation + new float3(0,0,d));
Debug.DrawLine(baseLocation + new float3(0,d,d), baseLocation + new float3(d,d,d));
Debug.DrawLine(baseLocation + new float3(d,0,d), baseLocation + new float3(d,0,0));
Debug.DrawLine(baseLocation + new float3(d,0,d), baseLocation + new float3(d,d,d));
Debug.DrawLine(baseLocation + new float3(d,0,d), baseLocation + new float3(0,0,d));
}
}

Loading

0 comments on commit 513aa69

Please sign in to comment.