OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.
TypeScript
import { describe, expect, it } from "vitest";
import {
MotionShaderRenderer,
parseShaderColor,
resolveShaderParamUpload,
} from "./motion-shader-renderer";
import type { MotionShaderCanvas } from "./motion-shader-renderer";
import type { MotionShaderDef } from "./shaders";
const GL_FLOAT = 5126;
const GL_FLOAT_VEC4 = 35666;
const webgl2 = MotionShaderRenderer.isSupported();
function readCenterPixel(canvas: MotionShaderCanvas): readonly [number, number, number, number] {
const copy = new OffscreenCanvas(canvas.width, canvas.height);
const ctx = copy.getContext("2d");
if (!ctx) throw new Error("no 2d context");
ctx.drawImage(canvas, 0, 0);
const cx = Math.floor(canvas.width / 2);
const cy = Math.floor(canvas.height / 2);
const data = ctx.getImageData(cx, cy, 1, 1).data;
return [data[0] ?? 0, data[1] ?? 0, data[2] ?? 0, data[3] ?? 0];
}
const FRAG_HEADER = `#version 300 es
precision highp float;
in vec2 vUv;
out vec4 fragColor;
`;
describe("parseShaderColor", () => {
it("parses #RRGGBB to a normalized vec4 with alpha 1", () => {
expect(parseShaderColor("#ff8000")).toEqual([1, 128 / 255, 0, 1]);
});
it("falls back to opaque black on invalid input", () => {
expect(parseShaderColor("not-a-color")).toEqual([0, 0, 0, 1]);
expect(parseShaderColor("")).toEqual([0, 0, 0, 1]);
expect(parseShaderColor("#12")).toEqual([0, 0, 0, 1]);
});
});
describe("resolveShaderParamUpload", () => {
const COLOR = [0.2, 0.4, 0.6, 1] as const;
it("uploads a vec4 when the active uniform is FLOAT_VEC4, regardless of param type", () => {
const result = resolveShaderParamUpload(
GL_FLOAT_VEC4,
GL_FLOAT_VEC4,
"color",
COLOR,
9,
);
expect(result).toEqual({ kind: "vec4", value: COLOR });
});
it("uploads color luminance as a float when a color param maps to a FLOAT uniform", () => {
const result = resolveShaderParamUpload(
GL_FLOAT,
GL_FLOAT_VEC4,
"color",
COLOR,
9,
);
expect(result.kind).toBe("float");
const expected = 0.2126 * 0.2 + 0.7152 * 0.4 + 0.0722 * 0.6;
if (result.kind !== "float") throw new Error("expected float upload");
expect(result.value).toBeCloseTo(expected, 6);
});
it("uploads the scalar for a numeric param on a FLOAT uniform", () => {
const result = resolveShaderParamUpload(
GL_FLOAT,
GL_FLOAT_VEC4,
"number",
COLOR,
0.75,
);
expect(result).toEqual({ kind: "float", value: 0.75 });
});
});
describe.skipIf(!webgl2)("MotionShaderRenderer type-driven uniform upload", () => {
it("uploads a float param via uniform1f", () => {
const def: MotionShaderDef = {
id: "test-float-param",
name: "Float",
category: "fill",
glsl: `${FRAG_HEADER}\nuniform float u_level;\nvoid main(){ fragColor = vec4(u_level, u_level, u_level, 1.0); }`,
params: [{ name: "level", label: "Level", type: "number", default: 0, min: 0, max: 1, step: 0.01 }],
};
const r = new MotionShaderRenderer();
const out = r.render(def, { width: 4, height: 4, time: 0, params: { level: 0.5 } });
expect(out).not.toBeNull();
const [red] = readCenterPixel(out!);
expect(Math.abs(red - 128)).toBeLessThanOrEqual(2);
r.dispose();
});
it("uploads a color param as vec4 (fixes color-as-float bug)", () => {
const def: MotionShaderDef = {
id: "test-color-param",
name: "Color",
category: "fill",
glsl: `${FRAG_HEADER}\nuniform vec4 u_tint;\nvoid main(){ fragColor = u_tint; }`,
params: [{ name: "tint", label: "Tint", type: "color", default: 0, min: 0, max: 1, step: 1 }],
};
const r = new MotionShaderRenderer();
const out = r.render(def, { width: 4, height: 4, time: 0, params: { tint: "#ff8000" } });
expect(out).not.toBeNull();
const [red, green, blue] = readCenterPixel(out!);
expect(Math.abs(red - 255)).toBeLessThanOrEqual(2);
expect(Math.abs(green - 128)).toBeLessThanOrEqual(2);
expect(Math.abs(blue - 0)).toBeLessThanOrEqual(2);
r.dispose();
});
it("lets a matching param override a staticUniform of the same name", () => {
const def: MotionShaderDef = {
id: "test-static-override",
name: "Override",
category: "fill",
glsl: `${FRAG_HEADER}\nuniform float u_level;\nvoid main(){ fragColor = vec4(u_level, 0.0, 0.0, 1.0); }`,
params: [{ name: "level", label: "Level", type: "number", default: 0, min: 0, max: 1, step: 0.01 }],
staticUniforms: { u_level: 0 },
};
const r = new MotionShaderRenderer();
const out = r.render(def, { width: 4, height: 4, time: 0, params: { level: 1 } });
expect(out).not.toBeNull();
const [red] = readCenterPixel(out!);
expect(Math.abs(red - 255)).toBeLessThanOrEqual(2);
r.dispose();
});
it("packs colorArrayParams and reads u_colors[1] back", () => {
const def: MotionShaderDef = {
id: "test-color-array",
name: "ColorArray",
category: "fill",
glsl: `${FRAG_HEADER}\nuniform vec4 u_colors[4];\nuniform int u_colorsCount;\nvoid main(){ fragColor = u_colors[1]; }`,
params: [
{ name: "color1", label: "C1", type: "color", default: 0, min: 0, max: 1, step: 1 },
{ name: "color2", label: "C2", type: "color", default: 0, min: 0, max: 1, step: 1 },
],
colorArrayParams: { uniform: "u_colors", countUniform: "u_colorsCount", max: 4 },
};
const r = new MotionShaderRenderer();
const out = r.render(def, {
width: 4,
height: 4,
time: 0,
params: { color1: "#000000", color2: "#00ff00" },
});
expect(out).not.toBeNull();
const [red, green, blue] = readCenterPixel(out!);
expect(Math.abs(red - 0)).toBeLessThanOrEqual(2);
expect(Math.abs(green - 255)).toBeLessThanOrEqual(2);
expect(Math.abs(blue - 0)).toBeLessThanOrEqual(2);
r.dispose();
});
it("binds the input texture to a custom inputUniform name", () => {
const source = new OffscreenCanvas(4, 4);
const sctx = source.getContext("2d");
if (!sctx) throw new Error("no 2d ctx");
sctx.fillStyle = "rgb(0,0,255)";
sctx.fillRect(0, 0, 4, 4);
const def: MotionShaderDef = {
id: "test-input-uniform",
name: "InputUniform",
category: "effect",
glsl: `${FRAG_HEADER}\nuniform sampler2D u_image;\nvoid main(){ fragColor = texture(u_image, vUv); }`,
params: [],
inputUniform: "u_image",
};
const r = new MotionShaderRenderer();
const out = r.render(def, { width: 4, height: 4, time: 0, params: {}, inputCanvas: source });
expect(out).not.toBeNull();
const [red, green, blue] = readCenterPixel(out!);
expect(blue).toBeGreaterThan(200);
expect(red).toBeLessThan(60);
expect(green).toBeLessThan(60);
r.dispose();
});
});