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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use crate::{
handle_properties, handle_property_changed,
inspector::handlers::node::base::handle_base_property_changed, scene::commands::camera::*,
SceneCommand,
};
use fyrox::{
core::{futures::executor::block_on, pool::Handle},
gui::inspector::{FieldKind, PropertyChanged},
resource::texture::{Texture, TextureWrapMode},
scene::{
camera::{
Camera, ColorGradingLut, Exposure, OrthographicProjection, PerspectiveProjection,
SkyBox, SkyBoxBuilder,
},
node::Node,
},
};
use std::any::TypeId;
#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(u32)]
enum SkyBoxFace {
Left,
Right,
Top,
Bottom,
Front,
Back,
}
fn modify_skybox(
camera: &Camera,
texture: Option<Texture>,
face: SkyBoxFace,
) -> Option<Box<SkyBox>> {
if let Some(skybox) = camera.skybox_ref() {
if let Some(texture) = texture.clone() {
block_on(texture).ok()?;
}
let skybox = SkyBoxBuilder {
front: if face == SkyBoxFace::Front {
texture.clone()
} else {
skybox.front()
},
back: if face == SkyBoxFace::Back {
texture.clone()
} else {
skybox.back()
},
left: if face == SkyBoxFace::Left {
texture.clone()
} else {
skybox.left()
},
right: if face == SkyBoxFace::Right {
texture.clone()
} else {
skybox.right()
},
top: if face == SkyBoxFace::Top {
texture.clone()
} else {
skybox.top()
},
bottom: if face == SkyBoxFace::Bottom {
texture
} else {
skybox.bottom()
},
}
.build()
.unwrap();
if let Some(cubemap) = skybox.cubemap() {
let mut data = cubemap.data_ref();
data.set_s_wrap_mode(TextureWrapMode::ClampToEdge);
data.set_t_wrap_mode(TextureWrapMode::ClampToEdge);
}
Some(Box::new(skybox))
} else {
None
}
}
pub fn handle_camera_property_changed(
args: &PropertyChanged,
handle: Handle<Node>,
node: &mut Node,
) -> Option<SceneCommand> {
if let Some(camera) = node.cast::<Camera>() {
match args.value {
FieldKind::Object(ref value) => {
handle_properties!(args.name.as_ref(), handle, value,
Camera::EXPOSURE => SetExposureCommand,
Camera::PROJECTION => SetProjectionCommand,
Camera::VIEWPORT => SetViewportCommand,
Camera::ENABLED => SetCameraPreviewCommand,
Camera::SKY_BOX => SetSkyBoxCommand,
Camera::ENVIRONMENT => SetEnvironmentMap,
Camera::COLOR_GRADING_LUT => SetColorGradingLutCommand,
Camera::COLOR_GRADING_ENABLED => SetColorGradingEnabledCommand
)
}
FieldKind::Inspectable(ref inner) => match args.name.as_ref() {
Camera::EXPOSURE => {
if let FieldKind::Object(ref value) = inner.value {
match inner.name.as_ref() {
Exposure::AUTO_KEY_VALUE => {
let mut current_auto_exposure = camera.exposure();
if let Exposure::Auto {
ref mut key_value, ..
} = current_auto_exposure
{
*key_value = *value.cast_value::<f32>()?;
}
Some(SceneCommand::new(SetExposureCommand::new(
handle,
current_auto_exposure,
)))
}
Exposure::AUTO_MIN_LUMINANCE => {
let mut current_auto_exposure = camera.exposure();
if let Exposure::Auto {
ref mut min_luminance,
..
} = current_auto_exposure
{
*min_luminance = *value.cast_value::<f32>()?;
}
Some(SceneCommand::new(SetExposureCommand::new(
handle,
current_auto_exposure,
)))
}
Exposure::AUTO_MAX_LUMINANCE => {
let mut current_auto_exposure = camera.exposure();
if let Exposure::Auto {
ref mut max_luminance,
..
} = current_auto_exposure
{
*max_luminance = *value.cast_value::<f32>()?;
}
Some(SceneCommand::new(SetExposureCommand::new(
handle,
current_auto_exposure,
)))
}
Exposure::MANUAL_F_0 => {
Some(SceneCommand::new(SetExposureCommand::new(
handle,
Exposure::Manual(value.cast_value::<f32>().cloned()?),
)))
}
_ => None,
}
} else {
None
}
}
Camera::COLOR_GRADING_LUT => {
if let FieldKind::Object(ref value) = inner.value {
match inner.name.as_ref() {
ColorGradingLut::LUT => {
if let Some(texture) =
value.cast_value::<Option<Texture>>().cloned()?
{
if let Ok(lut) = block_on(ColorGradingLut::new(texture)) {
Some(SceneCommand::new(SetColorGradingLutCommand::new(
handle,
Some(lut),
)))
} else {
None
}
} else {
None
}
}
_ => None,
}
} else {
None
}
}
Camera::SKY_BOX => {
if let FieldKind::Object(ref value) = inner.value {
let texture = value.cast_value::<Option<Texture>>().cloned()?;
let face = match inner.name.as_ref() {
SkyBox::FRONT => Some(SkyBoxFace::Front),
SkyBox::BACK => Some(SkyBoxFace::Back),
SkyBox::LEFT => Some(SkyBoxFace::Left),
SkyBox::RIGHT => Some(SkyBoxFace::Right),
SkyBox::BOTTOM => Some(SkyBoxFace::Bottom),
SkyBox::TOP => Some(SkyBoxFace::Top),
_ => None,
};
face.map(|face| {
SceneCommand::new(SetSkyBoxCommand::new(
handle,
modify_skybox(camera, texture, face),
))
})
} else {
None
}
}
Camera::PROJECTION => {
if let FieldKind::Inspectable(ref value) = inner.value {
if value.owner_type_id == TypeId::of::<PerspectiveProjection>() {
handle_perspective_property_changed(&**value, handle)
} else if value.owner_type_id == TypeId::of::<OrthographicProjection>() {
handle_ortho_property_changed(&**value, handle)
} else {
None
}
} else {
None
}
}
Camera::BASE => handle_base_property_changed(inner, handle, node),
_ => None,
},
_ => None,
}
} else {
None
}
}
pub fn handle_perspective_property_changed(
args: &PropertyChanged,
handle: Handle<Node>,
) -> Option<SceneCommand> {
handle_property_changed!(args, handle,
PerspectiveProjection::Z_NEAR => SetPerspectiveZNear,
PerspectiveProjection::Z_FAR => SetPerspectiveZFar,
PerspectiveProjection::FOV => SetPerspectiveFov
)
}
pub fn handle_ortho_property_changed(
args: &PropertyChanged,
handle: Handle<Node>,
) -> Option<SceneCommand> {
handle_property_changed!(args, handle,
OrthographicProjection::Z_NEAR => SetOrthoZNear,
OrthographicProjection::Z_FAR => SetOrthoZFar,
OrthographicProjection::VERTICAL_SIZE => SetOrthoVerticalSize
)
}