diff --git a/Assets/VoxelProjectSeries/Data.meta b/Assets/VoxelProjectSeries/Data.meta index 9a0b751..8e39a89 100644 --- a/Assets/VoxelProjectSeries/Data.meta +++ b/Assets/VoxelProjectSeries/Data.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 1237c5e0e6ee826429fa86d205421d6b +guid: 6b9e7d241ee864a4abb1053544f45744 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/VoxelProjectSeries/Data/Container.cs b/Assets/VoxelProjectSeries/Data/Container.cs index d54a6ff..3d8891e 100644 --- a/Assets/VoxelProjectSeries/Data/Container.cs +++ b/Assets/VoxelProjectSeries/Data/Container.cs @@ -3,8 +3,9 @@ using Unity.Mathematics; using UnityEngine; -namespace PixelReyn.VoxelSeries.Part1 +namespace PixelReyn.VoxelSeries.Part2 { + [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] [RequireComponent(typeof(MeshCollider))] @@ -12,6 +13,7 @@ public class Container : MonoBehaviour { public Vector3 containerPosition; + private Dictionary data; private MeshData meshData = new MeshData(); private MeshRenderer meshRenderer; @@ -21,41 +23,64 @@ public class Container : MonoBehaviour public void Initialize(Material mat, Vector3 position) { ConfigureComponents(); + data = new Dictionary(); meshRenderer.sharedMaterial = mat; containerPosition = position; } + public void ClearData() + { + data.Clear(); + } + + private void ConfigureComponents() + { + meshFilter = GetComponent(); + meshRenderer = GetComponent(); + meshCollider = GetComponent(); + } + 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 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++); + } } } } @@ -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(); - meshRenderer = GetComponent(); - meshCollider = GetComponent(); + 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; @@ -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(); @@ -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] { @@ -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] { @@ -170,7 +222,7 @@ public void UploadMesh(bool sharedVertices = false) {0,1,2,1,3,2}, {0,2,3,0,3,1}, }; + #endregion } - } \ No newline at end of file diff --git a/Assets/VoxelProjectSeries/Data/Container.cs.meta b/Assets/VoxelProjectSeries/Data/Container.cs.meta index 80631d9..714713e 100644 --- a/Assets/VoxelProjectSeries/Data/Container.cs.meta +++ b/Assets/VoxelProjectSeries/Data/Container.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: e48c441a3312dfb4fa9c62e817bd0e17 +guid: 566e7a097454e3f4f997b29cc0b50209 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/VoxelProjectSeries/Data/Voxel.cs b/Assets/VoxelProjectSeries/Data/Voxel.cs index 1272c50..7f48680 100644 --- a/Assets/VoxelProjectSeries/Data/Voxel.cs +++ b/Assets/VoxelProjectSeries/Data/Voxel.cs @@ -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; + } + } } } \ No newline at end of file diff --git a/Assets/VoxelProjectSeries/Data/Voxel.cs.meta b/Assets/VoxelProjectSeries/Data/Voxel.cs.meta index 46ef2fd..c0755e5 100644 --- a/Assets/VoxelProjectSeries/Data/Voxel.cs.meta +++ b/Assets/VoxelProjectSeries/Data/Voxel.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 07eacd23e9139d4469cbdedb0df612a7 +guid: 77246b96a654cbd44aee5bca1c1d6024 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/VoxelProjectSeries/Managers.meta b/Assets/VoxelProjectSeries/Managers.meta index 5d065b2..6f4b8ef 100644 --- a/Assets/VoxelProjectSeries/Managers.meta +++ b/Assets/VoxelProjectSeries/Managers.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 9d703dfaa085a144aba25b789d0821b3 +guid: 7cd07b6081705c94080f75c1be172a4c folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/VoxelProjectSeries/Managers/WorldManager.cs b/Assets/VoxelProjectSeries/Managers/WorldManager.cs index 9e4dd1e..b943738 100644 --- a/Assets/VoxelProjectSeries/Managers/WorldManager.cs +++ b/Assets/VoxelProjectSeries/Managers/WorldManager.cs @@ -2,13 +2,16 @@ 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"); @@ -16,8 +19,28 @@ void Start() container = cont.AddComponent(); 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() + { + + } + } } \ No newline at end of file diff --git a/Assets/VoxelProjectSeries/Managers/WorldManager.cs.meta b/Assets/VoxelProjectSeries/Managers/WorldManager.cs.meta index 3b6eb72..44adf1b 100644 --- a/Assets/VoxelProjectSeries/Managers/WorldManager.cs.meta +++ b/Assets/VoxelProjectSeries/Managers/WorldManager.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 3c345a3c44c9e814bb904e47c1370130 +guid: 35997360c8b74f543b623a3524b58bc9 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/VoxelProjectSeries/Scenes.meta b/Assets/VoxelProjectSeries/Scenes.meta index 6c4af1d..1815672 100644 --- a/Assets/VoxelProjectSeries/Scenes.meta +++ b/Assets/VoxelProjectSeries/Scenes.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: b20be6f2bc89c3f43b3834a3ce604ec7 +guid: f2b6f7de0d300be4d9664d00dd95b496 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/VoxelProjectSeries/Scenes/World.unity b/Assets/VoxelProjectSeries/Scenes/World.unity index 961e694..3ae7c15 100644 --- a/Assets/VoxelProjectSeries/Scenes/World.unity +++ b/Assets/VoxelProjectSeries/Scenes/World.unity @@ -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} diff --git a/Assets/VoxelProjectSeries/Scenes/World.unity.meta b/Assets/VoxelProjectSeries/Scenes/World.unity.meta index 126a1e2..e1d7a7a 100644 --- a/Assets/VoxelProjectSeries/Scenes/World.unity.meta +++ b/Assets/VoxelProjectSeries/Scenes/World.unity.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 8c1a7ea1fa01c024ba904e35497363b1 +guid: 2eb08ffdbff9f8a44ba02b325147760b DefaultImporter: externalObjects: {} userData: diff --git a/Assets/VoxelProjectSeries/WorldMaterial.mat b/Assets/VoxelProjectSeries/WorldMaterial.mat index 14be214..8d3b208 100644 --- a/Assets/VoxelProjectSeries/WorldMaterial.mat +++ b/Assets/VoxelProjectSeries/WorldMaterial.mat @@ -95,6 +95,7 @@ Material: - _Metallic: 0 - _OcclusionStrength: 1 - _Parallax: 0.005 + - _QueueControl: 0 - _QueueOffset: 0 - _ReceiveShadows: 1 - _Smoothness: 0 diff --git a/Assets/VoxelProjectSeries/WorldMaterial.mat.meta b/Assets/VoxelProjectSeries/WorldMaterial.mat.meta index 1ca9c71..d16eb32 100644 --- a/Assets/VoxelProjectSeries/WorldMaterial.mat.meta +++ b/Assets/VoxelProjectSeries/WorldMaterial.mat.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 1b6cee3e086ad8f45aecce1d131054e0 +guid: 4021f579d8ff00b4ca1a8c7d355362b3 NativeFormatImporter: externalObjects: {} mainObjectFileID: 2100000