Skip to content

Commit d21cdb1

Browse files
committed
Added Packing Scripts
The glbAnalyzer.py is a work in progress Most are, but they all have their purposes
1 parent 033773c commit d21cdb1

16 files changed

+1169
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ lerna-debug.log*
88
.pnpm-debug.log*
99

1010
dev
11-
packing
11+
packing/FBX2glTF.exe
1212
Source/js/pxlRooms/CabinEnvironment
1313
Source/js/pxlRooms/VoidEnvironment
1414

packing/KTX-Software-4.0.0-win64.exe

2 MB
Binary file not shown.

packing/fbx2gltf.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// If you plan on using FBX2GLTF, you need to have the fbx2gltf executable in the packing directory.
2+
// You can download it from here -
3+
// https://github.com/facebookincubator/FBX2glTF
4+
// This module exists, but I haven't used it for this project -
5+
// https://www.npmjs.com/package/fbx2gltf
6+
7+
8+
const { exec } = require('child_process');
9+
const fs = require('fs');
10+
const path = require('path');
11+
const readline = require('readline');
12+
13+
// Function to find all .gltf and .glb files recursively
14+
function findFBXFiles(dir) {
15+
let results = [];
16+
const files = fs.readdirSync(dir);
17+
18+
for (const file of files) {
19+
const filePath = path.join(dir, file);
20+
const stat = fs.statSync(filePath);
21+
22+
if (stat.isDirectory()) {
23+
results = results.concat(findFBXFiles(filePath));
24+
} else {
25+
if (file.endsWith('.fbx')) {
26+
results.push(filePath);
27+
}
28+
}
29+
}
30+
return results;
31+
}
32+
33+
// Start scanning from the assets directory
34+
const baseDir = path.join(__dirname, '..', 'docs', 'js', 'pxlRooms');
35+
36+
try {
37+
const files = findFBXFiles(baseDir);
38+
39+
if (files.length === 0) {
40+
console.log('No .gltf or .glb files found.');
41+
return;
42+
}
43+
44+
const rl = readline.createInterface({
45+
input: process.stdin,
46+
output: process.stdout
47+
});
48+
49+
// Process files sequentially
50+
let currentFileIndex = 0;
51+
52+
function processNextFile() {
53+
if (currentFileIndex >= files.length) {
54+
rl.close();
55+
return;
56+
}
57+
58+
const file = files[currentFileIndex];
59+
60+
const shortName = "." + file.replace(__dirname, '');
61+
62+
rl.question(`Do you want to process the file ${shortName}? (yes/no) `, (answer) => {
63+
if( ["yes","y"].includes(answer.toLowerCase()) ){
64+
console.log(`Processing file ${shortName}...`);
65+
//const env = Object.assign({}, process.env, { TOKTX_PATH: '.\\packing\\toktk.exe', shell: true });
66+
const env = { shell: true };
67+
const fileOutput = file.replace('.fbx', '.glb');
68+
//const command = `.\\packing\\fbx2gltf.exe --binary --draco --verbose --input ${file} --output ${fileOutput}`;
69+
const command = `.\\packing\\fbx2gltf.exe --binary --verbose --input ${file} --output ${fileOutput}`;
70+
exec(command, env, (err, stdout, stderr) => {
71+
if( err ){
72+
console.error(`Error processing file ${shortName}:`, err);
73+
}else if( stderr ){
74+
console.error(`Error output for file ${shortName}:`, stderr);
75+
}else{
76+
console.log(`Successfully processed file ${shortName}:`, stdout);
77+
}
78+
79+
currentFileIndex++;
80+
processNextFile();
81+
});
82+
}else{
83+
console.log(`Skipping file ${shortName}`);
84+
currentFileIndex++;
85+
processNextFile();
86+
}
87+
});
88+
}
89+
90+
processNextFile();
91+
92+
} catch (err) {
93+
console.error('Error scanning directory:', err);
94+
}

packing/glbAnalyzer.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import argparse
2+
import json
3+
import struct
4+
5+
def parse_glb(file_path):
6+
#if not file_path.startswith("..\\"):
7+
# file_path = "..\\" + file_path
8+
9+
ret = {}
10+
11+
with open(file_path, 'rb') as f:
12+
magic = f.read(4)
13+
if magic != b'glTF':
14+
raise ValueError("File is not a valid GLB file")
15+
16+
version, length = struct.unpack('<II', f.read(8))
17+
if version != 2:
18+
raise ValueError("Only GLB version 2 is supported")
19+
20+
content_length, content_format = struct.unpack('<II', f.read(8))
21+
if content_format != 0x4E4F534A: # JSON format
22+
raise ValueError("GLB content is not in JSON format")
23+
24+
content = json.loads(f.read(content_length))
25+
print("Keys in GLB content:", content.keys())
26+
27+
# Check for Draco and KTX compression
28+
for buffer_view in content.get('bufferViews', []):
29+
if 'extensions' in buffer_view:
30+
if 'KHR_draco_mesh_compression' in buffer_view['extensions']:
31+
print("Draco compression found")
32+
if 'KHR_texture_basisu' in buffer_view['extensions']:
33+
print("KTX compression found")
34+
35+
ret = content
36+
37+
return ret
38+
39+
def write_glb(file_path, content):
40+
with open(file_path, 'wb') as f:
41+
f.write(b'glTF')
42+
f.write(struct.pack('<II', 2, 0)) # version 2, placeholder for length
43+
44+
content_bytes = json.dumps(content).encode('utf-8')
45+
content_length = len(content_bytes)
46+
f.write(struct.pack('<II', content_length, 0x4E4F534A)) # JSON format
47+
f.write(content_bytes)
48+
49+
# Update the total length of the GLB file
50+
total_length = f.tell()
51+
f.seek(8)
52+
f.write(struct.pack('<I', total_length))
53+
54+
if __name__ == "__main__":
55+
parser = argparse.ArgumentParser(description='Parse a GLB file and print its keys.')
56+
parser.add_argument('-i', '--input', required=True, help='Path to the input GLB file')
57+
parser.add_argument('-o', '--output', required=True, help='Path to the output GLB file')
58+
args = parser.parse_args()
59+
60+
glbContent = parse_glb(args.input)
61+
print("GLB content:", glbContent)

packing/gltfpack.exe

507 KB
Binary file not shown.

packing/gltfpack.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const { exec } = require('child_process');
2+
const fs = require('fs');
3+
const path = require('path');
4+
const readline = require('readline');
5+
6+
// Function to find all .gltf and .glb files recursively
7+
function findGLTFFiles(dir) {
8+
let results = [];
9+
const files = fs.readdirSync(dir);
10+
11+
for (const file of files) {
12+
const filePath = path.join(dir, file);
13+
const stat = fs.statSync(filePath);
14+
15+
if (stat.isDirectory()) {
16+
results = results.concat(findGLTFFiles(filePath));
17+
} else {
18+
if (file.endsWith('.gltf') || file.endsWith('.glb')) {
19+
results.push(filePath);
20+
}
21+
}
22+
}
23+
return results;
24+
}
25+
26+
// Start scanning from the assets directory
27+
const baseDir = path.join(__dirname, '..', 'docs', 'js', 'pxlRooms');
28+
29+
try {
30+
const files = findGLTFFiles(baseDir);
31+
32+
if (files.length === 0) {
33+
console.log('No .gltf or .glb files found.');
34+
return;
35+
}
36+
37+
const rl = readline.createInterface({
38+
input: process.stdin,
39+
output: process.stdout
40+
});
41+
42+
// Process files sequentially
43+
let currentFileIndex = 0;
44+
45+
function processNextFile() {
46+
if (currentFileIndex >= files.length) {
47+
rl.close();
48+
return;
49+
}
50+
51+
const file = files[currentFileIndex];
52+
53+
const shortName = "." + file.replace(__dirname, '');
54+
55+
rl.question(`Do you want to process the file ${shortName}? (yes/no) `, (answer) => {
56+
if( ["yes","y"].includes(answer.toLowerCase()) ){
57+
console.log(`Processing file ${shortName}...`);
58+
//const env = Object.assign({}, process.env, { TOKTX_PATH: '.\\packing\\toktk.exe', shell: true });
59+
const env = { shell: true };
60+
const command = `.\\packing\\gltfpack -i "${file}" -o "${file}" -cc -kn -km -ke -c -tc -tp -ts 1.0 -v`;
61+
exec(command, env, (err, stdout, stderr) => {
62+
if( err ){
63+
console.error(`Error processing file ${shortName}:`, err);
64+
}else if( stderr ){
65+
console.error(`Error output for file ${shortName}:`, stderr);
66+
}else{
67+
console.log(`Successfully processed file ${shortName}:`, stdout);
68+
}
69+
70+
currentFileIndex++;
71+
processNextFile();
72+
});
73+
}else{
74+
console.log(`Skipping file ${shortName}`);
75+
currentFileIndex++;
76+
processNextFile();
77+
}
78+
});
79+
}
80+
81+
processNextFile();
82+
83+
} catch (err) {
84+
console.error('Error scanning directory:', err);
85+
}

packing/ktx.dll

1.74 MB
Binary file not shown.

packing/ktx2check.exe

117 KB
Binary file not shown.

packing/ktx2ktx2.exe

40.5 KB
Binary file not shown.

packing/ktxinfo.exe

26.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)