Skip to content

Instantly share code, notes, and snippets.

@thebluefish
Created April 19, 2023 18:54
Show Gist options
  • Save thebluefish/0e8149a25ed7ffba6d860d4a315b21bb to your computer and use it in GitHub Desktop.
Save thebluefish/0e8149a25ed7ffba6d860d4a315b21bb to your computer and use it in GitHub Desktop.
use bevy::prelude::*;
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use std::{env, fs};
use std::path::Path;
fn main() {
App::new()
.add_plugin(DevAssetPlugin::default())
.add_plugins(DefaultPlugins)
.add_plugin(WorldInspectorPlugin::new())
.add_startup_system(setup)
.run()
}
#[derive(Debug, Clone)]
pub struct DevAssetPlugin {
/// The maximum number of parents we will search for /assets in
pub max_depth: usize,
}
impl Default for DevAssetPlugin {
fn default() -> Self {
Self {
max_depth: 4,
}
}
}
impl DevAssetPlugin {
pub fn new(max_depth: usize) -> Self {
Self {
max_depth,
}
}
}
impl Plugin for DevAssetPlugin {
fn build(&self, app: &mut App) {
if let Err(_) = env::var("CARGO_MANIFEST_DIR") {
match env::current_dir() {
Ok(cwd) => {
let mut depth = self.max_depth;
let mut path = cwd;
while depth > 0 {
let assets = path.join("assets");
if assets.exists() {
match assets.to_str() {
None => println!("Failed to get string from path: {path:?}"),
Some(dir) => {
println!("Found /assets in {dir}");
env::set_var("CARGO_MANIFEST_DIR", path);
break
}
}
}
match path.parent() {
None => {
println!("Could not find /assets in CWD or any parent directory");
break
}
Some(p) => {
path = p.clone().into();
}
}
depth -= 1;
}
}
Err(e) => println!("Failed to grab CWD, unable to link /assets"),
}
}
}
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(0.7, 0.7, 1.0)
.looking_at(Vec3::new(0.0, 0.3, 0.0), Vec3::Y),
..default()
},
EnvironmentMapLight {
diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
},
));
commands.spawn(SceneBundle {
scene: asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"),
..default()
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment