diff --git a/Cargo.toml b/Cargo.toml index d573e64..65ed012 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bevy_transform_gizmo" -version = "0.11.2" +version = "0.12.0" authors = [ "Aevyrie Roessler ", "Foresight Mining Software Corporation", @@ -14,18 +14,18 @@ documentation = "https://docs.rs/bevy_transform_gizmo" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bevy = { version = "0.13.1", default-features = false, features = [ +bevy = { version = "0.14.0", default-features = false, features = [ "bevy_render", "bevy_core_pipeline", "bevy_pbr", ] } -bevy_mod_picking = { version = ">=0.18, <=0.19", default-features = false, features = [ +bevy_mod_picking = { version = ">=0.18, <=0.20", default-features = false, features = [ "selection", ] } -bevy_mod_raycast = "0.17" +bevy_mod_raycast = "0.18" [dev-dependencies] -bevy = { version = "0.13.1", default-features = false, features = [ +bevy = { version = "0.14.0", default-features = false, features = [ "bevy_pbr", "bevy_winit", "x11", @@ -33,7 +33,7 @@ bevy = { version = "0.13.1", default-features = false, features = [ "ktx2", "zstd", ] } -bevy_mod_picking = { version = ">=0.18, <=0.19", default-features = false, features = [ +bevy_mod_picking = { version = ">=0.18, <=0.20", default-features = false, features = [ "selection", "backend_raycast", "highlight", diff --git a/examples/minimal.rs b/examples/minimal.rs index 60def91..1950d5f 100644 --- a/examples/minimal.rs +++ b/examples/minimal.rs @@ -35,7 +35,7 @@ fn setup( commands.spawn(( PbrBundle { mesh: meshes.add(Plane3d::default()), - material: materials.add(Color::rgb(0.8, 0.8, 0.8)), + material: materials.add(Color::srgb(0.8, 0.8, 0.8)), transform: Transform::from_scale(Vec3::splat(5.0)), ..Default::default() }, @@ -46,7 +46,7 @@ fn setup( commands.spawn(( PbrBundle { mesh: meshes.add(Cuboid::from_size(Vec3::splat(1.0))), - material: materials.add(Color::rgb(0.8, 0.8, 0.8)), + material: materials.add(Color::srgb(0.8, 0.8, 0.8)), transform: Transform::from_xyz(0.0, 0.5, 0.0), ..Default::default() }, diff --git a/examples/parenting.rs b/examples/parenting.rs index b315fe2..3657bc4 100644 --- a/examples/parenting.rs +++ b/examples/parenting.rs @@ -30,7 +30,7 @@ fn setup( commands.spawn(( PbrBundle { mesh: meshes.add(Plane3d::default()), - material: materials.add(Color::rgb(0.3, 0.5, 0.3)), + material: materials.add(Color::srgb(0.3, 0.5, 0.3)), transform: Transform::from_translation(Vec3::new(0.0, -0.5, 0.0)) .with_scale(Vec3::splat(5.0)), ..Default::default() @@ -39,8 +39,8 @@ fn setup( bevy_transform_gizmo::GizmoTransformable, )); - let tan = Color::rgb_u8(204, 178, 153); - let red = Color::rgb_u8(127, 26, 26); + let tan = Color::srgb_u8(204, 178, 153); + let red = Color::srgb_u8(127, 26, 26); // cube commands diff --git a/src/gizmo_material.rs b/src/gizmo_material.rs index 69fc683..bdb212d 100644 --- a/src/gizmo_material.rs +++ b/src/gizmo_material.rs @@ -3,7 +3,7 @@ use bevy::{ prelude::*, reflect::TypePath, render::{ - mesh::MeshVertexBufferLayout, + mesh::MeshVertexBufferLayoutRef, render_resource::{ AsBindGroup, RenderPipelineDescriptor, ShaderRef, SpecializedMeshPipelineError, }, @@ -15,12 +15,14 @@ pub const GIZMO_SHADER_HANDLE: Handle = Handle::weak_from_u128(139538002 #[derive(Asset, TypePath, AsBindGroup, Debug, Clone)] pub struct GizmoMaterial { #[uniform(0)] - pub color: Color, + pub color: LinearRgba, } impl From for GizmoMaterial { fn from(color: Color) -> Self { - GizmoMaterial { color } + GizmoMaterial { + color: color.into(), + } } } @@ -40,7 +42,7 @@ impl Material for GizmoMaterial { fn specialize( _pipeline: &MaterialPipeline, descriptor: &mut RenderPipelineDescriptor, - _layout: &MeshVertexBufferLayout, + _layout: &MeshVertexBufferLayoutRef, _key: MaterialPipelineKey, ) -> Result<(), SpecializedMeshPipelineError> { descriptor.primitive.cull_mode = None; diff --git a/src/gizmo_material.wgsl b/src/gizmo_material.wgsl index 0b5bbe1..da88f4f 100644 --- a/src/gizmo_material.wgsl +++ b/src/gizmo_material.wgsl @@ -1,4 +1,4 @@ -#import bevy_pbr::mesh_functions::{get_model_matrix, mesh_position_local_to_clip} +#import bevy_pbr::mesh_functions::{get_world_from_local, mesh_position_local_to_clip} struct GizmoMaterial { color: vec4, @@ -20,7 +20,7 @@ struct VertexOutput { fn vertex(vertex: Vertex) -> VertexOutput { var out: VertexOutput; out.clip_position = mesh_position_local_to_clip( - get_model_matrix(vertex.instance_index), + get_world_from_local(vertex.instance_index), vec4(vertex.position, 1.0), ); return out; diff --git a/src/lib.rs b/src/lib.rs index 930a544..0c4c8c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ use bevy_mod_picking::{ prelude::{PickingInteraction, PointerId}, selection::{NoDeselect, PickSelection}, }; -use bevy_mod_raycast::prelude::{Primitive3d, RaycastSystem}; +use bevy_mod_raycast::prelude::RaycastSystem; use gizmo_material::GizmoMaterial; use mesh::{RotationGizmo, ViewTranslateGizmo}; use normalization::*; @@ -280,15 +280,15 @@ fn drag_gizmo( let vertical_vector = picking_ray.direction.cross(axis).normalize(); let plane_normal = axis.cross(vertical_vector).normalize(); let plane_origin = gizmo_origin; - let cursor_plane_intersection = if let Some(intersection) = picking_camera - .intersect_primitive(Primitive3d::Plane { - normal: plane_normal, - point: plane_origin, - }) { - intersection.position() + let cursor_plane_intersection = if let Some(intesect_point) = picking_camera + .get_ray() + .and_then(|ray| intersect_plane(ray, plane_normal, plane_origin)) + { + intesect_point } else { return; }; + let cursor_vector: Vec3 = cursor_plane_intersection - plane_origin; let cursor_projected_onto_handle = match &gizmo.drag_start { Some(drag_start) => *drag_start, @@ -321,11 +321,10 @@ fn drag_gizmo( TransformGizmoInteraction::TranslatePlane { normal, .. } => { let plane_origin = gizmo_origin; let cursor_plane_intersection = if let Some(intersection) = picking_camera - .intersect_primitive(Primitive3d::Plane { - normal, - point: plane_origin, - }) { - intersection.position() + .get_ray() + .and_then(|ray| intersect_plane(ray, normal, plane_origin)) + { + intersection } else { return; }; @@ -351,14 +350,11 @@ fn drag_gizmo( ); } TransformGizmoInteraction::RotateAxis { original: _, axis } => { - let rotation_plane = Primitive3d::Plane { - normal: axis.normalize(), - point: gizmo_origin, - }; - let cursor_plane_intersection = if let Some(intersection) = - picking_camera.intersect_primitive(rotation_plane) + let cursor_plane_intersection = if let Some(intersection) = picking_camera + .get_ray() + .and_then(|ray| intersect_plane(ray, axis.normalize(), gizmo_origin)) { - intersection.position() + intersection } else { return; }; @@ -398,6 +394,19 @@ fn drag_gizmo( } } +fn intersect_plane(ray: Ray3d, plane_normal: Vec3, plane_origin: Vec3) -> Option { + // assuming vectors are all normalized + let denominator = ray.direction.dot(plane_normal); + if denominator.abs() > f32::EPSILON { + let point_to_point = plane_origin - ray.origin; + let intersect_dist = plane_normal.dot(point_to_point) / denominator; + let intersect_position = ray.direction * intersect_dist + ray.origin; + Some(intersect_position) + } else { + None + } +} + fn hover_gizmo( gizmo_raycast_source: Query<(Entity, &GizmoPickSource)>, mut gizmo_query: Query<(