Last active
August 3, 2022 10:36
-
-
Save DGriffin91/2374c65d6f4f742a2450576c6d159d2e to your computer and use it in GitHub Desktop.
Mesh with many quads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// License: Apache-2.0 / MIT | |
//camera controller from https://github.com/DGriffin91/bevy_basic_camera | |
use bevy::{ | |
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, | |
prelude::*, | |
render::render_resource::PrimitiveTopology, | |
}; | |
use bevy_basic_camera::{CameraController, CameraControllerPlugin}; | |
fn main() { | |
App::new() | |
.add_plugins(DefaultPlugins) | |
.add_startup_system(setup) | |
.add_plugin(CameraControllerPlugin) | |
.add_plugin(LogDiagnosticsPlugin::default()) | |
.add_plugin(FrameTimeDiagnosticsPlugin::default()) | |
.run(); | |
} | |
#[inline] | |
fn quad(x: f32, y: f32, z: f32, w: f32, h: f32) -> Vec<[f32; 3]> { | |
vec![[x, y, z], [x, y + h, z], [x + w, y, z], [x + w, y + h, z]] | |
} | |
/// set up a simple 3D scene | |
fn setup( | |
mut commands: Commands, | |
mut meshes: ResMut<Assets<Mesh>>, | |
mut materials: ResMut<Assets<StandardMaterial>>, | |
) { | |
let now = std::time::Instant::now(); | |
let size = 200; | |
let mut positions = Vec::with_capacity(size * size * size * 4); | |
let mut indices = Vec::with_capacity(size * size * size * 6); | |
let mut colors = Vec::with_capacity(size * size * size * 4); | |
let mut i = 0; | |
for x in 0..size { | |
for y in 0..size { | |
for z in 0..size { | |
let fx = x as f32; | |
let fy = y as f32; | |
let fz = z as f32; | |
positions.append(&mut quad(fx, fy, fz, 0.2, 0.2)); | |
indices.append(&mut vec![i, i + 3, i + 1, i, i + 2, i + 3]); | |
colors.append(&mut vec![ | |
[ | |
(fx * 0.1).sin() * 0.5 + 0.5, | |
(fy * 0.1).sin() * 0.5 + 0.5, | |
(fz * 0.1).sin() * 0.5 + 0.5, | |
1.0 | |
]; | |
4 | |
]); | |
i += 4; | |
} | |
} | |
} | |
let normals = vec![[0.0, 1.0, 0.0]; positions.len()]; | |
let uvs = vec![[1.0, 1.0]; positions.len()]; | |
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList); | |
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions); | |
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals); | |
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs); | |
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, colors); | |
mesh.set_indices(Some(bevy::render::mesh::Indices::U32(indices))); | |
println!("Time to generate mesh: {:.2?}", now.elapsed()); | |
commands | |
// plane | |
.spawn_bundle(PbrBundle { | |
mesh: meshes.add(mesh), | |
material: materials.add(StandardMaterial { | |
unlit: true, | |
..default() | |
}), | |
transform: Transform::from_translation(Vec3::new(-100.0, -100.0, -200.0)), | |
..Default::default() | |
}); | |
// Camera | |
commands | |
.spawn_bundle(Camera3dBundle { | |
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), | |
..default() | |
}) | |
.insert(CameraController { | |
run_speed: 100.0, | |
..default() | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment