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.

8.5 KB
Raw Blame

3D

This is the short version. The 3D library is bevy (raea guide bevy): it routes to one file per topic (scene and camera, lights, materials, meshes, animation, 3D games, models and pictures, bloom and fog, limits), each with a working example. Read it as soon as a piece needs more than a turning shape under two lights.

What to do

  • The 3D names are Bevy's, each from its Bevy module: Camera3d (raea/camera), Transform and Quat (raea/transform), Mesh3d and the shapes (raea/mesh), StandardMaterial (raea/pbr), the lights (raea/light). Write the scene straight in your tree, inside a full-frame AbsoluteFill; no wrapper is needed. The 3D is drawn under the page, so 2D parts (titles, captions) can sit over it in the same composition.
  • Build the scene from:
    • <Camera3d transform={Transform.fromXyz(0, 1, 6).lookingAt([0, 0, 0])} projection={{ Perspective: { fov: (50 * Math.PI) / 180 } }} />: the fov is vertical, in radians;
    • <AmbientLight brightness color> (cd/m², 80-200) and <DirectionalLight transform illuminance color shadow_maps_enabled> (lux, 8,000-12,000 for daylight), the sun shining along its transform's -Z, so give it .lookingAt([0, 0, 0]);
    • <Mesh3d transform>, holding one shape and one <StandardMaterial base_color emissive metallic perceptual_roughness>. The shapes are <Cuboid half_size={[w / 2, h / 2, d / 2]} />, <Sphere radius />, <Plane3d normal half_size />, <Cylinder radius half_height /> and <Torus major_radius minor_radius />. For a cartoon look use <ToonMaterial base_color steps={2} outline={{ width: 3 }} /> instead (hard light bands and an ink line; inside a <Model> it tones the model's own materials); raea guide bevy/materials has it.
  • Animate a mesh by computing its transform from the frame, exactly as you would a 2D part: Transform.fromXyz(x, y, z).withRotation(Quat.fromEuler("XYZ", rx, ry, rz)).withScale(s). Rotation is in radians.
  • Or declare a mesh's motion once: <Mesh3d animate={{ y: track({ 0: 3, 18: 0 }, curve.spring({ damping: 12 })), rotateY: track({ 0: 0, 59: Math.PI }, "in-out-sine") }}>. x, y, z set its translation, rotateX, rotateY, rotate its rotation in radians, and scale its size. The engine samples it from the frame (motion/animating has the tracks and curves). A material's colour and glow are per-frame values: compute them from the frame.
  • Model shapes the primitives can't make with Manifold solids: manifold.box(w, h, d), roundedBox, sphere, cylinder(r, h) (along z), cone, torus and text3d(text, size, depth, font, weight), combined with .union(), .minus(), .intersect() and .hull() and moved with .move(), .rotate() (in degrees) and .scale(). Pass the result as <Mesh3d shape={solid}>. You can also ask a solid about itself with volume(), bounds() and slice(z).
  • Colour materials from the brand kit. A material's base_color is a plain string handed to the 3D renderer, so read it from brand.json (kit.palette.accent) rather than typing a hex. The element that holds the camera has its background cleared to show the scene, and its colour becomes the scene's clear colour.
  • For a scene with many moving things, keep them in an ECS world (raea/ecs) and draw it with <Scene world>. It draws the world's mesh, camera, light and ambient entities at the current frame (motion/games).

Why

3D elements render as plain elements that carry their props, which raea's engine turns into camera, light and mesh entities and draws with Bevy. A mesh's geometry reaches the renderer as a Manifold expression (box(1, 2, 3)), so a modelled solid and a primitive travel the same way and render the same frames.

Manifold solids are made by the engine when it renders. Check a scene built from solids by rendering it and looking at a still.

Common mistakes

  • Quat.fromEuler("XYZ", 0, 45, 0) meaning degrees. A transform's rotation is radians: Math.PI / 4. (Solid's .rotate() is degrees, so the two differ.)
  • A Mesh3d with two shapes or no material. Give it one of each.
  • metallic or perceptual_roughness outside [0, 1], or a scale of 0: they throw. A fov of 40 is radians, a fisheye: write (40 * Math.PI) / 180.
  • color="var(--brand-accent)" on a material. The 3D renderer doesn't read CSS. Use the kit's hex from brand.json.
  • A hex colour typed into a material. Read it from the kit.
  • Rebuilding the same solid on every frame. Build it once at module level (in the engine) and move the Mesh3d.

Example: a turning product block with the title over it

import { AbsoluteFill, Composition, Easing, interpolate, registerRoot, useCurrentFrame, useVideoConfig } from "raea";
import { Torus } from "raea/3d";
import { Camera3d } from "raea/camera";
import { css } from "./styles.gen";
import kit from "./brand/brand.json";
import { LinearRgba, Srgba } from "raea/color";
import { StandardMaterial } from "raea/pbr";
import { Cuboid, Mesh3d } from "raea/mesh";
import { Quat, Transform } from "raea/transform";
import { AmbientLight, DirectionalLight } from "raea/light";

const OUT = Easing.out(Easing.cubic);
const ACCENT = kit.palette.accent ?? kit.colors[0].hex;
const SURFACE = kit.palette.surface ?? kit.colors[0].hex;

export default function Turn() {
  const f = useCurrentFrame();
  const { fps } = useVideoConfig();
  const spin = (f / fps) * (Math.PI / 2); // a quarter turn a second
  const drop = interpolate(f, [0, 18], [3, 0], { easing: OUT, extrapolateLeft: "clamp", extrapolateRight: "clamp" });
  return (
    <AbsoluteFill style={{ backgroundColor: "var(--brand-background)" }}>
      <style>{css}</style>
      <AbsoluteFill>
        <Camera3d transform={Transform.fromXyz(0, 1.2, 6).lookingAt([0, 0, 0])} projection={{ Perspective: { fov: (40 * Math.PI) / 180 } }} />
        <AmbientLight brightness={160} />
        <DirectionalLight transform={Transform.fromXyz(3, 5, 4).lookingAt([0, 0, 0])} illuminance={12800} shadow_maps_enabled />
        <Mesh3d transform={Transform.fromXyz(0, drop, 0).withRotation(Quat.fromEuler("XYZ", 0.2, spin, 0))}>
          <Cuboid half_size={[0.8, 0.8, 0.8]} />
          <StandardMaterial base_color={ACCENT} perceptual_roughness={0.35} metallic={0.1} />
        </Mesh3d>
        <Mesh3d transform={Transform.fromXyz(0, -1.4, 0).withRotation(Quat.fromEuler("XYZ", Math.PI / 2, 0, 0))}>
          <Torus major_radius={1.8} minor_radius={0.05} />
          <StandardMaterial base_color={SURFACE} perceptual_roughness={0.5} emissive={LinearRgba.from(Srgba.hex(ACCENT)).mul(0.6)} />
        </Mesh3d>
      </AbsoluteFill>
      <div style={{ position: "absolute", left: 90, right: 90, top: 300, textAlign: "center", fontFamily: "var(--brand-font-heading)", fontWeight: 800, fontSize: 120, lineHeight: 1, color: "var(--brand-text)" }}>
        Built to last
      </div>
    </AbsoluteFill>
  );
}

registerRoot(() => <Composition id="turn" component={Turn} width={1080} height={1920} fps={30} durationInFrames={120} />);

Example: a modelled solid (engine only)

import { AbsoluteFill, Composition, registerRoot, useCurrentFrame } from "raea";
import { manifold, type Solid } from "raea/3d";
import { Camera3d } from "raea/camera";
import { css } from "./styles.gen";
import kit from "./brand/brand.json";
import { StandardMaterial } from "raea/pbr";
import { Mesh3d } from "raea/mesh";
import { Quat, Transform } from "raea/transform";
import { AmbientLight, DirectionalLight } from "raea/light";

/** A rounded tile with a hole through it, built once (the engine holds it). */
let tile: Solid | null = null;
function getTile(): Solid {
  tile ??= manifold.roundedBox(2, 2, 0.4, 0.15).minus(manifold.cylinder(0.45, 1));
  return tile;
}

export default function Tile() {
  const f = useCurrentFrame();
  return (
    <AbsoluteFill style={{ backgroundColor: "var(--brand-background)" }}>
      <style>{css}</style>
      <Camera3d transform={Transform.fromXyz(0, 0, 6)} projection={{ Perspective: { fov: (40 * Math.PI) / 180 } }} />
      <AmbientLight brightness={200} />
      <DirectionalLight transform={Transform.fromXyz(2, 4, 5).lookingAt([0, 0, 0])} illuminance={11200} shadow_maps_enabled />
      <Mesh3d transform={Transform.IDENTITY.withRotation(Quat.fromEuler("XYZ", 0.3, f * 0.03, 0))} shape={getTile()}>
        <StandardMaterial base_color={kit.palette.accent ?? kit.colors[0].hex} perceptual_roughness={0.4} />
      </Mesh3d>
    </AbsoluteFill>
  );
}

registerRoot(() => <Composition id="tile" component={Tile} width={1080} height={1080} fps={30} durationInFrames={90} />);