Skip to content

Code-first video & image, rendered natively on WebGPU.

Fix child boxes squeezed inside 3D-rotated containers

okanasl7e4e63caadda
diff --git a/nodes/node-compositor/src/renderers/webgpu-renderer.ts b/nodes/node-compositor/src/renderers/webgpu-renderer.ts
index 3fb7d78a..132d3d1d 100644
--- a/nodes/node-compositor/src/renderers/webgpu-renderer.ts
+++ b/nodes/node-compositor/src/renderers/webgpu-renderer.ts
@@ -2425,8 +2425,8 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
// and composite/blend the group texture onto destTex.
const groupExcludes = [activeTex, targetTexture, ...excludes];
const groupTex = ctx.renderer.getTemporaryTexture(
- nativeWidth,
- nativeHeight,
+ targetW,
+ targetH,
groupExcludes,
);
const groupView = groupTex.createView();
@@ -2435,8 +2435,8 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
encoder,
groupView,
{ r: 0, g: 0, b: 0, a: 0 },
- nativeWidth,
- nativeHeight,
+ targetW,
+ targetH,
"clear",
);
groupClearPass.end();
@@ -2453,8 +2453,8 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
encoder,
groupView,
{ r: 0, g: 0, b: 0, a: 0 },
- nativeWidth,
- nativeHeight,
+ targetW,
+ targetH,
"load",
);
ctx.renderer.pushTransform(layerMatrix);
@@ -2498,17 +2498,16 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
// overflow: hidden with a border radius: keep only what falls
// inside the rounded box (background and children alike).
if (clipRadius > 0) {
- const maskTex = ctx.renderer.getTemporaryTexture(
- nativeWidth,
- nativeHeight,
- [activeGroupTex, ...groupExcludes],
- );
+ const maskTex = ctx.renderer.getTemporaryTexture(targetW, targetH, [
+ activeGroupTex,
+ ...groupExcludes,
+ ]);
const maskPass = ctx.renderer.beginFrame(
encoder,
maskTex.createView(),
{ r: 0, g: 0, b: 0, a: 0 },
- nativeWidth,
- nativeHeight,
+ targetW,
+ targetH,
"clear",
);
ctx.renderer.pushTransform(layerMatrix);
@@ -2550,8 +2549,8 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
encoder,
activeGroupView,
{ r: 0, g: 0, b: 0, a: 0 },
- nativeWidth,
- nativeHeight,
+ targetW,
+ targetH,
"load",
);
ctx.renderer.pushTransform(layerMatrix);
@@ -2670,11 +2669,12 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
const lightsBuffer =
r3dInstance.getOrCreateLightsBuffer(activeLights);
- const relitTex = ctx.renderer.getTemporaryTexture(
- nativeWidth,
- nativeHeight,
- [activeGroupTex, activeTex, targetTexture, ...excludes],
- );
+ const relitTex = ctx.renderer.getTemporaryTexture(targetW, targetH, [
+ activeGroupTex,
+ activeTex,
+ targetTexture,
+ ...excludes,
+ ]);
const relitView = relitTex.createView();
const normalTex = relightOpts.normalTexture ?? relightOpts.normalMap;
@@ -2686,8 +2686,8 @@ export const CompositorWebGPURenderer: WebGPUNodeRenderer = async (args) => {
normalTex,
lightsBuffer,
{
- viewportWidth: nativeWidth,
- viewportHeight: nativeHeight,
+ viewportWidth: targetW,
+ viewportHeight: targetH,
layerX: 0,
layerY: 0,
roughness: resolveVal(
diff --git a/packages/gitframes/src/rotate-3d-children.test.ts b/packages/gitframes/src/rotate-3d-children.test.ts
new file mode 100644
index 00000000..7ff68b96
--- /dev/null
+++ b/packages/gitframes/src/rotate-3d-children.test.ts
@@ -0,0 +1,84 @@
+/**
+ * A container animated with rotateX/rotateY tracks that settle at 0 must lay
+ * out and composite its child boxes exactly as it would without the tracks.
+ */
+import { Canvas, loadImage } from "skia-canvas";
+import { describe, expect, it } from "vitest";
+import { Composition, Layer, LayerAnimation } from "./index.js";
+
+async function pixels(png: Buffer) {
+ const img = await loadImage(png);
+ const canvas = new Canvas(img.width, img.height);
+ const ctx = canvas.getContext("2d");
+ ctx.drawImage(img, 0, 0);
+ return (x: number, y: number) =>
+ Array.from(ctx.getImageData(x, y, 1, 1).data.slice(0, 3));
+}
+
+function scene(tilt: boolean) {
+ const comp = new Composition({
+ width: 1920,
+ height: 1080,
+ fps: 30,
+ durationFrames: 90,
+ backgroundColor: "#000000",
+ });
+ const box = Layer.box({
+ id: "card",
+ position: "absolute",
+ x: 120,
+ y: 300,
+ width: 1680,
+ height: 690,
+ perspective: 1400,
+ background: "#202020",
+ children: [
+ Layer.box({
+ id: "plot",
+ position: "absolute",
+ x: 100,
+ y: 100,
+ width: 1480,
+ height: 490,
+ background: "#ffffff",
+ }),
+ ],
+ });
+ comp.add(
+ tilt
+ ? box.animate(
+ LayerAnimation.create()
+ .fromTo("rotateX", 28, 0, { start: 0, end: 56 })
+ .fromTo("rotateY", -18, 0, { start: 0, end: 56 }),
+ )
+ : box,
+ );
+ return comp;
+}
+
+describe("3D-rotated container children", () => {
+ it("places child boxes identically once rotate tracks settle at 0", async () => {
+ const flat = await pixels(await scene(false).renderFrame({ frame: 70 }));
+ const tilted = await pixels(await scene(true).renderFrame({ frame: 70 }));
+ // Plot spans x 220..1700, y 400..890 in composition space.
+ for (const [x, y] of [
+ [225, 405],
+ [1695, 405],
+ [225, 885],
+ [1695, 885],
+ [960, 640],
+ ]) {
+ expect(flat(x, y)).toEqual([255, 255, 255]);
+ expect(tilted(x, y)).toEqual([255, 255, 255]);
+ }
+ for (const [x, y] of [
+ [215, 645],
+ [1705, 645],
+ [960, 395],
+ [960, 895],
+ ]) {
+ expect(flat(x, y)).toEqual([32, 32, 32]);
+ expect(tilted(x, y)).toEqual([32, 32, 32]);
+ }
+ });
+});