-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockHelper.cs
136 lines (118 loc) · 6.53 KB
/
BlockHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class BlockHelper
{
private static Direction[] directions =
{
Direction.backwards,
Direction.down,
Direction.foreward,
Direction.left,
Direction.right,
Direction.up
};
public static MeshData GetMeshData
(ChunkData chunk, int x, int y, int z, MeshData meshData, BlockType blockType)
{
if (blockType == BlockType.AIR || blockType == BlockType.NOTHING)
return meshData;
foreach (Direction direction in directions)
{
Vector3Int neighbourBlockCoordinates = new Vector3Int(x, y, z) + direction.GetVector();
BlockType neighbourBlockType = Chunk.GetBlockFromChunkCoordinates(chunk, neighbourBlockCoordinates);
if (neighbourBlockType != BlockType.NOTHING && BlockDataManager.blockTextureDataDictionary[neighbourBlockType].isSolid == false)
{
/*if (blockType == BlockType.TREE_LEAFS_SOLID)
{
meshData.leavesMesh = GetFaceDataIn(direction, chunk, x, y, z, meshData.leavesMesh, blockType);
}else */if (blockType == BlockType.WATER)
{
if (neighbourBlockType == BlockType.AIR)
meshData.waterMesh = GetFaceDataIn(direction, chunk, x, y, z, meshData.waterMesh, blockType);
}
else
{
meshData = GetFaceDataIn(direction, chunk, x, y, z, meshData, blockType);
}
}
}
return meshData;
}
public static MeshData GetFaceDataIn(Direction direction, ChunkData chunk, int x, int y, int z, MeshData meshData, BlockType blockType)
{
GetFaceVertices(direction, x, y, z, meshData, blockType);
meshData.AddQuadTriangles(BlockDataManager.blockTextureDataDictionary[blockType].generatesCollider);
meshData.uv.AddRange(FaceUVs(direction, blockType));
return meshData;
}
public static void GetFaceVertices(Direction direction, int x, int y, int z, MeshData meshData, BlockType blockType)
{
bool generatesCollider = BlockDataManager.blockTextureDataDictionary[blockType].generatesCollider;
//order of vertices matters for the normals and how we render the mesh
switch (direction)
{
case Direction.backwards:
meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f), generatesCollider);
break;
case Direction.foreward:
meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f), generatesCollider);
break;
case Direction.left:
meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f), generatesCollider);
break;
case Direction.right:
meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f), generatesCollider);
break;
case Direction.down:
meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z - 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z - 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x + 0.5f, y - 0.5f, z + 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x - 0.5f, y - 0.5f, z + 0.5f), generatesCollider);
break;
case Direction.up:
meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z + 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z + 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x + 0.5f, y + 0.5f, z - 0.5f), generatesCollider);
meshData.AddVertex(new Vector3(x - 0.5f, y + 0.5f, z - 0.5f), generatesCollider);
break;
default:
break;
}
}
public static Vector2[] FaceUVs(Direction direction, BlockType blockType)
{
Vector2[] UVs = new Vector2[4];
Vector2 tilePos = TexturePosition(direction, blockType);
UVs[0] = new Vector2(BlockDataManager.tileSizeX * tilePos.x + BlockDataManager.tileSizeX - BlockDataManager.textureOffset,
BlockDataManager.tileSizeY * tilePos.y + BlockDataManager.textureOffset);
UVs[1] = new Vector2(BlockDataManager.tileSizeX * tilePos.x + BlockDataManager.tileSizeX - BlockDataManager.textureOffset,
BlockDataManager.tileSizeY * tilePos.y + BlockDataManager.tileSizeY - BlockDataManager.textureOffset);
UVs[2] = new Vector2(BlockDataManager.tileSizeX * tilePos.x + BlockDataManager.textureOffset,
BlockDataManager.tileSizeY * tilePos.y + BlockDataManager.tileSizeY - BlockDataManager.textureOffset);
UVs[3] = new Vector2(BlockDataManager.tileSizeX * tilePos.x + BlockDataManager.textureOffset,
BlockDataManager.tileSizeY * tilePos.y + BlockDataManager.textureOffset);
return UVs;
}
public static Vector2 TexturePosition(Direction direction, BlockType blockType)
{
return direction switch
{
Direction.up => BlockDataManager.blockTextureDataDictionary[blockType].up,
Direction.down => BlockDataManager.blockTextureDataDictionary[blockType].down,
_ => BlockDataManager.blockTextureDataDictionary[blockType].side
};
}
}