Skip to content

Commit

Permalink
Merge pull request #1 from pixelreyn/Part3-FirstSplashOfColor
Browse files Browse the repository at this point in the history
Part 3, adding vertex colors
  • Loading branch information
pixelreyn authored Mar 16, 2022
2 parents 11726ef + 6973b3d commit 5ff39e6
Show file tree
Hide file tree
Showing 18 changed files with 1,073 additions and 39 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.

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

namespace PixelReyn.VoxelSeries.Part2
namespace PixelReyn.VoxelSeries.Part3
{

[RequireComponent(typeof(MeshFilter))]
Expand Down Expand Up @@ -33,13 +33,6 @@ public void ClearData()
data.Clear();
}

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

public void GenerateMesh()
{
meshData.ClearData();
Expand All @@ -51,17 +44,27 @@ public void GenerateMesh()
Vector3[] faceVertices = new Vector3[4];
Vector2[] faceUVs = new Vector2[4];

VoxelColor voxelColor;
Color voxelColorAlpha;
Vector2 voxelSmoothness;

foreach (KeyValuePair<Vector3, Voxel> kvp in data)
{
//Only check on solid blocks
if (!kvp.Value.isSolid)
continue;

blockPos = kvp.Key;
block = kvp.Value;

voxelColor = WorldManager.Instance.WorldColors[block.ID - 1];
voxelColorAlpha = voxelColor.color;
voxelColorAlpha.a = 1;
voxelSmoothness = new Vector2(voxelColor.metallic, voxelColor.smoothness);
//Iterate over each face direction
for (int i = 0; i < 6; i++)
{
//Check if there's a solid block against this face
if (this[blockPos + voxelFaceChecks[i]].isSolid)
continue;

Expand All @@ -78,13 +81,19 @@ public void GenerateMesh()
{
meshData.vertices.Add(faceVertices[voxelTris[i, j]]);
meshData.UVs.Add(faceUVs[voxelTris[i, j]]);
meshData.colors.Add(voxelColorAlpha);
meshData.UVs2.Add(voxelSmoothness);

meshData.triangles.Add(counter++);

}
}

}
}



public void UploadMesh()
{
meshData.UploadMesh();
Expand All @@ -93,10 +102,15 @@ public void UploadMesh()
ConfigureComponents();

meshFilter.mesh = meshData.mesh;

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

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

public Voxel this[Vector3 index]
Expand Down Expand Up @@ -128,7 +142,8 @@ public struct MeshData
public List<Vector3> vertices;
public List<int> triangles;
public List<Vector2> UVs;

public List<Vector2> UVs2;
public List<Color> colors;
public bool Initialized;

public void ClearData()
Expand All @@ -138,6 +153,8 @@ public void ClearData()
vertices = new List<Vector3>();
triangles = new List<int>();
UVs = new List<Vector2>();
UVs2 = new List<Vector2>();
colors = new List<Color>();

Initialized = true;
mesh = new Mesh();
Expand All @@ -147,17 +164,20 @@ public void ClearData()
vertices.Clear();
triangles.Clear();
UVs.Clear();
UVs2.Clear();
colors.Clear();

mesh.Clear();
}
}

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

mesh.SetUVs(0, UVs);
mesh.SetUVs(2, UVs2);

mesh.Optimize();

Expand All @@ -170,9 +190,7 @@ public void UploadMesh(bool sharedVertices = false)
}
#endregion


#region Voxel Statics

#region Static Variables
static readonly Vector3[] voxelVertices = new Vector3[8]
{
new Vector3(0,0,0),//0
Expand All @@ -185,6 +203,7 @@ 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
Expand Down Expand Up @@ -222,7 +241,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.

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

public bool isSolid
Expand Down
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.

14 changes: 14 additions & 0 deletions Assets/VoxelProjectSeries/Data/VoxelColor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PixelReyn.VoxelSeries.Part3
{
[System.Serializable]
public class VoxelColor
{
public Color color;
public float metallic;
public float smoothness;
}
}
11 changes: 11 additions & 0 deletions Assets/VoxelProjectSeries/Data/VoxelColor.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.

30 changes: 21 additions & 9 deletions Assets/VoxelProjectSeries/Managers/WorldManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@
using System.Collections.Generic;
using UnityEngine;

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

public VoxelColor[] WorldColors;
private Container container;


// Start is called before the first frame update
void Start()
{
if(_instance != null)
{
if (_instance != this)
Destroy(this);
}
else
{
_instance = this;
}

GameObject cont = new GameObject("Container");
cont.transform.parent = transform;
container = cont.AddComponent<Container>();
Expand All @@ -31,16 +39,20 @@ void Start()
}
}


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

// Update is called once per frame
void Update()
{
private static WorldManager _instance;

public static WorldManager Instance
{
get
{
if (_instance == null)
_instance = FindObjectOfType<WorldManager>();
return _instance;
}
}

}
}
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.

8 changes: 6 additions & 2 deletions Assets/VoxelProjectSeries/Scenes/World.unity
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,11 @@ MonoBehaviour:
m_GameObject: {fileID: 1755072223}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 35997360c8b74f543b623a3524b58bc9, type: 3}
m_Script: {fileID: 11500000, guid: e0e65ef4e51bde8429033f2ff79c2aa4, type: 3}
m_Name:
m_EditorClassIdentifier:
worldMaterial: {fileID: 2100000, guid: 4021f579d8ff00b4ca1a8c7d355362b3, type: 2}
worldMaterial: {fileID: 2100000, guid: d41578d92bc26b04bb2355aed476d2cb, type: 2}
WorldColors:
- color: {r: 0, g: 0.4834163, b: 0.9811321, a: 0}
metallic: 0.5
smoothness: 1
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.

8 changes: 8 additions & 0 deletions Assets/VoxelProjectSeries/Shaders.meta

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

Loading

0 comments on commit 5ff39e6

Please sign in to comment.