| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use std::fmt; |
| 5 |
|
| 6 |
use bevy::{image::ImageLoaderSettings, math::ops, prelude::*}; |
| 7 |
|
| 8 |
fn main() { |
| 9 |
App::new() |
| 10 |
.add_plugins(DefaultPlugins) |
| 11 |
.add_systems(Startup, setup) |
| 12 |
.add_systems( |
| 13 |
Update, |
| 14 |
( |
| 15 |
spin, |
| 16 |
move_camera, |
| 17 |
update_parallax_depth_scale, |
| 18 |
update_parallax_layers, |
| 19 |
switch_method, |
| 20 |
), |
| 21 |
) |
| 22 |
.run(); |
| 23 |
} |
| 24 |
|
| 25 |
#[derive(Component)] |
| 26 |
struct Spin { |
| 27 |
speed: f32, |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
#[derive(Component)] |
| 32 |
struct FreeCameraController; |
| 33 |
|
| 34 |
const DEPTH_CHANGE_RATE: f32 = 0.1; |
| 35 |
const DEPTH_UPDATE_STEP: f32 = 0.03; |
| 36 |
const MAX_DEPTH: f32 = 0.3; |
| 37 |
|
| 38 |
struct TargetDepth(f32); |
| 39 |
impl Default for TargetDepth { |
| 40 |
fn default() -> Self { |
| 41 |
TargetDepth(0.09) |
| 42 |
} |
| 43 |
} |
| 44 |
struct TargetLayers(f32); |
| 45 |
impl Default for TargetLayers { |
| 46 |
fn default() -> Self { |
| 47 |
TargetLayers(5.0) |
| 48 |
} |
| 49 |
} |
| 50 |
struct CurrentMethod(ParallaxMappingMethod); |
| 51 |
impl Default for CurrentMethod { |
| 52 |
fn default() -> Self { |
| 53 |
CurrentMethod(ParallaxMappingMethod::Relief { max_steps: 4 }) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
impl fmt::Display for CurrentMethod { |
| 58 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 59 |
match self.0 { |
| 60 |
ParallaxMappingMethod::Occlusion => write!(f, "Parallax Occlusion Mapping"), |
| 61 |
ParallaxMappingMethod::Relief { max_steps } => { |
| 62 |
write!(f, "Relief Mapping with {max_steps} steps") |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
impl CurrentMethod { |
| 69 |
fn next_method(&mut self) { |
| 70 |
use ParallaxMappingMethod::*; |
| 71 |
self.0 = match self.0 { |
| 72 |
Occlusion => Relief { max_steps: 2 }, |
| 73 |
Relief { max_steps } if max_steps < 3 => Relief { max_steps: 4 }, |
| 74 |
Relief { max_steps } if max_steps < 5 => Relief { max_steps: 8 }, |
| 75 |
Relief { .. } => Occlusion, |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
fn update_parallax_depth_scale( |
| 81 |
input: Res<ButtonInput<KeyCode>>, |
| 82 |
mut materials: ResMut<Assets<StandardMaterial>>, |
| 83 |
mut target_depth: Local<TargetDepth>, |
| 84 |
mut depth_update: Local<bool>, |
| 85 |
mut writer: TextUiWriter, |
| 86 |
text: Single<Entity, With<Text>>, |
| 87 |
) { |
| 88 |
if input.just_pressed(KeyCode::Digit1) { |
| 89 |
target_depth.0 -= DEPTH_UPDATE_STEP; |
| 90 |
target_depth.0 = target_depth.0.max(0.0); |
| 91 |
*depth_update = true; |
| 92 |
} |
| 93 |
if input.just_pressed(KeyCode::Digit2) { |
| 94 |
target_depth.0 += DEPTH_UPDATE_STEP; |
| 95 |
target_depth.0 = target_depth.0.min(MAX_DEPTH); |
| 96 |
*depth_update = true; |
| 97 |
} |
| 98 |
if *depth_update { |
| 99 |
for (_, mat) in materials.iter_mut() { |
| 100 |
let current_depth = mat.parallax_depth_scale; |
| 101 |
let new_depth = current_depth.lerp(target_depth.0, DEPTH_CHANGE_RATE); |
| 102 |
mat.parallax_depth_scale = new_depth; |
| 103 |
*writer.text(*text, 1) = format!("Parallax depth scale: {new_depth:.5}\n"); |
| 104 |
if (new_depth - current_depth).abs() <= 0.000000001 { |
| 105 |
*depth_update = false; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
fn switch_method( |
| 112 |
input: Res<ButtonInput<KeyCode>>, |
| 113 |
mut materials: ResMut<Assets<StandardMaterial>>, |
| 114 |
text: Single<Entity, With<Text>>, |
| 115 |
mut writer: TextUiWriter, |
| 116 |
mut current: Local<CurrentMethod>, |
| 117 |
) { |
| 118 |
if input.just_pressed(KeyCode::Space) { |
| 119 |
current.next_method(); |
| 120 |
} else { |
| 121 |
return; |
| 122 |
} |
| 123 |
let text_entity = *text; |
| 124 |
*writer.text(text_entity, 3) = format!("Method: {}\n", *current); |
| 125 |
|
| 126 |
for (_, mat) in materials.iter_mut() { |
| 127 |
mat.parallax_mapping_method = current.0; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
fn update_parallax_layers( |
| 132 |
input: Res<ButtonInput<KeyCode>>, |
| 133 |
mut materials: ResMut<Assets<StandardMaterial>>, |
| 134 |
mut target_layers: Local<TargetLayers>, |
| 135 |
text: Single<Entity, With<Text>>, |
| 136 |
mut writer: TextUiWriter, |
| 137 |
) { |
| 138 |
if input.just_pressed(KeyCode::Digit3) { |
| 139 |
target_layers.0 -= 1.0; |
| 140 |
target_layers.0 = target_layers.0.max(0.0); |
| 141 |
} else if input.just_pressed(KeyCode::Digit4) { |
| 142 |
target_layers.0 += 1.0; |
| 143 |
} else { |
| 144 |
return; |
| 145 |
} |
| 146 |
let layer_count = ops::exp2(target_layers.0); |
| 147 |
let text_entity = *text; |
| 148 |
*writer.text(text_entity, 2) = format!("Layers: {layer_count:.0}\n"); |
| 149 |
|
| 150 |
for (_, mat) in materials.iter_mut() { |
| 151 |
mat.max_parallax_layer_count = layer_count; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
fn spin(time: Res<Time>, mut query: Query<(&mut Transform, &Spin)>) { |
| 156 |
for (mut transform, spin) in query.iter_mut() { |
| 157 |
transform.rotate_local_y(spin.speed * time.delta_secs()); |
| 158 |
transform.rotate_local_x(spin.speed * time.delta_secs()); |
| 159 |
transform.rotate_local_z(-spin.speed * time.delta_secs()); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
const CAMERA_POSITIONS: &[Transform] = &[ |
| 165 |
Transform { |
| 166 |
translation: Vec3::new(1.5, 1.5, 1.5), |
| 167 |
rotation: Quat::from_xyzw(-0.279, 0.364, 0.115, 0.880), |
| 168 |
scale: Vec3::ONE, |
| 169 |
}, |
| 170 |
Transform { |
| 171 |
translation: Vec3::new(2.4, 0.0, 0.2), |
| 172 |
rotation: Quat::from_xyzw(0.094, 0.676, 0.116, 0.721), |
| 173 |
scale: Vec3::ONE, |
| 174 |
}, |
| 175 |
Transform { |
| 176 |
translation: Vec3::new(2.4, 2.6, -4.3), |
| 177 |
rotation: Quat::from_xyzw(0.170, 0.908, 0.308, 0.225), |
| 178 |
scale: Vec3::ONE, |
| 179 |
}, |
| 180 |
Transform { |
| 181 |
translation: Vec3::new(-1.0, 0.8, -1.2), |
| 182 |
rotation: Quat::from_xyzw(-0.004, 0.909, 0.247, -0.335), |
| 183 |
scale: Vec3::ONE, |
| 184 |
}, |
| 185 |
]; |
| 186 |
|
| 187 |
fn move_camera( |
| 188 |
mut camera: Single<&mut Transform, With<FreeCameraController>>, |
| 189 |
mut current_view: Local<usize>, |
| 190 |
button: Res<ButtonInput<MouseButton>>, |
| 191 |
) { |
| 192 |
if button.just_pressed(MouseButton::Left) { |
| 193 |
*current_view = (*current_view + 1) % CAMERA_POSITIONS.len(); |
| 194 |
} |
| 195 |
let target = CAMERA_POSITIONS[*current_view]; |
| 196 |
camera.translation = camera.translation.lerp(target.translation, 0.2); |
| 197 |
camera.rotation = camera.rotation.slerp(target.rotation, 0.2); |
| 198 |
} |
| 199 |
|
| 200 |
fn setup( |
| 201 |
mut commands: Commands, |
| 202 |
mut materials: ResMut<Assets<StandardMaterial>>, |
| 203 |
mut meshes: ResMut<Assets<Mesh>>, |
| 204 |
asset_server: Res<AssetServer>, |
| 205 |
) { |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
let normal_handle = asset_server |
| 210 |
.load_builder() |
| 211 |
.with_settings( |
| 212 |
|
| 213 |
|
| 214 |
|settings: &mut ImageLoaderSettings| settings.is_srgb = false, |
| 215 |
) |
| 216 |
.load("textures/parallax_example/cube_normal.png"); |
| 217 |
|
| 218 |
|
| 219 |
commands.spawn(( |
| 220 |
Camera3d::default(), |
| 221 |
Transform::from_xyz(1.5, 1.5, 1.5).looking_at(Vec3::ZERO, Vec3::Y), |
| 222 |
FreeCameraController, |
| 223 |
)); |
| 224 |
|
| 225 |
|
| 226 |
let mesh = meshes.add(Sphere::new(0.05).mesh().ico(3).unwrap()); |
| 227 |
|
| 228 |
|
| 229 |
commands.spawn(( |
| 230 |
PointLight { |
| 231 |
shadow_maps_enabled: true, |
| 232 |
..default() |
| 233 |
}, |
| 234 |
Transform::from_xyz(2.0, 1.0, -1.1), |
| 235 |
children![(Mesh3d(mesh), MeshMaterial3d(materials.add(Color::WHITE)))], |
| 236 |
)); |
| 237 |
|
| 238 |
|
| 239 |
commands.spawn(( |
| 240 |
Mesh3d(meshes.add(Plane3d::default().mesh().size(10.0, 10.0))), |
| 241 |
MeshMaterial3d(materials.add(StandardMaterial { |
| 242 |
|
| 243 |
|
| 244 |
perceptual_roughness: 0.45, |
| 245 |
reflectance: 0.18, |
| 246 |
..Color::srgb_u8(0, 80, 0).into() |
| 247 |
})), |
| 248 |
Transform::from_xyz(0.0, -1.0, 0.0), |
| 249 |
)); |
| 250 |
|
| 251 |
let parallax_depth_scale = TargetDepth::default().0; |
| 252 |
let max_parallax_layer_count = ops::exp2(TargetLayers::default().0); |
| 253 |
let parallax_mapping_method = CurrentMethod::default(); |
| 254 |
let parallax_material = materials.add(StandardMaterial { |
| 255 |
perceptual_roughness: 0.4, |
| 256 |
base_color_texture: Some(asset_server.load("textures/parallax_example/cube_color.png")), |
| 257 |
normal_map_texture: Some(normal_handle), |
| 258 |
|
| 259 |
|
| 260 |
depth_map: Some(asset_server.load("textures/parallax_example/cube_depth.png")), |
| 261 |
parallax_depth_scale, |
| 262 |
parallax_mapping_method: parallax_mapping_method.0, |
| 263 |
max_parallax_layer_count, |
| 264 |
..default() |
| 265 |
}); |
| 266 |
commands.spawn(( |
| 267 |
Mesh3d( |
| 268 |
meshes.add( |
| 269 |
|
| 270 |
|
| 271 |
Mesh::from(Cuboid::default()) |
| 272 |
.with_generated_tangents() |
| 273 |
.unwrap(), |
| 274 |
), |
| 275 |
), |
| 276 |
MeshMaterial3d(parallax_material.clone()), |
| 277 |
Spin { speed: 0.3 }, |
| 278 |
)); |
| 279 |
|
| 280 |
let background_cube = meshes.add( |
| 281 |
Mesh::from(Cuboid::new(40.0, 40.0, 40.0)) |
| 282 |
.with_generated_tangents() |
| 283 |
.unwrap(), |
| 284 |
); |
| 285 |
|
| 286 |
let background_cube_bundle = |translation| { |
| 287 |
( |
| 288 |
Mesh3d(background_cube.clone()), |
| 289 |
MeshMaterial3d(parallax_material.clone()), |
| 290 |
Transform::from_translation(translation), |
| 291 |
Spin { speed: -0.1 }, |
| 292 |
) |
| 293 |
}; |
| 294 |
commands.spawn(background_cube_bundle(Vec3::new(45., 0., 0.))); |
| 295 |
commands.spawn(background_cube_bundle(Vec3::new(-45., 0., 0.))); |
| 296 |
commands.spawn(background_cube_bundle(Vec3::new(0., 0., 45.))); |
| 297 |
commands.spawn(background_cube_bundle(Vec3::new(0., 0., -45.))); |
| 298 |
|
| 299 |
|
| 300 |
commands.spawn(( |
| 301 |
Text::default(), |
| 302 |
Node { |
| 303 |
position_type: PositionType::Absolute, |
| 304 |
top: px(12), |
| 305 |
left: px(12), |
| 306 |
..default() |
| 307 |
}, |
| 308 |
children![ |
| 309 |
(TextSpan(format!("Parallax depth scale: {parallax_depth_scale:.5}\n"))), |
| 310 |
(TextSpan(format!("Layers: {max_parallax_layer_count:.0}\n"))), |
| 311 |
(TextSpan(format!("{parallax_mapping_method}\n"))), |
| 312 |
(TextSpan::new("\n\n")), |
| 313 |
(TextSpan::new("Controls:\n")), |
| 314 |
(TextSpan::new("Left click - Change view angle\n")), |
| 315 |
(TextSpan::new("1/2 - Decrease/Increase parallax depth scale\n",)), |
| 316 |
(TextSpan::new("3/4 - Decrease/Increase layer count\n")), |
| 317 |
(TextSpan::new("Space - Switch parallaxing algorithm\n")), |
| 318 |
], |
| 319 |
)); |
| 320 |
} |