Skip to content

Commit 3dcc755

Browse files
Refactor texture definitions (#6)
* Refactor texture definitions * Introduce texture_manager * Add texture manager resource to terrain plugin, fix visibility * Fix clippy * Fix linter * Attempt fix clippy * Fix linter fr fr
1 parent 2fc59ce commit 3dcc755

File tree

4 files changed

+129
-47
lines changed

4 files changed

+129
-47
lines changed

src/client/terrain/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct TerrainPlugin;
1111
impl Plugin for TerrainPlugin {
1212
fn build(&self, app: &mut App) {
1313
app.insert_resource(terrain_resources::ChunkManager::new());
14+
app.insert_resource(util::TextureManager::new());
1415
app.add_event::<terrain_events::BlockUpdateEvent>();
1516
app.add_event::<terrain_events::ChunkMeshUpdateEvent>();
1617
app.add_systems(Startup, terrain_systems::setup_world_system);

src/client/terrain/systems.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub fn setup_world_system(
66
mut meshes: ResMut<Assets<Mesh>>,
77
mut materials: ResMut<Assets<StandardMaterial>>,
88
mut chunk_manager: ResMut<terrain_resources::ChunkManager>,
9+
texture_manager: ResMut<terrain_util::TextureManager>,
910
) {
1011
let generator = terrain_util::generator::Generator::new(0);
1112

@@ -24,12 +25,14 @@ pub fn setup_world_system(
2425
&mut meshes,
2526
&mut materials,
2627
chunk,
28+
&texture_manager,
2729
);
2830
}
2931

3032
chunk_manager.insert_chunks(chunks);
3133
}
3234

35+
#[allow(clippy::too_many_arguments)]
3336
pub fn handle_chunk_mesh_update_events(
3437
mut commands: Commands,
3538
asset_server: Res<AssetServer>,
@@ -38,6 +41,7 @@ pub fn handle_chunk_mesh_update_events(
3841
mut chunk_manager: ResMut<terrain_resources::ChunkManager>,
3942
mut chunk_mesh_update_events: EventReader<terrain_events::ChunkMeshUpdateEvent>,
4043
mut mesh_query: Query<(Entity, &terrain_components::ChunkMesh)>,
44+
texture_manager: ResMut<terrain_util::TextureManager>,
4145
) {
4246
for event in chunk_mesh_update_events.read() {
4347
let chunk_option = chunk_manager.get_chunk(event.position);
@@ -54,6 +58,7 @@ pub fn handle_chunk_mesh_update_events(
5458
&mut meshes,
5559
&mut materials,
5660
chunk,
61+
&texture_manager,
5762
);
5863
}
5964
None => {
@@ -69,9 +74,10 @@ fn add_chunk_objects(
6974
meshes: &mut ResMut<Assets<Mesh>>,
7075
materials: &mut ResMut<Assets<StandardMaterial>>,
7176
chunk: &terrain_util::Chunk,
77+
texture_manager: &terrain_util::TextureManager,
7278
) {
7379
let texture_handle: Handle<Image> = asset_server.load("textures/texture_atlas.png");
74-
let mesh_option = terrain_util::create_chunk_mesh(chunk);
80+
let mesh_option = terrain_util::create_chunk_mesh(chunk, texture_manager);
7581

7682
if mesh_option.is_none() {
7783
return;

src/client/terrain/util/blocks.rs

Lines changed: 108 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,64 @@
11
use crate::prelude::*;
22

3+
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
4+
pub enum TextureName {
5+
Air,
6+
Stone,
7+
Dirt,
8+
GrassTop,
9+
GrassSide,
10+
Bedrock,
11+
RedSand,
12+
BrownTerracotta,
13+
CyanTerracotta,
14+
GrayTerracotta,
15+
LightGrayTerracotta,
16+
OrangeTerracotta,
17+
RedTerracotta,
18+
Terracotta,
19+
YellowTerracotta,
20+
}
21+
22+
#[derive(Resource)]
23+
pub struct TextureManager {
24+
textures: HashMap<TextureName, TextureUV>,
25+
}
26+
27+
impl Default for TextureManager {
28+
fn default() -> Self {
29+
Self::new()
30+
}
31+
}
32+
33+
impl TextureManager {
34+
pub fn new() -> Self {
35+
let mut textures = HashMap::new();
36+
textures.insert(TextureName::Air, [-1.0, -1.0]);
37+
textures.insert(TextureName::Stone, [0.0, 0.0]);
38+
textures.insert(TextureName::Dirt, [0.25, 0.0]);
39+
textures.insert(TextureName::GrassTop, [0.5, 0.0]);
40+
textures.insert(TextureName::GrassSide, [0.75, 0.0]);
41+
textures.insert(TextureName::Bedrock, [0.0, 0.25]);
42+
textures.insert(TextureName::RedSand, [0.25, 0.25]);
43+
textures.insert(TextureName::BrownTerracotta, [0.5, 0.25]);
44+
textures.insert(TextureName::CyanTerracotta, [0.75, 0.25]);
45+
textures.insert(TextureName::GrayTerracotta, [0.0, 0.5]);
46+
textures.insert(TextureName::LightGrayTerracotta, [0.25, 0.5]);
47+
textures.insert(TextureName::OrangeTerracotta, [0.5, 0.5]);
48+
textures.insert(TextureName::RedTerracotta, [0.75, 0.5]);
49+
textures.insert(TextureName::Terracotta, [0.0, 0.75]);
50+
textures.insert(TextureName::YellowTerracotta, [0.25, 0.75]);
51+
Self { textures }
52+
}
53+
54+
pub fn get_texture_uv(&self, name: TextureName) -> Option<&TextureUV> {
55+
self.textures.get(&name)
56+
}
57+
}
58+
359
pub struct Block {
460
pub id: BlockId,
5-
pub texture_names: [&'static str; 6],
61+
pub texture_names: [TextureName; 6],
662
pub is_solid: bool,
763
}
864

@@ -17,61 +73,71 @@ macro_rules! add_block {
1773
}
1874

1975
pub static BLOCKS: [Block; 14] = [
20-
add_block!(BlockId::Air, ["air"; 6], false),
76+
add_block!(BlockId::Air, [TextureName::Air; 6], false),
2177
add_block!(
2278
BlockId::Grass,
2379
[
24-
"grass_top",
25-
"dirt",
26-
"grass_side",
27-
"grass_side",
28-
"grass_side",
29-
"grass_side",
80+
TextureName::GrassTop,
81+
TextureName::Dirt,
82+
TextureName::GrassSide,
83+
TextureName::GrassSide,
84+
TextureName::GrassSide,
85+
TextureName::GrassSide,
3086
],
3187
true
3288
),
33-
add_block!(BlockId::Dirt, ["dirt"; 6], true),
34-
add_block!(BlockId::Stone, ["stone"; 6], true),
35-
add_block!(BlockId::Bedrock, ["bedrock"; 6], true),
36-
add_block!(BlockId::RedSand, ["red_sand"; 6], true),
37-
add_block!(BlockId::BrownTerracotta, ["brown_terracotta"; 6], true),
38-
add_block!(BlockId::CyanTerracotta, ["cyan_terracotta"; 6], true),
39-
add_block!(BlockId::GrayTerracotta, ["gray_terracotta"; 6], true),
89+
add_block!(BlockId::Dirt, [TextureName::Dirt; 6], true),
90+
add_block!(BlockId::Stone, [TextureName::Stone; 6], true),
91+
add_block!(BlockId::Bedrock, [TextureName::Bedrock; 6], true),
92+
add_block!(BlockId::RedSand, [TextureName::RedSand; 6], true),
93+
add_block!(
94+
BlockId::BrownTerracotta,
95+
[TextureName::BrownTerracotta; 6],
96+
true
97+
),
98+
add_block!(
99+
BlockId::CyanTerracotta,
100+
[TextureName::CyanTerracotta; 6],
101+
true
102+
),
103+
add_block!(
104+
BlockId::GrayTerracotta,
105+
[TextureName::GrayTerracotta; 6],
106+
true
107+
),
40108
add_block!(
41109
BlockId::LightGrayTerracotta,
42-
["light_gray_terracotta"; 6],
110+
[TextureName::LightGrayTerracotta; 6],
111+
true
112+
),
113+
add_block!(
114+
BlockId::OrangeTerracotta,
115+
[TextureName::OrangeTerracotta; 6],
116+
true
117+
),
118+
add_block!(
119+
BlockId::RedTerracotta,
120+
[TextureName::RedTerracotta; 6],
121+
true
122+
),
123+
add_block!(BlockId::Terracotta, [TextureName::Terracotta; 6], true),
124+
add_block!(
125+
BlockId::YellowTerracotta,
126+
[TextureName::YellowTerracotta; 6],
43127
true
44128
),
45-
add_block!(BlockId::OrangeTerracotta, ["orange_terracotta"; 6], true),
46-
add_block!(BlockId::RedTerracotta, ["red_terracotta"; 6], true),
47-
add_block!(BlockId::Terracotta, ["terracotta"; 6], true),
48-
add_block!(BlockId::YellowTerracotta, ["yellow_terracotta"; 6], true),
49129
];
50130

51-
impl Block {
52-
pub fn get_texture_uvs(texture_name: &str) -> Option<[f32; 2]> {
53-
match texture_name {
54-
"stone" => Some([0.0, 0.0]),
55-
"dirt" => Some([0.25, 0.0]),
56-
"grass_top" => Some([0.5, 0.0]),
57-
"grass_side" => Some([0.75, 0.0]),
58-
"bedrock" => Some([0.0, 0.25]),
59-
"red_sand" => Some([0.25, 0.25]),
60-
"brown_terracotta" => Some([0.5, 0.25]),
61-
"cyan_terracotta" => Some([0.75, 0.25]),
62-
"gray_terracotta" => Some([0.0, 0.5]),
63-
"light_gray_terracotta" => Some([0.25, 0.5]),
64-
"orange_terracotta" => Some([0.5, 0.5]),
65-
"red_terracotta" => Some([0.75, 0.5]),
66-
"terracotta" => Some([0.0, 0.75]),
67-
"yellow_terracotta" => Some([0.25, 0.75]),
68-
_ => None,
69-
}
70-
}
131+
type TextureUV = [f32; 2];
71132

72-
pub fn get_block_face_uvs(block_id: BlockId, face: CubeFace) -> Option<[f32; 2]> {
133+
impl Block {
134+
pub fn get_block_face_uvs(
135+
block_id: BlockId,
136+
face: CubeFace,
137+
texture_manager: &TextureManager,
138+
) -> Option<[f32; 2]> {
73139
let block = &BLOCKS[block_id as usize];
74140
let texture_name = block.texture_names[face as usize];
75-
Self::get_texture_uvs(texture_name)
141+
texture_manager.get_texture_uv(texture_name).copied()
76142
}
77143
}

src/client/terrain/util/mesher.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use terrain_util::TextureManager;
2+
13
use crate::prelude::*;
24

35
pub fn create_cube_mesh_from_data(geometry_data: GeometryData) -> Option<Mesh> {
@@ -32,6 +34,7 @@ pub fn create_cube_geometry_data(
3234
z: f32,
3335
faces: u8,
3436
block_id: BlockId,
37+
texture_manager: &TextureManager,
3538
) -> GeometryData {
3639
let mut position = Vec::new();
3740
let mut uv = Vec::new();
@@ -52,7 +55,7 @@ pub fn create_cube_geometry_data(
5255
vertex.position[2] * 0.5 + z + 0.5,
5356
]);
5457

55-
let block_uvs = Block::get_block_face_uvs(block_id, *face).unwrap();
58+
let block_uvs = Block::get_block_face_uvs(block_id, *face, texture_manager).unwrap();
5659
uv.push([
5760
block_uvs[0] + vertex.uv[0] * 0.25 - 0.001,
5861
block_uvs[1] + (1.0 - vertex.uv[1]) * 0.25,
@@ -75,7 +78,7 @@ pub fn create_cube_geometry_data(
7578
}
7679
}
7780

78-
pub fn create_chunk_mesh(chunk: &Chunk) -> Option<Mesh> {
81+
pub fn create_chunk_mesh(chunk: &Chunk, texture_manager: &TextureManager) -> Option<Mesh> {
7982
let mut geometry_data = GeometryData {
8083
position: Vec::new(),
8184
uv: Vec::new(),
@@ -109,8 +112,14 @@ pub fn create_chunk_mesh(chunk: &Chunk) -> Option<Mesh> {
109112
update_mask(chunk, &mut mask, 0b010000, x, y, z - 1);
110113
update_mask(chunk, &mut mask, 0b100000, x, y, z + 1);
111114

112-
let cube_data =
113-
create_cube_geometry_data(x as f32, y as f32, z as f32, mask, block_id);
115+
let cube_data = create_cube_geometry_data(
116+
x as f32,
117+
y as f32,
118+
z as f32,
119+
mask,
120+
block_id,
121+
texture_manager,
122+
);
114123

115124
geometry_data.indices.extend(
116125
cube_data

0 commit comments

Comments
 (0)