Skip to content

Commit

Permalink
Uploading Part2 - The first chunk - adding a dictioinary and a full loop
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelreyn committed Mar 10, 2022
1 parent 4bb360c commit 11726ef
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Assets/VoxelProjectSeries/Data.meta

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

98 changes: 75 additions & 23 deletions Assets/VoxelProjectSeries/Data/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
using Unity.Mathematics;
using UnityEngine;

namespace PixelReyn.VoxelSeries.Part1
namespace PixelReyn.VoxelSeries.Part2
{

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class Container : MonoBehaviour
{
public Vector3 containerPosition;

private Dictionary<Vector3, Voxel> data;
private MeshData meshData = new MeshData();

private MeshRenderer meshRenderer;
Expand All @@ -21,41 +23,64 @@ public class Container : MonoBehaviour
public void Initialize(Material mat, Vector3 position)
{
ConfigureComponents();
data = new Dictionary<Vector3, Voxel>();
meshRenderer.sharedMaterial = mat;
containerPosition = position;
}

public void ClearData()
{
data.Clear();
}

private void ConfigureComponents()
{
meshFilter = GetComponent<MeshFilter>();
meshRenderer = GetComponent<MeshRenderer>();
meshCollider = GetComponent<MeshCollider>();
}

public void GenerateMesh()
{
meshData.ClearData();

Vector3 blockPos = new Vector3(8,8,8);
Voxel block = new Voxel() { ID = 1 };
Vector3 blockPos;
Voxel block;

int counter = 0;
Vector3[] faceVertices = new Vector3[4];
Vector2[] faceUVs = new Vector2[4];

//Iterate over each face direction
for (int i = 0; i < 6; i++)
foreach (KeyValuePair<Vector3, Voxel> kvp in data)
{
if (!kvp.Value.isSolid)
continue;

//Draw this face
blockPos = kvp.Key;
block = kvp.Value;

//Collect the appropriate vertices from the default vertices and add the block position
for (int j = 0; j < 4; j++)
//Iterate over each face direction
for (int i = 0; i < 6; i++)
{
faceVertices[j] = voxelVertices[voxelVertexIndex[i, j]] + blockPos;
faceUVs[j] = voxelUVs[j];
}
if (this[blockPos + voxelFaceChecks[i]].isSolid)
continue;

for (int j = 0; j < 6; j++)
{
meshData.vertices.Add(faceVertices[voxelTris[i, j]]);
meshData.UVs.Add(faceUVs[voxelTris[i, j]]);
//Draw this face

meshData.triangles.Add(counter++);
//Collect the appropriate vertices from the default vertices and add the block position
for (int j = 0; j < 4; j++)
{
faceVertices[j] = voxelVertices[voxelVertexIndex[i, j]] + blockPos;
faceUVs[j] = voxelUVs[j];
}

for (int j = 0; j < 6; j++)
{
meshData.vertices.Add(faceVertices[voxelTris[i, j]]);
meshData.UVs.Add(faceUVs[voxelTris[i, j]]);

meshData.triangles.Add(counter++);
}
}
}
}
Expand All @@ -68,20 +93,35 @@ public void UploadMesh()
ConfigureComponents();

meshFilter.mesh = meshData.mesh;

if (meshData.vertices.Count > 3)
meshCollider.sharedMesh = meshData.mesh;

}

private void ConfigureComponents()
public Voxel this[Vector3 index]
{
meshFilter = GetComponent<MeshFilter>();
meshRenderer = GetComponent<MeshRenderer>();
meshCollider = GetComponent<MeshCollider>();
get
{
if (data.ContainsKey(index))
return data[index];
else
return emptyVoxel;
}

set
{
if (data.ContainsKey(index))
data[index] = value;
else
data.Add(index, value);
}
}

public static Voxel emptyVoxel = new Voxel() { ID = 0 };

#region Mesh Data

public struct MeshData
{
public Mesh mesh;
Expand All @@ -107,13 +147,16 @@ public void ClearData()
vertices.Clear();
triangles.Clear();
UVs.Clear();

mesh.Clear();
}
}

public void UploadMesh(bool sharedVertices = false)
{
mesh.SetVertices(vertices);
mesh.SetTriangles(triangles, 0, false);

mesh.SetUVs(0, UVs);

mesh.Optimize();
Expand All @@ -125,10 +168,10 @@ public void UploadMesh(bool sharedVertices = false)
mesh.UploadMeshData(false);
}
}

#endregion

#region Static Variables

#region Voxel Statics

static readonly Vector3[] voxelVertices = new Vector3[8]
{
Expand All @@ -142,6 +185,15 @@ public void UploadMesh(bool sharedVertices = false)
new Vector3(0,1,1),//6
new Vector3(1,1,1),//7
};
static readonly Vector3[] voxelFaceChecks = new Vector3[6]
{
new Vector3(0,0,-1),//back
new Vector3(0,0,1),//front
new Vector3(-1,0,0),//left
new Vector3(1,0,0),//right
new Vector3(0,-1,0),//bottom
new Vector3(0,1,0)//top
};

static readonly int[,] voxelVertexIndex = new int[6, 4]
{
Expand Down Expand Up @@ -170,7 +222,7 @@ public void UploadMesh(bool sharedVertices = false)
{0,1,2,1,3,2},
{0,2,3,0,3,1},
};

#endregion
}

}
2 changes: 1 addition & 1 deletion Assets/VoxelProjectSeries/Data/Container.cs.meta

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

11 changes: 10 additions & 1 deletion Assets/VoxelProjectSeries/Data/Voxel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace PixelReyn.VoxelSeries.Part1
namespace PixelReyn.VoxelSeries.Part2
{
public struct Voxel
{
//Convert to single float and access via accessors...
public byte ID;

public bool isSolid
{
get
{
return ID != 0;
}
}
}
}
2 changes: 1 addition & 1 deletion Assets/VoxelProjectSeries/Data/Voxel.cs.meta

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

2 changes: 1 addition & 1 deletion Assets/VoxelProjectSeries/Managers.meta

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

25 changes: 24 additions & 1 deletion Assets/VoxelProjectSeries/Managers/WorldManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,45 @@
using System.Collections.Generic;
using UnityEngine;

namespace PixelReyn.VoxelSeries.Part1
namespace PixelReyn.VoxelSeries.Part2
{
public class WorldManager : MonoBehaviour
{
public Material worldMaterial;

private Container container;


// Start is called before the first frame update
void Start()
{
GameObject cont = new GameObject("Container");
cont.transform.parent = transform;
container = cont.AddComponent<Container>();
container.Initialize(worldMaterial, Vector3.zero);

for (int x = 0; x < 16; x++)
{
for (int z = 0; z < 16; z++)
{
int randomYHeight = Random.Range(1, 16);
for (int y = 0; y < randomYHeight; y++)
{
container[new Vector3(x, y, z)] = new Voxel() { ID = 1 };
}
}
}


container.GenerateMesh();
container.UploadMesh();
}

// Update is called once per frame
void Update()
{

}

}
}
2 changes: 1 addition & 1 deletion Assets/VoxelProjectSeries/Managers/WorldManager.cs.meta

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

2 changes: 1 addition & 1 deletion Assets/VoxelProjectSeries/Scenes.meta

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

4 changes: 2 additions & 2 deletions Assets/VoxelProjectSeries/Scenes/World.unity
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ MonoBehaviour:
m_GameObject: {fileID: 1755072223}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3c345a3c44c9e814bb904e47c1370130, type: 3}
m_Script: {fileID: 11500000, guid: 35997360c8b74f543b623a3524b58bc9, type: 3}
m_Name:
m_EditorClassIdentifier:
worldMaterial: {fileID: 2100000, guid: 1b6cee3e086ad8f45aecce1d131054e0, type: 2}
worldMaterial: {fileID: 2100000, guid: 4021f579d8ff00b4ca1a8c7d355362b3, type: 2}
2 changes: 1 addition & 1 deletion Assets/VoxelProjectSeries/Scenes/World.unity.meta

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

1 change: 1 addition & 0 deletions Assets/VoxelProjectSeries/WorldMaterial.mat
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ Material:
- _Metallic: 0
- _OcclusionStrength: 1
- _Parallax: 0.005
- _QueueControl: 0
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Smoothness: 0
Expand Down
2 changes: 1 addition & 1 deletion Assets/VoxelProjectSeries/WorldMaterial.mat.meta

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

0 comments on commit 11726ef

Please sign in to comment.