A bit of a remark on the following bit of code:
protected virtual MeshData FaceDataUp
(Chunk chunk, int x, int y, int z, MeshData meshData)
{
meshData.vertices.Add(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f));
meshData.vertices.Add(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f));
meshData.vertices.Add(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f));
meshData.vertices.Add(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f));
meshData.AddQuadTriangles();
return meshData;
}
Because MeshData and the lists in it are
reference type, it makes no difference if you return it again or not, you're not no point creating a copy of the variable but always overwriting it. As a result when you do this:
var data = new MeshData();
var data2 = FaceDataUp(chunk, 1, 1, 1, data);
"data" and "data2" will point to the exact same object. This may cause some confusion. Another example to make entirely clear what's happening, here's a snippet of code to be run in a C# console project:
using System;
namespace ReferenceExample
{
class MyObject
{
public int MyValue { get; set; }
}
class Program
{
static MyObject ChangeValue(MyObject obj)
{
obj.MyValue = 2;
return obj;
}
static void Main(string[] args)
{
var obj1 = new MyObject();
obj1.MyValue = 5;
Console.WriteLine("obj1.MyValue: " + obj1.MyValue);
Console.WriteLine("Running ChangeValue()...");
var obj2 = ChangeValue(obj1);
Console.WriteLine("obj1.MyValue: " + obj1.MyValue);
Console.WriteLine("obj2.MyValue: " + obj2.MyValue);
Console.ReadKey();
}
}
}
This will output:
obj1.MyValue: 5
Running ChangeValue()...
obj1.MyValue: 2
obj2.MyValue: 2