Skip to content

Commit

Permalink
new hotness
Browse files Browse the repository at this point in the history
  • Loading branch information
artnas committed Apr 14, 2023
1 parent 4cfe5c2 commit bdb7319
Show file tree
Hide file tree
Showing 33 changed files with 2,220 additions and 204 deletions.
31 changes: 1 addition & 30 deletions Assets/Compute/feedback.compute
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
#pragma kernel CSMain

#define CHUNK_SIZE 80

struct GeometryData {
uint vertexCount;
uint indexCount;
};

struct ChunkFeedback {
uint vertexOffset;
uint vertexCount;
uint indexOffset;
uint indexCount;
};
#include "voxels.hlsl"

RWStructuredBuffer<int> uVoxels;
RWStructuredBuffer<GeometryData> uFeedback;
Expand All @@ -21,22 +8,6 @@ RWStructuredBuffer<ChunkFeedback> uChunkFeedback;
groupshared uint sVertexCount;
groupshared uint sIndexCount;

uint to1D(uint3 pos) {
return pos.x + CHUNK_SIZE * (pos.y + CHUNK_SIZE * pos.z);
}

int to1D(int3 pos) {
return pos.x + CHUNK_SIZE * (pos.y + CHUNK_SIZE * pos.z);
}

uint3 to3D(uint idx) {
uint x = idx % CHUNK_SIZE;
uint y = (idx / CHUNK_SIZE) % CHUNK_SIZE;
uint z = idx / (CHUNK_SIZE * CHUNK_SIZE);

return uint3(x, y, z);
}

bool hasVoxel(int3 coord) {
if (coord.x < 0 || coord.x >= CHUNK_SIZE ||
coord.y < 0 || coord.y >= CHUNK_SIZE ||
Expand Down
28 changes: 28 additions & 0 deletions Assets/Compute/generate_voxels.compute
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma kernel CSMain
#include "voxels.hlsl"
#include "Library\PackageCache\[email protected]/Shader/ClassicNoise3D.hlsl"

RWStructuredBuffer<int> uVoxels;

float3 uPosition = float3(0, 0, 0);
float uFrequency = 0.1f;
float uAmplitude = 0.5f;

[numthreads(8, 8, 8)]
void CSMain (uint3 dispatchID : SV_DispatchThreadID)
{
const uint globalVoxelIndex = to1D(dispatchID);

int3 coord = int3(dispatchID);
float3 position = float3(coord) + uPosition;

float noise = ClassicNoise(position * uFrequency) * uAmplitude;

if (noise > 0)
{
uVoxels[globalVoxelIndex] = 1;
} else
{
uVoxels[globalVoxelIndex] = 0;
}
}
3 changes: 3 additions & 0 deletions Assets/Compute/generate_voxels.compute.meta

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

38 changes: 1 addition & 37 deletions Assets/Compute/voxelizer.compute
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
#pragma kernel CSMain

#define CHUNK_SIZE 80

struct Vertex {
float3 position;
float2 texcoord;
float3 normal;
};

struct GeometryData {
uint vertexCount;
uint indexCount;
};

struct ChunkFeedback {
uint vertexOffset;
uint vertexCount;
uint indexOffset;
uint indexCount;
};
#include "voxels.hlsl"

RWStructuredBuffer<Vertex> uVertices;
RWStructuredBuffer<uint> uIndices;
StructuredBuffer<int> uVoxels;
// StructuredBuffer<GeometryData> uFeedback;
StructuredBuffer<ChunkFeedback> uChunkFeedback;

groupshared uint sVertexOffset;
Expand All @@ -32,21 +12,6 @@ groupshared uint sIndexOffset;
groupshared uint sIndexCount;
groupshared uint sChunkIndex;

uint to1D(uint3 pos) {
return pos.x + CHUNK_SIZE * (pos.y + CHUNK_SIZE * pos.z);
}

int to1D(int3 pos) {
return pos.x + CHUNK_SIZE * (pos.y + CHUNK_SIZE * pos.z);
}

uint3 to3D(uint idx) {
uint x = idx % CHUNK_SIZE;
uint y = (idx / CHUNK_SIZE) % CHUNK_SIZE;
uint z = idx / (CHUNK_SIZE * CHUNK_SIZE);
return uint3(x, y, z);
}

bool hasVoxel(int3 coord) {
if (coord.x < 0 || coord.x >= CHUNK_SIZE ||
coord.y < 0 || coord.y >= CHUNK_SIZE ||
Expand All @@ -68,7 +33,6 @@ void setVertex(out Vertex vertex, in float3 position, in float2 texcoord, in flo
void CSMain (uint3 dispatchID : SV_DispatchThreadID, uint3 groupID : SV_GroupID, uint3 groupThreadID : SV_GroupThreadID)
{
if (groupThreadID.x == 0 && groupThreadID.y == 0 && groupThreadID.z == 0) {
// sChunkIndex = uChunkIndices.data[gl_WorkGroupID.x + 10 * (gl_WorkGroupID.y + 10 * gl_WorkGroupID.z)];
sChunkIndex = groupID.x + 10 * (groupID.y + 10 * groupID.z);
sVertexOffset = uChunkFeedback[sChunkIndex].vertexOffset;
sIndexOffset = uChunkFeedback[sChunkIndex].indexOffset;
Expand Down
35 changes: 35 additions & 0 deletions Assets/Compute/voxels.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#define CHUNK_SIZE 80

struct Vertex {
float3 position;
float2 texcoord;
float3 normal;
};

struct GeometryData {
uint vertexCount;
uint indexCount;
};

struct ChunkFeedback {
uint vertexOffset;
uint vertexCount;
uint indexOffset;
uint indexCount;
};

uint to1D(uint3 pos) {
return pos.x + CHUNK_SIZE * (pos.y + CHUNK_SIZE * pos.z);
}

int to1D(int3 pos) {
return pos.x + CHUNK_SIZE * (pos.y + CHUNK_SIZE * pos.z);
}

uint3 to3D(uint idx) {
uint x = idx % CHUNK_SIZE;
uint y = (idx / CHUNK_SIZE) % CHUNK_SIZE;
uint z = idx / (CHUNK_SIZE * CHUNK_SIZE);

return uint3(x, y, z);
}
3 changes: 3 additions & 0 deletions Assets/Compute/voxels.hlsl.meta

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
Expand Up @@ -23,7 +23,7 @@ Material:
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Voxel
m_Name: Voxel Basic
m_Shader: {fileID: 4800000, guid: 6e4ae4064600d784cac1e41a9e6f2e59, type: 3}
m_ValidKeywords:
- _DISABLE_SSR_TRANSPARENT
Expand Down
File renamed without changes.
Loading

0 comments on commit bdb7319

Please sign in to comment.