Skip to content

Commit

Permalink
Merge pull request #7 from pixelreyn/Part9-ByteVoxelsBugFixes
Browse files Browse the repository at this point in the history
Part 9, byte voxels and bug fixes!
  • Loading branch information
pixelreyn authored Jun 8, 2022
2 parents ea05041 + b8aba9e commit 1a32f47
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 79 deletions.
10 changes: 5 additions & 5 deletions Assets/VoxelProjectSeries/Scenes/World.unity
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ MonoBehaviour:
ReplaceBlockInPlace: 0
toolMode: 1
toolType: 1
radiusToAffect: 4
radiusToAffect: 3
voxelIDToPlace: 0
--- !u!1 &1147823335
GameObject:
Expand Down Expand Up @@ -500,7 +500,7 @@ MonoBehaviour:
- texture: {fileID: 2800000, guid: 64b479ecc48ec9343aa615792b72a11d, type: 3}
color:
serializedVersion: 2
rgba: 5091361
rgba: 45584
metallic: 0.1
smoothness: 0.5
- texture: {fileID: 2800000, guid: cb06f60397b2585409fc1667d04de54a, type: 3}
Expand All @@ -515,7 +515,7 @@ MonoBehaviour:
rgba: 19058
metallic: 0.4
smoothness: 0.1
- texture: {fileID: 0}
- texture: {fileID: 2800000, guid: 0dc0649bf1b30484099e1017ce7953bc, type: 3}
color:
serializedVersion: 2
rgba: 55618
Expand All @@ -532,9 +532,9 @@ MonoBehaviour:
maxHeight: 256
renderDistance: 64
sharedVertices: 0
useTextures: 0
useTextures: 1
mainCamera: {fileID: 414495109}
maxChunksToProcessPerFrame: 8
maxChunksToProcessPerFrame: 6
--- !u!114 &1755072227
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
7 changes: 4 additions & 3 deletions Assets/VoxelProjectSeries/Scripts/Camera/TerrainInteractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class TerrainInteractor : MonoBehaviour
public ToolMode toolMode;
public ToolType toolType;
public int radiusToAffect = 2;
public int voxelIDToPlace = 4;
public byte voxelIDToPlace = 4;


void Update()
Expand Down Expand Up @@ -38,7 +38,7 @@ void Update()
if (blockPos.y < 0)
return;

float v = voxelIDToPlace == 240 ? 1 : 0;
byte v = (byte)(voxelIDToPlace == 240 ? 100 : 0);
if (toolType == ToolType.SingleBlock)
WorldManager.Instance.SetVoxelAtCoord(chunkPos, math.round(blockPos), new Voxel() { ID = voxelIDToPlace, ActiveValue = v });
else
Expand All @@ -50,7 +50,8 @@ void Update()
Vector3 modPos = math.round(blockPos + new Vector3(x, y, z));
if ((modPos.y < 0 && voxelIDToPlace != 0) || (modPos.y < 1 && voxelIDToPlace == 0))
continue;
WorldManager.Instance.SetVoxelAtCoord(chunkPos, blockPos + new Vector3(x, y, z), new Voxel() { ID = voxelIDToPlace, ActiveValue = v });

WorldManager.Instance.SetVoxelAtCoord(chunkPos, modPos, new Voxel() { ID = voxelIDToPlace, ActiveValue = v });

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@

#include "Noise/SimplexNoise.compute"
#include "Noise/HashNoise.compute"

struct Voxel {
int ID;
float ActiveValue;
int CantUpdateFurther;
};
#include "Voxel.compute"

struct NoiseLayer {
float gain;
Expand Down Expand Up @@ -112,8 +107,8 @@ void FillArray(uint3 id : SV_DispatchThreadID)

float3 pos = id + chunkPosition + seedOffset;
float2 posXZ = pos.xz;

if (voxelArray[flattenCoord(id)].ID != 0)
if (GetVoxelData(0, voxelArray[flattenCoord(id)].VoxelData) != 0)
return;

uint terrainHeight;
Expand All @@ -122,41 +117,41 @@ void FillArray(uint3 id : SV_DispatchThreadID)
NoiseLayer selectednoise = noiseArray[noiseIndex];

Voxel vox;
vox.ID = 0;
vox.ActiveValue = 0;
vox.CantUpdateFurther = 1;
vox.VoxelData = 0;
int voxelId = 0;

if (id.y > terrainHeight)
{
vox.ID = 0;
voxelId = 0;

if (vox.ID == 0 && id.y <= 2)
if (voxelId == 0 && id.y <= 2)
{
vox.ID = 240;
voxelId = 240;
InterlockedAdd(count[0], 1);
}

UpdateVoxelData(0, voxelId, vox.VoxelData);
voxelArray[flattenCoord(id)] = vox;
return;
}

bool isSurfaceBlock = id.y >= terrainHeight - 3;

vox.ID = isSurfaceBlock ? selectednoise.surfaceVoxelId : selectednoise.subSurfaceVoxelId;
voxelId = isSurfaceBlock ? selectednoise.surfaceVoxelId : selectednoise.subSurfaceVoxelId;

if (generateCaves && evaluateNoise(pos, terrainHeight) > selectednoise.caveThreshold)
{
vox.ID = 0;
voxelId = 0;
}

if (id.y <= 1 && forceFloor)
vox.ID = selectednoise.surfaceVoxelId;
voxelId = selectednoise.surfaceVoxelId;


if (vox.ID != 0)
if (voxelId != 0)
InterlockedAdd(count[0], 1);

if (id.y == terrainHeight && vox.ID != 0 && id.y < 240)
if (id.y == terrainHeight && voxelId != 0 && id.y < 240)
{
bool placeFoliage = noise(float3(posXZ * 2, seed)) > 0.999;

Expand Down Expand Up @@ -191,16 +186,15 @@ void FillArray(uint3 id : SV_DispatchThreadID)
}
}
}


UpdateVoxelData(0, voxelId, vox.VoxelData);
voxelArray[flattenCoord(id)] = vox;
}

[numthreads(8, 8, 8)]
void ClearArray(uint3 id : SV_DispatchThreadID)
{
Voxel emptyVoxel;
emptyVoxel.ID = 0;
emptyVoxel.ActiveValue = 0;
emptyVoxel.CantUpdateFurther = 1;
emptyVoxel.VoxelData = 0;
voxelArray[flattenCoord(id)] = emptyVoxel;
}
22 changes: 22 additions & 0 deletions Assets/VoxelProjectSeries/Scripts/ComputeShaders/Voxel.compute
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
struct Voxel
{
int VoxelData;
};

struct VoxelDetails
{
float color;
float metallic;
float smoothness;
};

uint GetVoxelData(uint component, int voxelData)
{
uint shift = (8 * component);
return ((voxelData & (0xff << shift)) >> shift);
}

void UpdateVoxelData(int component, int value, inout uint voxelData)
{
voxelData |= value << (8 * component);
}

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

Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
#include "VoxelValues.cginc"

struct VoxelDetails
{
float color;
float metallic;
float smoothness;
};

struct Voxel {
int ID;
float ActiveValue;
int CantUpdateFurther;
};
#include "Voxel.compute"

struct sharedVert
{
Expand Down Expand Up @@ -63,12 +51,12 @@ uint flattenCoord(uint3 idx)

bool VoxelIsSolid(uint3 pos)
{
return voxelArray[flattenCoord(pos)].ID != 0;
return GetVoxelData(0, voxelArray[flattenCoord(pos)].VoxelData) != 0;
}

bool VoxelIsTransparent(uint3 pos)
{
return voxelArray[flattenCoord(pos)].ID == 240;
return GetVoxelData(0, voxelArray[flattenCoord(pos)].VoxelData) == 240;
}

static uint3 getCellCenterForIDX(int idx, int axis, int corner)
Expand Down Expand Up @@ -98,6 +86,7 @@ void calculateContour(uint3 blockCoord, out float3 position, out float3 normal,
uint3 vWorldPos = blockCoord + vertexPos; //take local block coord, add the cubed position
int counter = 0;
currentCell = voxelArray[flattenCoord(blockCoord)];
int currentCellId = GetVoxelData(0, currentCell.VoxelData);

for (int ax = 0; ax < 3; ax++)
{
Expand All @@ -110,17 +99,17 @@ void calculateContour(uint3 blockCoord, out float3 position, out float3 normal,
adjacentRootCell = voxelArray[flattenCoord(adjacentCellRoot)];
adjacentCell = voxelArray[flattenCoord(adjacentCellPos)];

adjacentRootCellId = adjacentRootCell.ID;
adjacentCellId = adjacentCell.ID;
adjacentRootCellId = GetVoxelData(0, adjacentRootCell.VoxelData);
adjacentCellId = GetVoxelData(0, adjacentCell.VoxelData);

if (adjacentRootCell.ID == 240 && currentCell.ID != 240)
if (adjacentRootCellId == 240 && currentCellId != 240)
adjacentRootCellId = 0;
if (adjacentCell.ID == 240 && currentCell.ID != 240)
if (adjacentCellId == 240 && currentCellId != 240)
adjacentCellId = 0;

if (currentCell.ID == 240 && adjacentRootCell.ID != 240)
if (currentCellId == 240 && adjacentRootCellId != 240)
adjacentRootCellId = 0;
if (currentCell.ID == 240 && adjacentCell.ID != 240)
if (currentCellId == 240 && adjacentCellId != 240)
adjacentCellId = 0;

adjacentRootCellDensity = adjacentRootCellId != 0 ? 1 : 0;
Expand Down Expand Up @@ -156,15 +145,16 @@ void CSMain(uint3 id : SV_DispatchThreadID)
return;

Voxel block = voxelArray[flattenCoord(id)];
int blockID = GetVoxelData(0, block.VoxelData);

if (block.ID == 0)
if (blockID == 0)
return;

float3 faceVertices[4];
float3 faceNorms[4];
float4 color = float4(block.ID == 240 ? 240 :
(useTextures && voxelColors[block.ID - 1].color == -1 ? ((float) block.ID - 1) : voxelColors[block.ID - 1].color),
packFloats(voxelColors[block.ID - 1].metallic, voxelColors[block.ID - 1].smoothness), 0, 0);
float4 color = float4(blockID == 240 ? 240 :
(useTextures && voxelColors[blockID - 1].color == -1 ? ((float) blockID - 1) : voxelColors[blockID - 1].color),
packFloats(voxelColors[blockID - 1].metallic, voxelColors[blockID - 1].smoothness), 0, 0);
uint vertCount = 0;
uint triCount = 0;

Expand All @@ -181,15 +171,15 @@ void CSMain(uint3 id : SV_DispatchThreadID)
if (VoxelIsSolid(id + voxelFaceChecks[i]) && !VoxelIsTransparent(id + voxelFaceChecks[i]))
continue;

if (block.ID == 240 && VoxelIsTransparent(id + voxelFaceChecks[i]))
if (blockID == 240 && VoxelIsTransparent(id + voxelFaceChecks[i]))
continue;

//Draw this face
//Collect the appropriate vertices from the default vertices and add the block position
if (!sharedVertices)
{
InterlockedAdd(counter[0], 6, vertCount);
if (block.ID != 240)
if (blockID != 240)
InterlockedAdd(counter[1], 6, triCount);
else
InterlockedAdd(counter[2], 6, triCount);
Expand All @@ -206,15 +196,15 @@ void CSMain(uint3 id : SV_DispatchThreadID)
color.b = voxelUVs[voxelTris[i][k]].x;
color.a = voxelUVs[voxelTris[i][k]].y;
colorBuffer[vertCount + k] = color;
if (block.ID != 240)
if (blockID != 240)
indexBuffer[triCount + k] = vertCount + k;
else
transparentIndexBuffer[triCount + k] = vertCount + k;
}
}
else
{
if (block.ID != 240)
if (blockID != 240)
InterlockedAdd(counter[1], 6, triCount);
else
InterlockedAdd(counter[2], 6, triCount);
Expand All @@ -239,7 +229,7 @@ void CSMain(uint3 id : SV_DispatchThreadID)
colorBuffer[vertCount] = color;
}

if (block.ID != 240)
if (blockID != 240)
indexBuffer[triCount + k] = sharedVerts[idx].index;
else
transparentIndexBuffer[triCount + k] = sharedVerts[idx].index;
Expand Down
54 changes: 51 additions & 3 deletions Assets/VoxelProjectSeries/Scripts/Data/Voxel.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public struct Voxel
{
public int ID;
public float ActiveValue;
public int CantUpdateFurther;
public int voxelData;

public byte ID
{
get
{
return GetVoxelData(0);
}
set
{
UpdateVoxelData(0, value);
}
}

public byte ActiveValue
{
get
{
return GetVoxelData(1);
}
set
{
UpdateVoxelData(1, value);
}
}
//From root coord up - technically a range of 0-100
public byte terrainHeight
{
get
{
return GetVoxelData(2);
}
set
{
UpdateVoxelData(2, value);
}
}



byte GetVoxelData(byte component)
{
byte shift = (byte)(8 * component);
return (byte)((voxelData & (0xff << shift)) >> shift);
}

void UpdateVoxelData(int component, byte value)
{
voxelData |= value << (8 * component);
}

public bool isSolid
{
Expand Down
Loading

0 comments on commit 1a32f47

Please sign in to comment.