@needle-tools/engine
Version:
Needle Engine is a web-based runtime for 3D apps. It runs on your machine for development with great integrations into editors like Unity or Blender - and can be deployed onto any device! It is flexible, extensible and networking and XR are built-in.
66 lines (50 loc) • 2.03 kB
JavaScript
async function generatePoster() {
try {
const { screenshot2, onStart } = await import("@needle-tools/engine");
// Keep in sync with og:image:width meta tags
// https://developers.facebook.com/docs/sharing/best-practices/
const width = 1080;
const height = 1080;
return new Promise(res => {
/** @ts-ignore */
onStart(async (context) => {
if (context.lodsManager.manager) {
await context.lodsManager.manager.awaitLoading({ frames: 5, maxPromisesPerObject: 2, waitForFirstCapture: true });
}
onStart(ctx => {
// Don't take poster screenshot if we're in XR mode
if (ctx.isInXR) {
res(null);
return;
}
const mimeType = "image/webp";
// We're reading back as a blob here because that's async, and doesn't seem
// to stress the GPU so much on memory-constrained devices.
screenshot2({ context, width, height, mimeType, type: "blob" })
// @ts-ignore
.then(blob => {
const reader = new FileReader();
reader.onload = function () {
res(reader.result);
};
reader.onloadend = function () {
res(null);
};
reader.readAsDataURL(blob);
})
}, { once: true });
}, { once: true });
})
}
catch (e) {
console.error(e);
return null;
}
}
if (import.meta.hot) {
async function run() {
const blob = await generatePoster();
import.meta.hot.send("needle:screenshot", { data: blob });
}
run();
}