The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.
TypeScript ai-videocomfyuicreative-toolsgenerative-aiideogramimage-generationimage-to-3dimage-to-videoltx-videominimax-h3nextjsopen-sourceopen-weightsqwen-imagetext-to-imagevideo-generationwan
Camera angles: a pose snaps to the nearest of QMA's 96 views, and back
Claude882f812937c6
diff --git a/src/lib/qma.ts b/src/lib/qma.tsnew file mode 100644index 00000000..c1548d78--- /dev/null+++ b/src/lib/qma.ts@@ -0,0 +1,173 @@+/**+ * QMA camera angles (package qma-angles): gribbliest's Qwen-Image 2.1 edit LoRA "qwen-image-2.1-qma-camera-angles" @ 13007eec+ * (Apache-2.0 on its card, rank 64, 319 MB), which re-shoots a picture from one of 96 cameras. Its prompt is always+ * "<qma> {azimuth} {elevation} {distance}", optionally followed by more edit words; directions are the ORIGINAL CAMERA's, not+ * the subject's ("right side view" = the camera swings 90° to its own right round the subject). Card settings: LoRA 1.0,+ * 30 steps, CFG 3, euler / simple, about 1 MP (768 px for previews).+ *+ * Fuobe's camera pose is the one the Angles app, Cinema's 3D scene and its location views already speak (src/lib/scene3d.ts+ * ScenePose, the same numbers as Qwen Multiple Angles' horizontal_angle / vertical_angle / zoom): horizontal 0–360° (0 = where+ * the photo was taken, 90 = moved round to the camera's right), vertical −30…90° (below eye level up to straight down),+ * zoom 0 (wide) … 5 (as framed) … 10 (close-up). Here it snaps to the nearest of the 96 views and back.+ *+ * Pure: the planner writes the words (planGeneration, promptFormat "qma"), and the Angles app's orbit pad, Cinema,+ * the turnaround and the Canvas read the same tables in the browser.+ */+import type { ScenePose } from "@/lib/scene3d";++/** The LoRA's trigger: every prompt starts with it. */+export const QMA_TRIGGER = "<qma>";++/** The 8 azimuths, 45° apart, from the front going round to the camera's right. */+export const QMA_AZIMUTHS = [+ "front view",+ "front-right quarter view",+ "right side view",+ "back-right quarter view",+ "back view",+ "back-left quarter view",+ "left side view",+ "front-left quarter view",+] as const;+/** The 4 heights, low to high. */+export const QMA_ELEVATIONS = ["low-angle shot", "eye-level shot", "elevated shot", "high-angle shot"] as const;+/** The 3 distances, near to far. */+export const QMA_DISTANCES = ["close-up", "medium shot", "wide shot"] as const;++export type QmaAzimuth = (typeof QMA_AZIMUTHS)[number];+export type QmaElevation = (typeof QMA_ELEVATIONS)[number];+export type QmaDistance = (typeof QMA_DISTANCES)[number];++export interface QmaView {+ azimuth: QmaAzimuth;+ elevation: QmaElevation;+ distance: QmaDistance;+}++/** Short names for the picker and for a run's label. */+export const QMA_AZIMUTH_LABELS: Record<QmaAzimuth, string> = {+ "front view": "Front",+ "front-right quarter view": "Front right",+ "right side view": "Right side",+ "back-right quarter view": "Back right",+ "back view": "Back",+ "back-left quarter view": "Back left",+ "left side view": "Left side",+ "front-left quarter view": "Front left",+};+export const QMA_ELEVATION_LABELS: Record<QmaElevation, string> = {+ "low-angle shot": "Low angle",+ "eye-level shot": "Eye level",+ "elevated shot": "Elevated",+ "high-angle shot": "High angle",+};+export const QMA_DISTANCE_LABELS: Record<QmaDistance, string> = { "close-up": "Close-up", "medium shot": "Medium", "wide shot": "Wide" };++/** The pose each height and distance stands for (the Angles app's own numbers: vertical_angle, zoom). */+export const QMA_ELEVATION_DEGREES: Record<QmaElevation, number> = { "low-angle shot": -30, "eye-level shot": 0, "elevated shot": 30, "high-angle shot": 60 };+export const QMA_DISTANCE_ZOOM: Record<QmaDistance, number> = { "close-up": 10, "medium shot": 5, "wide shot": 0 };++/** A setting of the model's (params.extra): the same keys the Angles app's choices set, so one pose reads everywhere. */+export const QMA_PARAMS = { horizontal: "horizontal_angle", vertical: "vertical_angle", zoom: "zoom" } as const;++/** The view a run takes when nothing else says: a quarter turn, the card's most reliable kind. */+export const QMA_DEFAULT_VIEW: QmaView = { azimuth: "front-right quarter view", elevation: "eye-level shot", distance: "medium shot" };++/** Every one of the 96 views, azimuth first, then height, then distance. */+export const QMA_VIEWS: readonly QmaView[] = QMA_AZIMUTHS.flatMap((azimuth) => QMA_ELEVATIONS.flatMap((elevation) => QMA_DISTANCES.map((distance) => ({ azimuth, elevation, distance }))));++const wrap360 = (n: number) => ((n % 360) + 360) % 360;++/** The azimuth nearest a horizontal angle, in 45° steps; exactly halfway (22.5°, 67.5°…) goes on round to the right. */+export function qmaAzimuth(horizontal: number): QmaAzimuth {+ const h = Number.isFinite(horizontal) ? wrap360(horizontal) : 0;+ return QMA_AZIMUTHS[Math.floor(h / 45 + 0.5) % 8];+}++/**+ * The height band of a vertical angle: below −15° low, up to 15° eye level, up to 45° elevated, from 45° high (the+ * midpoints between the Angles app's −30, 0, 30 and 60; a midpoint goes up).+ */+export function qmaElevation(vertical: number): QmaElevation {+ const v = Number.isFinite(vertical) ? vertical : 0;+ return v < -15 ? "low-angle shot" : v < 15 ? "eye-level shot" : v < 45 ? "elevated shot" : "high-angle shot";+}++/** The distance band of a zoom: 7.5 and up close-up, 2.5 and down wide, medium between (as Cinema's poseWords reads it). */+export function qmaDistance(zoom: number): QmaDistance {+ const z = Number.isFinite(zoom) ? zoom : 5;+ return z >= 7.5 ? "close-up" : z <= 2.5 ? "wide shot" : "medium shot";+}++/** The nearest of the 96 views to a camera pose. */+export function qmaView(pose: Partial<ScenePose>): QmaView {+ return { azimuth: qmaAzimuth(pose.horizontal ?? 0), elevation: qmaElevation(pose.vertical ?? 0), distance: qmaDistance(pose.zoom ?? 5) };+}++/** The pose a view stands for (for the orbit pad, and to show a QMA run as an angle). */+export function qmaPose(view: QmaView): ScenePose {+ return { horizontal: QMA_AZIMUTHS.indexOf(view.azimuth) * 45, vertical: QMA_ELEVATION_DEGREES[view.elevation], zoom: QMA_DISTANCE_ZOOM[view.distance] };+}++/** The camera words: "front-right quarter view eye-level shot medium shot". */+export function qmaWords(view: QmaView): string {+ return `${view.azimuth} ${view.elevation} ${view.distance}`;+}++/** The whole prompt: the trigger, the camera words, then any other edit words (the card: camera words first). */+export function qmaPrompt(view: QmaView, words?: string): string {+ const extra = words?.trim();+ return `${QMA_TRIGGER} ${qmaWords(view)}${extra ? ` ${extra}` : ""}`;+}++/** A view in a person's words: "Front right · Eye level · Medium". */+export function qmaLabel(view: QmaView): string {+ return `${QMA_AZIMUTH_LABELS[view.azimuth]} · ${QMA_ELEVATION_LABELS[view.elevation]} · ${QMA_DISTANCE_LABELS[view.distance]}`;+}++const phrase = (list: readonly string[], s: string) => list.filter((p) => s.startsWith(p)).sort((a, b) => b.length - a.length)[0];++/** The view and the other words of a QMA prompt; undefined when it doesn't start with the trigger and three camera words. */+export function parseQmaPrompt(prompt: string): { view: QmaView; words: string } | undefined {+ const s = prompt.trim();+ if (!s.startsWith(QMA_TRIGGER)) return undefined;+ let rest = s.slice(QMA_TRIGGER.length).trimStart();+ const azimuth = phrase(QMA_AZIMUTHS, rest) as QmaAzimuth | undefined;+ if (!azimuth) return undefined;+ rest = rest.slice(azimuth.length).trimStart();+ const elevation = phrase(QMA_ELEVATIONS, rest) as QmaElevation | undefined;+ if (!elevation) return undefined;+ rest = rest.slice(elevation.length).trimStart();+ const distance = phrase(QMA_DISTANCES, rest) as QmaDistance | undefined;+ if (!distance) return undefined;+ return { view: { azimuth, elevation, distance }, words: rest.slice(distance.length).replace(/^[\s.,;:]+/, "").trim() };+}++/** The pose a run's settings name (params.extra: horizontal_angle, vertical_angle, zoom), each missing one from the default view. */+export function qmaPoseOf(extra: Record<string, unknown> | undefined): ScenePose {+ const d = qmaPose(QMA_DEFAULT_VIEW);+ const num = (v: unknown, fallback: number) => (typeof v === "number" && Number.isFinite(v) ? v : typeof v === "string" && v.trim() && Number.isFinite(Number(v)) ? Number(v) : fallback);+ return { horizontal: num(extra?.[QMA_PARAMS.horizontal], d.horizontal), vertical: num(extra?.[QMA_PARAMS.vertical], d.vertical), zoom: num(extra?.[QMA_PARAMS.zoom], d.zoom) };+}++/**+ * The prompt a QMA run sends: words that already start with the trigger and three camera words go as they are (the Angles+ * app's, a Reuse); otherwise the camera words of the run's pose (params.extra) lead and the words follow.+ */+export function qmaRunPrompt(words: string, extra: Record<string, unknown> | undefined): string {+ if (parseQmaPrompt(words)) return words.trim();+ return qmaPrompt(qmaView(qmaPoseOf(extra)), words.replace(QMA_TRIGGER, "").trim());+}++/**+ * The orbit pad's drag: the pose moved by a drag of (dx, dy) pixels on a pad `size` pixels across. A full width turns the+ * camera once round (dragging right swings it to the right); a full height goes from straight below eye level (−30°) to+ * the top (90°), dragging up raising it.+ */+export function orbitDrag(start: ScenePose, dx: number, dy: number, size: number): ScenePose {+ const s = Math.max(1, size);+ return { horizontal: wrap360(start.horizontal + (dx / s) * 360), vertical: Math.max(-30, Math.min(90, start.vertical - (dy / s) * 120)), zoom: start.zoom };+}++/** The 8 eye-level views a turnaround still set makes, the front first, going round to the camera's right. */+export const QMA_TURN_SET: readonly QmaView[] = QMA_AZIMUTHS.map((azimuth) => ({ azimuth, elevation: "eye-level shot" as const, distance: "medium shot" as const }));diff --git a/tests/qma-prompt.test.ts b/tests/qma-prompt.test.tsnew file mode 100644index 00000000..af98579e--- /dev/null+++ b/tests/qma-prompt.test.ts@@ -0,0 +1,113 @@+import { describe, expect, it } from "vitest";+import {+ QMA_AZIMUTHS,+ QMA_DISTANCES,+ QMA_ELEVATIONS,+ QMA_TURN_SET,+ QMA_VIEWS,+ orbitDrag,+ parseQmaPrompt,+ qmaAzimuth,+ qmaDistance,+ qmaElevation,+ qmaLabel,+ qmaPose,+ qmaPoseOf,+ qmaPrompt,+ qmaRunPrompt,+ qmaView,+ qmaWords,+} from "@/lib/qma";++// Package qma-angles: Fuobe's camera pose (the Angles app's horizontal_angle / vertical_angle / zoom, Cinema's ScenePose) to+// the QMA LoRA's 96 views and back.+describe("QMA views", () => {+ it("has the card's 8 × 4 × 3 = 96 views, each once", () => {+ expect(QMA_AZIMUTHS).toHaveLength(8);+ expect(QMA_ELEVATIONS).toHaveLength(4);+ expect(QMA_DISTANCES).toHaveLength(3);+ expect(QMA_VIEWS).toHaveLength(96);+ expect(new Set(QMA_VIEWS.map(qmaWords)).size).toBe(96);+ });++ it("round-trips every one of the 96 views through its pose, its prompt and its label", () => {+ for (const v of QMA_VIEWS) {+ expect(qmaView(qmaPose(v))).toEqual(v);+ const p = qmaPrompt(v);+ expect(p).toBe(`<qma> ${v.azimuth} ${v.elevation} ${v.distance}`);+ expect(parseQmaPrompt(p)).toEqual({ view: v, words: "" });+ expect(parseQmaPrompt(qmaPrompt(v, "make it night"))).toEqual({ view: v, words: "make it night" });+ expect(qmaRunPrompt(p, { horizontal_angle: 999 })).toBe(p);+ expect(qmaLabel(v).split(" · ")).toHaveLength(3);+ }+ });++ it("keeps the card's order and wording in the prompt", () => {+ expect(qmaPrompt({ azimuth: "right side view", elevation: "eye-level shot", distance: "medium shot" })).toBe("<qma> right side view eye-level shot medium shot");+ expect(qmaPrompt({ azimuth: "back view", elevation: "elevated shot", distance: "wide shot" })).toBe("<qma> back view elevated shot wide shot");+ expect(qmaPrompt({ azimuth: "front-left quarter view", elevation: "low-angle shot", distance: "close-up" }, " ")).toBe("<qma> front-left quarter view low-angle shot close-up");+ });+});++describe("snapping a pose", () => {+ it("snaps the azimuth to 45° steps, halfway going on round to the right, and wraps", () => {+ expect(qmaAzimuth(0)).toBe("front view");+ expect(qmaAzimuth(22.49)).toBe("front view");+ expect(qmaAzimuth(22.5)).toBe("front-right quarter view");+ expect(qmaAzimuth(67.5)).toBe("right side view");+ expect(qmaAzimuth(180)).toBe("back view");+ expect(qmaAzimuth(337.49)).toBe("front-left quarter view");+ expect(qmaAzimuth(337.5)).toBe("front view");+ expect(qmaAzimuth(360)).toBe("front view");+ expect(qmaAzimuth(-45)).toBe("front-left quarter view");+ expect(qmaAzimuth(-90)).toBe("left side view");+ expect(qmaAzimuth(405)).toBe("front-right quarter view");+ expect(qmaAzimuth(Number.NaN)).toBe("front view");+ });++ it("bands the height at the midpoints of −30, 0, 30 and 60", () => {+ expect(qmaElevation(-90)).toBe("low-angle shot");+ expect(qmaElevation(-15.01)).toBe("low-angle shot");+ expect(qmaElevation(-15)).toBe("eye-level shot");+ expect(qmaElevation(14.99)).toBe("eye-level shot");+ expect(qmaElevation(15)).toBe("elevated shot");+ expect(qmaElevation(44.99)).toBe("elevated shot");+ expect(qmaElevation(45)).toBe("high-angle shot");+ expect(qmaElevation(90)).toBe("high-angle shot");+ });++ it("bands the distance as Cinema's pose words do", () => {+ expect(qmaDistance(0)).toBe("wide shot");+ expect(qmaDistance(2.5)).toBe("wide shot");+ expect(qmaDistance(2.51)).toBe("medium shot");+ expect(qmaDistance(7.49)).toBe("medium shot");+ expect(qmaDistance(7.5)).toBe("close-up");+ expect(qmaDistance(10)).toBe("close-up");+ });++ it("reads a run's settings, filling what's missing from a quarter turn at eye level", () => {+ expect(qmaPoseOf(undefined)).toEqual({ horizontal: 45, vertical: 0, zoom: 5 });+ expect(qmaPoseOf({ horizontal_angle: "180", vertical_angle: 60, zoom: 0 })).toEqual({ horizontal: 180, vertical: 60, zoom: 0 });+ expect(qmaRunPrompt("make it snow", { horizontal_angle: 90 })).toBe("<qma> right side view eye-level shot medium shot make it snow");+ expect(qmaRunPrompt("", {})).toBe("<qma> front-right quarter view eye-level shot medium shot");+ // A trigger with no camera words after it isn't a QMA prompt: the pose's words go in, the stray trigger out.+ expect(qmaRunPrompt("<qma> a red car", { horizontal_angle: 180 })).toBe("<qma> back view eye-level shot medium shot a red car");+ });+});++describe("the orbit pad and the turnaround set", () => {+ it("turns once round across the pad and raises the camera dragging up", () => {+ const start = { horizontal: 0, vertical: 0, zoom: 5 };+ expect(orbitDrag(start, 50, 0, 200).horizontal).toBe(90);+ expect(qmaView(orbitDrag(start, 50, 0, 200)).azimuth).toBe("right side view");+ expect(orbitDrag(start, -50, 0, 200).horizontal).toBe(270);+ expect(qmaView(orbitDrag(start, 0, -50, 200)).elevation).toBe("elevated shot");+ expect(orbitDrag(start, 0, -1000, 200).vertical).toBe(90);+ expect(orbitDrag(start, 0, 1000, 200).vertical).toBe(-30);+ });++ it("makes the 8 azimuths at eye level, front first", () => {+ expect(QMA_TURN_SET.map((v) => v.azimuth)).toEqual([...QMA_AZIMUTHS]);+ expect(new Set(QMA_TURN_SET.map((v) => v.elevation))).toEqual(new Set(["eye-level shot"]));+ });+});