-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathrender-clip.js
More file actions
284 lines (264 loc) · 11.2 KB
/
Copy pathrender-clip.js
File metadata and controls
284 lines (264 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Server-side renderer for the `render_avatar_clip` MCP tool and
// `/api/render/avatar-clip` HTTP endpoint.
//
// Boots headless chromium via puppeteer-core + @sparticuz/chromium-min,
// loads an inlined three.js viewer that:
// 1. Loads a GLB,
// 2. Optionally applies a pose preset's joint Euler rotations,
// 3. Frames the camera by the model's bounding box plus the requested
// `cameraOrbit` (theta, phi, radius) in degrees / meters,
// 4. Renders one PNG.
//
// The same module powers both transparent OG cards and the full clip
// renderer — single source of truth for headless three.js rendering so the
// MCP tool, the OG card, and any future video renderer share lighting +
// framing.
// puppeteer-core + @sparticuz/chromium-min are loaded lazily inside getBrowser()
// so Vercel's NFT doesn't statically trace the chromium tree for every route
// that transitively imports this module — that trace caused 45-min build hangs.
import { env } from './env.js';
import { fetchModel } from './fetch-model.js';
import { PRESETS } from '../../src/pose-presets.js';
// Cap on GLB bytes pulled into the renderer (OOM / render-budget guard).
const DEFAULT_MAX_GLB_BYTES = 25 * 1024 * 1024;
const DEFAULT_CHROMIUM_PACK =
'https://github.com/Sparticuz/chromium/releases/download/v148.0.0/chromium-v148.0.0-pack.x64.tar';
const CHROMIUM_PACK = env.CHROMIUM_PACK_URL || DEFAULT_CHROMIUM_PACK;
const THREE_VERSION = '0.176.0';
let _browserPromise = null;
async function getBrowser() {
if (_browserPromise) return _browserPromise;
_browserPromise = (async () => {
const [{ default: puppeteer }, { default: chromium }] = await Promise.all([
import('puppeteer-core'),
import('@sparticuz/chromium-min'),
]);
const executablePath = await chromium.executablePath(CHROMIUM_PACK);
return puppeteer.launch({
args: chromium.args,
defaultViewport: { width: 1024, height: 1024, deviceScaleFactor: 1 },
executablePath,
headless: chromium.headless,
});
})().catch((err) => {
_browserPromise = null;
throw err;
});
return _browserPromise;
}
function poseById(id) {
if (!id) return null;
const found = PRESETS.find((p) => p.id === id);
return found ? { id: found.id, label: found.label, pose: found.pose } : null;
}
function viewerHtml({ glbBase64, width, height, background, pose, cameraOrbit, expression }) {
const bg = background === 'transparent' ? 'null' : JSON.stringify(background || '#0a0a0a');
const poseJson = pose ? JSON.stringify(pose.pose) : 'null';
const orbitJson = JSON.stringify(cameraOrbit || { theta: 0, phi: 80, radius: null });
const expressionJson = JSON.stringify(expression || null);
return `<!doctype html>
<html><head><meta charset="utf-8" />
<style>html,body{margin:0;padding:0;background:transparent;overflow:hidden}</style>
</head><body>
<canvas id="c" width="${width}" height="${height}" style="display:block;width:${width}px;height:${height}px"></canvas>
<script>window.__GLB_B64=${JSON.stringify(glbBase64)};</script>
<script type="importmap">{ "imports": {
"three": "https://unpkg.com/three@${THREE_VERSION}/build/three.module.js",
"three/addons/": "https://unpkg.com/three@${THREE_VERSION}/examples/jsm/"
}}</script>
<script type="module">
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
import { MeshoptDecoder } from 'three/addons/libs/meshopt_decoder.module.js';
window.__renderDone = false;
window.__renderError = null;
const canvas = document.getElementById('c');
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: true, preserveDrawingBuffer: true });
renderer.setSize(${width}, ${height}, false);
renderer.setPixelRatio(1);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.0;
const scene = new THREE.Scene();
const bgColor = ${bg};
if (bgColor !== null) scene.background = new THREE.Color(bgColor);
const camera = new THREE.PerspectiveCamera(28, ${width}/${height}, 0.01, 100);
scene.add(new THREE.AmbientLight(0xffffff, 0.55));
const key = new THREE.DirectionalLight(0xffffff, 1.4); key.position.set(2, 3, 4); scene.add(key);
const fill = new THREE.DirectionalLight(0xbfd6ff, 0.6); fill.position.set(-3, 1, 2); scene.add(fill);
const rim = new THREE.DirectionalLight(0xc7a8ff, 0.5); rim.position.set(0, 2, -4); scene.add(rim);
const aliases = {
shoulderl: ['leftshoulder','shoulder_l','l_shoulder','mixamorig:leftshoulder'],
shoulderr: ['rightshoulder','shoulder_r','r_shoulder','mixamorig:rightshoulder'],
elbowl: ['leftforearm','leftelbow','elbow_l','mixamorig:leftforearm','mixamorig:leftelbow'],
elbowr: ['rightforearm','rightelbow','elbow_r','mixamorig:rightforearm','mixamorig:rightelbow'],
wristl: ['lefthand','wrist_l','mixamorig:lefthand'],
wristr: ['righthand','wrist_r','mixamorig:righthand'],
hipl: ['leftupleg','hip_l','mixamorig:leftupleg'],
hipr: ['rightupleg','hip_r','mixamorig:rightupleg'],
kneel: ['leftleg','knee_l','mixamorig:leftleg'],
kneer: ['rightleg','knee_r','mixamorig:rightleg'],
anklel: ['leftfoot','ankle_l','mixamorig:leftfoot'],
ankler: ['rightfoot','ankle_r','mixamorig:rightfoot'],
head: ['head','mixamorig:head'],
neck: ['neck','mixamorig:neck'],
spine: ['spine','spine1','mixamorig:spine','mixamorig:spine1'],
hips: ['hips','mixamorig:hips'],
};
function applyPose(root, poseMap) {
if (!poseMap) return;
const byName = new Map();
root.traverse((o) => { if (o.name) byName.set(o.name.toLowerCase(), o); });
function findJoint(k) {
const key = k.toLowerCase();
const direct = byName.get(key);
if (direct) return direct;
const list = aliases[key] || [];
for (const a of list) { const j = byName.get(a); if (j) return j; }
return null;
}
for (const [key, rot] of Object.entries(poseMap)) {
const joint = findJoint(key);
if (!joint) continue;
joint.rotation.set(rot.x || 0, rot.y || 0, rot.z || 0);
}
}
function applyExpression(root, expression) {
if (!expression || typeof expression !== 'object') return;
root.traverse((o) => {
if (!o.isMesh || !o.morphTargetDictionary || !o.morphTargetInfluences) return;
for (const [name, value] of Object.entries(expression)) {
const idx = o.morphTargetDictionary[name] ?? o.morphTargetDictionary[name.toLowerCase()];
if (typeof idx === 'number') o.morphTargetInfluences[idx] = Number(value) || 0;
}
});
}
function frameCamera(root, orbit) {
const box = new THREE.Box3().setFromObject(root);
const size = new THREE.Vector3(); box.getSize(size);
const center = new THREE.Vector3(); box.getCenter(center);
root.position.sub(center);
root.position.y += size.y * 0.05;
const maxDim = Math.max(size.x, size.y, size.z);
const fov = THREE.MathUtils.degToRad(camera.fov);
const defaultDist = (maxDim / 2) / Math.tan(fov / 2) * 1.45;
const radius = (typeof orbit.radius === 'number' && orbit.radius > 0) ? orbit.radius : defaultDist;
const theta = THREE.MathUtils.degToRad(Number(orbit.theta) || 0);
const phi = THREE.MathUtils.degToRad(Number(orbit.phi) || 80);
const x = radius * Math.sin(phi) * Math.sin(theta);
const y = radius * Math.cos(phi);
const z = radius * Math.sin(phi) * Math.cos(theta);
camera.position.set(x, y, z);
camera.lookAt(0, 0, 0);
}
const orbit = ${orbitJson};
const poseMap = ${poseJson};
const expression = ${expressionJson};
// Pipeline GLBs ship Draco geometry, Meshopt buffers, and KTX2 textures;
// a bare GLTFLoader throws "No DRACOLoader instance provided". Register
// every standard compression decoder from the pinned three.js release.
const ADDONS = 'https://unpkg.com/three@${THREE_VERSION}/examples/jsm/';
const dracoLoader = new DRACOLoader().setDecoderPath(ADDONS + 'libs/draco/');
const ktx2Loader = new KTX2Loader().setTranscoderPath(ADDONS + 'libs/basis/').detectSupport(renderer);
const loader = new GLTFLoader();
loader.setDRACOLoader(dracoLoader);
loader.setKTX2Loader(ktx2Loader);
loader.setMeshoptDecoder(MeshoptDecoder);
// GLB bytes are fetched server-side via the SSRF-pinned fetchModel path and
// embedded as base64, so chromium never fetches the user-supplied URL (no
// DNS-rebinding / redirect-to-internal SSRF).
function onLoaded(gltf) {
try {
const root = gltf.scene;
scene.add(root);
applyPose(root, poseMap);
applyExpression(root, expression);
frameCamera(root, orbit);
renderer.render(scene, camera);
requestAnimationFrame(() => {
renderer.render(scene, camera);
window.__renderDone = true;
});
} catch (err) {
window.__renderError = err.message || String(err);
}
}
(async () => {
try {
const buf = await (await fetch('data:application/octet-stream;base64,' + window.__GLB_B64)).arrayBuffer();
loader.parse(buf, '', onLoaded, (err) => {
window.__renderError = 'glb parse failed: ' + (err?.message || err);
});
} catch (err) {
window.__renderError = 'glb decode failed: ' + (err?.message || String(err));
}
})();
</script></body></html>`;
}
/**
* Render a GLB to a PNG buffer with optional pose preset and camera orbit.
*
* @param {object} opts
* @param {string} opts.glbUrl
* @param {number} [opts.width=1024]
* @param {number} [opts.height=1024]
* @param {string} [opts.background='#0a0a0a']
* @param {string} [opts.posePresetId]
* @param {{theta?:number,phi?:number,radius?:number}} [opts.cameraOrbit]
* @param {Object<string,number>} [opts.expression]
* @returns {Promise<{png:Buffer,pose:object|null}>}
*/
export async function renderClip({
glbUrl,
width = 1024,
height = 1024,
background = '#0a0a0a',
posePresetId = null,
cameraOrbit = null,
expression = null,
maxBytes = DEFAULT_MAX_GLB_BYTES,
} = {}) {
if (!glbUrl || typeof glbUrl !== 'string') {
throw Object.assign(new Error('glbUrl required'), { status: 400, code: 'invalid_args' });
}
const W = Math.max(64, Math.min(2048, Number(width) || 1024));
const H = Math.max(64, Math.min(2048, Number(height) || 1024));
const pose = poseById(posePresetId);
// Pull the GLB through the SSRF-pinned fetcher (DNS-pinned per hop, redirects
// re-validated, byte cap enforced) so chromium never fetches the untrusted URL.
let glbBase64;
try {
const { bytes } = await fetchModel(glbUrl, { maxBytes });
glbBase64 = Buffer.from(bytes).toString('base64');
} catch (err) {
throw Object.assign(new Error(`glb fetch failed: ${err?.message || err}`), {
status: err?.code === 'file_too_large' ? 413 : 400,
code: err?.code || 'glb_fetch_failed',
});
}
const browser = await getBrowser();
const page = await browser.newPage();
try {
await page.setViewport({ width: W, height: H, deviceScaleFactor: 1 });
const html = viewerHtml({ glbBase64, width: W, height: H, background, pose, cameraOrbit, expression });
await page.setContent(html, { waitUntil: 'domcontentloaded' });
await page.waitForFunction(
'window.__renderDone === true || window.__renderError !== null',
{ timeout: 20_000 },
);
const err = await page.evaluate(() => window.__renderError);
if (err) {
throw Object.assign(new Error(`render failed: ${err}`), { status: 502, code: 'render_failed' });
}
const png = await page.screenshot({
type: 'png',
omitBackground: background === 'transparent',
clip: { x: 0, y: 0, width: W, height: H },
});
return { png, pose };
} finally {
await page.close().catch(() => {});
}
}