Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

169 create material to support blend transparency #170

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified cosmos_client/assets/images/blocks/ice.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cosmos_client/assets/images/blocks/packed_ice.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified cosmos_client/assets/images/blocks/water.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed cosmos_client/assets/images/cosmos_stone.png
Binary file not shown.
390 changes: 269 additions & 121 deletions cosmos_client/src/asset/asset_loading.rs

Large diffs are not rendered by default.

37 changes: 29 additions & 8 deletions cosmos_client/src/inventory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ use cosmos_core::{
blockitems::BlockItems,
inventory::Inventory,
item::Item,
registry::{identifiable::Identifiable, Registry},
registry::{identifiable::Identifiable, many_to_one::ManyToOneRegistry, Registry},
};

use crate::{
asset::asset_loading::{BlockTextureIndex, MainAtlas},
asset::asset_loading::{BlockTextureIndex, MaterialDefinition},
netty::flags::LocalPlayer,
rendering::{BlockMeshRegistry, CosmosMeshBuilder, MeshBuilder},
state::game_state::GameState,
Expand Down Expand Up @@ -61,7 +61,7 @@ fn render_hotbar(
items: Res<Registry<Item>>,
blocks: Res<Registry<Block>>,

atlas: Res<MainAtlas>,
materials_registry: Res<ManyToOneRegistry<Block, MaterialDefinition>>,
block_textures: Res<Registry<BlockTextureIndex>>,
block_meshes: Res<BlockMeshRegistry>,
) {
Expand Down Expand Up @@ -106,18 +106,39 @@ fn render_hotbar(

let mut mesh_builder = CosmosMeshBuilder::default();

for face in [BlockFace::Top, BlockFace::Left, BlockFace::Back] {
let Some(mut mesh_info) = block_mesh_info.info_for_face(face).cloned() else {
let Some(material) = materials_registry.get_value(block) else {
warn!("Missing material for block {}", block.unlocalized_name());
continue;
};

if block_mesh_info.has_multiple_face_meshes() {
for face in [BlockFace::Top, BlockFace::Left, BlockFace::Back] {
let Some(mut mesh_info) = block_mesh_info.info_for_face(face).cloned() else {
break;
};

mesh_info.scale(Vec3::new(size, size, size));

let Some(image_index) = index.atlas_index_from_face(face) else {
continue;
};

let uvs = material.uvs_for_index(image_index);

mesh_builder.add_mesh_information(&mesh_info, Vec3::ZERO, uvs);
}
} else {
let Some(mut mesh_info) = block_mesh_info.info_for_whole_block().cloned() else {
break;
};

mesh_info.scale(Vec3::new(size, size, size));

let Some(image_index) = index.atlas_index_from_face(face) else {
let Some(image_index) = index.atlas_index("all") else {
continue;
};

let uvs = atlas.uvs_for_index(image_index);
let uvs = material.uvs_for_index(image_index);

mesh_builder.add_mesh_information(&mesh_info, Vec3::ZERO, uvs);
}
Expand All @@ -127,7 +148,7 @@ fn render_hotbar(
.spawn((
PbrBundle {
mesh: meshes.add(mesh_builder.build_mesh()),
material: atlas.unlit_material.clone(),
material: material.unlit_material().clone(),
transform,
..default()
},
Expand Down
80 changes: 27 additions & 53 deletions cosmos_client/src/materials/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,22 @@
use bevy::prelude::*;
use cosmos_core::{
block::Block,
registry::{self, identifiable::Identifiable, many_to_one::ManyToOneRegistry, Registry},
registry::{self, many_to_one::ManyToOneRegistry, Registry},
};

use crate::{
asset::asset_loading::{IlluminatedMaterial, MainAtlas},
state::game_state::GameState,
};

/// An identifiable `StandardMaterial`
pub struct CosmosMaterial {
/// The handle to the bevy `StandardMaterial`
pub handle: Handle<StandardMaterial>,

id: u16,
unlocalized_name: String,
}

impl CosmosMaterial {
/// Creates an identifiable `StandardMaterial`
pub fn new(unlocalized_name: String, handle: Handle<StandardMaterial>) -> Self {
Self {
unlocalized_name,
handle,
id: 0,
}
}
}

impl Identifiable for CosmosMaterial {
fn id(&self) -> u16 {
self.id
}

fn unlocalized_name(&self) -> &str {
&self.unlocalized_name
}

fn set_numeric_id(&mut self, id: u16) {
self.id = id;
}
}
use crate::{asset::asset_loading::MaterialDefinition, state::game_state::GameState};

fn register_materials(
blocks: Res<Registry<Block>>,
mut registry: ResMut<ManyToOneRegistry<Block, CosmosMaterial>>,
main_atlas: Res<MainAtlas>,
illum_atlas: Res<IlluminatedMaterial>,
materials: Res<Registry<MaterialDefinition>>,
mut registry: ResMut<ManyToOneRegistry<Block, MaterialDefinition>>,
) {
registry.insert_value(CosmosMaterial::new("cosmos:main".to_owned(), main_atlas.material.clone()));

registry.insert_value(CosmosMaterial::new("cosmos:illuminated".to_owned(), illum_atlas.material.clone()));

// TODO: Automate this in file or something

for block in blocks.iter() {
if block.unlocalized_name() != "cosmos:light" && block.unlocalized_name() != "cosmos:ship_core" {
registry.add_link(block, "cosmos:main").expect("Main material should exist");
}
for material in materials.iter() {
registry.insert_value(material.clone())
}

// TODO: Specify this in file or something

if let Some(block) = blocks.from_id("cosmos:light") {
registry
.add_link(block, "cosmos:illuminated")
Expand All @@ -74,10 +30,28 @@ fn register_materials(
.add_link(block, "cosmos:illuminated")
.expect("Illuminated material should exist");
}

if let Some(block) = blocks.from_id("cosmos:water") {
registry
.add_link(block, "cosmos:transparent")
.expect("Transparent material should exist");
}

if let Some(block) = blocks.from_id("cosmos:ice") {
registry
.add_link(block, "cosmos:transparent")
.expect("Transparent material should exist");
}

for block in blocks.iter() {
if !registry.contains(block) {
registry.add_link(block, "cosmos:main").expect("Main material should exist");
}
}
}

pub(super) fn register(app: &mut App) {
registry::many_to_one::create_many_to_one_registry::<Block, CosmosMaterial>(app);
registry::many_to_one::create_many_to_one_registry::<Block, MaterialDefinition>(app);

app.add_systems(OnExit(GameState::PostLoading), register_materials);
}
22 changes: 9 additions & 13 deletions cosmos_client/src/rendering/structure_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::block::lighting::{BlockLightProperties, BlockLighting};
use crate::materials::CosmosMaterial;
use crate::netty::flags::LocalPlayer;
use crate::state::game_state::GameState;
use crate::structure::planet::unload_chunks_far_from_players;
Expand Down Expand Up @@ -27,7 +26,7 @@ use std::collections::HashSet;
use std::f32::consts::PI;
use std::sync::Mutex;

use crate::asset::asset_loading::{BlockTextureIndex, MainAtlas};
use crate::asset::asset_loading::{BlockTextureIndex, MaterialDefinition};
use crate::{Assets, Commands, Entity, Handle, Query, Res, ResMut};

use super::{BlockMeshRegistry, CosmosMeshBuilder, MeshBuilder, MeshInformation};
Expand Down Expand Up @@ -160,11 +159,10 @@ struct ChunkMeshes(Vec<Entity>);
fn monitor_needs_rendered_system(
mut commands: Commands,
structure_query: Query<&Structure>,
atlas: Res<MainAtlas>,
mesh_query: Query<Option<&Handle<Mesh>>>,
mut meshes: ResMut<Assets<Mesh>>,
blocks: Res<Registry<Block>>,
materials: Res<ManyToOneRegistry<Block, CosmosMaterial>>,
materials: Res<ManyToOneRegistry<Block, MaterialDefinition>>,
meshes_registry: Res<BlockMeshRegistry>,
lighting: Res<Registry<BlockLighting>>,
lights_query: Query<&LightsHolder>,
Expand Down Expand Up @@ -215,8 +213,8 @@ fn monitor_needs_rendered_system(
// Render chunks in parallel
todo.par_iter().take(chunks_per_frame).copied().for_each(|(entity, ce, _)| {
let Ok(structure) = structure_query.get(ce.structure_entity) else {
return;
};
return;
};

let mut renderer = ChunkRenderer::new();

Expand All @@ -236,7 +234,6 @@ fn monitor_needs_rendered_system(
let front = structure.chunk_from_chunk_coordinates_unbound(unbound.front());

renderer.render(
&atlas,
&materials,
&lighting,
chunk,
Expand Down Expand Up @@ -464,8 +461,7 @@ impl ChunkRenderer {
/// Renders a chunk into mesh information that can then be turned into a bevy mesh
fn render(
&mut self,
atlas: &MainAtlas,
materials: &ManyToOneRegistry<Block, CosmosMaterial>,
materials: &ManyToOneRegistry<Block, MaterialDefinition>,
lighting: &Registry<BlockLighting>,
chunk: &Chunk,
left: Option<&Chunk>,
Expand Down Expand Up @@ -625,11 +621,11 @@ impl ChunkRenderer {
continue;
};

if !self.meshes.contains_key(&material.handle) {
self.meshes.insert(material.handle.clone(), Default::default());
if !self.meshes.contains_key(material.lit_material()) {
self.meshes.insert(material.lit_material().clone(), Default::default());
}

let mesh_builder = self.meshes.get_mut(&material.handle).unwrap();
let mesh_builder = self.meshes.get_mut(material.lit_material()).unwrap();

let rotation = block_info.get_rotation();

Expand All @@ -643,7 +639,7 @@ impl ChunkRenderer {
continue;
};

let uvs = atlas.uvs_for_index(image_index);
let uvs = material.uvs_for_index(image_index);

let rotation = match rotation {
BlockFace::Top => Quat::IDENTITY,
Expand Down
4 changes: 2 additions & 2 deletions cosmos_core/src/block/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn add_cosmos_blocks(

blocks.register(
BlockBuilder::new("cosmos:ice".to_owned(), 2.0)
.add_property(BlockProperty::Opaque)
.add_property(BlockProperty::Transparent)
.add_property(BlockProperty::Full)
.create(),
);
Expand All @@ -142,7 +142,7 @@ fn add_cosmos_blocks(

blocks.register(
BlockBuilder::new("cosmos:water".to_owned(), 6.0)
.add_property(BlockProperty::Opaque)
.add_property(BlockProperty::Transparent)
.add_property(BlockProperty::Full)
.create(),
);
Expand Down
2 changes: 1 addition & 1 deletion cosmos_core/src/structure/ship/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod ship_movement;
pub struct Ship;

pub(super) fn register<T: States + Clone + Copy>(app: &mut App, playing_state: T) {
pilot::regiter(app);
pilot::register(app);
ship_movement::register(app);
core::register(app, playing_state);
ship_builder::register(app);
Expand Down
2 changes: 1 addition & 1 deletion cosmos_core/src/structure/ship/pilot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ pub struct Pilot {
pub entity: Entity,
}

pub(super) fn regiter(app: &mut App) {
pub(super) fn register(app: &mut App) {
app.register_type::<Pilot>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ pub(super) fn register(app: &mut App) {
register_biosphere::<GrassBiosphereMarker, GrassChunkNeedsGeneratedEvent>(
app,
"cosmos:biosphere_grass",
TemperatureRange::new(50.0, 5000.0),
TemperatureRange::new(250.0, 400.0),
);

app.add_systems(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub fn generate_chunk_features(
}

pub(super) fn register(app: &mut App) {
register_biosphere::<IceBiosphereMarker, IceChunkNeedsGeneratedEvent>(app, "cosmos:biosphere_ice", TemperatureRange::new(0.0, 1.0));
register_biosphere::<IceBiosphereMarker, IceChunkNeedsGeneratedEvent>(app, "cosmos:biosphere_ice", TemperatureRange::new(0.0, 300.0));

app.add_systems(
Update,
Expand Down