-
Notifications
You must be signed in to change notification settings - Fork 0
/
outlines.rs
128 lines (117 loc) · 4.37 KB
/
outlines.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use bevy::input::mouse::MouseWheel;
use bevy::pbr::{ExtendedMaterial, MaterialExtension};
use bevy::prelude::*;
use bevy::ecs::schedule::SystemSet;
use outlines::outline::{Outline, OutlineLabel};
use outlines::rim_effect::RimEffect;
/// Adds outlines support and systems for updating outlines behaviour
pub struct OutlinesPlugin;
impl Plugin for OutlinesPlugin
{
fn build(&self, app: &mut App)
{
add_outline::< RimEffect >( app );
}
}
/// Used for ordering outline initialization systems
#[ derive( SystemSet, Debug, Hash, PartialEq, Eq, Clone ) ]
pub struct OutlineInitSet;
/// Incapsulates all systems scheduling, plugins, types registering for O outline
fn add_outline< O : OutlineLabel >( app : &mut App )
where
Outline< O > : MaterialExtension,
MaterialPlugin::< ExtendedMaterial< StandardMaterial, Outline< O > > >: Plugin
{
O::load_shader(app);
app.add_plugins(
MaterialPlugin::< ExtendedMaterial< StandardMaterial, Outline< O > > >::default( )
)
.register_type::< Outline< O > >()
.add_systems( Update, (
change_time_scale::< O >,
change_outline_width::< O >,
set_mode::< O >,
update_material_time::< O >
));
}
/// Updates time for every O outline material
fn update_material_time< O : OutlineLabel >(
time : Res< Time >,
mut materials : ResMut< Assets< ExtendedMaterial< StandardMaterial, Outline< O > > > >,
)
where Outline< O > : MaterialExtension{
for ( _, material ) in materials.iter_mut( ) {
material.extension.add_time( material.extension.time_scale * time.delta_seconds( ) );
}
}
/// For rim_effect outline width is power of Fresnel saturation
fn change_outline_width< O : OutlineLabel >(
keyboard_input : Res< ButtonInput< KeyCode > >,
mut mouse_wheel_events: EventReader< MouseWheel >,
mut materials : ResMut< Assets< ExtendedMaterial< StandardMaterial, Outline< O > > > >,
query: Query<&Handle<ExtendedMaterial< StandardMaterial, Outline< O > >>>
)
where Outline< O > : MaterialExtension{
if keyboard_input.pressed( KeyCode::KeyW ){
for event in mouse_wheel_events.read( ){
for handle in query.iter(){
if let Some(material) = materials.get_mut(handle){
if event.y > 0.0 {
if material.extension.width < 10000.0{
material.extension.width *= 1.25;
}
}else {
if material.extension.width > 0.1{
material.extension.width /= 1.25;
}
}
info!( "Width(power): {}", material.extension.width );
}
}
}
}
}
/// Switch width change state: (const width)/(time depended)
fn set_mode< O : OutlineLabel >(
keyboard_input : Res< ButtonInput< KeyCode > >,
mut materials : ResMut< Assets< ExtendedMaterial< StandardMaterial, Outline< O > > > >,
query: Query<&Handle<ExtendedMaterial< StandardMaterial, Outline< O > >>>
)
where Outline< O > : MaterialExtension
{
if keyboard_input.pressed( KeyCode::KeyR ){
for handle in query.iter(){
if let Some(material) = materials.get_mut(handle){
let last = material.extension.is_time_related;
material.extension.is_time_related = ( !( last > 0 ) ) as u32;
}
}
}
}
/// Set outline animation speed
pub fn change_time_scale< O : OutlineLabel >(
keyboard_input : Res< ButtonInput< KeyCode > >,
mut mouse_wheel_events: EventReader< MouseWheel >,
mut materials : ResMut< Assets< ExtendedMaterial< StandardMaterial, Outline< O >>>>,
query: Query<&Handle<ExtendedMaterial< StandardMaterial, Outline< O > >>>
)
where Outline< O > : MaterialExtension
{
if keyboard_input.pressed( KeyCode::KeyT )
{
for event in mouse_wheel_events.read( )
{
for handle in query.iter(){
if let Some(material) = materials.get_mut(handle){
if event.y > 0.0 {
material.extension.time_scale *= 1.25;
}
else {
material.extension.time_scale /= 1.25;
}
info!( "Time scale: {}", material.extension.time_scale );
}
}
}
}
}