-
Notifications
You must be signed in to change notification settings - Fork 4
/
default_loggers.rs
380 lines (342 loc) · 12.8 KB
/
default_loggers.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use bevy::{
ecs::component::ComponentInfo,
prelude::*,
render::{mesh::PlaneMeshBuilder, primitives::Aabb},
utils::HashMap,
};
use crate::{compute_entity_path, Aliased, RerunLogger, ToRerun};
// ---
/// The default [`RerunLogger`]s that are used if no user-defined logger is specified.
///
/// See [`crate::RerunComponentLoggers`] for more information.
///
/// Public so end users can easily inspect what is configured by default.
#[derive(Resource, Deref, DerefMut, Clone, Debug)]
pub struct DefaultRerunComponentLoggers(HashMap<rerun::ComponentName, Option<RerunLogger>>);
// TODO(cmc): DataUi being typed makes aliases uninspectable :(
#[allow(clippy::too_many_lines)]
impl Default for DefaultRerunComponentLoggers {
fn default() -> Self {
let mut loggers = HashMap::default();
loggers.insert(
"bevy_transform::components::transform::Transform".into(),
Some(RerunLogger::new_static(&bevy_transform)),
);
loggers.insert(
"bevy_transform::components::global_transform::GlobalTransform".into(),
Some(RerunLogger::new_static(&bevy_global_transform)),
);
loggers.insert(
"bevy_render::mesh::components::Mesh2d".into(),
Some(RerunLogger::new_static(&bevy_mesh2d)),
);
loggers.insert(
"bevy_render::mesh::components::Mesh3d".into(),
Some(RerunLogger::new_static(&bevy_mesh3d)),
);
loggers.insert(
"bevy_render::camera::projection::Projection".into(),
Some(RerunLogger::new_static(&bevy_projection)),
);
loggers.insert(
"bevy_render::camera::projection::OrthographicProjection".into(),
Some(RerunLogger::new_static(&bevy_projection_orthographic)),
);
loggers.insert(
"bevy_render::camera::projection::PerspectiveProjection".into(),
Some(RerunLogger::new_static(&bevy_projection_perspective)),
);
loggers.insert(
"bevy_sprite::sprite::Sprite".into(),
Some(RerunLogger::new_static(&bevy_sprite)),
);
loggers.insert(
"bevy_render::primitives::Aabb".into(),
Some(RerunLogger::new_static(&bevy_aabb)),
);
loggers.insert(
"bevy_hierarchy::components::parent::Parent".into(),
Some(RerunLogger::new_static(&bevy_parent)),
);
loggers.insert(
"bevy_hierarchy::components::children::Children".into(),
Some(RerunLogger::new_static(&bevy_children)),
);
loggers.insert("revy::entity_path::RerunEntityPath".into(), None);
Self(loggers)
}
}
// ---
fn bevy_transform<'w>(
_world: &'w World,
_all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
_component: &'w ComponentInfo,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
let suffix = None;
let data = entity
.get::<Transform>()
.map(|transform| transform.to_rerun())
.map(|data| Box::new(data) as _);
(suffix, data)
}
fn bevy_global_transform<'w>(
_world: &'w World,
_all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
_component: &'w ComponentInfo,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
let suffix = None;
// TODO(cmc): once again the DataUi does the wrong thing... we really need to
// go typeless.
let data = entity.get::<GlobalTransform>().map(|transform| {
Box::new(vec![
Box::new(Aliased::<rerun::datatypes::Vec3D>::new(
"GlobalTransform3D.translation",
transform.translation().to_rerun(),
)) as Box<dyn rerun::AsComponents>,
Box::new(Aliased::<rerun::datatypes::Quaternion>::new(
"GlobalTransform3D.rotation",
transform.rotation().to_rerun(),
)),
Box::new(Aliased::<rerun::datatypes::Vec3D>::new(
"GlobalTransform3D.scale",
transform.scale().to_rerun(),
)),
]) as _
});
(suffix, data)
}
fn bevy_mesh<'w>(
world: &'w World,
_all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
_component: &'w ComponentInfo,
handle: Option<&Handle<Mesh>>,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
let suffix: Option<&str> = None;
let data = handle
.and_then(|handle| world.resource::<Assets<Mesh>>().get(handle))
.and_then(ToRerun::to_rerun)
.map(|mut mesh| {
if let Some(mat) = entity
.get::<MeshMaterial2d<ColorMaterial>>()
.and_then(|handle| world.resource::<Assets<ColorMaterial>>().get(handle))
{
mesh = mesh.with_albedo_factor(mat.color.to_rerun());
}
if let Some(mat) = entity
.get::<MeshMaterial3d<StandardMaterial>>()
.and_then(|handle| world.resource::<Assets<StandardMaterial>>().get(handle))
{
mesh = mesh.with_albedo_factor(mat.base_color.to_rerun());
if let Some((image_format, image_data)) = mat
.base_color_texture
.as_ref()
.and_then(|handle| world.resource::<Assets<Image>>().get(handle))
.and_then(ToRerun::to_rerun)
{
mesh = mesh.with_albedo_texture(image_format, image_data);
}
}
mesh
})
.map(|mesh| Box::new(mesh) as _);
(suffix, data)
}
fn bevy_mesh2d<'w>(
world: &'w World,
all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
component: &'w ComponentInfo,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
let suffix = Some("mesh2d");
let (_, data) = bevy_mesh(
world,
all_entities,
entity,
component,
entity.get::<Mesh2d>().map(|handle| &handle.0),
);
(suffix, data)
}
fn bevy_mesh3d<'w>(
world: &'w World,
all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
component: &'w ComponentInfo,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
let suffix = Some("mesh3d");
let (_, data) = bevy_mesh(
world,
all_entities,
entity,
component,
entity.get::<Mesh3d>().map(|handle| &handle.0),
);
(suffix, data)
}
fn bevy_camera<'w, C: Component + ToRerun<rerun::Pinhole>>(
_world: &'w World,
_all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
_component: &'w ComponentInfo,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
let suffix = Some("cam");
let data = entity
.get::<C>()
// TODO(cmc): log visible entities too?
.map(ToRerun::to_rerun)
.map(|mesh| Box::new(mesh) as _);
(suffix, data)
}
fn bevy_projection<'w>(
world: &'w World,
all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
component: &'w ComponentInfo,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
bevy_camera::<Projection>(world, all_entities, entity, component)
}
fn bevy_projection_orthographic<'w>(
world: &'w World,
all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
component: &'w ComponentInfo,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
bevy_camera::<OrthographicProjection>(world, all_entities, entity, component)
}
fn bevy_projection_perspective<'w>(
world: &'w World,
all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
component: &'w ComponentInfo,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
bevy_camera::<PerspectiveProjection>(world, all_entities, entity, component)
}
// TODO(cmc): check if sprite has custom sizes etc
fn bevy_sprite<'w>(
world: &'w World,
_all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
_component: &'w ComponentInfo,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
let suffix = Some("sprite");
let data = entity
.get::<Sprite>()
.and_then(|sprite| {
world
.resource::<Assets<Image>>()
.get(sprite.image.id())
.and_then(ToRerun::to_rerun)
.and_then(|(image_format, image_data)| {
let mesh = PlaneMeshBuilder::default()
.normal(Dir3::Z)
.size(image_format.width as _, image_format.height as _)
.build();
mesh.to_rerun().map(|mesh| {
mesh.with_albedo_factor(sprite.color.to_rerun())
.with_albedo_texture(image_format, image_data)
})
})
})
.map(|data| Box::new(data) as _);
(suffix, data)
}
fn bevy_aabb<'w>(
world: &'w World,
_all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
_component: &'w ComponentInfo,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
let suffix = Some("aabb");
let data = entity
.get::<Aabb>()
.map(|aabb| {
rerun::Boxes3D::from_centers_and_half_sizes(
[aabb.center.to_rerun()],
[aabb.half_extents.to_rerun()],
)
})
.map(|aabb| {
if let Some(mat) = entity
.get::<MeshMaterial2d<ColorMaterial>>()
.and_then(|handle| world.resource::<Assets<ColorMaterial>>().get(handle))
{
aabb.with_colors([mat.color.to_rerun()])
} else if let Some(mat) = entity
.get::<MeshMaterial3d<StandardMaterial>>()
.and_then(|handle| world.resource::<Assets<StandardMaterial>>().get(handle))
{
aabb.with_colors([mat.base_color.to_rerun()])
} else if let Some(sprite) = entity.get::<Sprite>() {
aabb.with_colors([sprite.color.to_rerun()])
} else {
aabb
}
})
.map(|data| Box::new(data) as _);
(suffix, data)
}
fn bevy_parent<'w>(
world: &'w World,
all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
_component: &'w ComponentInfo,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
let suffix = None;
let data = entity
.get::<Parent>()
.map(|parent| {
let parent_entity_path = compute_entity_path(world, all_entities, parent.get());
Aliased::<rerun::datatypes::EntityPath>::new(
"Parent",
rerun::datatypes::EntityPath(parent_entity_path.to_string().into()),
)
})
.map(|data| Box::new(data) as _);
(suffix, data)
}
fn bevy_children<'w>(
world: &'w World,
all_entities: &'w QueryState<(Entity, Option<&'w Parent>, Option<&'w Name>)>,
entity: EntityRef<'_>,
_component: &'w ComponentInfo,
) -> (Option<&'static str>, Option<Box<dyn rerun::AsComponents>>) {
let suffix = None;
// TODO(cmc): it is once again super annoying that number of instances gets resolved at logging
// time... we need those clamp-to-edge semantics asap.
// let data = entity
// .get::<Children>()
// .map(|children| {
// let children = children
// .iter()
// .map(|entity_id| {
// rerun::datatypes::EntityPath(
// compute_entity_path(world, all_entities, *entity_id)
// .to_string()
// .into(),
// )
// })
// .collect::<Vec<_>>();
// Aliased::<Vec<rerun::datatypes::EntityPath>>::new(
// "RawChildren",
// children,
// )
// })
// .map(|data| Box::new(data) as _);
let data = entity
.get::<Children>()
.map(|children| {
let children = children
.iter()
.map(|entity_id| compute_entity_path(world, all_entities, *entity_id).to_string())
.collect::<Vec<_>>();
Aliased::<rerun::components::Text>::new(
"RawChildren",
rerun::components::Text(children.join("\n").into()),
)
})
.map(|data| Box::new(data) as _);
(suffix, data)
}