Skip to content

Instantly share code, notes, and snippets.

@superdump
Created August 26, 2021 10:24
Show Gist options
  • Save superdump/5a02caa6747de6892448dbed749dc767 to your computer and use it in GitHub Desktop.
Save superdump/5a02caa6747de6892448dbed749dc767 to your computer and use it in GitHub Desktop.
Just a bunch of cubes for benchmarking use of inverse transpose model matrix vs not in bevy
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
ecs::prelude::*,
math::{EulerRot, Mat4, Vec3},
pbr2::{DirectionalLight, DirectionalLightBundle, PbrBundle, StandardMaterial},
prelude::{App, Assets, Transform},
render2::{
camera::{OrthographicProjection, PerspectiveCameraBundle},
color::Color,
mesh::{shape, Mesh},
},
window::WindowDescriptor,
PipelinedDefaultPlugins,
};
fn main() {
println!(
"Controls:
WSAD - forward/back/strafe left/right
LShift - 'run'
E - up
Q - down"
);
App::new()
.insert_resource(WindowDescriptor {
vsync: false,
..Default::default()
})
.add_plugins(PipelinedDefaultPlugins)
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(LogDiagnosticsPlugin::default())
.add_startup_system(setup)
.run();
}
/// set up a 3D scene to test shadow biases and perspective projections
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let spawn_grid_size = 46;
let cube_size = 0.25;
// Perspective projection has fov pi/4, so half the fov is pi/8
// tan(pi/8) = half grid size / distance from grid
// distance from grid = half grid size / tan(pi/8)
let camera_z = -(0.5 * spawn_grid_size as f32) / std::f32::consts::FRAC_PI_8.tan();
let white_handle = materials.add(StandardMaterial {
base_color: Color::WHITE,
perceptual_roughness: 1.0,
..Default::default()
});
let cube_handle = meshes.add(Mesh::from(shape::Cube { size: cube_size }));
let theta = std::f32::consts::FRAC_PI_4;
let light_transform = Mat4::from_euler(EulerRot::ZYX, 0.0, std::f32::consts::FRAC_PI_2, -theta);
commands.spawn_bundle(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: 100000.0,
shadow_projection: OrthographicProjection {
left: -0.35,
right: 500.35,
bottom: -0.1,
top: 5.0,
near: -5.0,
far: 5.0,
..Default::default()
},
..Default::default()
},
transform: Transform::from_matrix(light_transform),
..Default::default()
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(
0.5 * (spawn_grid_size - 1) as f32,
0.5 * (spawn_grid_size - 1) as f32,
camera_z,
)
.looking_at(
Vec3::new(
0.5 * (spawn_grid_size - 1) as f32,
0.5 * (spawn_grid_size - 1) as f32,
0.0,
),
Vec3::Y,
),
..Default::default()
});
for z_i32 in 0..spawn_grid_size {
for x_i32 in 0..spawn_grid_size {
for y_i32 in 0..spawn_grid_size {
commands.spawn_bundle(PbrBundle {
mesh: cube_handle.clone(),
material: white_handle.clone(),
transform: Transform::from_xyz(x_i32 as f32, y_i32 as f32, z_i32 as f32),
..Default::default()
});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment