Skip to content

Commit

Permalink
Add Unity Jobs to parallel process mesh data
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrock4t committed Sep 29, 2023
1 parent 7d510e5 commit cab46db
Show file tree
Hide file tree
Showing 11 changed files with 624 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Assets/Scripts/Automata.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Scripts/Automata/Rendering.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Scripts/Automata/Rendering/CPU (Mesh).meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

145 changes: 145 additions & 0 deletions Assets/Scripts/Automata/Rendering/CPU (Mesh)/BuildVoxelWorldMesh.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
using static WorldAutomatonCPU;
using static WorldAutomaton.Elemental;
using static VoxelUtils;
using WorldCellInfo = CellInfo<WorldAutomaton.Elemental.Element>;


/// <summary>
/// Parallel calculate all possible vertices in the world, so they can be accessed later.
/// </summary>

[BurstCompile]
public struct BuildVoxelWorldMesh : IJobParallelFor
{
public int3 automaton_dimensions;

[NativeDisableParallelForRestriction]
public NativeArray<float3x4> vertices;



// the index of each voxel
public void Execute(int i)
{
CreateVoxelMesh(i, VoxelUtils.Index_int3FromFlat(i, this.automaton_dimensions));
}


public void CreateVoxelMesh(int i, int3 index)
{
for(int j = 0; j < 6; j++)
{
CreateQuad(i, index, (BlockSide)j);
}


}

public void CreateQuad(int i, int3 index, BlockSide side)
{
float3x4 verts = FetchQuadVertices(side, index);
this.vertices[i * 6 + (int)side] = verts;
}


/// <summary>
///
/// </summary>
/// <param name="neighborX"></param>
/// <param name="neighborY"></param>
/// <param name="neighborZ"></param>
/// <param name="type"></param>
/// <returns>True if neighbor doesnt need to be drawn, False if neighbor does need to be drawn</returns>
public bool CanHideQuadToNeighbor(NativeArray<WorldCellInfo> cell_grid, int neighborX, int neighborY, int neighborZ, Element type)
{
if (type == Element.Empty) return true; // empty doesnt have quads

int3 neighborIndex = new int3(neighborX, neighborY, neighborZ);
if (VoxelUtils.IsOutOfBounds(neighborIndex, this.automaton_dimensions)) return false;
Element neighborTypeOrNull = VoxelAutomaton<Element>.GetCellNextState(cell_grid, this.automaton_dimensions, neighborX, neighborY, neighborZ);
Element neighborType = neighborTypeOrNull;


if (neighborType == Element.Empty) return false; // must show to empty space

//if neighbor is the same type, can hide the quad (e.g., water neighboring water)
if (neighborType == type)
{
return true;
}

// if neighbor is air or water it is translucent, can't hide the quad
if (IsSolid(type) && !IsSolid(neighborTypeOrNull))
{
return false;
}

//in any other situation, can hide the quad
return true;
}

public float3x4 FetchQuadVertices(BlockSide side, float3 index)
{
float3x4 val = new float3x4();

switch (side)
{
case BlockSide.BOTTOM:
val[0] = voxel_vertices[0];
val[1] = voxel_vertices[1];
val[2] = voxel_vertices[2];
val[3] = voxel_vertices[3];
break;
case BlockSide.TOP:
val[0] = voxel_vertices[7];
val[1] = voxel_vertices[6];
val[2] = voxel_vertices[5];
val[3] = voxel_vertices[4];
break;
case BlockSide.LEFT:
val[0] = voxel_vertices[7];
val[1] = voxel_vertices[4];
val[2] = voxel_vertices[0];
val[3] = voxel_vertices[3];
break;
case BlockSide.RIGHT:
val[0] = voxel_vertices[5];
val[1] = voxel_vertices[6];
val[2] = voxel_vertices[2];
val[3] = voxel_vertices[1];
break;
case BlockSide.FRONT:
val[0] = voxel_vertices[4];
val[1] = voxel_vertices[5];
val[2] = voxel_vertices[1];
val[3] = voxel_vertices[0];
break;
case BlockSide.BACK:
val[0] = voxel_vertices[6];
val[1] = voxel_vertices[7];
val[2] = voxel_vertices[3];
val[3] = voxel_vertices[2];
break;
default:
Debug.LogError("error");
val[0] = voxel_vertices[0];
val[1] = voxel_vertices[2];
val[2] = voxel_vertices[4];
val[3] = voxel_vertices[6];
break;
}
val[0] += index;
val[1] += index;
val[2] += index;
val[3] += index;

return val;
}


}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cab46db

Please sign in to comment.