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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use hashbrown::{HashMap, HashSet};
use log::info;
use serde::{Deserialize, Serialize};
use crate::BlockFace;
use super::voxels::Block;
const TEXTURE_BLEEDING_OFFSET: f32 = 1.0 / 64.0;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UV {
pub start_u: f32,
pub end_u: f32,
pub start_v: f32,
pub end_v: f32,
}
#[derive(Default, Clone)]
pub struct Registry {
pub ranges: HashMap<String, UV>,
pub blocks_by_name: HashMap<String, Block>,
pub blocks_by_id: HashMap<u32, Block>,
pub textures: HashSet<String>,
name_map: HashMap<u32, String>,
type_map: HashMap<String, u32>,
}
impl Registry {
pub fn new() -> Self {
let air = Block::new("Air")
.is_block(false)
.is_empty(true)
.is_x_transparent(true)
.is_y_transparent(true)
.is_z_transparent(true)
.aabbs(&[])
.build();
let mut instance = Self::default();
instance.record_block(&air);
instance
}
pub fn generate(&mut self) {
let count_per_side = self.per_side();
let mut row = 0;
let mut col = 0;
for texture in self.textures.iter() {
if col >= count_per_side {
col = 0;
row += 1;
}
let start_x = col as f32;
let start_y = row as f32;
let offset = 1.0 / (count_per_side as f32 * 4.0);
let start_u = start_x / count_per_side as f32;
let end_u = (start_x + 1.0) / count_per_side as f32;
let start_v = start_y / count_per_side as f32;
let end_v = (start_y + 1.0) / count_per_side as f32;
let start_u = start_u + offset;
let end_u = end_u - offset;
let start_v = start_v + offset;
let end_v = end_v - offset;
self.ranges.insert(
texture.to_owned(),
UV {
start_u,
end_u,
start_v,
end_v,
},
);
col += 1;
}
}
pub fn register_blocks(&mut self, blocks: &[Block]) {
blocks.into_iter().for_each(|block| {
self.register_block(block);
});
}
pub fn register_block(&mut self, block: &Block) {
let mut block = block.to_owned();
if block.id == 0 {
let mut next_available = 1;
loop {
if self.blocks_by_id.contains_key(&next_available) {
next_available += 1;
} else {
break;
}
}
block.id = next_available;
}
if self.blocks_by_id.contains_key(&block.id) {
panic!("Duplicated key: {}-{}", block.name, block.id);
}
self.record_block(&block);
}
pub fn get_block_by_name(&self, name: &str) -> &Block {
self.blocks_by_name
.get(&name.to_lowercase())
.unwrap_or_else(|| panic!("Block name not found: {name}",))
}
pub fn get_block_by_id(&self, id: u32) -> &Block {
self.blocks_by_id
.get(&id)
.unwrap_or_else(|| panic!("Block id not found: {id}"))
}
pub fn get_id_by_name(&self, name: &str) -> u32 {
*self
.type_map
.get(&name.to_lowercase())
.unwrap_or_else(|| panic!("Block name not found: {name}"))
}
pub fn get_fluiditiy_by_id(&self, id: u32) -> bool {
self.get_block_by_id(id).is_fluid
}
pub fn get_fluiditiy_by_name(&self, name: &str) -> bool {
self.get_block_by_name(name).is_fluid
}
pub fn get_opacity_by_id(&self, id: u32) -> bool {
self.get_block_by_id(id).is_opaque
}
pub fn get_opacity_by_name(&self, name: &str) -> bool {
self.get_block_by_name(name).is_opaque
}
pub fn get_emptiness_by_id(&self, id: u32) -> bool {
self.get_block_by_id(id).is_empty
}
pub fn get_emptiness_by_name(&self, name: &str) -> bool {
self.get_block_by_name(name).is_empty
}
pub fn get_faces_by_id(&self, id: u32) -> &Vec<BlockFace> {
&self.get_block_by_id(id).faces
}
pub fn get_faces_by_name(&self, name: &str) -> &Vec<BlockFace> {
&self.get_block_by_name(name).faces
}
pub fn get_uv_by_id(&self, id: u32) -> HashMap<String, &UV> {
self.get_uv_map(self.get_block_by_id(id))
}
pub fn get_uv_by_name(&self, name: &str) -> HashMap<String, &UV> {
self.get_uv_map(self.get_block_by_name(name))
}
pub fn is_air(&self, id: u32) -> bool {
self.get_block_by_id(id).name == "Air"
}
pub fn is_fluid(&self, id: u32) -> bool {
self.get_block_by_id(id).is_fluid
}
pub fn is_plant(&self, id: u32) -> bool {
self.get_block_by_id(id).is_plant
}
pub fn is_plantable(&self, id: u32, above: u32) -> bool {
self.get_block_by_id(id).is_plantable && self.get_block_by_id(above).is_empty
}
pub fn get_type_map(&self, blocks: &[&str]) -> HashMap<String, u32> {
let mut type_map = HashMap::new();
for block in blocks {
let &id = self
.type_map
.get(&block.to_lowercase())
.unwrap_or_else(|| panic!("Block name not found: {}", block));
type_map.insert((*block).to_owned(), id);
}
type_map
}
pub fn check_height(&self, id: u32) -> bool {
let block = self.get_block_by_id(id);
id != 0 && !block.is_plant
}
pub fn has_type(&self, id: u32) -> bool {
self.blocks_by_id.contains_key(&id)
}
pub fn get_uv_map(&self, block: &Block) -> HashMap<String, &UV> {
let mut uv_map = HashMap::new();
for source in block.faces.iter() {
let uv = self
.ranges
.get(&Registry::make_side_name(&block.name, source))
.unwrap_or_else(|| panic!("UV range not found: {:?}", source));
uv_map.insert(source.name.to_owned(), uv);
}
uv_map
}
fn per_side(&self) -> usize {
let mut i = 1.0;
let sqrt = (self.textures.len() as f32).sqrt().ceil();
while i < sqrt {
i = i * 2.0;
}
i as usize
}
fn record_block(&mut self, block: &Block) {
let Block {
id,
name,
faces,
is_plant,
..
} = block;
let lower_name = name.to_lowercase();
self.blocks_by_name
.insert(lower_name.clone(), block.clone());
self.blocks_by_id.insert(*id, block.clone());
self.name_map.insert(*id, lower_name.clone());
self.type_map.insert(lower_name.clone(), *id);
for side in faces.iter() {
let side_name = Registry::make_side_name(name, side);
self.textures.insert(side_name);
}
}
fn make_side_name(name: &str, side: &BlockFace) -> String {
format!(
"{}__{}",
name.to_lowercase().replace(" ", "_"),
side.name.to_lowercase()
)
}
}