This commit is contained in:
1 parent
f29712d158
commit
3e053a81ab
123 files changed
+31930
-1404
No files matched your search
@@ -2,10 +2,17 @@
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use bevy::asset::{load_internal_binary_asset, uuid_handle, RenderAssetUsages};
|
||||
use bevy::asset::{
|
||||
load_internal_asset, load_internal_binary_asset, uuid_handle, RenderAssetUsages,
|
||||
};
|
||||
use bevy::camera::visibility::NoFrustumCulling;
|
||||
use bevy::image::{CompressedImageFormats, ImageSampler, ImageType};
|
||||
use bevy::pbr::{Material, MaterialPlugin};
|
||||
use bevy::prelude::*;
|
||||
use bevy::render::render_resource::{
|
||||
AsBindGroup, BlendState, CompareFunction, RenderPipelineDescriptor, ShaderType,
|
||||
};
|
||||
use bevy::shader::ShaderRef;
|
||||
use shared::{
|
||||
ActorKind, AuthoringLightKind, ColliderDesc, LevelObject, LightDesc, ModelRef, ObjectiveMarker,
|
||||
PhysicsBody, PlayerSpawn, PostProcessVolumeDesc, PrefabInstance, PrefabRef, Primitive,
|
||||
@@ -19,11 +26,13 @@ use crate::ui::UiState;
|
||||
use crate::viewport::ViewportDisplayMode;
|
||||
use crate::visualizers::EditorVisualizationSettings;
|
||||
|
||||
const ICON_TARGET_PIXELS: f32 = 30.0;
|
||||
const ICON_MIN_WORLD_SIZE: f32 = 0.2;
|
||||
const ICON_MAX_WORLD_SIZE: f32 = 3.0;
|
||||
const ICON_MIN_WORLD_SIZE: f32 = 0.001;
|
||||
const ICON_MAX_WORLD_SIZE: f32 = 10_000.0;
|
||||
const ICON_BRIGHTNESS: f32 = 1.6;
|
||||
const DEFAULT_PERSPECTIVE_FOV: f32 = std::f32::consts::FRAC_PI_4;
|
||||
|
||||
const ACTOR_ICON_OVERLAY_SHADER: Handle<Shader> =
|
||||
uuid_handle!("2edfe544-3ed1-4e13-a880-f56b09804b52");
|
||||
const ICON_01_ACTOR_EMPTY: Handle<Image> = uuid_handle!("d87c7796-b219-4011-8305-4db55422f1c7");
|
||||
const ICON_02_ACTOR_GENERIC: Handle<Image> = uuid_handle!("c9a1a84f-4dcd-4a2b-a5b6-c365dc7d6795");
|
||||
const ICON_03_ACTOR_MISSING: Handle<Image> = uuid_handle!("cca7cdf9-0c7d-4546-9bb9-1176e49a635c");
|
||||
@@ -69,6 +78,85 @@ pub struct ActorIconProxy {
|
||||
pub target: Entity,
|
||||
}
|
||||
|
||||
#[derive(Resource, Debug, Clone)]
|
||||
pub struct ActorIconSettings {
|
||||
pub size_pixels: f32,
|
||||
}
|
||||
|
||||
impl ActorIconSettings {
|
||||
pub const DEFAULT_SIZE_PIXELS: f32 = 34.0;
|
||||
pub const MIN_SIZE_PIXELS: f32 = 16.0;
|
||||
pub const MAX_SIZE_PIXELS: f32 = 96.0;
|
||||
|
||||
pub fn clamped_size_pixels(&self) -> f32 {
|
||||
self.size_pixels
|
||||
.clamp(Self::MIN_SIZE_PIXELS, Self::MAX_SIZE_PIXELS)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ActorIconSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
size_pixels: Self::DEFAULT_SIZE_PIXELS,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, ShaderType)]
|
||||
struct ActorIconOverlayUniform {
|
||||
brightness: f32,
|
||||
}
|
||||
|
||||
#[derive(Asset, TypePath, AsBindGroup, Clone)]
|
||||
struct ActorIconOverlayMaterial {
|
||||
#[texture(0)]
|
||||
#[sampler(1)]
|
||||
texture: Handle<Image>,
|
||||
#[uniform(2)]
|
||||
uniform: ActorIconOverlayUniform,
|
||||
}
|
||||
|
||||
impl Material for ActorIconOverlayMaterial {
|
||||
fn fragment_shader() -> ShaderRef {
|
||||
ACTOR_ICON_OVERLAY_SHADER.into()
|
||||
}
|
||||
|
||||
fn alpha_mode(&self) -> AlphaMode {
|
||||
AlphaMode::Blend
|
||||
}
|
||||
|
||||
fn enable_prepass() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn enable_shadows() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn specialize(
|
||||
_pipeline: &bevy::pbr::MaterialPipeline,
|
||||
descriptor: &mut RenderPipelineDescriptor,
|
||||
_layout: &bevy::mesh::MeshVertexBufferLayoutRef,
|
||||
_key: bevy::pbr::MaterialPipelineKey<Self>,
|
||||
) -> Result<(), bevy::render::render_resource::SpecializedMeshPipelineError> {
|
||||
descriptor.primitive.cull_mode = None;
|
||||
if let Some(depth) = &mut descriptor.depth_stencil {
|
||||
depth.depth_write_enabled = false;
|
||||
depth.depth_compare = CompareFunction::Always;
|
||||
}
|
||||
if let Some(fragment) = &mut descriptor.fragment {
|
||||
if let Some(target) = fragment
|
||||
.targets
|
||||
.first_mut()
|
||||
.and_then(|target| target.as_mut())
|
||||
{
|
||||
target.blend = Some(BlendState::ALPHA_BLENDING);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
enum ActorIconImage {
|
||||
Empty,
|
||||
@@ -126,7 +214,7 @@ struct ActorIconComponents {
|
||||
#[derive(Resource)]
|
||||
struct ActorIconAssets {
|
||||
mesh: Handle<Mesh>,
|
||||
materials: HashMap<ActorIconImage, Handle<StandardMaterial>>,
|
||||
materials: HashMap<ActorIconImage, Handle<ActorIconOverlayMaterial>>,
|
||||
}
|
||||
|
||||
pub struct ActorIconsPlugin;
|
||||
@@ -134,7 +222,16 @@ pub struct ActorIconsPlugin;
|
||||
impl Plugin for ActorIconsPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
register_actor_icon_images(app);
|
||||
app.add_systems(Startup, init_actor_icon_assets)
|
||||
load_internal_asset!(
|
||||
app,
|
||||
ACTOR_ICON_OVERLAY_SHADER,
|
||||
"../../assets/shaders/actor_icon_overlay.wgsl",
|
||||
Shader::from_wgsl
|
||||
);
|
||||
|
||||
app.init_resource::<ActorIconSettings>()
|
||||
.add_plugins(MaterialPlugin::<ActorIconOverlayMaterial>::default())
|
||||
.add_systems(Startup, init_actor_icon_assets)
|
||||
.add_systems(Update, sync_actor_icons.run_if(scene_tools_active));
|
||||
}
|
||||
}
|
||||
@@ -204,7 +301,7 @@ fn register_actor_icon_images(app: &mut App) {
|
||||
fn init_actor_icon_assets(
|
||||
mut commands: Commands,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
mut materials: ResMut<Assets<ActorIconOverlayMaterial>>,
|
||||
) {
|
||||
let mesh = meshes.add(Rectangle::new(1.0, 1.0));
|
||||
let mut actor_materials = HashMap::new();
|
||||
@@ -229,14 +326,11 @@ fn init_actor_icon_assets(
|
||||
] {
|
||||
actor_materials.insert(
|
||||
image,
|
||||
materials.add(StandardMaterial {
|
||||
base_color_texture: Some(texture.clone()),
|
||||
alpha_mode: AlphaMode::Blend,
|
||||
unlit: true,
|
||||
cull_mode: None,
|
||||
fog_enabled: false,
|
||||
depth_bias: -40.0,
|
||||
..default()
|
||||
materials.add(ActorIconOverlayMaterial {
|
||||
texture: texture.clone(),
|
||||
uniform: ActorIconOverlayUniform {
|
||||
brightness: ICON_BRIGHTNESS,
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -251,13 +345,14 @@ fn init_actor_icon_assets(
|
||||
fn sync_actor_icons(
|
||||
display: Res<ViewportDisplayMode>,
|
||||
settings: Res<EditorVisualizationSettings>,
|
||||
icon_settings: Res<ActorIconSettings>,
|
||||
ui_state: Res<UiState>,
|
||||
assets: Res<ActorIconAssets>,
|
||||
mut commands: Commands,
|
||||
mut icons: Query<(
|
||||
Entity,
|
||||
&ActorIconProxy,
|
||||
&MeshMaterial3d<StandardMaterial>,
|
||||
&MeshMaterial3d<ActorIconOverlayMaterial>,
|
||||
&mut Transform,
|
||||
)>,
|
||||
camera_query: Query<(&Camera, &GlobalTransform, Option<&Projection>), With<EditorCamera>>,
|
||||
@@ -325,7 +420,12 @@ fn sync_actor_icons(
|
||||
if !icon_category_enabled(&settings, spec.category) {
|
||||
continue;
|
||||
}
|
||||
let transform = desired_icon_transform(global, camera, ui_state.viewport_rect.height());
|
||||
let transform = desired_icon_transform(
|
||||
global,
|
||||
camera,
|
||||
ui_state.viewport_rect.height(),
|
||||
icon_settings.clamped_size_pixels(),
|
||||
);
|
||||
desired.insert(target, (spec.image, transform));
|
||||
}
|
||||
}
|
||||
@@ -390,7 +490,12 @@ fn sync_actor_icons(
|
||||
let Some(material) = assets.materials.get(&spec.image).cloned() else {
|
||||
continue;
|
||||
};
|
||||
let transform = desired_icon_transform(global, camera, ui_state.viewport_rect.height());
|
||||
let transform = desired_icon_transform(
|
||||
global,
|
||||
camera,
|
||||
ui_state.viewport_rect.height(),
|
||||
icon_settings.clamped_size_pixels(),
|
||||
);
|
||||
commands.spawn((
|
||||
EditorOnly,
|
||||
RaytracingExcluded,
|
||||
@@ -419,6 +524,7 @@ fn desired_icon_transform(
|
||||
parent_global: &GlobalTransform,
|
||||
camera: Option<(&Camera, &GlobalTransform, Option<&Projection>)>,
|
||||
viewport_height: f32,
|
||||
target_pixels: f32,
|
||||
) -> Transform {
|
||||
let translation = parent_global.translation();
|
||||
let Some((_, camera_global, projection)) = camera else {
|
||||
@@ -429,6 +535,7 @@ fn desired_icon_transform(
|
||||
camera_global,
|
||||
projection,
|
||||
viewport_height.max(1.0),
|
||||
target_pixels,
|
||||
);
|
||||
let desired_global = Transform {
|
||||
translation,
|
||||
@@ -444,18 +551,20 @@ fn icon_world_size(
|
||||
camera_global: &GlobalTransform,
|
||||
projection: Option<&Projection>,
|
||||
viewport_height: f32,
|
||||
target_pixels: f32,
|
||||
) -> f32 {
|
||||
let camera_position = camera_global.translation();
|
||||
let pixels_to_view = ICON_TARGET_PIXELS / viewport_height;
|
||||
let pixels_to_view = target_pixels / viewport_height;
|
||||
let world_height = match projection {
|
||||
Some(Projection::Perspective(projection)) => {
|
||||
let distance = camera_position.distance(target).max(0.1);
|
||||
2.0 * distance * (projection.fov * 0.5).tan()
|
||||
let target_view = camera_global.affine().inverse().transform_point3(target);
|
||||
let depth = (-target_view.z).abs().max(0.1);
|
||||
2.0 * depth * (projection.fov * 0.5).tan()
|
||||
}
|
||||
Some(Projection::Orthographic(projection)) => projection.area.height().abs().max(0.1),
|
||||
_ => {
|
||||
let distance = camera_position.distance(target).max(0.1);
|
||||
2.0 * distance * (DEFAULT_PERSPECTIVE_FOV * 0.5).tan()
|
||||
let target_view = camera_global.affine().inverse().transform_point3(target);
|
||||
let depth = (-target_view.z).abs().max(0.1);
|
||||
2.0 * depth * (DEFAULT_PERSPECTIVE_FOV * 0.5).tan()
|
||||
}
|
||||
};
|
||||
(world_height * pixels_to_view).clamp(ICON_MIN_WORLD_SIZE, ICON_MAX_WORLD_SIZE)
|
||||
@@ -653,4 +762,29 @@ mod tests {
|
||||
assert_eq!(spec.image, ActorIconImage::PhysicsCollider);
|
||||
assert_eq!(spec.category, ActorIconCategory::Physics);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn perspective_icon_size_uses_view_depth() {
|
||||
let camera = GlobalTransform::IDENTITY;
|
||||
let projection = Projection::Perspective(PerspectiveProjection {
|
||||
fov: DEFAULT_PERSPECTIVE_FOV,
|
||||
..default()
|
||||
});
|
||||
let centered = icon_world_size(
|
||||
Vec3::new(0.0, 0.0, -12.0),
|
||||
&camera,
|
||||
Some(&projection),
|
||||
900.0,
|
||||
ActorIconSettings::DEFAULT_SIZE_PIXELS,
|
||||
);
|
||||
let offset = icon_world_size(
|
||||
Vec3::new(8.0, 0.0, -12.0),
|
||||
&camera,
|
||||
Some(&projection),
|
||||
900.0,
|
||||
ActorIconSettings::DEFAULT_SIZE_PIXELS,
|
||||
);
|
||||
|
||||
assert_eq!(centered, offset);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user