@threlte/gltf 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/cli.js +85 -0
- package/logo.svg +145 -0
- package/package.json +71 -0
- package/readme.md +246 -0
- package/src/bin/DRACOLoader.js +208 -0
- package/src/bin/GLTFLoader.js +2734 -0
- package/src/index.js +76 -0
- package/src/utils/exports.js +5 -0
- package/src/utils/glftLoader.js +4 -0
- package/src/utils/isVarName.js +10 -0
- package/src/utils/parser.js +516 -0
- package/src/utils/transform.js +48 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { NodeIO } from '@gltf-transform/core'
|
|
2
|
+
import { simplify, weld, dedup, resample, prune, textureCompress, draco } from '@gltf-transform/functions'
|
|
3
|
+
import { ALL_EXTENSIONS } from '@gltf-transform/extensions'
|
|
4
|
+
import { MeshoptDecoder, MeshoptEncoder, MeshoptSimplifier } from 'meshoptimizer'
|
|
5
|
+
import draco3d from 'draco3dgltf'
|
|
6
|
+
import sharp from 'sharp'
|
|
7
|
+
|
|
8
|
+
async function transform(file, output, config = {}) {
|
|
9
|
+
await MeshoptDecoder.ready
|
|
10
|
+
await MeshoptEncoder.ready
|
|
11
|
+
const io = new NodeIO().registerExtensions(ALL_EXTENSIONS).registerDependencies({
|
|
12
|
+
'draco3d.decoder': await draco3d.createDecoderModule(),
|
|
13
|
+
'draco3d.encoder': await draco3d.createEncoderModule(),
|
|
14
|
+
'meshopt.decoder': MeshoptDecoder,
|
|
15
|
+
'meshopt.encoder': MeshoptEncoder,
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
const document = await io.read(file)
|
|
19
|
+
const resolution = config.resolution ?? 1024
|
|
20
|
+
|
|
21
|
+
const functions = [
|
|
22
|
+
// Losslessly resample animation frames.
|
|
23
|
+
resample(),
|
|
24
|
+
// Remove duplicate vertex or texture data, if any.
|
|
25
|
+
dedup(),
|
|
26
|
+
// Remove unused nodes, textures, or other data.
|
|
27
|
+
prune(),
|
|
28
|
+
// Resize and convert textures (using webp and sharp)
|
|
29
|
+
textureCompress({ targetFormat: 'webp', encoder: sharp, resize: [resolution, resolution] }),
|
|
30
|
+
// Add Draco compression.
|
|
31
|
+
draco(),
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
if (config.simplify) {
|
|
35
|
+
functions.push(
|
|
36
|
+
// Weld vertices
|
|
37
|
+
weld({ tolerance: config.weld ?? 0.0001 }),
|
|
38
|
+
// Simplify meshes
|
|
39
|
+
simplify({ simplifier: MeshoptSimplifier, ratio: config.ratio ?? 0.75, error: config.error ?? 0.001 })
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
await document.transform(...functions)
|
|
44
|
+
|
|
45
|
+
await io.write(output, document)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export default transform
|