Skip to content

Commit 6138575

Browse files
rparrettmockersf
andcommitted
Fix panics in scene_viewer and audio_control (#16983)
Fixes #16978 While testing, discovered that the morph weight interface in `scene_viewer` has been broken for a while (panics when loaded model has morph weights), probably since #15591. Fixed that too. While testing, saw example text in morph interface with [wrong padding](https://bevyengine.org/learn/contribute/helping-out/creating-examples/#visual-guidelines). Fixed that too. Left the small font size because there may be a lot of morphs to display, so that seems intentional. Use normal queries and bail early Morph interface can be tested with ``` cargo run --example scene_viewer assets/models/animated/MorphStressTest.gltf ``` I noticed that this fix is different than what is happening in #16976. Feel free to discard this for an alternative fix. I opened this anyway to document the issue with morph weight display. This is on top of #16966 which is required to test. --------- Co-authored-by: François Mockers <[email protected]> Co-authored-by: François Mockers <[email protected]>
1 parent 1f72e07 commit 6138575

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

examples/tools/scene_viewer/morph_viewer_plugin.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use crate::scene_viewer_plugin::SceneHandle;
1212
use bevy::prelude::*;
1313
use std::fmt;
1414

15+
const FONT_SIZE: f32 = 13.0;
16+
1517
const WEIGHT_PER_SECOND: f32 = 0.8;
1618
const ALL_MODIFIERS: &[KeyCode] = &[KeyCode::ShiftLeft, KeyCode::ControlLeft, KeyCode::AltLeft];
1719
const AVAILABLE_KEYS: [MorphKey; 56] = [
@@ -122,8 +124,8 @@ impl fmt::Display for Target {
122124
}
123125
}
124126
impl Target {
125-
fn text_span(&self, key: &str, style: TextFont) -> (String, TextFont) {
126-
(format!("[{key}] {self}\n"), style)
127+
fn text_span(&self, key: &str, style: TextFont) -> (TextSpan, TextFont) {
128+
(TextSpan::new(format!("[{key}] {self}\n")), style)
127129
}
128130
fn new(
129131
entity_name: Option<&Name>,
@@ -178,13 +180,18 @@ impl MorphKey {
178180
}
179181
fn update_text(
180182
controls: Option<ResMut<WeightsControl>>,
181-
text: Single<Entity, With<Text>>,
183+
texts: Query<Entity, With<Text>>,
182184
morphs: Query<&MorphWeights>,
183185
mut writer: TextUiWriter,
184186
) {
185187
let Some(mut controls) = controls else {
186188
return;
187189
};
190+
191+
let Ok(text) = texts.get_single() else {
192+
return;
193+
};
194+
188195
for (i, target) in controls.weights.iter_mut().enumerate() {
189196
let Ok(weights) = morphs.get(target.entity) else {
190197
continue;
@@ -196,7 +203,8 @@ fn update_text(
196203
target.weight = actual_weight;
197204
}
198205
let key_name = &AVAILABLE_KEYS[i].name;
199-
*writer.text(*text, i + 3) = format!("[{key_name}] {target}\n");
206+
207+
*writer.text(text, i + 3) = format!("[{key_name}] {target}\n");
200208
}
201209
}
202210
fn update_morphs(
@@ -254,12 +262,12 @@ fn detect_morphs(
254262
}
255263
detected.truncate(AVAILABLE_KEYS.len());
256264
let style = TextFont {
257-
font_size: 13.0,
265+
font_size: FONT_SIZE,
258266
..default()
259267
};
260268
let mut spans = vec![
261-
("Morph Target Controls\n".into(), style.clone()),
262-
("---------------\n".into(), style.clone()),
269+
(TextSpan::new("Morph Target Controls\n"), style.clone()),
270+
(TextSpan::new("---------------\n"), style.clone()),
263271
];
264272
let target_to_text =
265273
|(i, target): (usize, &Target)| target.text_span(AVAILABLE_KEYS[i].name, style.clone());
@@ -270,14 +278,15 @@ fn detect_morphs(
270278
Text::default(),
271279
Node {
272280
position_type: PositionType::Absolute,
273-
top: Val::Px(10.0),
274-
left: Val::Px(10.0),
281+
top: Val::Px(12.0),
282+
left: Val::Px(12.0),
275283
..default()
276284
},
277285
))
278286
.with_children(|p| {
279-
p.spawn((TextSpan::new("Morph Target Controls\n"), style.clone()));
280-
p.spawn((TextSpan::new("---------------\n"), style));
287+
for span in spans {
288+
p.spawn(span);
289+
}
281290
});
282291
}
283292

0 commit comments

Comments
 (0)