Skip to content

Instantly share code, notes, and snippets.

@theoparis
Created May 16, 2022 09:46
Show Gist options
  • Save theoparis/48449bb5df8bdebfe75bfabb7ec9d865 to your computer and use it in GitHub Desktop.
Save theoparis/48449bb5df8bdebfe75bfabb7ec9d865 to your computer and use it in GitHub Desktop.
Bevy voxel test
[package]
name = "mc-rust"
version = "0.0.1"
authors = ["Theo Paris <[email protected]>"]
edition = "2021"
license = "MIT"
description = "An open source minecraft like voxel world game using rust bevy engine"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
noise = "0.7.0"
env_logger = "0.9.0"
serde = "1.0.137"
bevy-inspector-egui = "0.11.0"
bevy = "0.7.0"
smooth-bevy-cameras = "0.4.0"
[profile.dev]
opt-level = 3
[profile.release]
lto = true
opt-level = 3
incremental = false
codegen-units = 1
use bevy::prelude::*;
use bevy_inspector_egui::WorldInspectorPlugin;
use noise::{NoiseFn, OpenSimplex};
use smooth_bevy_cameras::{
controllers::unreal::{
UnrealCameraBundle, UnrealCameraController, UnrealCameraPlugin,
},
LookTransformPlugin,
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(Msaa { samples: 4 })
.add_plugin(WorldInspectorPlugin::new())
.add_plugin(LookTransformPlugin)
.add_plugin(UnrealCameraPlugin::default())
.add_startup_system(setup)
.insert_resource(Seed { value: 69420.0 })
.run();
}
#[derive(Component)]
pub struct Chunk;
#[derive(Component)]
pub struct Cube;
#[derive(Default)]
pub struct Seed {
value: f64,
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
asset_server: Res<AssetServer>,
seed: Res<Seed>,
) {
commands.spawn_bundle(UnrealCameraBundle::new(
UnrealCameraController::default(),
PerspectiveCameraBundle::default(),
Vec3::new(0.0, 35.0, 5.0),
Vec3::new(0., 0., 0.),
));
let noise = OpenSimplex::new();
// TODO: lighting
commands
.spawn()
.insert_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 1.0 })),
material: materials.add(Color::rgb(0.5, 0.5, 1.0).into()),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
..Default::default()
})
.insert(Chunk)
.with_children(|parent| {
let texture_handle = asset_server.load("textures/dirt.png");
for x in -32..32 {
for z in -32..32 {
let y = (noise.get([
(x as f32 / 20.0) as f64,
(z as f32 / 20.0) as f64,
seed.value,
]) * 15.0 + 16.0) as u32;
parent
.spawn()
.insert_bundle(PbrBundle {
mesh: meshes
.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(StandardMaterial {
base_color: Color::rgba(1.0, 1.0, 1.0, 1.0),
base_color_texture: Some(
texture_handle.clone(),
),
..Default::default()
}),
transform: Transform::from_translation(Vec3::new(
x as f32, y as f32, z as f32,
)),
..Default::default()
})
.insert(Cube);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment