Skip to content

Commit

Permalink
tree transparent support
Browse files Browse the repository at this point in the history
  • Loading branch information
Sieluna committed Jan 30, 2022
1 parent dac8645 commit cf4df8f
Show file tree
Hide file tree
Showing 32 changed files with 122 additions and 73 deletions.
Binary file modified Assets/Art/Block/Textures/Block - c.psd
Binary file not shown.
Binary file modified Assets/Art/Block/Textures/Block - n.psd
Binary file not shown.
Binary file modified Assets/Art/Block/Textures/Block - s.psd
Binary file not shown.
Binary file modified Assets/Art/Block/Textures/Block.psd
Binary file not shown.
1 change: 1 addition & 0 deletions Assets/Scenes/Main.unity
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ MonoBehaviour:
- {fileID: 2100000, guid: 5167570986bf5cd44869cbfe02ccd31f, type: 2}
- {fileID: 2100000, guid: 662d3143085c8844faff67675a9d8e1e, type: 2}
- {fileID: 2100000, guid: 8f5747920c17ccb43ab444373b21066f, type: 2}
- {fileID: 2100000, guid: 5167570986bf5cd44869cbfe02ccd31f, type: 2}
maxGenerateChunksInFrame: 1
--- !u!1 &934507233
GameObject:
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Environment/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public enum BlockType : byte

public enum BlockShape : byte
{
Empty, Block, Leaves, Foliage, Liquid
Empty, Block, Transparent, Foliage, Liquid
}

}
15 changes: 9 additions & 6 deletions Assets/Scripts/Environment/Chunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,20 @@ private IEnumerator UpdateMesh()
{
mesh.Clear();
colmesh.Clear();
mesh.subMeshCount = 3;
mesh.subMeshCount = 4;
mesh.SetVertices(meshData.nativeVertices, 0, verticesSize);
mesh.SetNormals(meshData.nativeNormals, 0, verticesSize);
mesh.SetColors(meshData.nativeColors, 0, verticesSize);
mesh.SetUVs(0, meshData.nativeUVs, 0, verticesSize);
mesh.SetIndices(meshData.nativeIndices.AsArray(), MeshTopology.Triangles, 0);
mesh.SetIndices(meshData.nativeBlockIndices.AsArray(), MeshTopology.Triangles, 0);
mesh.SetIndices(meshData.nativeLiquidIndices.AsArray(), MeshTopology.Triangles, 1);
mesh.SetIndices(meshData.nativeFoliageIndices.AsArray(), MeshTopology.Triangles, 2);
mesh.SetIndices(meshData.nativeTransparentIndices.AsArray(), MeshTopology.Triangles, 3);
colmesh.subMeshCount = 2;
colmesh.SetVertices(meshData.nativeVertices, 0, verticesSize);
colmesh.SetIndices(meshData.nativeIndices.AsArray(), MeshTopology.Triangles, 0);
mesh.SetIndices(meshData.nativeSubIndices.AsArray(), MeshTopology.Triangles, 1);
mesh.SetIndices(meshData.nativeMorIndices.AsArray(), MeshTopology.Triangles, 2);

colmesh.SetIndices(meshData.nativeBlockIndices.AsArray(), MeshTopology.Triangles, 0);
colmesh.SetIndices(meshData.nativeTransparentIndices.AsArray(), MeshTopology.Triangles, 1);

mesh.RecalculateNormals();
mesh.RecalculateBounds();

Expand Down
4 changes: 3 additions & 1 deletion Assets/Scripts/Environment/Data/BlockData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Runtime.CompilerServices;
using Environment.System;
using Unity.Burst;
using Unity.Burst.CompilerServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
Expand Down Expand Up @@ -35,7 +37,7 @@ private void Awake()
private void OnDestroy() => m_NativeBlockData.Dispose();
}

public static class BlockDataStatic
public static class BlockDataExtension
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte PackUVCoord(this int2 uv) => (byte) ((uv.x & 0xF) << 4 | ((uv.y & 0xF) << 0));
Expand Down
29 changes: 17 additions & 12 deletions Assets/Scripts/Environment/Data/NativeMeshData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ public class NativeMeshData
private NativeArray<Block> nativeBlocks;
public NativeArray<float3> nativeVertices;
public NativeArray<float3> nativeNormals;
public NativeList<int> nativeIndices;
public NativeList<int> nativeSubIndices;
public NativeList<int> nativeMorIndices;
public NativeList<int> nativeBlockIndices;
public NativeList<int> nativeLiquidIndices;
public NativeList<int> nativeFoliageIndices;
public NativeList<int> nativeLeavesIndices;
public NativeList<int> nativeTransparentIndices;
public NativeArray<float4> nativeUVs;
public NativeArray<Color> nativeColors;
public JobHandle jobHandle;
Expand All @@ -31,9 +33,10 @@ public NativeMeshData(int3 chunkSize)
nativeNormals = new NativeArray<float3>(12 * numBlocks, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
nativeUVs = new NativeArray<float4>(12 * numBlocks, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
nativeColors = new NativeArray<Color>(12 * numBlocks, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
nativeIndices = new NativeList<int>(18 * numBlocks, Allocator.TempJob);
nativeSubIndices = new NativeList<int>(36 * numLayer, Allocator.TempJob);
nativeMorIndices = new NativeList<int>(72 * numLayer, Allocator.TempJob);
nativeBlockIndices = new NativeList<int>(18 * numBlocks, Allocator.TempJob);
nativeLiquidIndices = new NativeList<int>(numLayer, Allocator.TempJob);
nativeFoliageIndices = new NativeList<int>(numLayer, Allocator.TempJob);
nativeTransparentIndices = new NativeList<int>(18 * numBlocks, Allocator.TempJob);
counter = new NativeCounter(Allocator.TempJob);
}

Expand All @@ -49,9 +52,10 @@ public void Dispose()
if (nativeNormals.IsCreated) nativeNormals.Dispose();
if (nativeUVs.IsCreated) nativeUVs.Dispose();
if (nativeColors.IsCreated) nativeColors.Dispose();
if (nativeIndices.IsCreated) nativeIndices.Dispose();
if (nativeSubIndices.IsCreated) nativeSubIndices.Dispose();
if (nativeMorIndices.IsCreated) nativeMorIndices.Dispose();
if (nativeBlockIndices.IsCreated) nativeBlockIndices.Dispose();
if (nativeLiquidIndices.IsCreated) nativeLiquidIndices.Dispose();
if (nativeFoliageIndices.IsCreated) nativeFoliageIndices.Dispose();
if (nativeTransparentIndices.IsCreated) nativeTransparentIndices.Dispose();
if (counter.IsCreated) counter.Dispose();
}

Expand All @@ -68,9 +72,10 @@ public IEnumerator ScheduleMeshingJob(Block[] blocks, NativeLightData lightData,
normals = nativeNormals,
uvs = nativeUVs,
colors = nativeColors,
indices = nativeIndices,
subIndices = nativeSubIndices,
morIndices = nativeMorIndices,
blockIndices = nativeBlockIndices,
liquidIndices = nativeLiquidIndices,
foliageIndices = nativeFoliageIndices,
transparentIndices = nativeTransparentIndices,
lightData = lightData.nativeLightData,
counter = counter
}.Schedule();
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Environment/System/BuildColliderSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private IEnumerator BakeUpdater()
}
}

[BurstCompile]
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
public struct BakeJob : IJobParallelFor
{
public NativeArray<int> meshIds;
Expand Down
13 changes: 8 additions & 5 deletions Assets/Scripts/Environment/System/BuildLightSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Environment.System
{
[BurstCompile]
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
public struct BuildLightSystem : IJobParallelFor
{
[ReadOnly] public NativeArray<long> blockData;
Expand Down Expand Up @@ -66,9 +66,9 @@ public unsafe void Execute(int index)

for (int i = 0; i < 4; i++)
{
bool side1 = TransparencyCheck(blocks, blockData, neighbors[Shared.AONeighborOffsets[i * 3]]);
bool corner = TransparencyCheck(blocks, blockData, neighbors[Shared.AONeighborOffsets[i * 3 + 1]]);
bool side2 = TransparencyCheck(blocks, blockData, neighbors[Shared.AONeighborOffsets[i * 3 + 2]]);
var side1 = TransparencyCheck(blocks, blockData, neighbors[Shared.AONeighborOffsets[i * 3]], chunkSize, chunkPosition, ref neighborHashMap, ref blocksWithNeighbor);
var corner = TransparencyCheck(blocks, blockData, neighbors[Shared.AONeighborOffsets[i * 3 + 1]], chunkSize, chunkPosition, ref neighborHashMap, ref blocksWithNeighbor);
var side2 = TransparencyCheck(blocks, blockData, neighbors[Shared.AONeighborOffsets[i * 3 + 2]], chunkSize, chunkPosition, ref neighborHashMap, ref blocksWithNeighbor);

if (side1 && side2)
blockLight.ambient[i + direction * 4] = 0f;
Expand All @@ -80,7 +80,10 @@ public unsafe void Execute(int index)
lightDatas[index] = blockLight;
}

private bool TransparencyCheck(in NativeSlice<Block> blocks, in NativeArray<long> blockData, int3 gridPosition)

private static bool TransparencyCheck(in NativeSlice<Block> blocks, in NativeArray<long> blockData,
int3 gridPosition, int3 chunkSize, int3 chunkPosition,
ref NativeHashMap<int3, int> neighborHashMap, ref NativeArray<Block> blocksWithNeighbor)
{
if (gridPosition.BoundaryCheck(chunkSize))
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Environment/System/BuildMapSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Environment.System
{
[BurstCompile]
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
public struct BuildMapSystem : IJobParallelFor
{
[ReadOnly] public int3 chunkPosition;
Expand Down
73 changes: 38 additions & 35 deletions Assets/Scripts/Environment/System/BuildMeshSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@

namespace Environment.System
{
[BurstCompile]
[BurstCompile(DisableSafetyChecks = true, OptimizeFor = OptimizeFor.Performance)]
public struct BuildMeshSystem : IJob
{
[ReadOnly] public NativeArray<Block> blocks;
[ReadOnly] public NativeArray<long> blockData;
[ReadOnly] public int3 chunkSize;
[ReadOnly] public NativeArray<BlockLight> lightData;

[WriteOnly] [NativeDisableParallelForRestriction] public NativeArray<float3> vertices;
[WriteOnly] [NativeDisableParallelForRestriction] public NativeArray<float3> normals;
[WriteOnly] [NativeDisableParallelForRestriction] public NativeArray<float4> uvs;
[WriteOnly] [NativeDisableParallelForRestriction] public NativeArray<Color> colors;
[WriteOnly] [NativeDisableParallelForRestriction] public NativeList<int> indices;
[WriteOnly] [NativeDisableParallelForRestriction] public NativeList<int> subIndices;
[WriteOnly] [NativeDisableParallelForRestriction] public NativeList<int> morIndices;

[WriteOnly] public NativeArray<float3> vertices;
[WriteOnly] public NativeArray<float3> normals;
[WriteOnly] public NativeArray<float4> uvs;
[WriteOnly] public NativeArray<Color> colors;

[WriteOnly] public NativeList<int> blockIndices;
[WriteOnly] public NativeList<int> liquidIndices;
[WriteOnly] public NativeList<int> foliageIndices;
[WriteOnly] public NativeList<int> transparentIndices;

[WriteOnly] public NativeCounter counter;

private struct Empty { }
Expand Down Expand Up @@ -67,20 +69,22 @@ public void Execute()
if (hashMap.ContainsKey(gridPosition)) { y++; continue; }

int3 neighborPosition = gridPosition + Shared.CubeDirectionOffsets[direction];

if (TransparencyCheck(blocks, blockData, neighborPosition, chunkSize)) { y++; continue; }
var neighborBlock = blocks[neighborPosition.To1DIndex(chunkSize)].type;
var neighborShape = blockData[(byte) neighborBlock].GetBlockShape();

if (TransparencyCheck(neighborShape, neighborPosition, chunkSize)) { y++; continue; }

if (shape == (long) BlockShape.Liquid)
{
if (direction != 2) { y++; continue; }
if (blocks[neighborPosition.To1DIndex(chunkSize)].type == BlockType.Water) { y++; continue; }
if (neighborBlock == BlockType.Water) { y++; continue; }
}

if (shape == (long) BlockShape.Foliage)
{
if (direction != 2) { y++; continue; }
}

#endregion

#region Step2: Get the calculated light data
Expand All @@ -95,45 +99,45 @@ public void Execute()
#region Step3: Mesh combine in xy direction

int height = 1, width = 1;
if (shape != UnsafeUtility.EnumToInt(BlockShape.Leaves))
if (shape != (long) BlockShape.Transparent && shape != (long) BlockShape.Foliage)
{
for (height = 1; height + y < chunkSize[Shared.DirectionAlignedY[direction]]; height++)
{
int3 nextPosition = gridPosition;
nextPosition[Shared.DirectionAlignedY[direction]] += height;

var nextBlock = blocks[nextPosition.To1DIndex(chunkSize)];
var nextLight = lightData[nextPosition.To1DIndex(chunkSize)];

if (nextBlock.type != block.type) break;
if (!nextLight.CompareFace(light, direction)) break;
if (hashMap.ContainsKey(nextPosition)) break;

hashMap.TryAdd(nextPosition, new Empty());
}

bool isDone = false;

for (width = 1; width + x < chunkSize[Shared.DirectionAlignedX[direction]]; width++)
{
for (int dy = 0; dy < height; dy++)
{
int3 nextPosition = gridPosition;
nextPosition[Shared.DirectionAlignedX[direction]] += width;
nextPosition[Shared.DirectionAlignedY[direction]] += dy;

var nextBlock = blocks[nextPosition.To1DIndex(chunkSize)];
var nextLight = lightData[nextPosition.To1DIndex(chunkSize)];

if (nextBlock.type != block.type || hashMap.ContainsKey(nextPosition) || !nextLight.CompareFace(light, direction))
{
isDone = true;
break;
}
}

if (isDone) break;

for (int dy = 0; dy < height; dy++)
{
int3 nextPosition = gridPosition;
Expand All @@ -143,28 +147,28 @@ public void Execute()
}
}
}

#endregion

switch (shape)
{
case (long) BlockShape.Liquid:
AddQuad(width, height, gridPosition, counter.Increment(),
ref vertices, ref normals, ref uvs, ref colors, ref subIndices);
ref vertices, ref normals, ref uvs, ref colors, ref liquidIndices);
break;
case (long) BlockShape.Foliage:
AddCrossShape(gridPosition, counter.Increment(2), 1,
ref vertices, ref normals, ref uvs, ref colors, ref morIndices);
ref vertices, ref normals, ref uvs, ref colors, ref foliageIndices);
break;
case (long) BlockShape.Block:
AddQuadByDirection(direction, blockData, block.type, light,
width, height, gridPosition, counter.Increment(),
ref vertices, ref normals, ref uvs, ref colors, ref indices);
ref vertices, ref normals, ref uvs, ref colors, ref blockIndices);
break;
case (long) BlockShape.Leaves:
case (long) BlockShape.Transparent:
AddQuadByDirection(direction, blockData, block.type, light,
width, height, gridPosition, counter.Increment(),
ref vertices, ref normals, ref uvs, ref colors, ref indices);
ref vertices, ref normals, ref uvs, ref colors, ref transparentIndices);
break;
}

Expand All @@ -179,11 +183,10 @@ public void Execute()

#region Private Function (move outside?)

private static bool TransparencyCheck(in NativeArray<Block> blocks, in NativeArray<long> blockData, int3 position, int3 chunkSize)
private static bool TransparencyCheck(long neighborShape, int3 position, int3 chunkSize)
{
if (!position.BoundaryCheck(chunkSize)) return false;
var neighborBlock = blockData[(byte) blocks[position.To1DIndex(chunkSize)].type].GetBlockShape();
return neighborBlock is not ((long) BlockShape.Empty or (long) BlockShape.Foliage or (long) BlockShape.Liquid);
return neighborShape is not ((long) BlockShape.Empty or (long) BlockShape.Foliage or (long) BlockShape.Transparent or (long) BlockShape.Liquid);
}

private static void AddCrossShape(int3 gridPosition, int numFace, int rand,
Expand Down Expand Up @@ -220,7 +223,7 @@ private static void AddCrossShape(int3 gridPosition, int numFace, int rand,
}

for (int i = 0, iMax = Shared.CubeCrossIndices.Length; i < iMax; i++)
indices.AddNoResize(numVertices + Shared.CubeCrossIndices[i]);
indices.Add(numVertices + Shared.CubeCrossIndices[i]);
}

private static void AddQuad(float width, float height, int3 gridPosition, int numFace,
Expand All @@ -244,7 +247,7 @@ private static void AddQuad(float width, float height, int3 gridPosition, int nu
}

for (int i = 0; i < 6; i++)
indices.AddNoResize(Shared.CubeFlippedIndices[12 + i] + numVertices);
indices.Add(Shared.CubeFlippedIndices[12 + i] + numVertices);
}

private static unsafe void AddQuadByDirection(int direction, in NativeArray<long> blockData, BlockType type, in BlockLight blockLight,
Expand Down
8 changes: 8 additions & 0 deletions Assets/Settings/UniversalRenderPipeline.meta

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

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ MonoBehaviour:
m_RendererDataList:
- {fileID: 11400000, guid: e634585d5c4544dd297acaee93dc2beb, type: 2}
m_DefaultRendererIndex: 0
m_RequireDepthTexture: 0
m_RequireOpaqueTexture: 0
m_RequireDepthTexture: 1
m_RequireOpaqueTexture: 1
m_OpaqueDownsampling: 1
m_SupportsTerrainHoles: 1
m_SupportsTerrainHoles: 0
m_StoreActionsOptimization: 0
m_SupportsHDR: 1
m_MSAA: 1
Expand All @@ -48,6 +48,8 @@ MonoBehaviour:
m_ShadowDepthBias: 1
m_ShadowNormalBias: 1
m_SoftShadowsSupported: 1
m_ConservativeEnclosingSphere: 0
m_NumIterationsEnclosingSphere: 64
m_AdditionalLightsCookieResolution: 512
m_AdditionalLightsCookieFormat: 1
m_UseSRPBatcher: 1
Expand Down
File renamed without changes.
Loading

0 comments on commit cf4df8f

Please sign in to comment.