summaryrefslogtreecommitdiff
path: root/block.gd
blob: bfa9928fb564f4cf9a6122f19963cf1e621c4a60 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
class_name Block
extends Resource


const FACE_CORNERS = [
	Vector2(0, 0), Vector2(1, 0),
	Vector2(0, 1), Vector2(1, 1),
]

enum Face {
	FRONT,
	BACK,
	LEFT,
	RIGHT,
	TOP,
	BOTTOM,
}

const FACES = [
	[ # FRONT
		[Vector3(-1.0, 1.0, 1.0), Vector3(1.0, -1.0, 1.0), Vector3(-1.0, -1.0, 1.0)],
		[Vector3(-1.0, 1.0, 1.0), Vector3(1.0, 1.0, 1.0), Vector3(1.0, -1.0, 1.0)],
	],
	[ # BACK
		[Vector3(1.0, 1.0, -1.0), Vector3(-1.0, -1.0, -1.0), Vector3(1.0, -1.0, -1.0)],
		[Vector3(1.0, 1.0, -1.0), Vector3(-1.0, 1.0, -1.0), Vector3(-1.0, -1.0, -1.0)],
	],
	[ # LEFT
		[Vector3(-1.0, 1.0, -1.0), Vector3(-1.0, -1.0, 1.0), Vector3(-1.0, -1.0, -1.0)],
		[Vector3(-1.0, 1.0, -1.0), Vector3(-1.0, 1.0, 1.0), Vector3(-1.0, -1.0, 1.0)],
	],
	[ # RIGHT
		[Vector3(1.0, 1.0, 1.0), Vector3(1.0, -1.0, -1.0), Vector3(1.0, -1.0, 1.0)],
		[Vector3(1.0, 1.0, 1.0), Vector3(1.0, 1.0, -1.0), Vector3(1.0, -1.0, -1.0)],
	],
	[ # TOP
		[Vector3(-1.0, 1.0, -1.0), Vector3(1.0, 1.0, 1.0), Vector3(-1.0, 1.0, 1.0)],
		[Vector3(-1.0, 1.0, -1.0), Vector3(1.0, 1.0, -1.0), Vector3(1.0, 1.0, 1.0)],
	],
	[ # BOTTOM
		[Vector3(-1.0, -1.0, 1.0), Vector3(1.0, -1.0, -1.0), Vector3(-1.0, -1.0, -1.0)],
		[Vector3(-1.0, -1.0, 1.0), Vector3(1.0, -1.0, 1.0), Vector3(1.0, -1.0, -1.0)],
	],
]

const FACE_UVS = [
	[ # FRONT
		[Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
		[Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
	],
	[ # BACK
		[Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
		[Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
	],
	[ # LEFT
		[Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
		[Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
	],
	[ # RIGHT
		[Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
		[Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
	],
	[ # TOP
		[Vector2(0.0, 1.0), Vector2(1.0, 2.0), Vector2(0.0, 2.0)],
		[Vector2(0.0, 1.0), Vector2(1.0, 1.0), Vector2(1.0, 2.0)],
	],
	[ # BOTTOM
		[Vector2(1.0, 0.0), Vector2(2.0, 1.0), Vector2(1.0, 1.0)],
		[Vector2(1.0, 0.0), Vector2(2.0, 0.0), Vector2(2.0, 1.0)],
	],
]

const FACE_NORMALS = [
	Vector3(0, 0, 1), # FRONT
	Vector3(0, 0, -1), # BACK
	Vector3(-1, 0, 0), # LEFT
	Vector3(1, 0, 0), # RIGHT
	Vector3(0, 1, 0), # TOP
	Vector3(0, -1, 0), # BOTTOM
]

enum Type {
	AIR,
	GRASS,
	DIRT,
	LEAVES,
	STONE,
}

const BLOCK_TYPES = {
	Type.GRASS: {
		"uv2s": [
			Vector2(0, 0), # FRONT
			Vector2(0, 0), # BACK
			Vector2(0, 0), # LEFT
			Vector2(0, 0), # RIGHT
			Vector2(0, 1), # TOP
			Vector2(1, 0), # BOTTOM
		],
	},
	Type.DIRT: {
		"uv2s": [
			Vector2(1, 0), # FRONT
			Vector2(1, 0), # BACK
			Vector2(1, 0), # LEFT
			Vector2(1, 0), # RIGHT
			Vector2(1, 0), # TOP
			Vector2(1, 0), # BOTTOM
		],
	},
	Type.LEAVES: {
		"uv2s": [
			Vector2(2, 2), # FRONT
			Vector2(2, 2), # BACK
			Vector2(2, 2), # LEFT
			Vector2(2, 2), # RIGHT
			Vector2(2, 2), # TOP
			Vector2(2, 2), # BOTTOM
		],
	},
	Type.STONE: {
		"uv2s": [
			Vector2(2, 0), # FRONT
			Vector2(2, 0), # BACK
			Vector2(2, 0), # LEFT
			Vector2(2, 0), # RIGHT
			Vector2(2, 0), # TOP
			Vector2(2, 0), # BOTTOM
		],
	},
}

var position: Vector3
var type: Type


func add_face(surface_tool: SurfaceTool, face: Face):
	var uv2s = []
	uv2s.resize(3)
	uv2s.fill(BLOCK_TYPES[type].uv2s[face])
	
	var normals = []
	normals.resize(3)
	normals.fill(FACE_NORMALS[face])
	
	for idx in FACES[face].size():
		var triangle = FACES[face][idx]
		var triangle_positions = triangle.map(func(item):
			return item + position
		)
		
		var uvs = FACE_UVS[face][idx]
		
		surface_tool.add_triangle_fan(
			PackedVector3Array(triangle_positions),
			PackedVector2Array(uvs),
			PackedColorArray(),
			PackedVector2Array(uv2s),
			PackedVector3Array(normals)
		)