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

Fix normal map bit depth #172

Merged
merged 3 commits into from
Feb 20, 2023
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
4 changes: 2 additions & 2 deletions assets/scenes/old_town.glb
Git LFS file not shown
4 changes: 2 additions & 2 deletions resources/half_timbered_house.blend
Git LFS file not shown
4 changes: 2 additions & 2 deletions resources/old_town.blend
Git LFS file not shown
8 changes: 6 additions & 2 deletions src/level_instantiation/spawning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use crate::level_instantiation::spawning::objects::primitives::{
};
use crate::level_instantiation::spawning::objects::skydome::SkydomeSpawner;
use crate::level_instantiation::spawning::objects::sunlight::SunlightSpawner;
use crate::level_instantiation::spawning::post_spawn_modification::{despawn_removed, set_hidden};
use crate::level_instantiation::spawning::post_spawn_modification::{
despawn_removed, generate_tangents, set_color, set_hidden,
};
use crate::level_instantiation::spawning::spawn::{
despawn, spawn_delayed, spawn_requested, DelayedSpawnEvents, Despawn,
};
Expand Down Expand Up @@ -62,7 +64,9 @@ impl Plugin for SpawningPlugin {
.add_system_set(
SystemSet::on_update(GameState::Playing)
.with_system(set_hidden)
.with_system(despawn_removed),
.with_system(despawn_removed)
.with_system(generate_tangents)
.with_system(set_color.pipe(log_errors)),
);
}
}
Expand Down
59 changes: 59 additions & 0 deletions src/level_instantiation/spawning/post_spawn_modification.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::level_instantiation::spawning::spawn::Despawn;
use anyhow::{Context, Result};
use bevy::prelude::*;
use regex::Regex;
use std::sync::LazyLock;

pub fn set_hidden(mut added_name: Query<(&Name, &mut Visibility), Added<Name>>) {
for (name, mut visibility) in added_name.iter_mut() {
Expand All @@ -19,3 +22,59 @@ pub fn despawn_removed(
}
}
}

/// Needed for normal mapping,
/// see [`StandardMaterial::normal_map_texture`](https://docs.rs/bevy/latest/bevy/pbr/struct.StandardMaterial.html#structfield.normal_map_texture).
pub fn generate_tangents(
mut mesh_asset_events: EventReader<AssetEvent<Mesh>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
for event in mesh_asset_events.iter() {
if let AssetEvent::Created { handle } = event {
// Guaranteed to work because we just created the mesh
let mesh = meshes
.get_mut(handle)
.expect("Failed to get mesh even though it was just created");
if let Err(e) = mesh.generate_tangents() {
warn!("Failed to generate tangents for mesh: {}", e);
}
}
}
}

static COLOR_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"\[color:\s*(\d+),\s*(\d+),\s*(\d+),\s*(\d+)\]")
.expect("Failed to compile color regex")
});

pub fn set_color(
added_name: Query<(&Name, &Children), Added<Name>>,
material_handles: Query<&Handle<StandardMaterial>>,
mut standard_materials: ResMut<Assets<StandardMaterial>>,
) -> Result<()> {
for (name, children) in added_name.iter() {
if let Some(captures) = COLOR_REGEX.captures(&name.to_lowercase()) {
let color = Color::rgba_u8(
captures[1].parse().expect("Failed to parse color"),
captures[2].parse().expect("Failed to parse color"),
captures[3].parse().expect("Failed to parse color"),
captures[4].parse().expect("Failed to parse color"),
);
let material_handle = children
.iter()
.filter_map(|entity| material_handles.get(*entity).ok())
.next()
.with_context(|| {
format!(
"Failed to find child containing material handle when setting color on: {}",
name,
)
})?;
let material = standard_materials
.get_mut(material_handle)
.context("Failed to get standard material from handle")?;
material.base_color = color;
}
}
Ok(())
}