Skip to content

Commit

Permalink
Got blocks to render
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyTornetta committed Apr 6, 2023
1 parent 0b8b8cf commit d809039
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
12 changes: 6 additions & 6 deletions cosmos_client/assets/blocks/thruster.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"texture": {
"back": "missing",
"front": "laser_cannon_front",
"left": "laser_cannon_left_right",
"right": "laser_cannon_left_right",
"top": "laser_cannon_top_bottom",
"bottom": "laser_cannon_top_bottom"
"back": "thruster_back",
"front": "ship_hull",
"left": "thruster_left_right",
"right": "thruster_left_right",
"top": "thruster_top_bottom",
"bottom": "thruster_top_bottom"
}
}
18 changes: 17 additions & 1 deletion cosmos_client/src/asset/asset_loading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ pub struct MainAtlas {
pub uv_mapper: UVMapper,
}

impl MainAtlas {
pub fn uvs_for_index(&self, index: usize) -> Rect {
let rect = self.atlas.textures[index];

Rect::new(
rect.min.x / self.atlas.size.x,
rect.min.y / self.atlas.size.y,
rect.max.x / self.atlas.size.x,
rect.max.y / self.atlas.size.y,
)
}
}

#[derive(Resource, Reflect, FromReflect)]
pub struct IlluminatedAtlas {
pub material: Handle<StandardMaterial>,
Expand Down Expand Up @@ -112,6 +125,7 @@ fn check_assets_ready(
let mut texture_atlas_builder = TextureAtlasBuilder::default();

for handle in &asset.handles {
println!("doing {:?}", server.get_handle_path(handle));
let Some(texture) = images.get(&handle) else {
warn!("{:?} did not resolve to an `Image` asset.", server.get_handle_path(handle));
continue;
Expand All @@ -122,7 +136,7 @@ fn check_assets_ready(

let atlas = texture_atlas_builder
.finish(&mut images)
.expect("Built atlas");
.expect("Failed to build atlas");

let material_handle = materials.add(StandardMaterial {
base_color_texture: Some(atlas.texture.clone()),
Expand Down Expand Up @@ -298,6 +312,8 @@ fn load_block_textxures(
}
}

println!("{unlocalized_name}: {map:?}");

registry.register(BlockTextureIndex {
id: 0,
unlocalized_name: unlocalized_name.to_owned(),
Expand Down
21 changes: 11 additions & 10 deletions cosmos_client/src/rendering/structure_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use crate::block::lighting::{BlockLightProperties, BlockLighting};
use crate::materials::CosmosMaterial;
use crate::state::game_state::GameState;
use bevy::prelude::{
Added, App, BuildChildren, Component, DespawnRecursiveExt, EventReader, IntoSystemConfigs,
Mesh, OnUpdate, PbrBundle, PointLight, PointLightBundle, StandardMaterial, Transform, Vec3,
Without,
warn, Added, App, BuildChildren, Component, DespawnRecursiveExt, EventReader,
IntoSystemConfigs, Mesh, OnUpdate, PbrBundle, PointLight, PointLightBundle, StandardMaterial,
Transform, Vec3, Without,
};
use bevy::reflect::{FromReflect, Reflect};
use bevy::render::mesh::{Indices, PrimitiveTopology};
Expand Down Expand Up @@ -91,7 +91,7 @@ impl StructureRenderer {
fn render(
&mut self,
structure: &Structure,
uv_mapper: &UVMapper,
atlas: &MainAtlas,
blocks: &Registry<Block>,
lighting: &Registry<BlockLighting>,
materials: &OneToManyRegistry<Block, CosmosMaterial>,
Expand Down Expand Up @@ -140,7 +140,7 @@ impl StructureRenderer {
};

self.chunk_renderers[flatten(x, y, z, self.width, self.height)].render(
uv_mapper,
atlas,
materials,
lighting,
chunk,
Expand Down Expand Up @@ -405,7 +405,7 @@ fn monitor_needs_rendered_system(

renderer.render(
structure,
&atlas.uv_mapper,
&atlas,
&blocks,
&lighting,
&materials,
Expand Down Expand Up @@ -633,7 +633,7 @@ impl ChunkRenderer {
/// Renders a chunk into mesh information that can then be turned into a bevy mesh
fn render(
&mut self,
uv_mapper: &UVMapper,
atlas: &MainAtlas,
materials: &OneToManyRegistry<Block, CosmosMaterial>,
lighting: &Registry<BlockLighting>,
chunk: &Chunk,
Expand Down Expand Up @@ -771,16 +771,17 @@ impl ChunkRenderer {
});

let Some(image_index) = index.atlas_index_from_face(*face) else {
warn!("Missing image index -- {index:?}");
continue;
};

let uvs = uv_mapper.map(image_index);
let uvs = atlas.uvs_for_index(image_index);

mesh_info.add_mesh_information(
mesh.info_for_face(*face),
[cx, cy, cz],
[uvs[0].x, uvs[0].y],
[uvs[1].x, uvs[1].y],
[uvs.min.x, uvs.min.y],
[uvs.max.x, uvs.max.y],
);
}

Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit d809039

Please sign in to comment.