mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-01 06:53:19 -10:00
A material: row sets only the fields it gives on the part's own StandardMaterial, the rest as they were (part of #38). patch_material deserialized the given fields as a whole StandardMaterial, every field it wasn't given at its default, and applied all of them. So material-field's patch of emissive_exposure_weight also blacked out the cube's emissive and turned its black base white. On frame 0 the scene sync put the row's colours back. From frame 1 on, a render leaves an unchanged mesh alone (StillSynced), so the program route drew the cube grey (red 49) at a weight of 0.14, in stills too. Live play re-synced every frame, which hid it, and so the two routes differed on 29 of 30 frames. Each given field is now deserialized by its own type and applied to that field alone. An unknown field is refused by name.
material-field-0, red at the cube's centre on frames 0, 1, 5, 10, 20, 29: the program route was 216, 49, 49, 49, 49, 49 and now draws 215, 206, 165, 101, 14, 14, the same as live play. That's the ramp Bevy's emissive * mix(1, exposure, weight) gives the rows' eased track. At frame 20 the cube is the black base's specular (14, 9, 10), not grey. The grey (50, 48, 49) that control.feature FC1c recorded was this bug, and the case comments now say dark. The probes (r>150 at 0, r<80 at 20 and live) pass as before. A unit test (a_material_row_sets_only_its_fields) checks that a patch keeps the emissive and base colour; it fails on the old patch with emissive black and base white. bevy-ts check.sh: 92 pass, 1 open (shader-character: its body.glb has no joint it names, open before this too). fps, material-field-0 back to back, three runs each: 21.9, 16.9, 23.0 ms a frame before, 22.2, 21.7, 17.4 after.
This commit is contained in:
1 parent
900411c90e
commit
c0fd55f43d
4 files changed
+40
-4
No files matched your search
@@ -2,6 +2,7 @@
|
||||
//! registers (fields, shapes, roles), for the generated TypeScript.
|
||||
|
||||
use super::*;
|
||||
use bevy::reflect::structs::Struct;
|
||||
|
||||
/// Before the first frame: every asset a handle field loaded by path, loaded with its dependencies (a material's
|
||||
/// textures), or the load's error.
|
||||
@@ -239,7 +240,16 @@ pub fn patch_material(world: &mut World, e: Entity, path: &str, json: &Value) ->
|
||||
}
|
||||
let registry = world.resource::<AppTypeRegistry>().clone();
|
||||
let reg = registry.read();
|
||||
let v = value_of(world, ®, registration(®, path)?, path, json)?;
|
||||
// each field given, by its own type: the whole StandardMaterial deserialized from a partial one took every field
|
||||
// it wasn't given at its default, so a patch of emissive_exposure_weight blacked out the emissive (#38: a render's
|
||||
// frames after the first, which the scene sync leaves alone, drew the glowing cube grey)
|
||||
let Value::Object(given) = json else { return Err(format!("material:{path}: give its fields as an object")) };
|
||||
let TypeInfo::Struct(info) = registration(®, path)?.type_info() else { return Err(format!("material:{path}: not a struct")) };
|
||||
let mut fields = Vec::with_capacity(given.len());
|
||||
for (name, x) in given {
|
||||
let ty = info.field(name).ok_or_else(|| format!("material:{path}: no field {name}"))?.type_path();
|
||||
fields.push((name.as_str(), value_of(world, ®, registration(®, ty)?, &format!("{path}.{name}"), x)?));
|
||||
}
|
||||
let Some(h) = world.get::<MeshMaterial3d<StandardMaterial>>(e).map(|m| m.0.clone()) else {
|
||||
return Err(format!("material:{path}: the part has no StandardMaterial"));
|
||||
};
|
||||
@@ -254,5 +264,9 @@ pub fn patch_material(world: &mut World, e: Entity, path: &str, json: &Value) ->
|
||||
};
|
||||
let mut mats = world.resource_mut::<Assets<StandardMaterial>>();
|
||||
let Some(mut m) = mats.get_mut(&h) else { return Err(format!("material:{path}: its material isn't loaded")) };
|
||||
PartialReflect::try_apply(&mut *m, v.as_partial_reflect()).map_err(|err| format!("material:{path}: {err}"))
|
||||
for (name, v) in fields {
|
||||
let f = Struct::field_mut(&mut *m, name).ok_or_else(|| format!("material:{path}: no field {name}"))?;
|
||||
f.try_apply(v.as_partial_reflect()).map_err(|err| format!("material:{path}.{name}: {err}"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -87,3 +87,25 @@ fn keyed_components_land_on_their_frame_forwards_backwards_and_jumping() {
|
||||
assert_eq!((x(w, a), x(w, b)), (Some(want_a), Some(want_b)), "frame {f}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A `material:` row sets only the fields it gives on the part's own copy of its material (#38): it took the whole
|
||||
/// StandardMaterial from them, every other field at its default, so a patch of emissive_exposure_weight blacked out the
|
||||
/// emissive and whitened the base, and a render's frames after the first drew the glowing cube grey
|
||||
#[test]
|
||||
fn a_material_row_sets_only_its_fields() {
|
||||
let mut app = app();
|
||||
app.register_type::<StandardMaterial>();
|
||||
let w = app.world_mut();
|
||||
let mut mats = Assets::<StandardMaterial>::default();
|
||||
let (black, red) = (Color::BLACK, LinearRgba::rgb(1.0, 0.0, 0.0));
|
||||
let shared = mats.add(StandardMaterial { base_color: black, emissive: red, ..default() });
|
||||
w.insert_resource(mats);
|
||||
let e = w.spawn(MeshMaterial3d(shared.clone())).id();
|
||||
insert_reflect(w, e, "material:bevy_pbr::pbr_material::StandardMaterial", &json!({"emissive_exposure_weight": 0.5})).unwrap();
|
||||
let own = w.get::<MeshMaterial3d<StandardMaterial>>(e).unwrap().0.clone();
|
||||
assert_ne!(own, shared, "the part's own copy, not the shared material");
|
||||
let m = w.resource::<Assets<StandardMaterial>>().get(&own).unwrap();
|
||||
assert_eq!((m.emissive_exposure_weight, m.emissive, m.base_color), (0.5, red, black));
|
||||
let err = insert_reflect(w, e, "material:bevy_pbr::pbr_material::StandardMaterial", &json!({"no_such": 1.0})).unwrap_err();
|
||||
assert!(err.contains("no field no_such"), "{err}");
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// gap: FC1c
|
||||
// A StandardMaterial field past the row columns' four, animated (docs/control.feature FC1c): a black cube glowing red
|
||||
// whose emissive_exposure_weight runs from 0 at frame 0 to 1 at frame 20, set by reflection on its own material each
|
||||
// frame. At frame 20 it is grey, not red: the emissive under the camera's exposure leaves only the black base's specular.
|
||||
// frame. At frame 20 it is dark, not red: the emissive under the camera's exposure leaves only the black base's specular.
|
||||
// frame: 20
|
||||
// probe: 32 32 r<80
|
||||
import { AbsoluteFill, Composition, registerRoot } from "raea";
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// gap: FC1c
|
||||
// A StandardMaterial field past the row columns' four, animated (docs/control.feature FC1c): a black cube glowing red
|
||||
// whose emissive_exposure_weight runs from 0 at frame 0 to 1 at frame 20, set by reflection on its own material each
|
||||
// frame. Played live (0.85 s, frame 25, past the track's end) it is grey, not red: the emissive under the camera's exposure leaves only the black base's specular.
|
||||
// frame. Played live (0.85 s, frame 25, past the track's end) it is dark, not red: the emissive under the camera's exposure leaves only the black base's specular.
|
||||
// play: live-world.json 0.85
|
||||
// probe: 32 32 r<80
|
||||
import { AbsoluteFill, Composition, registerRoot } from "raea";
|
||||
|
||||
Reference in new issue
Block a user