|
| 1 | +use bevy::ecs::component::Component; |
| 2 | +use bevy::math::{Affine3A, Mat4, UVec2, Vec4Swizzles}; |
| 3 | +use bevy::prelude::*; |
| 4 | +use bevy::render::camera::Viewport; |
| 5 | +use bevy::render::extract_component::ExtractComponent; |
| 6 | +use bevy::render::primitives::Aabb; |
| 7 | + |
| 8 | +use crate::computed::ComputedOutline; |
| 9 | +use crate::uniforms::DrawMode; |
| 10 | + |
| 11 | +/// Component for storing mesh bounds information needed for scissoring jump-flood operations |
| 12 | +#[derive(Clone, Component)] |
| 13 | +pub struct FloodMeshBounds { |
| 14 | + /// The axis-aligned bounding box of the mesh in model space |
| 15 | + pub aabb: Aabb, |
| 16 | + /// The world-from-local transform for the entity |
| 17 | + pub world_from_local: Affine3A, |
| 18 | +} |
| 19 | + |
| 20 | +impl FloodMeshBounds { |
| 21 | + pub fn calculate_screen_space_bounds( |
| 22 | + &self, |
| 23 | + clip_from_world: &Mat4, |
| 24 | + viewport: &Viewport, |
| 25 | + border: u32, |
| 26 | + ) -> Option<URect> { |
| 27 | + // Get the 8 corners of the AABB |
| 28 | + let min = self.aabb.min(); |
| 29 | + let max = self.aabb.max(); |
| 30 | + let corners = [ |
| 31 | + Vec3::new(min.x, min.y, min.z), |
| 32 | + Vec3::new(min.x, min.y, max.z), |
| 33 | + Vec3::new(min.x, max.y, min.z), |
| 34 | + Vec3::new(min.x, max.y, max.z), |
| 35 | + Vec3::new(max.x, min.y, min.z), |
| 36 | + Vec3::new(max.x, min.y, max.z), |
| 37 | + Vec3::new(max.x, max.y, min.z), |
| 38 | + Vec3::new(max.x, max.y, max.z), |
| 39 | + ]; |
| 40 | + |
| 41 | + // Initialize min/max screen coordinates with extreme values |
| 42 | + let mut min_screen = UVec2::MAX; |
| 43 | + let mut max_screen = UVec2::MIN; |
| 44 | + |
| 45 | + // Use the physical target size for width and height |
| 46 | + let width = viewport.physical_size.x; |
| 47 | + let height = viewport.physical_size.y; |
| 48 | + |
| 49 | + // Project each corner to screen space and update min/max values |
| 50 | + for corner in corners.iter() { |
| 51 | + // Convert corner to world space |
| 52 | + let world_pos = self.world_from_local.transform_point3(*corner); |
| 53 | + |
| 54 | + // Project to clip space |
| 55 | + let clip_pos = *clip_from_world * world_pos.extend(1.0); |
| 56 | + |
| 57 | + // Perspective divide to get NDC coordinates |
| 58 | + let ndc = clip_pos.xyz() / clip_pos.w; |
| 59 | + |
| 60 | + // Skip if behind camera |
| 61 | + if ndc.z < -1.0 || ndc.z > 1.0 { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + // Convert from NDC to pixel coordinates |
| 66 | + let screen_x = ((ndc.x + 1.0) * 0.5 * width as f32).max(0.0) as u32; |
| 67 | + let screen_y = ((-ndc.y + 1.0) * 0.5 * height as f32).max(0.0) as u32; |
| 68 | + |
| 69 | + min_screen.x = min_screen.x.min(screen_x); |
| 70 | + min_screen.y = min_screen.y.min(screen_y); |
| 71 | + max_screen.x = max_screen.x.max(screen_x); |
| 72 | + max_screen.y = max_screen.y.max(screen_y); |
| 73 | + } |
| 74 | + |
| 75 | + // If nothing is in view, return None |
| 76 | + if min_screen.x >= max_screen.x || min_screen.y >= max_screen.y { |
| 77 | + None |
| 78 | + } else { |
| 79 | + Some(URect::new( |
| 80 | + viewport.physical_position.x + min_screen.x.saturating_sub(border).min(width), |
| 81 | + viewport.physical_position.y + min_screen.y.saturating_sub(border).min(height), |
| 82 | + viewport.physical_position.x + (max_screen.x + border).min(width), |
| 83 | + viewport.physical_position.y + (max_screen.y + border).min(height), |
| 84 | + )) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl ExtractComponent for FloodMeshBounds { |
| 90 | + type QueryData = ( |
| 91 | + &'static Aabb, |
| 92 | + &'static GlobalTransform, |
| 93 | + &'static ComputedOutline, |
| 94 | + ); |
| 95 | + type QueryFilter = (); |
| 96 | + type Out = Self; |
| 97 | + |
| 98 | + fn extract_component( |
| 99 | + (aabb, global_transform, computed_outline): bevy::ecs::query::QueryItem<Self::QueryData>, |
| 100 | + ) -> Option<Self::Out> { |
| 101 | + let Some(computed_outline) = &computed_outline.0 else { |
| 102 | + return None; |
| 103 | + }; |
| 104 | + |
| 105 | + match computed_outline.mode.value.draw_mode { |
| 106 | + DrawMode::JumpFlood => Some(FloodMeshBounds { |
| 107 | + aabb: *aabb, |
| 108 | + world_from_local: global_transform.affine(), |
| 109 | + }), |
| 110 | + _ => None, |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments