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: 2D parts take Bevy's quaternion (quat), for camera/2d_on_ui

shinyobjectz0105bb67c484
diff --git a/bench/bevy-parity/examples.list b/bench/bevy-parity/examples.list
index 91f19ed9..899ab6d0 100644
--- a/bench/bevy-parity/examples.list
+++ b/bench/bevy-parity/examples.list
@@ -114,3 +114,4 @@ ui/text/system_fonts
ui/text/font_atlas_debug
3d/two_passes
3d/split_screen
+camera/2d_on_ui
diff --git a/bench/bevy-parity/runs.tsv b/bench/bevy-parity/runs.tsv
index c67c4bb6..8a475362 100644
--- a/bench/bevy-parity/runs.tsv
+++ b/bench/bevy-parity/runs.tsv
@@ -113,3 +113,4 @@ ui/text/system_fonts - 10
ui/text/font_atlas_debug - 10
3d/two_passes - 10
3d/split_screen - 10
+camera/2d_on_ui - 5,60
diff --git a/runtime/scene/src/sprite2d.rs b/runtime/scene/src/sprite2d.rs
index 96110792..d4523a80 100644
--- a/runtime/scene/src/sprite2d.rs
+++ b/runtime/scene/src/sprite2d.rs
@@ -19,6 +19,13 @@ fn place2d(p: &mut Program, row: &mut BTreeMap<String, V>, id: &str, its: &[Item
for (k, col) in ["scale_x", "scale_y"].iter().enumerate() {
column(p, row, id, col, &its.iter().map(|i| scale(i, k)).collect::<Vec<_>>());
}
+ // `quat`: Bevy's rotation as given, in place of `rot` (q_x q_y q_z q_w, as a 3D part's)
+ if its.iter().any(|i| i.props.get("quat").is_some()) {
+ let q = |i: &Item, k: usize| i.props.get("quat").and_then(|q| q.get(k)).and_then(Value::as_f64).unwrap_or(if k == 3 { 1.0 } else { 0.0 });
+ for (k, col) in ["q_x", "q_y", "q_z", "q_w"].iter().enumerate() {
+ column(p, row, id, col, &its.iter().map(|i| q(i, k)).collect::<Vec<_>>());
+ }
+ }
tint(p, row, id, its, color);
}
diff --git a/sdk/raea/bench/bevy-parity/camera/2d_on_ui.tsx b/sdk/raea/bench/bevy-parity/camera/2d_on_ui.tsx
new file mode 100644
index 00000000..119ac4ec
--- /dev/null
+++ b/sdk/raea/bench/bevy-parity/camera/2d_on_ui.tsx
@@ -0,0 +1,38 @@
+// Bevy v0.20.0 examples/camera/2d_on_ui.rs in raea's TSX (docs/control.feature FC0a): a full-window tailwind ROSE_400
+// node centring a box (20% x 30%, at least 150 px, a 2 px white border rounded 25%) drawn by the default UI camera, and
+// over it, by a second Camera2d (order 1, ClearColorConfig::None, RenderLayers 1), the sensei (textures/rpg/chars/
+// sensei/sensei.png) as a 100 x 100 Sprite in the middle, turned each update by Rz(0.5 dt) * Ry(dt). The second camera
+// over the UI is the layered <Canvas3D> after the page's node: the 3D lies over the elements before it. In Bevy's
+// default 1280 x 720 window.
+import { AbsoluteFill, Composition, registerRoot, staticFile, useCurrentFrame } from "raea";
+import { Canvas3D, Sprite } from "raea/3d";
+import { Srgba } from "raea/color";
+import { Quat } from "raea/transform";
+import { BorderRadius, Node, UiRect, percent, px } from "raea/ui";
+
+// tailwind::ROSE_400
+const ROSE_400 = new Srgba(251 / 255, 113 / 255, 133 / 255);
+
+/** rotate_sprite over the updates up to frame f: rotation *= Rz(0.5 dt) * Ry(dt), dt a 1/60 s step (none on the first) */
+function rotationAt(f: number): Quat {
+ let q = Quat.IDENTITY;
+ const dt = Math.fround(16_666_667e-9);
+ for (let k = 1; k <= f; k++) q = Quat.mul(q, Quat.mul(Quat.fromRotationZ(dt * 0.5), Quat.fromRotationY(dt)));
+ return q;
+}
+
+function Shot() {
+ const q = rotationAt(useCurrentFrame());
+ return (
+ <AbsoluteFill>
+ <Node width={percent(100)} height={percent(100)} display="Flex" justify_content="Center" align_items="Center" background_color={ROSE_400}>
+ <Node height={percent(30)} width={percent(20)} min_height={px(150)} min_width={px(150)} border={UiRect.all(px(2))} border_radius={BorderRadius.all(percent(25))} border_color={new Srgba(1, 1, 1)} />
+ </Node>
+ <Canvas3D layered>
+ <Sprite src={staticFile("textures/rpg/chars/sensei/sensei.png")} width={100} height={100} x={640} y={360} quat={q} />
+ </Canvas3D>
+ </AbsoluteFill>
+ );
+}
+
+registerRoot(() => <Composition id="main" component={Shot} durationInFrames={61} fps={60} width={1280} height={720} />);
diff --git a/sdk/render/src/3d/sprite.tsx b/sdk/render/src/3d/sprite.tsx
index 91d8436c..336ded7d 100644
--- a/sdk/render/src/3d/sprite.tsx
+++ b/sdk/render/src/3d/sprite.tsx
@@ -1,6 +1,7 @@
// 2D over the 3D: sprites and flat shapes, drawn over the Canvas3D's 3D by a 2D camera of its own (raea-sys-sprite).
import { useId } from "react";
import { node3d } from "./core";
+import type { Quat } from "../bevy3d/core";
import { samplerSpec, type SamplerSpec } from "./materials";
import { usePointer2d, type Flat, type PointerHandlers3d } from "../events3d";
@@ -12,6 +13,9 @@ export interface Place2dProps extends PointerHandlers3d {
y?: number;
/** radians, clockwise (as CSS rotate) */
rotation?: number;
+ /** Bevy's rotation as it is, a quaternion [x, y, z, w] in Bevy's axes (y up, z toward the viewer), in place of
+ * `rotation`: a turn out of the frame's plane too, as a Sprite's Transform takes it (camera/2d_on_ui) */
+ quat?: Quat;
/** 1 by default; [x, y] to stretch */
scale?: number | [number, number];
/** draw order among the 2D parts: higher draws over lower (0 by default) */
@@ -127,6 +131,9 @@ function flatOf(p: Place2dProps, w: number, h: number, round: boolean): Flat {
function check2d(owner: string, p: Place2dProps): void {
for (const k of ["x", "y", "rotation", "z"] as const) if (p[k] !== undefined && !Number.isFinite(p[k])) throw new RangeError(`${owner}: ${k} must be a number, got ${p[k]}`);
+ const q = p.quat;
+ if (q !== undefined && !(Array.isArray(q) && q.length === 4 && q.every(Number.isFinite) && Math.abs(Math.hypot(...q) - 1) < 1e-3)) throw new TypeError(`${owner}: quat is a unit quaternion [x, y, z, w], got ${JSON.stringify(q)}`);
+ if (q !== undefined && p.rotation !== undefined) throw new TypeError(`${owner}: give rotation or quat, not both`);
if (p.opacity !== undefined && !(Number.isFinite(p.opacity) && p.opacity >= 0 && p.opacity <= 1)) throw new RangeError(`${owner}: opacity is 0 to 1, got ${p.opacity}`);
const sc = p.scale;
if (sc !== undefined && !(Number.isFinite(sc) || (Array.isArray(sc) && sc.length === 2 && sc.every(Number.isFinite)))) throw new TypeError(`${owner}: scale is a number or [x, y], got ${JSON.stringify(sc)}`);
diff --git a/systems/sprite/src/lib.rs b/systems/sprite/src/lib.rs
index e127ba00..342bb593 100644
--- a/systems/sprite/src/lib.rs
+++ b/systems/sprite/src/lib.rs
@@ -46,7 +46,11 @@ fn color_of(v: &BTreeMap<String, V>) -> Color {
fn place_of(v: &BTreeMap<String, V>) -> Transform {
Transform::from_xyz(num(v, "x", 0.0), -num(v, "y", 0.0), num(v, "z", 0.0))
- .with_rotation(Quat::from_rotation_z(-num(v, "rot", 0.0)))
+ .with_rotation(match v.contains_key("q_w") {
+ // a quaternion in Bevy's axes as the part gave it
+ true => Quat::from_xyzw(num(v, "q_x", 0.0), num(v, "q_y", 0.0), num(v, "q_z", 0.0), num(v, "q_w", 1.0)),
+ false => Quat::from_rotation_z(-num(v, "rot", 0.0)),
+ })
.with_scale(Vec3::new(num(v, "scale_x", 1.0), num(v, "scale_y", 1.0), 1.0))
}