-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathbuild_node_binary.sh
More file actions
544 lines (457 loc) · 16.3 KB
/
Copy pathbuild_node_binary.sh
File metadata and controls
544 lines (457 loc) · 16.3 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#!/bin/bash
# Build Node.js binary for embedding in Python executable
set -e
echo "🔧 Building Node.js Binary for VoxBridge"
echo "========================================"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
print_status() {
echo -e "${GREEN}[✅] $1${NC}"
}
print_warning() {
echo -e "${YELLOW}[⚠️] $1${NC}"
}
print_error() {
echo -e "${RED}[❌] $1${NC}"
}
print_info() {
echo -e "${BLUE}[ℹ️] $1${NC}"
}
# Check if Node.js is available
if ! command -v node &> /dev/null; then
print_error "Node.js is not installed. Please install Node.js first."
exit 1
fi
NODE_VERSION=$(node --version)
print_status "Node.js version: $NODE_VERSION"
# Check if npm is available
if ! command -v npm &> /dev/null; then
print_error "npm is not installed. Please install npm first."
exit 1
fi
# Create build directory
mkdir -p build/node_binary
# Install Node.js dependencies
print_status "Installing Node.js dependencies..."
cd node_scripts
npm install
cd ..
# Copy node_modules to build directory
print_status "Copying Node.js modules..."
cp -r node_scripts/node_modules build/node_binary/
# Create a bundled Node.js script
print_status "Creating bundled Node.js script..."
# Copy the simple_processor.js instead of process_complex.js to preserve animations
cp node_scripts/simple_processor.js build/node_binary/voxbridge_node.js
chmod +x build/node_binary/voxbridge_node.js
# Comment out the old heredoc stub creation
: <<'EOF_SKIP'
cat > build/node_binary/voxbridge_node.js << 'EOF'
#!/usr/bin/env node
/**
* VoxBridge Node.js Binary
* Bundled version of process_complex.js for embedding in Python executable
*/
const { program } = require("commander");
const fs = require("fs-extra");
const path = require("path");
const { execSync } = require("child_process");
// Import gltf-transform modules
const { NodeIO } = require("@gltf-transform/core");
const {
quantize,
textureResize,
textureCompress,
simplify,
} = require("@gltf-transform/functions");
const sharp = require("sharp");
// Initialize MeshOptimizer for simplification
try {
const MeshoptDecoder = require("meshoptimizer/meshopt_decoder.js");
const MeshoptEncoder = require("meshoptimizer/meshopt_encoder.js");
require("@gltf-transform/functions").ready = Promise.resolve();
} catch (err) {
console.warn("MeshOptimizer initialization warning:", err.message);
}
// Import Draco with fallback
let draco;
try {
draco = require("@gltf-transform/functions").draco;
if (typeof draco !== "function") {
console.warn("Draco function not available in @gltf-transform/functions");
draco = null;
}
} catch (error) {
console.warn("Failed to import Draco:", error.message);
draco = null;
}
program
.name("voxbridge-node")
.description("VoxBridge Node.js processor for complex GLTF/GLB files")
.requiredOption("-i, --input <file>", "Input GLTF/GLB file")
.requiredOption("-o, --output <dir>", "Output directory")
.requiredOption("-t, --target <platform>", "Target platform (unity|roblox)")
.option("--simplify", "Enable mesh simplification")
.option("--simplify-ratio <ratio>", "Simplification ratio (0.0-1.0)", "0.5")
.option("--quantize", "Enable quantization")
.option("--use-draco", "Enable Draco compression")
.option("--no-draco", "Disable Draco compression")
.option("--texture-size <size>", "Texture size", "1024")
.option("--keep-temp", "Keep intermediate files")
.option("--verbose", "Enable verbose output")
.parse();
const options = program.opts();
async function processComplexGLTF() {
const startTime = Date.now();
const stats = {
input: options.input,
target: options.target,
start: startTime,
success: false,
error: null,
warnings: [],
optimizations: [],
};
try {
console.log(`Processing complex GLTF: ${options.input}`);
console.log(`Target platform: ${options.target}`);
// Ensure output directory exists
await fs.ensureDir(options.output);
// Load the GLTF file
console.log("Loading GLTF file...");
const io = new NodeIO();
let document;
try {
document = await io.read(options.input);
} catch (readError) {
console.error(`GLTF loading failed: ${readError.message}`);
throw readError;
}
// Get initial stats
const initialStats = getDocumentStats(document);
stats.initialStats = initialStats;
console.log(
`Initial stats: ${initialStats.meshes} meshes, ${initialStats.materials} materials, ${initialStats.triangles} triangles`
);
// Apply optimizations based on target platform
try {
if (options.target === "roblox") {
console.log("Applying Roblox optimizations...");
await applyRobloxOptimizations(document, options);
stats.optimizations.push("roblox_optimizations");
} else if (options.target === "unity") {
console.log("Applying Unity optimizations...");
await applyUnityOptimizations(document, options);
stats.optimizations.push("unity_optimizations");
}
} catch (optError) {
console.error(`Optimization error: ${optError.message}`);
stats.warnings.push(`Optimization failed: ${optError.message}`);
}
// Apply general optimizations
try {
// Handle specular-glossiness extension conversion first
await handleSpecularGlossinessExtension(document, stats);
// Apply mesh simplification if requested
if (options.simplify) {
try {
const simplifyRatio = parseFloat(options.simplifyRatio);
console.log(
`Applying mesh simplification with ratio ${simplifyRatio}...`
);
// Get triangle count before simplification
const beforeStats = getDocumentStats(document);
const beforeTriangles = beforeStats.triangles;
console.log(`Before simplification: ${beforeTriangles} triangles`);
// Configure simplify with meshoptimizer encoder
const MeshoptEncoder = require("meshoptimizer");
await MeshoptEncoder.ready;
await document.transform(
simplify({
ratio: simplifyRatio,
encoder: MeshoptEncoder,
})
);
// Get triangle count after simplification
const afterStats = getDocumentStats(document);
const afterTriangles = afterStats.triangles;
console.log(`After simplification: ${afterTriangles} triangles`);
// Calculate reduction statistics
const triangleReduction = beforeTriangles - afterTriangles;
const reductionPercent = beforeTriangles > 0 ?
((triangleReduction / beforeTriangles) * 100).toFixed(1) : 0;
console.log(`Triangle reduction: ${triangleReduction} triangles (${reductionPercent}%)`);
// Store simplification stats
stats.meshSimplification = {
beforeTriangles,
afterTriangles,
triangleReduction,
reductionPercent: parseFloat(reductionPercent),
simplifyRatio
};
stats.optimizations.push(`mesh_simplification_${simplifyRatio}`);
console.log("Mesh simplification applied successfully");
} catch (simplifyError) {
console.error(`Mesh simplification failed: ${simplifyError.message}`);
stats.warnings.push(
`Mesh simplification skipped: ${simplifyError.message}`
);
}
}
if (options.quantize) {
console.log("Applying quantization...");
await document.transform(quantize());
stats.optimizations.push("quantization");
}
if (options.useDraco === true) {
if (draco === null) {
console.warn("Draco compression not available");
stats.warnings.push("Draco compression not available");
} else {
try {
console.log("Applying Draco compression...");
await document.transform(draco());
stats.optimizations.push("draco_compression");
} catch (dracoError) {
console.error(`Draco compression failed: ${dracoError.message}`);
stats.warnings.push(
`Draco compression failed: ${dracoError.message}`
);
console.log("Continuing without Draco compression...");
}
}
} else {
console.log("Draco compression disabled");
}
} catch (genError) {
console.error(`General optimization error: ${genError.message}`);
stats.warnings.push(`General optimization failed: ${genError.message}`);
}
// Resize textures if specified
try {
const textureSize = parseInt(options.textureSize);
if (textureSize < 1024) {
console.log(`Resizing textures to ${textureSize}px...`);
await document.transform(textureResize({ size: textureSize }));
stats.optimizations.push(`texture_resize_${textureSize}`);
}
} catch (textureError) {
console.error(`Texture resize error: ${textureError.message}`);
stats.warnings.push(`Texture resize failed: ${textureError.message}`);
}
// Get final stats
const finalStats = getDocumentStats(document);
stats.finalStats = finalStats;
console.log(
`Final stats: ${finalStats.meshes} meshes, ${finalStats.triangles} triangles`
);
// Determine output format
const inputExt = path.extname(options.input).toLowerCase();
const baseName = path.basename(options.input, inputExt);
const outputName = `${baseName}_${options.target}.gltf`;
const outputPath = path.join(options.output, outputName);
// Write the processed file
await io.write(outputPath, document);
console.log(`Output written to: ${outputPath}`);
// Apply platform-specific post-processing
if (options.target === "roblox") {
await applyRobloxPostProcessing(outputPath);
} else if (options.target === "unity") {
await applyUnityPostProcessing(outputPath);
}
// Always package the output
const packagePath = await packageOutput(
outputPath,
options.output,
options.target,
baseName
);
// Write stats
stats.success = true;
stats.timeSec = (Date.now() - startTime) / 1000;
stats.outputPath = packagePath;
const statsPath = path.join(options.output, "voxbridge_report.json");
await fs.writeJson(statsPath, stats, { spaces: 2 });
console.log(`Processing completed in ${stats.timeSec.toFixed(2)}s`);
console.log(`Stats written to: ${statsPath}`);
// Clean up intermediate files, keep only the final ZIP (unless --keep-temp is set)
if (!options.keepTemp) {
await cleanupIntermediateFiles(options.output);
} else {
console.log("Skipping cleanup due to --keep-temp flag");
}
} catch (error) {
stats.success = false;
stats.error = error.message;
stats.timeSec = (Date.now() - startTime) / 1000;
console.error(`Processing failed: ${error.message}`);
// Write error stats
const statsPath = path.join(options.output, "voxbridge_report.json");
await fs.writeJson(statsPath, stats, { spaces: 2 });
process.exit(1);
}
}
function getDocumentStats(document) {
const root = document.getRoot();
const meshes = root.listMeshes();
const materials = root.listMaterials();
const textures = root.listTextures();
const nodes = root.listNodes();
const animations = root.listAnimations();
const skins = root.listSkins();
// Calculate triangle count
let triangles = 0;
if (meshes && meshes.length > 0) {
for (const mesh of meshes) {
const primitives = mesh.listPrimitives();
if (primitives && primitives.length > 0) {
for (const primitive of primitives) {
const indices = primitive.getIndices();
if (indices) {
triangles += indices.getCount() / 3;
} else {
const position = primitive.getAttribute("POSITION");
if (position) {
triangles += position.getCount() / 3;
}
}
}
}
}
}
return {
meshes: meshes.length,
materials: materials.length,
textures: textures.length,
nodes: nodes.length,
animations: animations.length,
skins: skins.length,
triangles: Math.floor(triangles),
};
}
async function applyRobloxOptimizations(document, options) {
console.log("Applying Roblox optimizations...");
// Add Roblox-specific optimizations here
}
async function applyUnityOptimizations(document, options) {
console.log("Applying Unity optimizations...");
// Add Unity-specific optimizations here
}
async function handleSpecularGlossinessExtension(document, stats) {
console.log("Handling specular-glossiness extensions...");
stats.optimizations.push("specular_glossiness_to_metallic_roughness");
}
async function applyRobloxPostProcessing(gltfPath) {
console.log("Applying Roblox post-processing...");
}
async function applyUnityPostProcessing(gltfPath) {
console.log("Applying Unity post-processing...");
}
async function packageOutput(gltfPath, outputDir, target, baseName) {
console.log("Packaging output...");
try {
const zip = require("adm-zip");
const zipPath = path.join(outputDir, `${baseName}_${target}.zip`);
const zipFile = new zip();
// Add GLTF file and related files
zipFile.addLocalFile(gltfPath);
// Add any related files (textures, etc.)
const gltfDir = path.dirname(gltfPath);
const files = await fs.readdir(gltfDir);
for (const file of files) {
const filePath = path.join(gltfDir, file);
const stat = await fs.stat(filePath);
if (stat.isFile() && file !== path.basename(gltfPath)) {
zipFile.addLocalFile(filePath);
}
}
await zipFile.writeZip(zipPath);
console.log(`Package created: ${zipPath}`);
return zipPath;
} catch (error) {
console.error(`Packaging failed: ${error.message}`);
throw error;
}
}
async function cleanupIntermediateFiles(outputDir) {
try {
console.log("Cleaning up intermediate files...");
const files = await fs.readdir(outputDir);
let cleanedCount = 0;
for (const file of files) {
const filePath = path.join(outputDir, file);
const stat = await fs.stat(filePath);
// Keep only ZIP files and JSON reports
if (!file.endsWith(".zip") && !file.endsWith(".json")) {
if (stat.isDirectory()) {
await fs.remove(filePath);
console.log(`Removed directory: ${file}`);
} else {
await fs.remove(filePath);
console.log(`Removed file: ${file}`);
}
cleanedCount++;
}
}
if (cleanedCount > 0) {
console.log(`Cleaned up ${cleanedCount} intermediate files`);
} else {
console.log("No intermediate files to clean up");
}
} catch (error) {
console.warn(`Cleanup failed: ${error.message}`);
}
}
// Run the processing
if (require.main === module) {
processComplexGLTF().catch(console.error);
}
EOF
EOF_SKIP
# The heredoc stub is now skipped, using actual process_complex.js copied above
# Create a simple test script
print_status "Creating test script..."
cat > build/node_binary/test_node.js << 'EOF'
#!/usr/bin/env node
console.log("VoxBridge Node.js Binary Test");
console.log("=============================");
console.log("Node.js version:", process.version);
console.log("Platform:", process.platform);
console.log("Architecture:", process.arch);
// Test basic functionality
try {
const fs = require('fs');
const path = require('path');
console.log("✅ File system access: OK");
const { NodeIO } = require("@gltf-transform/core");
console.log("✅ GLTF Transform: OK");
console.log("✅ All tests passed!");
} catch (error) {
console.error("❌ Test failed:", error.message);
process.exit(1);
}
EOF
chmod +x build/node_binary/test_node.js
# Test the binary
print_status "Testing Node.js binary..."
cd build/node_binary
node test_node.js
cd ../..
print_status "Node.js binary built successfully!"
print_info "Binary location: build/node_binary/voxbridge_node.js"
print_info "Test script: build/node_binary/test_node.js"
echo ""
print_info "The Node.js binary includes:"
echo " ✅ Complete GLTF processing pipeline"
echo " ✅ Mesh simplification with triangle counting"
echo " ✅ Spec-gloss conversion"
echo " ✅ Texture optimization"
echo " ✅ Platform-specific optimizations"
echo " ✅ ZIP packaging"
echo " ✅ Cleanup functionality"
echo ""
print_status "Ready for embedding in Python executable! 🚀"