Skip to content

Instantly share code, notes, and snippets.

@mxgrey
Created February 28, 2023 13:00
Show Gist options
  • Select an option

  • Save mxgrey/615c3d53d93d14046b6377b5efd1a0e8 to your computer and use it in GitHub Desktop.

Select an option

Save mxgrey/615c3d53d93d14046b6377b5efd1a0e8 to your computer and use it in GitHub Desktop.
z-fighting test for depth bias
[package]
name = "depth_bias_test"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = "0.9"
use std::f32::consts::PI;
use bevy::{
prelude::*,
render::mesh::shape::Cube,
window::close_on_esc,
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_system(oscillate)
.add_system(close_on_esc)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let red = materials.add(StandardMaterial {
base_color: Color::RED,
depth_bias: 0.0,
..default()
});
let cyan = materials.add(StandardMaterial {
base_color: Color::CYAN,
depth_bias: 1.0,
..default()
});
commands
.spawn(PbrBundle {
mesh: meshes.add(Cube::new(1.0).into()),
material: red,
..default()
});
commands
.spawn(PbrBundle {
mesh: meshes.add(Cube::new(1.0).into()),
material: cyan,
transform: Transform::from_xyz(0.2, 0.0, 0.0),
..default()
});
commands
.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
..default()
},
transform: Transform::from_xyz(4.0, 4.0, 8.0),
..default()
});
commands
.spawn(SpatialBundle::default())
.insert(Oscillate {
lower: -PI * 30.0 / 180.0,
upper: PI * 30.0 / 180.0,
rate: 3.0 / (2.0 * PI),
axis: Vec3::X,
})
.with_children(|p| {
p.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Z),
..default()
});
});
}
#[derive(Component)]
struct Oscillate {
lower: f32,
upper: f32,
rate: f32,
axis: Vec3,
}
fn oscillate(
mut oscillating: Query<(&mut Transform, &Oscillate)>,
time: Res<Time>,
) {
for (mut tf, osc) in &mut oscillating {
let angle = (osc.upper - osc.lower) * (osc.rate * time.elapsed_seconds()).cos() + osc.lower;
tf.rotation = Quat::from_axis_angle(osc.axis, angle);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment