Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazers #131

Merged
merged 2 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ rust-version.workspace = true

[workspace]
resolver = "2"
members = [
"crates/dummy",
]
members = ["crates/bevy_suis_lasers", "crates/dummy"]

# These settings will apply to all members of the workspace that opt in to them
[workspace.package]
Expand All @@ -25,8 +23,6 @@ rust-version = "1.81.0"
[workspace.dependencies]




[dependencies]
uuid = "1.10.0"
bevy_derive = "0.14.2"
Expand All @@ -38,20 +34,20 @@ bincode = "1.3.3"
avian3d = { version = "0.1.2", features = ["serialize"] }
bevy_embedded_assets = "0.11.0"
bevy_web_file_drop = "0.0.6"
bevy_vr_controller = { path = "../bevy_vr_controller"}
bevy_vr_controller = { path = "../bevy_vr_controller" }
bevy_mod_openxr = { git = "https://github.com/awtterpip/bevy_oxr" }
bevy_xr_utils = { git = "https://github.com/awtterpip/bevy_oxr"}
bevy_xr_utils = { git = "https://github.com/awtterpip/bevy_oxr" }
bevy_spatial_egui = { path = "../bevy_spatial_egui" }
bevy_egui = "0.29.0"
bevy-suis = { git = "https://github.com/Schmarni-Dev/bevy-suis", branch = "further_work" }
bevy_vrm = "0.0.12"
egui-aesthetix = {version = "0.2.4", features = ["tokyo_night", "default"]}
egui-aesthetix = { version = "0.2.4", features = ["tokyo_night", "default"] }

[profile.release]
lto = true
codegen-units = 1
opt-level = "z"
panic="abort"
panic = "abort"

[profile.dev]
opt-level = 3
16 changes: 16 additions & 0 deletions crates/bevy_suis_lasers/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "bevy_suis_lasers"
version = "0.1.0"
edition.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true

[dependencies]
# really should pin a commit here
bevy-suis = { git = "https://github.com/Schmarni-Dev/bevy-suis", branch = "further_work" }
bevy = { version = "0.14", default-features = false }

[dev-dependencies]
bevy = { version = "0.14", default-features = true }
bevy_mod_openxr.git = "https://github.com/awtterpip/bevy_oxr"
32 changes: 32 additions & 0 deletions crates/bevy_suis_lasers/examples/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use bevy::prelude::*;
use bevy_mod_openxr::add_xr_plugins;
use bevy_suis::{
debug::SuisDebugGizmosPlugin, window_pointers::SuisWindowPointerPlugin,
xr_controllers::SuisXrControllerPlugin, CaptureContext, Field, InputHandler,
SuisCorePlugin,
};
use bevy_suis_lasers::{
draw_lasers::{Laser, LaserPlugin},
laser_input_methods::LaserInputMethodPlugin,
};
fn main() -> AppExit {
App::new()
.add_plugins(add_xr_plugins(DefaultPlugins))
.add_plugins((
SuisCorePlugin,
SuisXrControllerPlugin,
SuisWindowPointerPlugin,
SuisDebugGizmosPlugin,
))
.add_plugins((LaserPlugin, LaserInputMethodPlugin))
.add_systems(Startup, setup)
.run()
}

fn setup(mut cmds: Commands) {
cmds.spawn(SpatialBundle::from_transform(Transform::from_xyz(
0.0, 1.0, -2.0,
)))
.insert(Field::Cuboid(Cuboid::from_size(Vec3::splat(0.25))))
.insert(InputHandler::new(|_: In<CaptureContext>| true));
}
152 changes: 152 additions & 0 deletions crates/bevy_suis_lasers/src/draw_lasers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
use bevy::prelude::*;
pub struct LaserPlugin;

impl Plugin for LaserPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, update_laser);
app.add_systems(Startup, setup);
}
}

#[derive(Component)]
pub struct Laser {
pub ray: Ray3d,
pub current_dir: Dir3,
pub length: f32,
}

#[derive(Component)]
pub struct LaserSegment;

#[derive(Resource)]
struct LaserSegmentStuff {
mesh: Handle<Mesh>,
material: Handle<StandardMaterial>,
}

impl Laser {
pub fn new() -> Laser {
Laser {
ray: Ray3d::new(Vec3::ZERO, Vec3::NEG_Z),
current_dir: Dir3::NEG_Z,
length: 1.0,
}
}
}

const TOTAL_LENGTH: f32 = SEGMENT_LENGTH + GAP_LENGTH;

const SEGMENT_LENGTH: f32 = 0.05;
const GAP_LENGTH: f32 = 0.03;
const INTERPOLATION_SPEED: f32 = 5.0;

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let capsule_mesh = meshes.add(Mesh::from(Capsule3d::new(0.005, SEGMENT_LENGTH)));

let mat = StandardMaterial {
base_color: Color::srgb(8.0, 0.2, 0.2),
emissive: LinearRgba::rgb(8.0, 0.0, 0.0),
perceptual_roughness: 0.0,
metallic: 0.0,
reflectance: 0.0,
diffuse_transmission: 0.0,
specular_transmission: 0.0,
thickness: 0.0,
ior: 0.0,
attenuation_distance: 0.0,
clearcoat: 0.0,
clearcoat_perceptual_roughness: 0.0,
anisotropy_strength: 0.0,
anisotropy_rotation: 0.0,
unlit: true,
..default()
};

let capsule_material = materials.add(mat);

commands.insert_resource(LaserSegmentStuff {
mesh: capsule_mesh,
material: capsule_material,
});
}
const K: u32 = 20; // Adjust every 10 segments

fn update_laser(
mut commands: Commands,
mut laser_query: Query<(Entity, &mut Laser)>,
laser_segment_stuff: Res<LaserSegmentStuff>,
time: Res<Time>,
) {
for (entity, mut laser) in laser_query.iter_mut() {
// Remove existing laser segments
commands.entity(entity).despawn_descendants();

laser.current_dir = laser.current_dir.slerp(
laser.ray.direction,
time.delta_seconds() * INTERPOLATION_SPEED,
);

// Calculate length
let length = laser.length;

// Avoid division by zero
if length <= 0.0 {
continue;
}

// Direction from start to current_end
let laser_direction = *laser.current_dir;

let mut position_along = 0.0;
let mut segment_count = 0;
let mut level = 0;
let mut segment_length = SEGMENT_LENGTH;
let mut gap_length = GAP_LENGTH;

while position_along < length {
// Adjust segment length and gap length every K segments
if segment_count > 0 && segment_count % K == 0 {
level += 1;
segment_length *= 2.0;
gap_length *= 2.0;
}

// Check if adding this segment exceeds the laser length
if position_along + segment_length / 2.0 > length {
break;
}

// Compute segment position
let segment_position =
laser.ray.origin + laser_direction * (position_along + segment_length / 2.0);

// Compute rotation to align with laser_direction
let rotation = Quat::from_rotation_arc(Vec3::Y, laser_direction);

// Scale the segment mesh along Y to represent the new length
let mut transform = Transform {
translation: segment_position,
rotation,
scale: Vec3::new(1.0, segment_length / SEGMENT_LENGTH, 1.0),
..Default::default()
};

let bundle = PbrBundle {
mesh: laser_segment_stuff.mesh.clone_weak(),
material: laser_segment_stuff.material.clone_weak(),
transform,
..Default::default()
};
let child = commands.spawn(bundle).id();
commands.entity(entity).add_child(child);

// Update position_along
position_along += segment_length + gap_length;
segment_count += 1;
}
}
}
Loading
Loading