feat: add GLTF transformation and compression support
- Updated package.json to include @gltf-transform/core, @gltf-transform/extensions, @gltf-transform/functions, and draco3dgltf for GLB file processing. - Implemented GLB compression using Draco in the upload model API route. - Adjusted camera keyframes in ScrollScene for improved animation. - Enhanced overlay styles in ScrollOverlays for better visual clarity. - Added original GLB files for Kandoura, Suit, Unitree_G1, and Vest. - Updated existing GLB files for Kandoura, Suit, Unitree_G1, and Vest.
This commit is contained in:
@@ -1,8 +1,22 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { cookies } from 'next/headers';
|
||||
import { jwtVerify } from 'jose';
|
||||
import { writeFile, mkdir } from 'fs/promises';
|
||||
import { writeFile, mkdir, readFile } from 'fs/promises';
|
||||
import path from 'path';
|
||||
import { Document, NodeIO } from '@gltf-transform/core';
|
||||
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
|
||||
import { draco, dedup, prune, weld, simplify } from '@gltf-transform/functions';
|
||||
import draco3d from 'draco3dgltf';
|
||||
|
||||
async function compressGLB(inputPath: string): Promise<void> {
|
||||
const io = new NodeIO()
|
||||
.registerExtensions(ALL_EXTENSIONS)
|
||||
.registerDependencies({ 'draco3d.decoder': await draco3d.createDecoderModule(), 'draco3d.encoder': await draco3d.createEncoderModule() });
|
||||
|
||||
const document = await io.read(inputPath);
|
||||
await document.transform(dedup(), prune(), weld(), draco());
|
||||
await io.write(inputPath, document);
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
// Verify admin JWT
|
||||
@@ -51,6 +65,13 @@ export async function POST(request: Request) {
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
await writeFile(destPath, buffer);
|
||||
|
||||
// Compress the uploaded GLB with Draco to reduce file size
|
||||
try {
|
||||
await compressGLB(destPath);
|
||||
} catch (err) {
|
||||
console.warn('GLB compression failed, serving uncompressed:', err);
|
||||
}
|
||||
|
||||
// Append a version timestamp so useGLTF cache is busted on replacement uploads
|
||||
const version = Date.now();
|
||||
return NextResponse.json({ modelPath: `/models/${safeId}.glb?v=${version}` });
|
||||
|
||||
Reference in New Issue
Block a user