Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic bring-your-own-encoder rendering #92

Merged
merged 1 commit into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion renin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
"tslib": "^2.4.0",
"typescript": "^4.8.4",
"vite": "^3.1.8"
}
},
"dependencies": {}
}
52 changes: 52 additions & 0 deletions renin/src/render.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Renin } from './renin';
interface RenderOptions {
encoder: {
addFrame: (bitmap: ImageBitmap, frame: number) => Promise<void>;
end: () => Promise<ArrayBuffer>;
};
width: number;
height: number;
}

const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

export async function render(renin: Renin, options: RenderOptions) {
const { width, height, encoder } = options;

renin.jumpToFrame(0);
renin.isFullscreen = true;
renin.resize(width, height);

/* Drive the UI a little bit to make sure all animations have settled :D */
for (let i = 0; i < 300; i++) {
await delay(1);
renin.uiTime = Date.now() / 1000;
renin.uiUpdate();
renin.uiRender();
}
window.innerWidth = width;
window.innerHeight = height;
renin.resize(width, height);
renin.renderer.setPixelRatio(1);

const extraPaddingAtTheEnd = 60;
const numberOfFramesToRender = renin.music.getDuration() * 60 + extraPaddingAtTheEnd;

for (let i = 0; i < numberOfFramesToRender; i++) {
await delay(1);
renin.jumpToFrame(i);
const bitmap = await createImageBitmap(renin.renderer.domElement);
await encoder.addFrame(bitmap, i);
}

const buffer = await encoder.end();
const url = URL.createObjectURL(new Blob([buffer], { type: 'video/mp4' }));

const a = document.createElement('a');
a.style.display = 'none';
document.body.appendChild(a);
a.href = url;
a.download = 'render.mp4';
a.click();
URL.revokeObjectURL(url);
}
1 change: 1 addition & 0 deletions renin/src/renin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { getSetting, setSetting } from './ui/storedSettings';
/* otherwise it won't be added to the build */
export * as vite from './ui/vite';
export * as ReninNode from './ReninNode';
export * as render from './render';

export const defaultVertexShader = defaultVert;

Expand Down