Skip to content

Commit

Permalink
Part 8, water, active water, and terrain manipulation is here!
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelreyn committed Apr 19, 2022
1 parent 07870a6 commit 1d20320
Show file tree
Hide file tree
Showing 16 changed files with 3,294 additions and 46 deletions.
24 changes: 22 additions & 2 deletions Assets/VoxelProjectSeries/Scenes/World.unity
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ GameObject:
- component: {fileID: 414495108}
- component: {fileID: 414495107}
- component: {fileID: 414495110}
- component: {fileID: 414495112}
- component: {fileID: 414495111}
m_Layer: 0
m_Name: Main Camera
Expand Down Expand Up @@ -258,6 +259,23 @@ MonoBehaviour:
freeLookSensitivity: 3
zoomSensitivity: 10
fastZoomSensitivity: 50
--- !u!114 &414495112
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 414495106}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fa0594ff7769e8f44bb15eb5bf3f67db, type: 3}
m_Name:
m_EditorClassIdentifier:
ReplaceBlockInPlace: 0
toolMode: 1
toolType: 1
radiusToAffect: 4
voxelIDToPlace: 0
--- !u!1 &1147823335
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -475,7 +493,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: e0e65ef4e51bde8429033f2ff79c2aa4, type: 3}
m_Name:
m_EditorClassIdentifier:
worldMaterial: {fileID: 2100000, guid: d41578d92bc26b04bb2355aed476d2cb, type: 2}
worldMaterials:
- {fileID: 2100000, guid: d41578d92bc26b04bb2355aed476d2cb, type: 2}
- {fileID: 2100000, guid: 313f0b3da4a8a8b4ea348a14fbeb4548, type: 2}
voxelDetails:
- texture: {fileID: 2800000, guid: 64b479ecc48ec9343aa615792b72a11d, type: 3}
color:
Expand Down Expand Up @@ -512,7 +532,7 @@ MonoBehaviour:
maxHeight: 256
renderDistance: 64
sharedVertices: 0
useTextures: 1
useTextures: 0
mainCamera: {fileID: 414495109}
maxChunksToProcessPerFrame: 8
--- !u!114 &1755072227
Expand Down
124 changes: 124 additions & 0 deletions Assets/VoxelProjectSeries/Scripts/Camera/TerrainInteractor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

public class TerrainInteractor : MonoBehaviour
{
public bool ReplaceBlockInPlace = false;
public ToolMode toolMode;
public ToolType toolType;
public int radiusToAffect = 2;
public int voxelIDToPlace = 4;


void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
ReplaceBlockInPlace = !ReplaceBlockInPlace;
}
if (Input.GetKeyDown(KeyCode.Tab))
{
int indexOf = (int)toolMode;
toolMode = indexOf == 1 ? ToolMode.Single : ToolMode.Continuous;
}
if (Input.GetKeyDown(KeyCode.M))
{
toolMode = ToolMode.Continuous;
toolType = ToolType.SingleBlock;
voxelIDToPlace = 240;
}

if (toolMode == ToolMode.Single ? Input.GetMouseButtonDown(1) : Input.GetMouseButton(1))
{
//Spawn water as demo
if (GetBlockCoordAtRay(out Vector3 chunkPos, out Vector3 blockPos))
{
if (blockPos.y < 0)
return;

float v = voxelIDToPlace == 240 ? 1 : 0;
if (toolType == ToolType.SingleBlock)
WorldManager.Instance.SetVoxelAtCoord(chunkPos, math.round(blockPos), new Voxel() { ID = voxelIDToPlace, ActiveValue = v });
else
{
for (int x = -radiusToAffect; x < radiusToAffect; x++)
for (int y = -radiusToAffect; y < radiusToAffect; y++)
for (int z = -radiusToAffect; z < radiusToAffect; z++)
{
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 });

}
}
}

}
}

bool GetBlockCoordAtRay(out Vector3 ChunkPos, out Vector3 blockPos)
{
if (Physics.Raycast(new Ray(transform.position, transform.forward), out var hitInfo))
{
if (hitInfo.collider.transform.GetComponent<Chunk>() != null)
{
ChunkPos = hitInfo.collider.transform.GetComponent<Chunk>().chunkPosition;
blockPos = math.floor(hitInfo.point - ChunkPos);
if (hitInfo.normal.x != 0)
{
blockPos.x = math.round(blockPos.x);
}
else
{
blockPos.x = math.floor(blockPos.x);
}
if (hitInfo.normal.y != 0)
{
blockPos.y = math.round(blockPos.y);
}
else
{
blockPos.y = math.floor(blockPos.y);
}
if (hitInfo.normal.z != 0)
{
blockPos.z = math.round(blockPos.z);
}
else
{
blockPos.z = math.floor(blockPos.z);
}

if (ReplaceBlockInPlace)
{
if (hitInfo.normal.x > 0 || hitInfo.normal.y > 0 || hitInfo.normal.z > 0)
blockPos -= hitInfo.normal;
}
else
{

if (hitInfo.normal.x < 0 || hitInfo.normal.y < 0 || hitInfo.normal.z < 0)
blockPos += hitInfo.normal;
}
return true;
}
}
ChunkPos = Vector3.zero;
blockPos = Vector3.zero;
return false;
}

public enum ToolMode
{
Single,
Continuous
}
public enum ToolType
{
SingleBlock,
Radius
}
}
11 changes: 11 additions & 0 deletions Assets/VoxelProjectSeries/Scripts/Camera/TerrainInteractor.cs.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 @@ -7,6 +7,8 @@

struct Voxel {
int ID;
float ActiveValue;
int CantUpdateFurther;
};

struct NoiseLayer {
Expand Down Expand Up @@ -121,10 +123,19 @@ void FillArray(uint3 id : SV_DispatchThreadID)

Voxel vox;
vox.ID = 0;
vox.ActiveValue = 0;
vox.CantUpdateFurther = 1;

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

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

voxelArray[flattenCoord(id)] = vox;
return;
}
Expand Down Expand Up @@ -189,5 +200,7 @@ void ClearArray(uint3 id : SV_DispatchThreadID)
{
Voxel emptyVoxel;
emptyVoxel.ID = 0;
emptyVoxel.ActiveValue = 0;
emptyVoxel.CantUpdateFurther = 1;
voxelArray[flattenCoord(id)] = emptyVoxel;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ struct VoxelDetails

struct Voxel {
int ID;
float ActiveValue;
int CantUpdateFurther;
};

struct sharedVert
Expand All @@ -25,6 +27,7 @@ RWStructuredBuffer<float3> vertexBuffer;
RWStructuredBuffer<float3> normalBuffer;
RWStructuredBuffer<float4> colorBuffer;
RWStructuredBuffer<int> indexBuffer;
RWStructuredBuffer<int> transparentIndexBuffer;
RWStructuredBuffer<uint> counter;

float3 chunkPosition;
Expand Down Expand Up @@ -63,6 +66,11 @@ bool VoxelIsSolid(uint3 pos)
return voxelArray[flattenCoord(pos)].ID != 0;
}

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

static uint3 getCellCenterForIDX(int idx, int axis, int corner)
{
uint3 cellCenter = CellCentersByAxis[axis][corner];
Expand All @@ -85,6 +93,8 @@ void calculateContour(uint3 blockCoord, out float3 position, out float3 normal,
float3 adjacentCellPos;
float3 adjacentCellRoot;
float3 liveCellPos;
int adjacentRootCellId;
int adjacentCellId;
uint3 vWorldPos = blockCoord + vertexPos; //take local block coord, add the cubed position
int counter = 0;
currentCell = voxelArray[flattenCoord(blockCoord)];
Expand All @@ -99,16 +109,28 @@ void calculateContour(uint3 blockCoord, out float3 position, out float3 normal,

adjacentRootCell = voxelArray[flattenCoord(adjacentCellRoot)];
adjacentCell = voxelArray[flattenCoord(adjacentCellPos)];

adjacentRootCellId = adjacentRootCell.ID;
adjacentCellId = adjacentCell.ID;

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

adjacentRootCellDensity = adjacentRootCell.ID != 0 ? 1 : 0;
adjacentCellDensitiy = adjacentCell.ID != 0 ? 1 : 0;
if (currentCell.ID == 240 && adjacentRootCell.ID != 240)
adjacentRootCellId = 0;
if (currentCell.ID == 240 && adjacentCell.ID != 240)
adjacentCellId = 0;

adjacentRootCellDensity = adjacentRootCellId != 0 ? 1 : 0;
adjacentCellDensitiy = adjacentCellId != 0 ? 1 : 0;

normal += (adjacentCellDensitiy - adjacentRootCellDensity) * -axis[idx][ax];
if (adjacentRootCellDensity != adjacentCellDensitiy)
{
liveCellPos = adjacentCell.ID != 0 ? adjacentCellPos : adjacentCellRoot;
adjacentCellPos = adjacentCell.ID == 0 ? adjacentCellPos : adjacentCellRoot;
liveCellPos = adjacentCellId != 0 ? adjacentCellPos : adjacentCellRoot;
adjacentCellPos = adjacentCellId == 0 ? adjacentCellPos : adjacentCellRoot;

position += adjacentCellPos + ((liveCellPos - adjacentCellPos) * getCellCenterForIDX(idx, ax, corner)) / 2; //lerp(adjacentCellRoot + ((float3) (adjacentCellPos - adjacentCellRoot) * ((0 - adjacentRootCellDensity) / (adjacentCellDensitiy - adjacentRootCellDensity))), vWorldPos, 0); //change 0 to byte value
counter++;
Expand Down Expand Up @@ -140,8 +162,9 @@ void CSMain(uint3 id : SV_DispatchThreadID)

float3 faceVertices[4];
float3 faceNorms[4];
float4 color = float4(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(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);
uint vertCount = 0;
uint triCount = 0;

Expand All @@ -155,15 +178,21 @@ void CSMain(uint3 id : SV_DispatchThreadID)
{

//Check if there's a solid block against this face
if (VoxelIsSolid(id + voxelFaceChecks[i]))
if (VoxelIsSolid(id + voxelFaceChecks[i]) && !VoxelIsTransparent(id + voxelFaceChecks[i]))
continue;

if (block.ID == 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);
InterlockedAdd(counter[1], 6, triCount);
if (block.ID != 240)
InterlockedAdd(counter[1], 6, triCount);
else
InterlockedAdd(counter[2], 6, triCount);

for (int j = 0; j < 4; j++)
{
Expand All @@ -177,12 +206,18 @@ void CSMain(uint3 id : SV_DispatchThreadID)
color.b = voxelUVs[voxelTris[i][k]].x;
color.a = voxelUVs[voxelTris[i][k]].y;
colorBuffer[vertCount + k] = color;
indexBuffer[triCount + k] = vertCount + k;
if (block.ID != 240)
indexBuffer[triCount + k] = vertCount + k;
else
transparentIndexBuffer[triCount + k] = vertCount + k;
}
}
else
{
InterlockedAdd(counter[1], 6, triCount);
if (block.ID != 240)
InterlockedAdd(counter[1], 6, triCount);
else
InterlockedAdd(counter[2], 6, triCount);
for (int k = 0; k < 6; k++)
{
int idx = voxelTrisMapped[i][k];
Expand All @@ -204,7 +239,10 @@ void CSMain(uint3 id : SV_DispatchThreadID)
colorBuffer[vertCount] = color;
}

indexBuffer[triCount + k] = sharedVerts[idx].index;
if (block.ID != 240)
indexBuffer[triCount + k] = sharedVerts[idx].index;
else
transparentIndexBuffer[triCount + k] = sharedVerts[idx].index;
}

}
Expand Down
Loading

0 comments on commit 1d20320

Please sign in to comment.