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: Viewport, the UI camera's viewport (bevy_camera's Viewport: its roots laid out in that region of the window, clipped to it); ui/relative_cursor_position ported

shinyobjectz1b023081a669
diff --git a/bench/bevy-parity/examples.list b/bench/bevy-parity/examples.list
index cea50083..3dba70b6 100644
--- a/bench/bevy-parity/examples.list
+++ b/bench/bevy-parity/examples.list
@@ -72,3 +72,4 @@ ui/styling/stacked_gradients
ui/styling/gradients
ui/text/generic_font_families
ui/layout/ghost_nodes
+ui/relative_cursor_position
diff --git a/bench/bevy-parity/runs.tsv b/bench/bevy-parity/runs.tsv
index 62d208f0..006fa729 100644
--- a/bench/bevy-parity/runs.tsv
+++ b/bench/bevy-parity/runs.tsv
@@ -71,3 +71,4 @@ ui/styling/stacked_gradients - 10
ui/styling/gradients - 10
ui/text/generic_font_families - 10
ui/layout/ghost_nodes - 10
+ui/relative_cursor_position - 10
diff --git a/docs/bevy-parity.md b/docs/bevy-parity.md
index 86bf4c20..038b68dd 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 **72**, passing **68**, excluded **16** (docs/bevy-parity/excluded.tsv), left **354**.
+Examples: **438**, ported **73**, passing **69**, excluded **16** (docs/bevy-parity/excluded.tsv), left **353**.
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.
@@ -128,6 +128,7 @@ doesn't pay.
| ui/styling/gradients | 10 | 0.988141 | 14 | 0 / 0 | 553 / 492 | 5 / 7 | 1 / 1 | 40.253 / 4.425 = 2.089 + 2.336 | yes | |
| ui/text/generic_font_families | 10 | 0.990063 | 19 | 0 / 0 | 457 / 492 | 5 / 7 | 1 / 1 | 41.156 / 2.365 = 2.083 + 0.282 | yes | |
| ui/layout/ghost_nodes | 10 | 0.998544 | 9 | 0 / 0 | 455 / 492 | 5 / 7 | 1 / 1 | 35.333 / 1.312 = 1.13 + 0.182 | yes | |
+| ui/relative_cursor_position | 10 | 0.999907 | 2 | 0 / 0 | 443 / 492 | 5 / 7 | 1 / 1 | 52.279 / 3.978 = 3.723 + 0.255 | yes | |
## Excluded
diff --git a/docs/bevy-parity/results.jsonl b/docs/bevy-parity/results.jsonl
index bacadf13..989894f3 100644
--- a/docs/bevy-parity/results.jsonl
+++ b/docs/bevy-parity/results.jsonl
@@ -115,3 +115,4 @@
{"example":"ui/styling/gradients","frame":10,"ssim":0.988141,"tile":14,"bevy":{"batch_keys":0,"entities":553,"extents":[],"frame":10,"frame_ms":40.253,"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":2.089,"step_ms":2.336,"cost_ms":4.425},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
{"example":"ui/text/generic_font_families","frame":10,"ssim":0.990063,"tile":19,"bevy":{"batch_keys":0,"entities":457,"extents":[],"frame":10,"frame_ms":41.156,"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":2.083,"step_ms":0.282,"cost_ms":2.365},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
{"example":"ui/layout/ghost_nodes","frame":10,"ssim":0.998544,"tile":9,"bevy":{"batch_keys":0,"entities":455,"extents":[],"frame":10,"frame_ms":35.333,"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.13,"step_ms":0.182,"cost_ms":1.312},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
+{"example":"ui/relative_cursor_position","frame":10,"ssim":0.999907,"tile":2,"bevy":{"batch_keys":0,"entities":443,"extents":[],"frame":10,"frame_ms":52.279,"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":3.723,"step_ms":0.255,"cost_ms":3.978},"pixels":true,"draws":true,"time":true,"pass":true,"why":""}
diff --git a/sdk/raea/bench/bevy-parity/ui/relative_cursor_position.tsx b/sdk/raea/bench/bevy-parity/ui/relative_cursor_position.tsx
new file mode 100644
index 00000000..607ba82c
--- /dev/null
+++ b/sdk/raea/bench/bevy-parity/ui/relative_cursor_position.tsx
@@ -0,0 +1,24 @@
+// Bevy v0.20.0 examples/ui/relative_cursor_position.rs in raea's TSX: the UI camera's viewport is 600 x 600 px at
+// (200, 100); in it a full-size root centres a column of a 250 x 250 red-orange node (15 px under it) and its
+// RelativeCursorPosition in FiraSans-Bold 33 px: "unknown" in red, as no pointer is over the window here. Bevy's
+// Camera2d is the layered <Canvas3D> over the default clear colour. In Bevy's default 1280 x 720 window.
+import { AbsoluteFill, Composition, registerRoot, staticFile } from "raea";
+import { Canvas3D } from "raea/3d";
+import { Node, Text, UiRect, Viewport, percent, px } from "raea/ui";
+import { Srgba } from "raea/color";
+
+function Shot() {
+ return (
+ <AbsoluteFill>
+ <Canvas3D layered clearColor="rgb(43, 44, 47)" />
+ <Viewport physical_position={[200, 100]} physical_size={[600, 600]}>
+ <Node width={percent(100)} height={percent(100)} align_items="Center" justify_content="Center" flex_direction="Column">
+ <Node width={px(250)} height={px(250)} margin={UiRect.bottom(px(15))} background_color={new Srgba(0.92, 0.14, 0.05)} />
+ <Text font={staticFile("fonts/FiraSans-Bold.ttf")} font_size={{ Px: 33 }} color={new Srgba(0.9, 0.1, 0.1)}>unknown</Text>
+ </Node>
+ </Viewport>
+ </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 9bc806c6..61681415 100644
--- a/sdk/raea/conformance/ui.test.tsx
+++ b/sdk/raea/conformance/ui.test.tsx
@@ -1,7 +1,7 @@
// docs/control.feature FC0a: bevy_ui's Node, Text and TextSpan on the page, with Bevy's defaults, values and blend
// (ui/styling/transparency_ui, ui/layout/fixed_node).
import { describe, expect, test } from "bun:test";
-import { AngularColorStop, GhostNode, UiRect as GradRect, FontSource, GridPlacement, GridTrack, RepeatedGridTrack, ColorStop, ConicGradient, LinearGradient, RadialGradient, UiPosition, BorderRadius, CornerRadius, FontVariationTag, ImageNode, InlineImage, ImagePlugin, Node, TextureAtlasLayout, Overflow, OverflowClipMargin, Text, TextShadow, Text2d, UiScale, TextSpan, UiRect, auto, percent, px, vw } from "raea/ui";
+import { AngularColorStop, GhostNode, Viewport, UiRect as GradRect, FontSource, GridPlacement, GridTrack, RepeatedGridTrack, ColorStop, ConicGradient, LinearGradient, RadialGradient, UiPosition, BorderRadius, CornerRadius, FontVariationTag, ImageNode, InlineImage, ImagePlugin, Node, TextureAtlasLayout, Overflow, OverflowClipMargin, Text, TextShadow, Text2d, UiScale, TextSpan, UiRect, auto, percent, px, vw } from "raea/ui";
import { LinearRgba, Srgba } from "raea/color";
import { Canvas3D } from "raea/3d";
import { renderFrame } from "../src/test";
@@ -254,6 +254,11 @@ describe("A sliced ImageNode, a sampled picture and UiScale", () => {
expect(top.match(/data-raea-blend=\"linear\"/g)?.length).toBe(2);
expect(top).not.toContain("display:contents");
});
+ test("Viewport is the region of the window the UI's roots lay out in, clipped to it", () => {
+ const out = html(<Viewport physical_position={[200, 100]} physical_size={[600, 500]}><Node /></Viewport>);
+ expect(out).toContain("position:absolute;left:200px;top:100px;width:600px;height:500px;overflow:hidden");
+ expect(() => html(<Viewport physical_position={[0, 0]} physical_size={[0, 5]} />)).toThrow(/physical_size/);
+ });
test("BorderColor takes a colour per side", () => {
const out = html(<Node border={UiRect.all(px(2))} border_color={{ top: new Srgba(1, 0, 0), right: new Srgba(0, 0, 1), bottom: new Srgba(1, 1, 0), left: new Srgba(0, 1, 0) }} />);
expect(out).toContain("border-color:rgba(255, 0, 0, 1) rgba(0, 0, 255, 1) rgba(255, 255, 0, 1) rgba(0, 255, 0, 1)");
diff --git a/sdk/raea/src/ui.ts b/sdk/raea/src/ui.ts
index 6776081d..f5682b95 100644
--- a/sdk/raea/src/ui.ts
+++ b/sdk/raea/src/ui.ts
@@ -6,6 +6,6 @@ export * from "@raea/video/region";
// InputRegion (input-region.tsx) joins when it is committed
export * from "@raea/video/panel";
// bevy_ui's Node and Text, and bevy_text's TextSpan, with their values (Val, UiRect, BorderRadius, Overflow)
-export { Node, GhostNode, Text, TextSpan, TextShadow, Text2d, UiScale, ImageNode, InlineImage, type TextureSlicer, type SliceScaleMode, ImagePlugin, FontVariationTag, TextureAtlasLayout, type ImageNodeProps, px, percent, vw, vh, vmin, vmax, auto, UiRect, BorderRadius, CornerRadius, Overflow, OverflowClipMargin, type NodeProps, type TextProps, type TextStyleProps, type TextRunProps, type Text2dProps, type Val } from "@raea/render/bevy3d";
+export { Node, GhostNode, Text, TextSpan, TextShadow, Text2d, UiScale, Viewport, ImageNode, InlineImage, type TextureSlicer, type SliceScaleMode, ImagePlugin, FontVariationTag, TextureAtlasLayout, type ImageNodeProps, px, percent, vw, vh, vmin, vmax, auto, UiRect, BorderRadius, CornerRadius, Overflow, OverflowClipMargin, type NodeProps, type TextProps, type TextStyleProps, type TextRunProps, type Text2dProps, type Val } from "@raea/render/bevy3d";
export { LinearGradient, RadialGradient, ConicGradient, ColorStop, AngularColorStop, UiPosition, type Gradient, type RadialGradientShape, type InterpolationColorSpace } from "@raea/render/bevy3d";
export { GridTrack, RepeatedGridTrack, GridPlacement, type GridTrackRepetition, type GridAutoFlow, type MinTrackSizingFunction, type MaxTrackSizingFunction, FontSource, type GenericFontFamily } from "@raea/render/bevy3d";
diff --git a/sdk/render/src/bevy3d.tsx b/sdk/render/src/bevy3d.tsx
index 09e3ff71..0776376a 100644
--- a/sdk/render/src/bevy3d.tsx
+++ b/sdk/render/src/bevy3d.tsx
@@ -28,7 +28,7 @@ export { Hdr } from "./bevy3d/core";
// Lightmap (bevy_pbr::lightmap): a bake on a <Model>'s glTF meshes by name (docs/control.feature FC0a)
export { Lightmap, type LightmapProps } from "./bevy3d/lightmap";
// bevy_ui's Node, Text and TextSpan, and their values, drawn by the page
-export { Node, GhostNode, Text, TextSpan, TextShadow, Text2d, UiScale, FontVariationTag, type NodeProps, type TextProps, type TextStyleProps, type TextRunProps, type Text2dProps } from "./bevy3d/ui";
+export { Node, GhostNode, Text, TextSpan, TextShadow, Text2d, UiScale, Viewport, FontVariationTag, type NodeProps, type TextProps, type TextStyleProps, type TextRunProps, type Text2dProps } from "./bevy3d/ui";
export { ImageNode, InlineImage, ImagePlugin, TextureAtlasLayout, type TextureSlicer, type SliceScaleMode, type ImageNodeProps } from "./bevy3d/ui-image";
export { px, percent, vw, vh, vmin, vmax, auto, UiRect, BorderRadius, CornerRadius, Overflow, OverflowClipMargin, type Val } from "./bevy3d/ui-val";
export { LinearGradient, RadialGradient, ConicGradient, ColorStop, AngularColorStop, UiPosition, type Gradient, type RadialGradientShape, type InterpolationColorSpace } from "./bevy3d/ui-gradient";
diff --git a/sdk/render/src/bevy3d/ui.tsx b/sdk/render/src/bevy3d/ui.tsx
index 874efb2b..2563800d 100644
--- a/sdk/render/src/bevy3d/ui.tsx
+++ b/sdk/render/src/bevy3d/ui.tsx
@@ -400,3 +400,13 @@ export function UiScale({ value, children }: { value: number; children?: ReactNo
if (!(Number.isFinite(value) && value > 0)) throw new RangeError(`UiScale: value is above 0, got ${value}`);
return <div style={{ position: "absolute", left: 0, top: 0, width: `${100 / value}%`, height: `${100 / value}%`, transform: `scale(${value})`, transformOrigin: "0 0" }}>{children}</div>;
}
+
+/** The UI camera's viewport (bevy_camera's Viewport on the Camera2d the UI draws through): its roots lay out in this
+ * region of the window, `physical_position` from its top left and `physical_size` big (px, at a scale factor of 1),
+ * and are clipped to it. @bevy bevy_camera::Viewport */
+export function Viewport({ physical_position, physical_size, children }: { physical_position: [number, number]; physical_size: [number, number]; children?: ReactNode }) {
+ const ok = (v: unknown, min: number) => Array.isArray(v) && v.length === 2 && v.every((n) => Number.isInteger(n) && n >= min);
+ if (!ok(physical_position, 0) || !ok(physical_size, 1)) throw new RangeError(`Viewport: physical_position is [x, y] >= 0 and physical_size [w, h] >= 1 in whole px, got ${JSON.stringify({ physical_position, physical_size })}`);
+ const [[left, top], [width, height]] = [physical_position, physical_size];
+ return <div style={{ position: "absolute", left, top, width, height, overflow: "hidden" }}>{children}</div>;
+}
diff --git a/sdk/skills/raea/reference/sdk.md b/sdk/skills/raea/reference/sdk.md
index c72ad5f6..b8a51081 100644
--- a/sdk/skills/raea/reference/sdk.md
+++ b/sdk/skills/raea/reference/sdk.md
@@ -99,7 +99,7 @@ with a capital, hooks with `use`. A component's props are in its file.
## `raea/ui` (sdk/raea/src/ui.ts)
-`AbsoluteFill`, `AngularColorStop`, `BorderRadius`, `ColorStop`, `ConicGradient`, `CornerRadius`, `FontSource`, `FontVariationTag`, `GhostNode`, `GridPlacement`, `GridTrack`, `ImageNode`, `ImagePlugin`, `InlineImage`, `LinearGradient`, `Node`, `Overflow`, `OverflowClipMargin`, `RadialGradient`, `RepeatedGridTrack`, `Text`, `Text2d`, `TextShadow`, `TextSpan`, `TextureAtlasLayout`, `UiPosition`, `UiRect`, `UiScale`, `auto`, `percent`, `px`, `vh`, `vmax`, `vmin`, `vw`
+`AbsoluteFill`, `AngularColorStop`, `BorderRadius`, `ColorStop`, `ConicGradient`, `CornerRadius`, `FontSource`, `FontVariationTag`, `GhostNode`, `GridPlacement`, `GridTrack`, `ImageNode`, `ImagePlugin`, `InlineImage`, `LinearGradient`, `Node`, `Overflow`, `OverflowClipMargin`, `RadialGradient`, `RepeatedGridTrack`, `Text`, `Text2d`, `TextShadow`, `TextSpan`, `TextureAtlasLayout`, `UiPosition`, `UiRect`, `UiScale`, `Viewport`, `auto`, `percent`, `px`, `vh`, `vmax`, `vmin`, `vw`
## `raea/video` (sdk/raea/src/video.ts)