Skip to content

Commit e846602

Browse files
committed
More rendering improvements regarding liquid sources and flowing liquids
1 parent 322004d commit e846602

3 files changed

Lines changed: 51 additions & 10 deletions

File tree

src/gfx/map.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct MeshgenInfo {
5050
// i optimized the shit out of these
5151
textures: Vec<AtlasSlice>,
5252
nodes: [Option<Box<NodeDef>>; u16::MAX as usize + 1],
53+
node_names_to_ids: HashMap<String, u16>,
5354
}
5455

5556
type MeshQueue = HashMap<Point3<i16>, MeshData>;
@@ -439,10 +440,16 @@ impl MapRender {
439440
multiview: None,
440441
});
441442

443+
let mut node_names_to_ids: HashMap<String, u16> = HashMap::new();
444+
for (id, def) in &nodes {
445+
node_names_to_ids.insert(def.name.clone(), *id);
446+
}
447+
442448
let meshgen_queue = Arc::new(Mutex::new(HashMap::new()));
443449
let meshgen_info = Arc::new(MeshgenInfo {
444-
nodes: std::array::from_fn(|i| nodes.get(&(i as u16)).cloned().map(Box::new)),
445450
textures: atlas_slices,
451+
nodes: std::array::from_fn(|i| nodes.get(&(i as u16)).cloned().map(Box::new)),
452+
node_names_to_ids,
446453
});
447454
let mut meshgen_threads = Vec::new();
448455
let (meshgen_tx, meshgen_rx) = crossbeam_channel::unbounded();

src/gfx/map/atlas.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,16 @@ pub(super) fn create_atlas(
5252
.unwrap();
5353
match tile.animation {
5454
TileAnim::VerticalFrame {
55-
n_frames: whatever, ..
55+
n_frames: frame_aspect,
56+
..
5657
} => (|| {
57-
if whatever.x == 0 || whatever.y == 0 {
58+
if frame_aspect.x == 0 || frame_aspect.y == 0 {
5859
eprintln!("invalid animation for tile {}", string);
5960
return;
6061
}
6162
let tex_size = tex.dimensions();
6263
let frame_height =
63-
(tex_size.0 as f32 / whatever.x as f32 * whatever.y as f32) as u32;
64+
(tex_size.0 as f32 / frame_aspect.x as f32 * frame_aspect.y as f32) as u32;
6465
tex =
6566
image::imageops::crop(&mut tex, 0, 0, tex_size.0, frame_height).to_image();
6667
})(),

src/gfx/map/mesh.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use std::ops;
2+
13
use super::{LeavesMode, MapRenderSettings, MeshgenInfo, Vertex, CUBE, FACE_DIR};
24
use cgmath::Point3;
3-
use mt_net::MapBlock;
5+
use mt_net::{MapBlock, TileDef};
46

57
#[derive(Clone)]
68
pub(super) struct MeshData {
@@ -21,6 +23,21 @@ impl MeshData {
2123
}
2224
}
2325

26+
enum CowTileArray<'a> {
27+
Borrowed(&'a [TileDef; 6]),
28+
Owned([&'a TileDef; 6]),
29+
}
30+
impl<'a> ops::Index<usize> for CowTileArray<'a> {
31+
type Output = TileDef;
32+
33+
fn index(&self, index: usize) -> &Self::Output {
34+
match self {
35+
CowTileArray::Borrowed(tiles) => &tiles[index],
36+
CowTileArray::Owned(tiles) => tiles[index],
37+
}
38+
}
39+
}
40+
2441
pub(super) fn create_mesh(
2542
mkinfo: &MeshgenInfo,
2643
settings: &MapRenderSettings,
@@ -38,21 +55,24 @@ pub(super) fn create_mesh(
3855
use mt_net::{DrawType, Param1Type};
3956
use std::array::from_fn as array;
4057

41-
let mut tiles = &def.tiles;
58+
let mut tiles = CowTileArray::Borrowed(&def.tiles);
4259
let mut draw_type = def.draw_type;
4360

4461
match draw_type {
4562
DrawType::AllFacesOpt => {
4663
draw_type = match settings.leaves {
4764
LeavesMode::Opaque => DrawType::Cube,
4865
LeavesMode::Simple => {
49-
tiles = &def.special_tiles;
50-
66+
tiles = CowTileArray::Borrowed(&def.special_tiles);
5167
DrawType::GlassLike
5268
}
5369
LeavesMode::Fancy => DrawType::AllFaces,
5470
};
5571
}
72+
DrawType::Flowing => {
73+
let s = &def.special_tiles;
74+
tiles = CowTileArray::Owned([&s[1], &s[1], &s[0], &s[0], &s[1], &s[1]]);
75+
}
5676
DrawType::None => continue,
5777
_ => {}
5878
}
@@ -70,8 +90,17 @@ pub(super) fn create_mesh(
7090

7191
let pos: [i16; 3] = array(|i| ((index >> (4 * i)) & 0xf) as i16);
7292

93+
let alt_content = match draw_type {
94+
DrawType::Liquid => mkinfo.node_names_to_ids.get(&def.flowing_alt).copied(),
95+
DrawType::Flowing => mkinfo.node_names_to_ids.get(&def.source_alt).copied(),
96+
_ => None,
97+
};
98+
7399
for (f, face) in CUBE.iter().enumerate() {
74-
if draw_type == DrawType::Cube || draw_type == DrawType::Liquid {
100+
if draw_type == DrawType::Cube
101+
|| draw_type == DrawType::Liquid
102+
|| draw_type == DrawType::Flowing
103+
{
75104
let c = [1, 1, 0, 0, 2, 2][f];
76105

77106
let mut nblk = block;
@@ -93,7 +122,11 @@ pub(super) fn create_mesh(
93122
if let Some(ndef) = &mkinfo.nodes[ncontent as usize] {
94123
if match draw_type {
95124
DrawType::Cube => ndef.draw_type == DrawType::Cube,
96-
DrawType::Liquid => ndef.draw_type == DrawType::Cube || ncontent == content,
125+
DrawType::Liquid | DrawType::Flowing => {
126+
ndef.draw_type == DrawType::Cube
127+
|| ncontent == content
128+
|| Some(ncontent) == alt_content
129+
}
97130
_ => false,
98131
} {
99132
continue;

0 commit comments

Comments
 (0)