Spaces:
Running
Running
Fix Draco compression: dedup transform + clean NodeIO writer
Browse files- Import dedup from @gltf-transform/functions
- Apply dedup() BEFORE weld() to materialize Draco data
- Use clean NodeIO (without Draco encoder) for writing
- Add accessor buffer assignment for bufferView fix
- This prevents Draco re-encoding and fixes accessor=-1 issue
- services/processor/index.ts +41 -5
services/processor/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import path from 'path';
|
|
| 3 |
import fs from 'fs';
|
| 4 |
import { NodeIO } from '@gltf-transform/core';
|
| 5 |
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
|
|
|
|
| 6 |
import draco3d from 'draco3dgltf';
|
| 7 |
import sharp from 'sharp';
|
| 8 |
|
|
@@ -79,17 +80,48 @@ export async function convertGlbToSkp(inputPath: string, outputPath: string, opt
|
|
| 79 |
console.log(`[DEBUG] Draco extension detected: ${dracoFound}. Converted ${sparseConverted} sparse accessors to dense.`);
|
| 80 |
|
| 81 |
// 2. Remove problematic extensions (Draco, WebP)
|
|
|
|
| 82 |
const extensions = root.listExtensions();
|
|
|
|
|
|
|
| 83 |
for (const ext of extensions) {
|
| 84 |
-
const extName = ext.getName()
|
| 85 |
// Remove Draco, WebP, and other non-standard extensions
|
| 86 |
-
if (extName.
|
| 87 |
-
extName.includes('webp') ||
|
|
|
|
|
|
|
| 88 |
console.log(` Disposing extension: ${extName}`);
|
| 89 |
ext.dispose();
|
| 90 |
}
|
| 91 |
}
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
// Optional: Remove Skins/Animations
|
| 94 |
for (const skin of root.listSkins()) skin.dispose();
|
| 95 |
for (const anim of root.listAnimations()) anim.dispose();
|
|
@@ -152,8 +184,12 @@ export async function convertGlbToSkp(inputPath: string, outputPath: string, opt
|
|
| 152 |
console.log(' - No textures to process');
|
| 153 |
}
|
| 154 |
|
| 155 |
-
// Write processed GLB
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
const stats = fs.statSync(processedGlbPath);
|
| 159 |
console.log(`[DEBUG] Processed GLB saved to ${processedGlbPath} (Size: ${stats.size} bytes)`);
|
|
|
|
| 3 |
import fs from 'fs';
|
| 4 |
import { NodeIO } from '@gltf-transform/core';
|
| 5 |
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
|
| 6 |
+
import { weld, dedup } from '@gltf-transform/functions';
|
| 7 |
import draco3d from 'draco3dgltf';
|
| 8 |
import sharp from 'sharp';
|
| 9 |
|
|
|
|
| 80 |
console.log(`[DEBUG] Draco extension detected: ${dracoFound}. Converted ${sparseConverted} sparse accessors to dense.`);
|
| 81 |
|
| 82 |
// 2. Remove problematic extensions (Draco, WebP)
|
| 83 |
+
// CRITICAL: Must remove these BEFORE writing or they'll be re-encoded
|
| 84 |
const extensions = root.listExtensions();
|
| 85 |
+
console.log(`[DEBUG] Extensions found: ${extensions.map(e => e.extensionName).join(', ') || 'none'}`);
|
| 86 |
+
|
| 87 |
for (const ext of extensions) {
|
| 88 |
+
const extName = ext.extensionName; // Use extensionName, not getName()
|
| 89 |
// Remove Draco, WebP, and other non-standard extensions
|
| 90 |
+
if (extName.toLowerCase().includes('draco') ||
|
| 91 |
+
extName.includes('webp') ||
|
| 92 |
+
extName === 'EXT_texture_webp' ||
|
| 93 |
+
extName === 'KHR_draco_mesh_compression') {
|
| 94 |
console.log(` Disposing extension: ${extName}`);
|
| 95 |
ext.dispose();
|
| 96 |
}
|
| 97 |
}
|
| 98 |
|
| 99 |
+
// ======= CRITICAL FIX FOR DRACO: Force accessor buffer assignment =======
|
| 100 |
+
// When Draco is decoded, accessors may still have bufferView = -1
|
| 101 |
+
// We MUST assign each accessor to a valid buffer so gltf-transform writes proper
|
| 102 |
+
// bufferView references when serializing. This is the KEY fix for accessor=-1.
|
| 103 |
+
const buffers = root.listBuffers();
|
| 104 |
+
let buffer = buffers.length > 0 ? buffers[0] : document.createBuffer();
|
| 105 |
+
let accessorsFixed = 0;
|
| 106 |
+
|
| 107 |
+
for (const acc of root.listAccessors()) {
|
| 108 |
+
// Force the accessor to use our buffer - this makes gltf-transform
|
| 109 |
+
// generate proper bufferView references when writing
|
| 110 |
+
acc.setBuffer(buffer);
|
| 111 |
+
accessorsFixed++;
|
| 112 |
+
}
|
| 113 |
+
console.log(`[DEBUG] Assigned ${accessorsFixed} accessors to buffer`);
|
| 114 |
+
|
| 115 |
+
// 4. Apply dedup() FIRST - this forces accessor data to be materialized into buffers
|
| 116 |
+
// This is CRITICAL: dedup() breaks the Draco "lazy" reference and creates real buffer data
|
| 117 |
+
await document.transform(dedup());
|
| 118 |
+
console.log(`[DEBUG] Applied dedup() transform`);
|
| 119 |
+
|
| 120 |
+
// 5. Apply weld() transform - consolidates vertices and generates proper index buffer
|
| 121 |
+
// This is important for Draco-decoded data to work properly in SketchUp
|
| 122 |
+
await document.transform(weld());
|
| 123 |
+
console.log(`[DEBUG] Applied weld() transform`);
|
| 124 |
+
|
| 125 |
// Optional: Remove Skins/Animations
|
| 126 |
for (const skin of root.listSkins()) skin.dispose();
|
| 127 |
for (const anim of root.listAnimations()) anim.dispose();
|
|
|
|
| 184 |
console.log(' - No textures to process');
|
| 185 |
}
|
| 186 |
|
| 187 |
+
// Write processed GLB using a CLEAN NodeIO (without Draco encoder)
|
| 188 |
+
// CRITICAL: If we use the same 'io' that has ALL_EXTENSIONS registered,
|
| 189 |
+
// gltf-transform will RE-ENCODE the mesh with Draco during write!
|
| 190 |
+
const cleanIO = new NodeIO(); // No extensions registered = no Draco re-encoding
|
| 191 |
+
await cleanIO.write(processedGlbPath, document);
|
| 192 |
+
console.log(`[DEBUG] Written with CLEAN NodeIO (no Draco encoder)`);
|
| 193 |
|
| 194 |
const stats = fs.statSync(processedGlbPath);
|
| 195 |
console.log(`[DEBUG] Processed GLB saved to ${processedGlbPath} (Size: ${stats.size} bytes)`);
|