Question by
nitrogames79 · Jul 12, 2020 at 07:54 AM ·
classnullclass instancereturn value
Returned class is always null
The MeshHolder class that GenerateChunkMesh returns is always null. No matter what I do to try and make it return the class it continues to return null.
public class MeshGenerator {
private byte[,,] faces = new byte[Chunk.chunksize, Chunk.chunksize, Chunk.chunksize];
private Voxel[,,] blocks = new Voxel[Chunk.chunksize, Chunk.chunksize, Chunk.chunksize];
private Vector3[] vertices;
private int[] triangles;
private Vector2[] uvs;
private int verticesPrediction;
private int verticesIndex, trianglesIndex;
private Vector3 pos;
private World world;
private Voxel newblock;
public MeshGenerator(Vector3 pos, Voxel[,,] blocks, World world) {
this.pos = pos;
this.blocks = blocks;
this.world = world;
}
public MeshHolder GenerateChunkMesh() {
Voxel newblock;
for (int x = 0; x < Chunk.chunksize; x++){
for (int y = 0; y < Chunk.chunksize; y++){
for (int z = 0; z < Chunk.chunksize; z++){
if(blocks[x, y, z].IsTransparent()){
continue;
}
if (MathVox.InsideChunk(z + 1)){
if (blocks[x, y, z + 1].IsTransparent()){
faces[x, y, z] |= (byte)Direction.north;
verticesPrediction += 4;
}
}
else{
newblock = world.GetBlock(pos, x, y, z + 1);
//if (newblock.IsTransparent()){
faces[x, y, z] |= (byte)Direction.north;
verticesPrediction += 4;
//}
}
if (MathVox.InsideChunk(z - 1)){
if (blocks[x, y, z - 1].IsTransparent()){
faces[x, y, z] |= (byte)Direction.south;
verticesPrediction += 4;
}
}
else{
newblock = world.GetBlock(pos, x, y, z - 1);
if (newblock.IsTransparent()){
faces[x, y, z] |= (byte)Direction.south;
verticesPrediction += 4;
}
}
if (MathVox.InsideChunk(y + 1)){
if (blocks[x, y + 1, z].IsTransparent()){
faces[x, y, z] |= (byte)Direction.up;
verticesPrediction += 4;
}
}
else{
newblock = world.GetBlock(pos, x, y + 1, z);
if (newblock.IsTransparent()){
faces[x, y, z] |= (byte)Direction.up;
verticesPrediction += 4;
}
}
if (MathVox.InsideChunk(y - 1)){
if (blocks[x, y - 1, z].IsTransparent()){
faces[x, y, z] |= (byte)Direction.down;
verticesPrediction += 4;
}
}
else{
newblock = world.GetBlock(pos, x, y - 1, z);
if (newblock.IsTransparent()){
faces[x, y, z] |= (byte)Direction.down;
verticesPrediction += 4;
}
}
if (MathVox.InsideChunk(x + 1)){
if (blocks[x + 1, y, z].IsTransparent()){
faces[x, y, z] |= (byte)Direction.east;
verticesPrediction += 4;
}
}
else{
newblock = world.GetBlock(pos, x + 1, y, z);
if (newblock.IsTransparent()){
faces[x, y, z] |= (byte)Direction.east;
verticesPrediction += 4;
}
}
if (MathVox.InsideChunk(x - 1)){
if (blocks[x - 1, y, z].IsTransparent()){
faces[x, y, z] |= (byte)Direction.west;
verticesPrediction += 4;
}
}
else{
newblock = world.GetBlock(pos, x - 1, y, z);
if (newblock.IsTransparent()){
faces[x, y, z] |= (byte)Direction.west;
verticesPrediction += 4;
}
}
}
}
}
if (verticesPrediction == 0){
Debug.Log("pass");
//return null;
}
vertices = new Vector3[verticesPrediction];
uvs = new Vector2[verticesPrediction];
triangles = new int[(int)(verticesPrediction * 1.5f)];
for (int x = 0; x < Chunk.chunksize; x++){
for (int y = 0; y < Chunk.chunksize; y++){
for (int z = 0; z < Chunk.chunksize; z++){
if (faces[x, y, z] == 0){
continue;
}
if ((faces[x, y, z] & (byte)Direction.north) != 0){
vertices[verticesIndex] = new Vector3(pos.x + x + 0.5f, pos.y + y - 0.5f, pos.z + z + 0.5f);
vertices[verticesIndex + 1] = new Vector3(pos.x + x - 0.5f, pos.y + y - 0.5f, pos.z + z + 0.5f);
vertices[verticesIndex + 2] = new Vector3(pos.x + x - 0.5f, pos.y + y + 0.5f, pos.z + z + 0.5f);
vertices[verticesIndex + 3] = new Vector3(pos.x + x + 0.5f, pos.y + y + 0.5f, pos.z + z + 0.5f);
triangles[trianglesIndex] = verticesIndex + 2;
triangles[trianglesIndex + 1] = verticesIndex + 1;
triangles[trianglesIndex + 2] = verticesIndex;
triangles[trianglesIndex + 3] = verticesIndex;
triangles[trianglesIndex + 4] = verticesIndex + 3;
triangles[trianglesIndex + 5] = verticesIndex + 2;
TextureManager.AddTextures(blocks[x, y, z], Direction.north, verticesIndex, uvs);
verticesIndex += 4;
trianglesIndex += 6;
}
if ((faces[x, y, z] & (byte)Direction.south) != 0){
vertices[verticesIndex] = new Vector3(pos.x + x + 0.5f, pos.y + y - 0.5f, pos.z + z - 0.5f);
vertices[verticesIndex + 1] = new Vector3(pos.x + x - 0.5f, pos.y + y - 0.5f, pos.z + z - 0.5f);
vertices[verticesIndex + 2] = new Vector3(pos.x + x - 0.5f, pos.y + y + 0.5f, pos.z + z - 0.5f);
vertices[verticesIndex + 3] = new Vector3(pos.x + x + 0.5f, pos.y + y + 0.5f, pos.z + z - 0.5f);
triangles[trianglesIndex] = verticesIndex;
triangles[trianglesIndex + 1] = verticesIndex + 1;
triangles[trianglesIndex + 2] = verticesIndex + 2;
triangles[trianglesIndex + 3] = verticesIndex + 2;
triangles[trianglesIndex + 4] = verticesIndex + 3;
triangles[trianglesIndex + 5] = verticesIndex;
TextureManager.AddTextures(blocks[x, y, z], Direction.south, verticesIndex, uvs);
verticesIndex += 4;
trianglesIndex += 6;
}
if ((faces[x, y, z] & (byte)Direction.up) != 0){
vertices[verticesIndex] = new Vector3(pos.x + x - 0.5f, pos.y + y + 0.5f, pos.z + z - 0.5f);
vertices[verticesIndex + 1] = new Vector3(pos.x + x - 0.5f, pos.y + y + 0.5f, pos.z + z + 0.5f);
vertices[verticesIndex + 2] = new Vector3(pos.x + x + 0.5f, pos.y + y + 0.5f, pos.z + z + 0.5f);
vertices[verticesIndex + 3] = new Vector3(pos.x + x + 0.5f, pos.y + y + 0.5f, pos.z + z - 0.5f);
triangles[trianglesIndex] = verticesIndex;
triangles[trianglesIndex + 1] = verticesIndex + 1;
triangles[trianglesIndex + 2] = verticesIndex + 2;
triangles[trianglesIndex + 3] = verticesIndex + 2;
triangles[trianglesIndex + 4] = verticesIndex + 3;
triangles[trianglesIndex + 5] = verticesIndex;
TextureManager.AddTextures(blocks[x, y, z], Direction.up, verticesIndex, uvs);
verticesIndex += 4;
trianglesIndex += 6;
}
if ((faces[x, y, z] & (byte)Direction.down) != 0){
vertices[verticesIndex] = new Vector3(pos.x + x - 0.5f, pos.y + y - 0.5f, pos.z + z - 0.5f);
vertices[verticesIndex + 1] = new Vector3(pos.x + x - 0.5f, pos.y + y - 0.5f, pos.z + z + 0.5f);
vertices[verticesIndex + 2] = new Vector3(pos.x + x + 0.5f, pos.y + y - 0.5f, pos.z + z + 0.5f);
vertices[verticesIndex + 3] = new Vector3(pos.x + x + 0.5f, pos.y + y - 0.5f, pos.z + z - 0.5f);
triangles[trianglesIndex] = verticesIndex + 2;
triangles[trianglesIndex + 1] = verticesIndex + 1;
triangles[trianglesIndex + 2] = verticesIndex;
triangles[trianglesIndex + 3] = verticesIndex;
triangles[trianglesIndex + 4] = verticesIndex + 3;
triangles[trianglesIndex + 5] = verticesIndex + 2;
TextureManager.AddTextures(blocks[x, y, z], Direction.down, verticesIndex, uvs);
verticesIndex += 4;
trianglesIndex += 6;
}
if ((faces[x, y, z] & (byte)Direction.east) != 0){
vertices[verticesIndex] = new Vector3(pos.x + x + 0.5f, pos.y + y - 0.5f, pos.z + z - 0.5f);
vertices[verticesIndex + 1] = new Vector3(pos.x + x + 0.5f, pos.y + y - 0.5f, pos.z + z + 0.5f);
vertices[verticesIndex + 2] = new Vector3(pos.x + x + 0.5f, pos.y + y + 0.5f, pos.z + z + 0.5f);
vertices[verticesIndex + 3] = new Vector3(pos.x + x + 0.5f, pos.y + y + 0.5f, pos.z + z - 0.5f);
triangles[trianglesIndex] = verticesIndex + 2;
triangles[trianglesIndex + 1] = verticesIndex + 1;
triangles[trianglesIndex + 2] = verticesIndex;
triangles[trianglesIndex + 3] = verticesIndex;
triangles[trianglesIndex + 4] = verticesIndex + 3;
triangles[trianglesIndex + 5] = verticesIndex + 2;
TextureManager.AddTextures(blocks[x, y, z], Direction.east, verticesIndex, uvs);
verticesIndex += 4;
trianglesIndex += 6;
}
if ((faces[x, y, z] & (byte)Direction.west) != 0){
vertices[verticesIndex] = new Vector3(pos.x + x - 0.5f, pos.y + y - 0.5f, pos.z + z - 0.5f);
vertices[verticesIndex + 1] = new Vector3(pos.x + x - 0.5f, pos.y + y - 0.5f, pos.z + z + 0.5f);
vertices[verticesIndex + 2] = new Vector3(pos.x + x - 0.5f, pos.y + y + 0.5f, pos.z + z + 0.5f);
vertices[verticesIndex + 3] = new Vector3(pos.x + x - 0.5f, pos.y + y + 0.5f, pos.z + z - 0.5f);
triangles[trianglesIndex] = verticesIndex;
triangles[trianglesIndex + 1] = verticesIndex + 1;
triangles[trianglesIndex + 2] = verticesIndex + 2;
triangles[trianglesIndex + 3] = verticesIndex + 2;
triangles[trianglesIndex + 4] = verticesIndex + 3;
triangles[trianglesIndex + 5] = verticesIndex;
TextureManager.AddTextures(blocks[x, y, z], Direction.west, verticesIndex, uvs);
verticesIndex += 4;
trianglesIndex += 6;
}
}
}
}
return new MeshHolder(vertices, triangles, uvs);
}
}
public class MeshHolder {
private Vector3[] vertices;
private int[] triangles;
private Vector2[] uvs;
public MeshHolder(Vector3[] vertices, int[] triangles, Vector2[] uvs){
this.vertices = vertices;
this.triangles = triangles;
this.uvs = uvs;
}
public Mesh CreateMesh(){
Mesh mesh = new Mesh();
if(vertices == null){
return null;
}
mesh.vertices = vertices;
mesh.triangles = triangles;
//mesh.uv = uvs;
mesh.RecalculateNormals();
return mesh;
}
}
Comment
Best Answer
Answer by nitrogames79 · Jul 23, 2020 at 08:17 PM
The code that got the MeshHolder was
public void GetMesh(){
MeshHolder myMesh = new MeshHolder();
meshGen = new MeshGenerator(pos, blocks, world);
myMesh = meshGen.GenerateChunkMesh();
if(myMesh != null){
mesh = myMesh.CreateMesh();
}
updateChunk = false;
}
I got rid of MeshHolder myMesh = new MeshHolder(); and that ended up fixing it.