forked from pmndrs/gltfjsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·97 lines (92 loc) · 3.94 KB
/
cli.js
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
#!/usr/bin/env node
'use strict'
import meow from 'meow'
import path from 'path'
import { fileURLToPath } from 'url'
import { dirname } from 'path'
import gltfjsx from './src/gltfjsx.js'
import { readPackageUpSync } from 'read-pkg-up'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const cli = meow(
`
Usage
$ npx gltfjsx [Model.glb] [options]
Options
--output, -o Output file name/path
--types, -t Add Typescript definitions
--keepnames, -k Keep original names
--keepgroups, -K Keep (empty) groups, disable pruning
--meta, -m Include metadata (as userData)
--shadows, s Let meshes cast and receive shadows
--printwidth, w Prettier printWidth (default: 120)
--precision, -p Number of fractional digits (default: 3)
--draco, -d Draco binary path
--root, -r Sets directory from which .gltf file is served
--instance, -i Instance re-occuring geometry
--instanceall, -I Instance every geometry (for cheaper re-use)
--exportdefault, -E Use default export
--transform, -T Transform the asset for the web (draco, prune, resize)
--resolution, -R Resolution for texture resizing (default: 1024)
--keepmeshes, -j Do not join compatible meshes
--keepmaterials, -M Do not palette join materials
--format, -f Texture format (default: "webp")
--simplify, -S Mesh simplification (default: false)
--weld Weld tolerance (default: 0.00005)
--ratio Simplifier ratio (default: 0)
--error Simplifier error threshold (default: 0.0001)
--debug, -D Debug output
`,
{
importMeta: import.meta,
flags: {
output: { type: 'string', shortFlag: 'o' },
types: { type: 'boolean', shortFlag: 't' },
keepnames: { type: 'boolean', shortFlag: 'k' },
keepgroups: { type: 'boolean', shortFlag: 'K' },
shadows: { type: 'boolean', shortFlag: 's' },
printwidth: { type: 'number', shortFlag: 'p', default: 1000 },
meta: { type: 'boolean', shortFlag: 'm' },
precision: { type: 'number', shortFlag: 'p', default: 3 },
draco: { type: 'string', shortFlag: 'd' },
root: { type: 'string', shortFlag: 'r' },
instance: { type: 'boolean', shortFlag: 'i' },
instanceall: { type: 'boolean', shortFlag: 'I' },
transform: { type: 'boolean', shortFlag: 'T' },
resolution: { type: 'number', shortFlag: 'R', default: 1024 },
degrade: { type: 'string', shortFlag: 'q', default: '' },
degraderesolution: { type: 'number', shortFlag: 'Q', default: 512 },
simplify: { type: 'boolean', shortFlag: 'S', default: false },
keepmeshes: { type: 'boolean', shortFlag: 'j', default: false },
keepmaterials: { type: 'boolean', shortFlag: 'M', default: false },
format: { type: 'string', shortFlag: 'f', default: 'webp' },
exportdefault: { type: 'boolean', shortFlag: 'E' },
weld: { type: 'number', default: 0.0001 },
ratio: { type: 'number', default: 0.75 },
error: { type: 'number', default: 0.001 },
debug: { type: 'boolean', shortFlag: 'D' },
},
}
)
const { packageJson } = readPackageUpSync({ cwd: __dirname, normalize: false })
if (cli.input.length === 0) {
console.log(cli.help)
} else {
const config = {
...cli.flags,
header: `Auto-generated by: https://github.com/pmndrs/gltfjsx
Command: npx gltfjsx@${packageJson.version} ${process.argv.slice(2).join(' ')}`,
}
const file = cli.input[0]
let nameExt = file.match(/[-_\w\d\s]+[.][\w]+$/i)[0]
let name = nameExt.split('.').slice(0, -1).join('.')
const output = config.output ?? name.charAt(0).toUpperCase() + name.slice(1) + (config.types ? '.tsx' : '.jsx')
const showLog = (log) => {
console.info('log:', log)
}
try {
const response = await gltfjsx(file, output, { ...config, showLog, timeout: 0, delay: 1 })
} catch (e) {
console.error(e)
}
}