Photo mode
Wraith has a free camera and posing built in, which players reach through Photo mode. They need the game’s camera and characters, and that’s where a plugin for the game, or its engine, comes in. It hooks where the game sets them each frame and hands them over. Wraith does the rest: the keys, settings, windows and drawing.
The Unity plugin does both for Unity games, and the Unreal plugin for Unreal ones, so a game on those only needs this if its camera or characters are its own.
The camera
Section titled “The camera”The camera area is the free camera, Wraith.FreeCamera. First claim the camera’s lease with camera.claim(): only the holder’s calls count, so a game’s plugin can take over from its engine’s. Tell Wraith how the game’s world is laid out once, with camera.setWorld(world): which axis is up, which way the camera looks, whether it’s left-handed, and how many units make a metre. Then each frame, pass the camera, a FreeCamera.State, to camera.camera(id, state). While the free camera’s on, Wraith changes it in place to its own. id says which of the game’s cameras it is: 0 for a game with one, or its id in the list below for a game with more.
The camera can point by rotation, a quaternion, or at a target point with roll. Set aim to say which. fov is vertical, in radians.
import { Wraith } from "wraith";
const camera = Wraith.camera();camera.claim(null);
camera.setWorld({ up: "pos_y", look: "pos_z", left_handed: true, metre: 1 });
// From your hook, each frameconst state = { position, aim: Wraith.FreeCamera.Aim.rotation, target: { x: 0, y: 0, z: 0 }, up: { x: 0, y: 1, z: 0 }, rotation, roll: 0, fov };camera.camera(0, state);// Put state.position, state.rotation and state.fov back on the game's cameralocal Wraith = require("wraith").Wraith
local camera = Wraith.camera()camera:claim(nil)
camera:setWorld({ up = "pos_y", look = "pos_z", left_handed = true, metre = 1 })
-- From your hook, each framelocal state = { position = position, aim = Wraith.FreeCamera.Aim.rotation, target = { x = 0, y = 0, z = 0 }, up = { x = 0, y = 1, z = 0 }, rotation = rotation, roll = 0, fov = fov }camera:camera(0, state)-- Put state.position, state.rotation and state.fov back on the game's cameravar cam = FreeCamera.Instance;cam.Claim(null);cam.SetWorld(new FreeCamera.World { Up = FreeCamera.Axis.PosY, Look = FreeCamera.Axis.PosZ, LeftHanded = true, Metre = 1 });
// From your hook, each framevar state = new FreeCamera.State { Position = position, Aim = FreeCamera.Aim.Rotation, Up = new Vec3 { Y = 1 }, Rotation = rotation, Roll = 0, Fov = fov };cam.Camera(0, ref state);// Put state.Position, state.Rotation and state.Fov back on the game's camera// From your hook, each framestatic void on_camera(Wraith::Vec3 position, Wraith::Quat rotation, float fov) { Wraith::FreeCamera::State state{.position = position, .aim = Wraith::FreeCamera::Aim::rotation, .up = {0, 1, 0}, .rotation = rotation, .fov = fov}; wraith::api().camera().camera(0, &state); // Put state.position, state.rotation and state.fov back on the game's camera}
// in startauto camera = wraith::api().camera();camera.claim({});camera.setWorld({.up = Wraith::FreeCamera::Axis::pos_y, .look = Wraith::FreeCamera::Axis::pos_z, .left_handed = true, .metre = 1});use wraith::{FreeCameraAim, FreeCameraAxis, FreeCameraState, FreeCameraWorld, LeaseChanged, Quat, Vec3};
fn start_camera() { let camera = wraith::api().camera(); let _ = camera.claim(LeaseChanged::default()); camera.set_world(FreeCameraWorld { up: FreeCameraAxis::POS_Y, look: FreeCameraAxis::POS_Z, left_handed: true, metre: 1.0, ..Default::default() });}
// From your hook, each framefn on_camera(position: Vec3, rotation: Quat, fov: f32) { let mut state = FreeCameraState { position, aim: FreeCameraAim::ROTATION, up: Vec3 { x: 0.0, y: 1.0, z: 0.0 }, rotation, fov, ..Default::default() }; wraith::api().camera().camera(0, &mut state); // Put state.position, state.rotation and state.fov back on the game's camera}const W = sdk.api.wraith;const cam = ctx.wraith.camera();self.camera_lease = try sdk.Lease.claimCamera(.none);cam.setWorld(.{ .up = .pos_y, .look = .pos_z, .left_handed = true, .metre = 1 });
// From your hook, each framevar state: W.FreeCamera.State = .{ .position = position, .aim = .rotation, .target = .{}, .up = .{ .y = 1 }, .rotation = rotation, .roll = 0, .fov = fov };const before = state;cam.camera(0, &state);if (!std.meta.eql(state, before)) { // Put it back on the game's camera}/* From your hook, each frame */static void on_camera(Wraith_Vec3 position, Wraith_Quat rotation, float fov) { Wraith_FreeCamera_State state = { .position = position, .aim = Wraith_FreeCamera_Aim_rotation, .up = { 0, 1, 0 }, .rotation = rotation, .fov = fov }; Wraith_FreeCamera_camera(Wraith_Api_camera(w), 0, &state); /* Put state.position, state.rotation and state.fov back on the game's camera */}
/* in start */Wraith_FreeCamera camera = Wraith_Api_camera(w);Wraith_Lease lease;Wraith_FreeCamera_claim(camera, (Wraith_Lease_Changed){ 0 }, &lease);Wraith_FreeCamera_World world = { .up = Wraith_FreeCamera_Axis_pos_y, .look = Wraith_FreeCamera_Axis_pos_z, .left_handed = true, .metre = 1 };Wraith_FreeCamera_setWorld(camera, world);Hook somewhere after the game’s own camera code has run for the frame, or it’ll undo the free camera. When the free camera’s turned off, camera leaves the state alone again, so a game that sets its camera every frame snaps straight back. One that doesn’t needs the plugin to put back what it had.
active() says whether the free camera’s on. Any plugin can call focus(point) with a point in the world. That turns the free camera on if it isn’t, and orbits the point. Posing uses it to orbit the picked bone.
Games with more than one camera
Section titled “Games with more than one camera”The free camera always moves whichever camera’s on screen. A game with several can list them in Photo mode’s Camera panel, where players see which is on screen, and can tick Follow selection to show another instead:
cameras(add, rename)gives the plugin’s list, anItemList.setthe game’s cameras on it each time they change, with anidfor each that stays the same while it’s around, andcurrenton the one on screen. Passaddandrenameif the plugin can make and rename cameras, or leave them out.- Each frame, put
wantedCamera()on screen, or the game’s own for 0. That can mean switching the camera the game draws with, like the Unity plugin does, or drawing the game’s camera from where the picked one is, like the Unreal plugin does. A camera the player adds can be just a view the plugin keeps, with no object in the game at all. - Hand
camerawhichever’s on screen, with its id. When the id changes, the free camera carries on from the new one rather than moving it where the last one was. - When the free camera leaves one of the game’s own cameras, put it back where the game had it. Ones the player added stay where they were put.
Posing
Section titled “Posing”Hook wherever the game has just animated a skeleton, and hand its bones to pose. Claim posing’s lease first with posing.claim(), since only the holder’s skeletons are posed. It has five calls:
active()says whether posing wants your skeletons right now: you hold the lease and posing’s on. Skip building them when it’s false. It’s cheap.pose(skeleton, matrices), each frame for each character, once the game’s done with the skeleton (animation, IK and physics) and before anything follows it, like attached weapons. Posing changes the matrices in place.shapes(actor, list, weights), for characters with blend shapes or morph targets, like faces. Call it afterposefor the same character, with the skeleton’sid, and before the game skins it. Posing changes the weights in place.place(actor, to_world)lets the player move a character. Call it beforeposewith where the game has it, and it changesto_worldto where they’ve moved it, returning true if they have. Use that as the skeleton’sto_worldand hand the bones over relative to it. Leave it out for things your plugin can’t move.wanted(actor)says whether the player’s picked that character or edited it. A game that animates far or minor characters less, or at lower detail, can animate just these ones in full while posing’s open.
Getting the spot right matters. Too early and the game’s IK or physics undoes the edits. Too late and anything attached, like a sword in a hand, stays where it was.
A skeleton has an id that stays the same for that character while it’s around, like its address, a name for the list, its bones and to_world. Each bone has a name and its parent‘s place in the list, or -1, and parents come before their children. Edits are kept by bone name, so names should stay the same from frame to frame. matrices points at one matrix per bone, in the same order, from the bones’ space. to_world takes that to the world, or is the identity if they’re in the world already.
Posing draws the bones and the gizmo with the game’s camera, so your plugin needs to claim the view and set it too, with display.setView each frame.
Each shape has a name and the mesh it’s on, and edits are kept by both. weights points at one f32 per shape, in the same order, 0 for none of it and 1 for all of it. Scale the game’s own to that, like Unity’s 0 to 100. Players can edit a shape on one mesh, or every mesh’s shape of the same name at once. For that, what’s before the last . doesn’t count, so Face.blink and Lashes.blink go together.
import { Wraith } from "wraith";
const posing = Wraith.posing();posing.claim(null);
// From your hook, after the game animates a characterif (posing.active()) { const bones = [{ name: "hips", parent: -1 }, { name: "spine", parent: 0 }]; // The address of the character's first bone matrix, in the game's memory posing.pose({ id: BigInt(character), name: "Velvet", bones, to_world: identity }, matricesAddress);}local Wraith = require("wraith").Wraith
-- From your hook, after the game animates a characterlocal posing = Wraith.posing()posing:claim(nil)
if posing:active() then local bones = { { name = "hips", parent = -1 }, { name = "spine", parent = 0 } } -- The address of the character's first bone matrix, in the game's memory posing:pose({ id = character, name = "Velvet", bones = bones, to_world = identity }, matrices_address)endvar posing = Posing.Instance;posing.Claim(null);
// From your hook, after the game animates a characterif (posing.Active()){ Posing.Bone[] bones = [new() { Name = "hips", Parent = -1 }, new() { Name = "spine", Parent = 0 }]; // matrices points at the character's first bone matrix, in the game's memory posing.Pose(new Posing.Skeleton { Id = character, Name = "Velvet", Bones = bones, ToWorld = identity }, matrices); // And its face, if it has blend shapes Posing.Shape[] shapes = [new() { Name = "blink", Mesh = "Face" }, new() { Name = "smile", Mesh = "Face" }]; var weights = stackalloc float[] { blink, smile }; posing.Shapes(character, shapes, weights);}// From your hook, after the game animates a characterstatic void on_animated(uint64_t character, Wraith::Mat4* matrices, Wraith::Mat4 identity, float blink, float smile) { auto posing = wraith::api().posing(); if (!posing.active()) return; static const Wraith::Posing::Bone bones[] = {{sal::String::of("hips"), -1}, {sal::String::of("spine"), 0}}; posing.pose({.id = character, .name = sal::String::of("Velvet"), .bones = sal::Slice<Wraith::Posing::Bone>::of(bones), .to_world = identity}, matrices); // And its face, if it has blend shapes static const Wraith::Posing::Shape shapes[] = {{sal::String::of("blink"), sal::String::of("Face")}, {sal::String::of("smile"), sal::String::of("Face")}}; float weights[] = {blink, smile}; posing.shapes(character, shapes, weights);}
// in startwraith::api().posing().claim({});use salamander::{Slice, Str};use wraith::{LeaseChanged, Mat4, PosingBone, PosingShape, PosingSkeleton};
// From your hook, after the game animates a characterfn on_animated(character: u64, matrices: *mut Mat4, identity: Mat4, blink: f32, smile: f32) { let posing = wraith::api().posing(); if !posing.active() { return; } let bones = [PosingBone { name: Str::new("hips"), parent: -1, ..Default::default() }, PosingBone { name: Str::new("spine"), parent: 0, ..Default::default() }]; let skeleton = PosingSkeleton { id: character, name: Str::new("Velvet"), bones: Slice::of(&bones), to_world: identity }; posing.pose(skeleton, matrices); // And its face, if it has blend shapes let shapes = [PosingShape { name: Str::new("blink"), mesh: Str::new("Face") }, PosingShape { name: Str::new("smile"), mesh: Str::new("Face") }]; let mut weights = [blink, smile]; posing.shapes(character, &shapes, weights.as_mut_ptr());}
fn start_posing() { let _ = wraith::api().posing().claim(LeaseChanged::default());}const posing = ctx.wraith.posing();self.posing_lease = try sdk.Lease.claimPosing(.none);
// From your hook, after the game animates a characterif (posing.active()) { const matrices: [*]sdk.math.Mat4 = boneMatrices(character); posing.pose(.{ .id = @intFromPtr(character), .name = "Velvet", .bones = bones, .to_world = .identity }, &matrices[0]); // And its face, if it has blend shapes const shapes = [_]W.Posing.Shape{ .{ .name = "blink", .mesh = "Face" }, .{ .name = "smile", .mesh = "Face" } }; var weights = [_]f32{ blink, smile }; posing.shapes(@intFromPtr(character), &shapes, &weights[0]);}/* From your hook, after the game animates a character */static void on_animated(uint64_t character, Wraith_Mat4 *matrices, Wraith_Mat4 identity, float blink, float smile) { Wraith_Posing posing = Wraith_Api_posing(w); if (!Wraith_Posing_active(posing)) return; static const Wraith_Posing_Bone bones[] = { { SAL_STR_INIT("hips"), -1 }, { SAL_STR_INIT("spine"), 0 } }; Wraith_Posing_Skeleton skeleton = { .id = character, .name = SAL_STR("Velvet"), .bones = { bones, 2 }, .to_world = identity }; Wraith_Posing_pose(posing, skeleton, matrices); /* And its face, if it has blend shapes */ static const Wraith_Posing_Shape shapes[] = { { SAL_STR_INIT("blink"), SAL_STR_INIT("Face") }, { SAL_STR_INIT("smile"), SAL_STR_INIT("Face") } }; float weights[] = { blink, smile }; Wraith_Posing_shapes(posing, character, (Slice_Wraith_Posing_Shape){ shapes, 2 }, weights);}IK solvers
Section titled “IK solvers”Players can give a chain of bones IK, and pick its solver from Wraith’s own and any plugin’s. If the game has IK of its own, like foot placement, posing.addSolver(info, solve, options) lists it too. Any plugin can add one, without the lease.
infohas anid(letters, digits,_,-and.), atitleandhelpfor the list, how many bones a chain it takes can have (min_bones, andmax_bonesor 0 for no most), andpoleif it bends toward one.solve(request)is called on any thread, for each chain using it each frame, after the player’s edits.request.chainis the chain’s matrices, root first, in the character’s space. Return the chain solved, the same length, or nothing to leave it. Wraith handles Match rotation, the weight, and the bones hanging off.optionsis optional. It draws your solver’s settings on the render thread from a string in your own format, and returns it as changed. Chains and saved poses keep it.
Chains name a plugin’s solver as plugin.id. One added again with the same id replaces it, and it’s gone when removeSolver(id) is called or your plugin goes. Chains using a solver that isn’t loaded are left alone.
const Foot = struct { out: [3]sdk.math.Mat4 = undefined,
fn solve(self: *Foot, req: W.Ik.Request) []const sdk.math.Mat4 { if (req.chain.len != 3) return &.{}; @memcpy(&self.out, req.chain); gameFootIk(&self.out, req.target, if (req.has_pole) req.pole else null); return &self.out; }};
var foot: Foot = .{};try posing.addSolver(.{ .id = "foot", .title = "Foot", .help = "The game's own foot placement.", .min_bones = 3, .max_bones = 3, .pole = true }, .init(&foot, Foot.solve), null);Both are in Wraith’s API schemas, camera.sal and posing.sal.
wraith.io · Mods on the Hub · Get Wraith
Made with ♥ by Asgard
