Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

FC0a: a sliced UI picture takes an atlas cell (its rect in the layout, scaled to the sheet as Bevy's atlas UVs are); ui/images/ui_texture_atlas_slice ported

shinyobjectzb952bb580b88
diff --git a/bench/bevy-parity/examples.list b/bench/bevy-parity/examples.list
index b3af8690..20021573 100644
--- a/bench/bevy-parity/examples.list
+++ b/bench/bevy-parity/examples.list
@@ -60,4 +60,5 @@ ui/scroll_and_overflow/overflow
ui/scroll_and_overflow/overflow_clip_margin
ui/images/ui_texture_atlas
ui/images/ui_texture_slice
+ui/images/ui_texture_atlas_slice
ui/images/ui_texture_slice_flip_and_tile
diff --git a/bench/bevy-parity/runs.tsv b/bench/bevy-parity/runs.tsv
index 5bc08788..9707d7b6 100644
--- a/bench/bevy-parity/runs.tsv
+++ b/bench/bevy-parity/runs.tsv
@@ -60,3 +60,4 @@ ui/layout/flex_layout - 10
ui/scroll_and_overflow/overflow_clip_margin - 10
ui/images/ui_texture_slice - 10
ui/images/ui_texture_slice_flip_and_tile - 10
+ui/images/ui_texture_atlas_slice - 10
diff --git a/docs/bevy-parity.md b/docs/bevy-parity.md
index 09df1be0..a28c0a8d 100644
--- a/docs/bevy-parity.md
+++ b/docs/bevy-parity.md
@@ -4,7 +4,7 @@ Bevy v0.20.0's examples against their raea TSX ports (docs/control.feature FC0a)
don't edit. An example passes when every probed frame passes pixels (SSIM 0.98 and no 32 px tile off by more than 24 of 255) and cost (batch keys and
frame time at most 1.25x the example's). Wall time is one machine's, measured in the same run.
-Examples: **438**, ported **61**, passing **58**, excluded **16** (docs/bevy-parity/excluded.tsv), left **364**.
+Examples: **438**, ported **62**, passing **59**, excluded **16** (docs/bevy-parity/excluded.tsv), left **363**.
raea's frame ms is its update's plus the time React takes to step a frame for its 3D (step), which the example
doesn't pay.
@@ -115,8 +115,9 @@ doesn't pay.
| ui/images/image_node | 5 | 0.999893 | 1 | 0 / 0 | 442 / 492 | 5 / 7 | 1 / 1 | 48.07 / 1.966 = 1.766 + 0.200 | yes | |
| ui/images/ui_texture_atlas | 10 | 0.999601 | 7 | 0 / 0 | 445 / 492 | 5 / 7 | 1 / 1 | 16.378 / 0.959 = 0.832 + 0.127 | yes | |
| ui/images/ui_texture_atlas | 5 | 0.999601 | 7 | 0 / 0 | 445 / 492 | 5 / 7 | 1 / 1 | 48.378 / 2.164 = 1.981 + 0.183 | yes | |
-| ui/images/ui_texture_slice | 10 | 0.998957 | 8 | 0 / 0 | 447 / 492 | 5 / 7 | 1 / 1 | 34.134 / 1.088 = 0.943 + 0.145 | yes | |
| ui/images/ui_texture_slice_flip_and_tile | 10 | 1.000000 | 0 | 0 / 0 | 461 / 492 | 5 / 7 | 1 / 1 | 34.186 / 1.095 = 0.895 + 0.200 | yes | |
+| ui/images/ui_texture_slice | 10 | 0.998957 | 8 | 0 / 0 | 447 / 492 | 5 / 7 | 1 / 1 | 34.421 / 1.249 = 1.104 + 0.145 | yes | |
+| ui/images/ui_texture_atlas_slice | 10 | 0.990791 | 17 | 0 / 0 | 447 / 492 | 5 / 7 | 1 / 1 | 31.071 / 1.311 = 1.175 + 0.136 | yes | |
## Excluded
diff --git a/docs/bevy-parity/results.jsonl b/docs/bevy-parity/results.jsonl
index 9a35d988..b9467696 100644
--- a/docs/bevy-parity/results.jsonl
+++ b/docs/bevy-parity/results.jsonl
@@ -102,5 +102,6 @@
{"example":"ui/images/image_node","frame":5,"ssim":0.999893,"tile":1,"bevy":{"batch_keys":0,"entities":442,"extents":[],"frame":5,"frame_ms":48.07,"materials":1,"mesh_entities":0,"meshes":5,"other_material":0,"visible":0},"raea":{"frame":12,"entities":492,"mesh_entities":0,"visible":0,"meshes":7,"materials":1,"batch_keys":0,"other_material":0,"extents":[],"frame_ms":1.766,"step_ms":0.200,"cost_ms":1.966},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
{"example":"ui/images/ui_texture_atlas","frame":10,"ssim":0.999601,"tile":7,"bevy":{"batch_keys":0,"entities":445,"extents":[],"frame":10,"frame_ms":16.378,"materials":1,"mesh_entities":0,"meshes":5,"other_material":0,"visible":0},"raea":{"frame":17,"entities":492,"mesh_entities":0,"visible":0,"meshes":7,"materials":1,"batch_keys":0,"other_material":0,"extents":[],"frame_ms":0.832,"step_ms":0.127,"cost_ms":0.959},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
{"example":"ui/images/ui_texture_atlas","frame":5,"ssim":0.999601,"tile":7,"bevy":{"batch_keys":0,"entities":445,"extents":[],"frame":5,"frame_ms":48.378,"materials":1,"mesh_entities":0,"meshes":5,"other_material":0,"visible":0},"raea":{"frame":12,"entities":492,"mesh_entities":0,"visible":0,"meshes":7,"materials":1,"batch_keys":0,"other_material":0,"extents":[],"frame_ms":1.981,"step_ms":0.183,"cost_ms":2.164},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
-{"example":"ui/images/ui_texture_slice","frame":10,"ssim":0.998957,"tile":8,"bevy":{"batch_keys":0,"entities":447,"extents":[],"frame":10,"frame_ms":34.134,"materials":1,"mesh_entities":0,"meshes":5,"other_material":0,"visible":0},"raea":{"frame":17,"entities":492,"mesh_entities":0,"visible":0,"meshes":7,"materials":1,"batch_keys":0,"other_material":0,"extents":[],"frame_ms":0.943,"step_ms":0.145,"cost_ms":1.088},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
{"example":"ui/images/ui_texture_slice_flip_and_tile","frame":10,"ssim":1.000000,"tile":0,"bevy":{"batch_keys":0,"entities":461,"extents":[],"frame":10,"frame_ms":34.186,"materials":1,"mesh_entities":0,"meshes":5,"other_material":0,"visible":0},"raea":{"frame":17,"entities":492,"mesh_entities":0,"visible":0,"meshes":7,"materials":1,"batch_keys":0,"other_material":0,"extents":[],"frame_ms":0.895,"step_ms":0.200,"cost_ms":1.095},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
+{"example":"ui/images/ui_texture_slice","frame":10,"ssim":0.998957,"tile":8,"bevy":{"batch_keys":0,"entities":447,"extents":[],"frame":10,"frame_ms":34.421,"materials":1,"mesh_entities":0,"meshes":5,"other_material":0,"visible":0},"raea":{"frame":17,"entities":492,"mesh_entities":0,"visible":0,"meshes":7,"materials":1,"batch_keys":0,"other_material":0,"extents":[],"frame_ms":1.104,"step_ms":0.145,"cost_ms":1.249},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
+{"example":"ui/images/ui_texture_atlas_slice","frame":10,"ssim":0.990791,"tile":17,"bevy":{"batch_keys":0,"entities":447,"extents":[],"frame":10,"frame_ms":31.071,"materials":1,"mesh_entities":0,"meshes":5,"other_material":0,"visible":0},"raea":{"frame":17,"entities":492,"mesh_entities":0,"visible":0,"meshes":7,"materials":1,"batch_keys":0,"other_material":0,"extents":[],"frame_ms":1.175,"step_ms":0.136,"cost_ms":1.311},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
diff --git a/sdk/raea/bench/bevy-parity/ui/images/ui_texture_atlas_slice.tsx b/sdk/raea/bench/bevy-parity/ui/images/ui_texture_atlas_slice.tsx
new file mode 100644
index 00000000..ab739eb5
--- /dev/null
+++ b/sdk/raea/bench/bevy-parity/ui/images/ui_texture_atlas_slice.tsx
@@ -0,0 +1,33 @@
+// Bevy v0.20.0 examples/ui/images/ui_texture_atlas_slice.rs in raea's TSX (docs/control.feature FC0a): a full-window node
+// centring three buttons, 150 x 150, 300 x 150 and 150 x 300 with 20 px margins, each an ImageNode of a
+// cell (0, 7 and 13) of border_sheet.png, laid out from_grid 50 x 50, 6 by 6 with 2 px padding, sliced by a
+// TextureSlicer (24 px borders, sides and centre stretched, max_corner_scale 1) and
+// holding "Button" in FiraSans-Bold 33 px, 0.9 grey. Their hover and press styles see no pointer here. Bevy's
+// Camera2d over the default clear colour is the layered <Canvas3D>. In Bevy's default 1280 x 720 window.
+import { AbsoluteFill, Composition, registerRoot, staticFile } from "raea";
+import { Canvas3D } from "raea/3d";
+import { ImageNode, Node, Text, UiRect, percent, px, TextureAtlasLayout, type TextureSlicer } from "raea/ui";
+import { Srgba } from "raea/color";
+
+const IMAGE = staticFile("textures/fantasy_ui_borders/border_sheet.png");
+const FONT = staticFile("fonts/FiraSans-Bold.ttf");
+const SLICER: TextureSlicer = { border: { left: 24, right: 24, top: 24, bottom: 24 }, center_scale_mode: "Stretch", sides_scale_mode: "Stretch", max_corner_scale: 1 };
+const LAYOUT = TextureAtlasLayout.from_grid([50, 50], 6, 6, [2, 2]);
+const SIZES = [[0, 150, 150], [7, 300, 150], [13, 150, 300]];
+
+function Shot() {
+ return (
+ <AbsoluteFill>
+ <Canvas3D layered clearColor="rgb(43, 44, 47)" />
+ <Node width={percent(100)} height={percent(100)} align_items="Center" justify_content="Center">
+ {SIZES.map(([index, w, h]) => (
+ <ImageNode key={`${w}x${h}`} image={IMAGE} texture_atlas={{ layout: LAYOUT, index }} image_mode={{ Sliced: SLICER }} width={px(w)} height={px(h)} justify_content="Center" align_items="Center" margin={UiRect.all(px(20))}>
+ <Text font={FONT} font_size={{ Px: 33 }} color={new Srgba(0.9, 0.9, 0.9)}>Button</Text>
+ </ImageNode>
+ ))}
+ </Node>
+ </AbsoluteFill>
+ );
+}
+
+registerRoot(() => <Composition id="main" component={Shot} durationInFrames={11} fps={60} width={1280} height={720} />);
diff --git a/sdk/raea/conformance/ui.test.tsx b/sdk/raea/conformance/ui.test.tsx
index 9b917af7..899f4418 100644
--- a/sdk/raea/conformance/ui.test.tsx
+++ b/sdk/raea/conformance/ui.test.tsx
@@ -178,6 +178,12 @@ describe("A sliced ImageNode, a sampled picture and UiScale", () => {
expect(() => html(<ImageNode image="p.png" image_mode={{ Sliced: { ...slicer, center_scale_mode: { Tile: { stretch_value: 0 } } } }} />)).toThrow(/center_scale_mode is/);
expect(() => html(<ImageNode image="p.png" image_mode={{ Sliced: { border: { left: -1, right: 0, top: 0, bottom: 0 } } }} />)).toThrow(/px >= 0/);
});
+ test("a sliced atlas cell puts its rect in its layout on the marker, which Blitz slices", () => {
+ const layout = TextureAtlasLayout.from_grid([50, 50], 6, 6, [2, 2]);
+ const out = html(<ImageNode image="p.png" texture_atlas={{ layout, index: 13 }} image_mode={{ Sliced: slicer }} />);
+ expect(out).toContain('data-raea-bevy-slice="2 t1.5 s cell 52 104 50 50 of 310 310"');
+ expect(() => html(<ImageNode image="p.png" texture_atlas={{ layout, index: 36 }} image_mode={{ Sliced: slicer }} />)).toThrow(/not one of its layout's 36 cells/);
+ });
test("a flipped one draws its slices on a mirrored layer behind its children", () => {
const out = html(<ImageNode image="p.png" flip_x image_mode={{ Sliced: slicer }}><Text>hi</Text></ImageNode>);
for (const s of ["z-index:-1", "transform:scaleX(-1)", "position:relative", "z-index:0"]) expect(out).toContain(s);
diff --git a/sdk/render/src/bevy3d/ui.tsx b/sdk/render/src/bevy3d/ui.tsx
index dc626946..bc89be59 100644
--- a/sdk/render/src/bevy3d/ui.tsx
+++ b/sdk/render/src/bevy3d/ui.tsx
@@ -425,7 +425,14 @@ function SlicedNode({ image, color, flip_x, flip_y, children, fixed, texture_atl
const sl = image_mode?.Sliced;
const b = sl?.border;
if (!sl || !b || ![b.left, b.right, b.top, b.bottom].every((v) => Number.isFinite(v) && v >= 0)) throw new TypeError(`ImageNode: image_mode is { Sliced: { border: { left, right, top, bottom } } } (px >= 0), got ${JSON.stringify(image_mode)}`);
- if (texture_atlas !== undefined) throw new TypeError("ImageNode: a sliced picture takes no texture_atlas here yet");
+ // a sliced atlas cell: its rect in the layout, which Blitz scales to the sheet as Bevy's atlas UVs are
+ let cellOf = "";
+ if (texture_atlas !== undefined) {
+ const { layout, index } = texture_atlas;
+ const c = layout?.textures?.[index];
+ if (!c) throw new RangeError(`ImageNode: texture_atlas.index ${index} is not one of its layout's ${layout?.textures?.length ?? 0} cells`);
+ cellOf = ` cell ${c.x} ${c.y} ${c.w} ${c.h} of ${layout.size[0]} ${layout.size[1]}`;
+ }
if (color !== undefined && !(color.red === 1 && color.green === 1 && color.blue === 1 && color.alpha === 1)) throw new TypeError("ImageNode: a sliced picture is untinted here yet (color white)");
const max = sl.max_corner_scale ?? 1;
if (!(Number.isFinite(max) && max > 0)) throw new RangeError(`ImageNode: max_corner_scale is above 0, got ${max}`);
@@ -435,7 +442,7 @@ function SlicedNode({ image, color, flip_x, flip_y, children, fixed, texture_atl
if (!(typeof v === "number" && Number.isFinite(v) && v > 0)) throw new TypeError(`ImageNode: ${which} is "Stretch" or { Tile: { stretch_value } } (above 0), got ${JSON.stringify(m)}`);
return `t${v}`;
};
- const marker = { "data-raea-bevy-slice": `${max} ${mode(sl.sides_scale_mode, "sides_scale_mode")} ${mode(sl.center_scale_mode, "center_scale_mode")}` };
+ const marker = { "data-raea-bevy-slice": `${max} ${mode(sl.sides_scale_mode, "sides_scale_mode")} ${mode(sl.center_scale_mode, "center_scale_mode")}${cellOf}` };
const ui = useContext(InUi);
const n = nodeCss("ImageNode", p, ui?.back);
const rendering = RENDERING[useContext(Sampler)];
diff --git a/vendor/blitz-paint/src/render/border_image.rs b/vendor/blitz-paint/src/render/border_image.rs
index 48d08545..b868088b 100644
--- a/vendor/blitz-paint/src/render/border_image.rs
+++ b/vendor/blitz-paint/src/render/border_image.rs
@@ -61,6 +61,16 @@ impl ElementCx<'_, '_> {
let ImageData::Raster(image) = &res.image else { return false };
let border = self.style.get_border();
let (iw, ih) = (image.width as f64, image.height as f64);
+ // a sliced atlas cell (data-raea-bevy-slice's "cell x y w h of LW LH": the cell's rect in its layout of LW x LH):
+ // that rect is what's sliced, scaled from the layout to the picture as Bevy's atlas UVs are (the rect over the
+ // layout's size)
+ let cell = self.element.bevy_slice().and_then(|a| {
+ let n: Vec<f64> = a.split(" cell ").nth(1)?.split_whitespace().filter(|x| *x != "of").filter_map(|x| x.parse().ok()).collect();
+ let [x, y, w, h, lw, lh] = n[..] else { return None };
+ let (kx, ky) = (iw / lw.max(1.0), ih / lh.max(1.0));
+ Some((x * kx, y * ky, (w * kx).min(iw - x * kx), (h * ky).min(ih - y * ky)))
+ });
+ let (ox, oy, iw, ih) = cell.unwrap_or((0.0, 0.0, iw, ih));
if iw <= 0.0 || ih <= 0.0 {
return false;
}
@@ -125,7 +135,7 @@ impl ElementCx<'_, '_> {
if tile.width() <= 0.0 || tile.height() <= 0.0 {
continue;
}
- let map = Affine::translate((x, y)) * Affine::scale_non_uniform(w / src.width(), h / src.height()) * Affine::translate((-src.x0, -src.y0));
+ let map = Affine::translate((x, y)) * Affine::scale_non_uniform(w / src.width(), h / src.height()) * Affine::translate((-(src.x0 + ox), -(src.y0 + oy)));
scene.fill(peniko::Fill::NonZero, self.transform, brush.as_ref(), Some(map), &tile);
}
}