Add a Sphere to anisotropy
example (#17676)
# Objective Add a mesh with a more regular shape to visualize the anisotropy effect. ## Solution Add a `Sphere` to the scene. ## Testing Ran `anisotropy` example ## Showcase   Note: defects are already mentioned on #16179
This commit is contained in:
parent
1b2cf7d6cd
commit
fdc7cb3031
@ -1,7 +1,13 @@
|
|||||||
//! Demonstrates anisotropy with the glTF sample barn lamp model.
|
//! Demonstrates anisotropy with the glTF sample barn lamp model.
|
||||||
|
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
use bevy::{
|
use bevy::{
|
||||||
color::palettes::css::WHITE, core_pipeline::Skybox, math::vec3, prelude::*, time::Stopwatch,
|
color::palettes::{self, css::WHITE},
|
||||||
|
core_pipeline::Skybox,
|
||||||
|
math::vec3,
|
||||||
|
prelude::*,
|
||||||
|
time::Stopwatch,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The initial position of the camera.
|
/// The initial position of the camera.
|
||||||
@ -14,6 +20,8 @@ struct AppStatus {
|
|||||||
light_mode: LightMode,
|
light_mode: LightMode,
|
||||||
/// Whether anisotropy is enabled.
|
/// Whether anisotropy is enabled.
|
||||||
anisotropy_enabled: bool,
|
anisotropy_enabled: bool,
|
||||||
|
/// Which mesh is visible
|
||||||
|
visible_scene: Scene,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Which type of light we're using: a directional light, a point light, or an
|
/// Which type of light we're using: a directional light, a point light, or an
|
||||||
@ -43,6 +51,32 @@ struct MaterialVariants {
|
|||||||
isotropic: Handle<StandardMaterial>,
|
isotropic: Handle<StandardMaterial>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Clone, Copy, PartialEq, Eq, Component)]
|
||||||
|
enum Scene {
|
||||||
|
#[default]
|
||||||
|
BarnLamp,
|
||||||
|
Sphere,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Scene {
|
||||||
|
fn next(&self) -> Self {
|
||||||
|
match self {
|
||||||
|
Self::BarnLamp => Self::Sphere,
|
||||||
|
Self::Sphere => Self::BarnLamp,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for Scene {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let scene_name = match self {
|
||||||
|
Self::BarnLamp => "Barn Lamp",
|
||||||
|
Self::Sphere => "Sphere",
|
||||||
|
};
|
||||||
|
write!(f, "{scene_name}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The application entry point.
|
/// The application entry point.
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
@ -74,6 +108,25 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, app_status: Res
|
|||||||
commands.spawn((
|
commands.spawn((
|
||||||
SceneRoot(asset_server.load("models/AnisotropyBarnLamp/AnisotropyBarnLamp.gltf#Scene0")),
|
SceneRoot(asset_server.load("models/AnisotropyBarnLamp/AnisotropyBarnLamp.gltf#Scene0")),
|
||||||
Transform::from_xyz(0.0, 0.07, -0.13),
|
Transform::from_xyz(0.0, 0.07, -0.13),
|
||||||
|
Scene::BarnLamp,
|
||||||
|
));
|
||||||
|
|
||||||
|
commands.spawn((
|
||||||
|
Mesh3d(
|
||||||
|
asset_server.add(
|
||||||
|
Mesh::from(Sphere::new(0.1))
|
||||||
|
.with_generated_tangents()
|
||||||
|
.unwrap(),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
MeshMaterial3d(asset_server.add(StandardMaterial {
|
||||||
|
base_color: palettes::tailwind::GRAY_300.into(),
|
||||||
|
anisotropy_rotation: 0.5,
|
||||||
|
anisotropy_strength: 1.,
|
||||||
|
..default()
|
||||||
|
})),
|
||||||
|
Scene::Sphere,
|
||||||
|
Visibility::Hidden,
|
||||||
));
|
));
|
||||||
|
|
||||||
spawn_text(&mut commands, &app_status);
|
spawn_text(&mut commands, &app_status);
|
||||||
@ -162,6 +215,7 @@ fn handle_input(
|
|||||||
cameras: Query<Entity, With<Camera>>,
|
cameras: Query<Entity, With<Camera>>,
|
||||||
lights: Query<Entity, Or<(With<DirectionalLight>, With<PointLight>)>>,
|
lights: Query<Entity, Or<(With<DirectionalLight>, With<PointLight>)>>,
|
||||||
mut meshes: Query<(&mut MeshMaterial3d<StandardMaterial>, &MaterialVariants)>,
|
mut meshes: Query<(&mut MeshMaterial3d<StandardMaterial>, &MaterialVariants)>,
|
||||||
|
mut scenes: Query<(&mut Visibility, &Scene)>,
|
||||||
keyboard: Res<ButtonInput<KeyCode>>,
|
keyboard: Res<ButtonInput<KeyCode>>,
|
||||||
mut app_status: ResMut<AppStatus>,
|
mut app_status: ResMut<AppStatus>,
|
||||||
) {
|
) {
|
||||||
@ -218,6 +272,18 @@ fn handle_input(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if keyboard.just_pressed(KeyCode::KeyQ) {
|
||||||
|
app_status.visible_scene = app_status.visible_scene.next();
|
||||||
|
for (mut visibility, scene) in scenes.iter_mut() {
|
||||||
|
let new_vis = if *scene == app_status.visible_scene {
|
||||||
|
Visibility::Inherited
|
||||||
|
} else {
|
||||||
|
Visibility::Hidden
|
||||||
|
};
|
||||||
|
*visibility = new_vis;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A system that updates the help text based on the current app status.
|
/// A system that updates the help text based on the current app status.
|
||||||
@ -283,11 +349,15 @@ impl AppStatus {
|
|||||||
LightMode::EnvironmentMap => "Press Space to switch to a directional light",
|
LightMode::EnvironmentMap => "Press Space to switch to a directional light",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Choose the appropriate help text for the scene selector.
|
||||||
|
let mesh_help_text = format!("Press Q to change to {}", self.visible_scene.next());
|
||||||
|
|
||||||
// Build the `Text` object.
|
// Build the `Text` object.
|
||||||
Text(format!(
|
format!(
|
||||||
"{}\n{}",
|
"{}\n{}\n{}",
|
||||||
material_variant_help_text, light_help_text
|
material_variant_help_text, light_help_text, mesh_help_text,
|
||||||
))
|
)
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,6 +366,7 @@ impl Default for AppStatus {
|
|||||||
Self {
|
Self {
|
||||||
light_mode: default(),
|
light_mode: default(),
|
||||||
anisotropy_enabled: true,
|
anisotropy_enabled: true,
|
||||||
|
visible_scene: default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user