Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transform): Updates to support glTF Transform v4 #267

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ const cli = meow(
--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
--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)
--console, -c Log JSX to console, won't produce a file
Expand Down Expand Up @@ -66,7 +65,6 @@ const cli = meow(
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 },
console: { type: 'boolean', shortFlag: 'c' },
Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"node": ">=16"
},
"dependencies": {
"@gltf-transform/core": "4.0.4",
"@gltf-transform/extensions": "4.0.4",
"@gltf-transform/functions": "4.0.4",
"@gltf-transform/core": "4.0.8",
"@gltf-transform/extensions": "4.0.8",
"@gltf-transform/functions": "4.0.8",
"@node-loader/babel": "^2.0.1",
"draco3dgltf": "^1.5.7",
"is-var-name": "^2.0.0",
Expand All @@ -59,7 +59,10 @@
"sharp": "<0.33.0"
},
"resolutions": {
"sharp": "<0.33.0"
"sharp": "<0.33.0",
"@gltf-transform/core": "4.0.8",
"@gltf-transform/extensions": "4.0.8",
"@gltf-transform/functions": "4.0.8"
},
"devDependencies": {
"@babel/core": "7.23.6",
Expand Down
13 changes: 6 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ A small command-line tool that turns GLTF assets into declarative and re-usable

### GLTFJSX fixes that

- 🧑‍💻 It creates a virtual graph of all objects and materials. Now you can easily alter contents and re-use.
- 🧑‍💻 It creates a virtual graph of all objects and materials. Now you can easily alter contents and re-use.
- 🏎️ The graph gets pruned (empty groups, unnecessary transforms, ...) and will perform better.
- ⚡️ It will optionally compress your model with up to 70%-90% size reduction.

Expand All @@ -45,11 +45,10 @@ Options
--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
--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)
--console, -c Log JSX to console, won't produce a file
Expand Down Expand Up @@ -81,7 +80,7 @@ export function Model(props) {
const { nodes, materials } = useGLTF('/model-transformed.glb')
return (
<group {...props} dispose={null}>
<PerspectiveCamera name="camera" fov={40} near={10} far={1000} position={[10, 0, 50]} />
<PerspectiveCamera name="camera" fov={40} near={10} far={1000} position={[10, 0, 50]} />
<pointLight intensity={10} position={[100, 50, 100]} rotation={[-Math.PI / 2, 0, 0]} />
<group position={[10, -5, 0]}>
<mesh geometry={nodes.robot.geometry} material={materials.metal} />
Expand Down Expand Up @@ -110,7 +109,7 @@ import { Model } from './Model'
function App() {
return (
<Canvas>
<Model />
<Model />
```

You can re-use it, it will re-use geometries and materials out of the box:
Expand Down Expand Up @@ -208,7 +207,7 @@ useEffect(() => {

#### ⚡️ Auto-instancing

Use the `--instance` flag and it will look for similar geometry and create instances of them. Look into [drei/Merged](https://github.com/pmndrs/drei#instances) to understand how it works. It does not matter if you instanced the model previously in Blender, it creates instances for each mesh that has a specific geometry and/or material.
Use the `--instance` flag and it will look for similar geometry and create instances of them. Look into [drei/Merged](https://github.com/pmndrs/drei#instances) to understand how it works. It does not matter if you instanced the model previously in Blender, it creates instances for each mesh that has a specific geometry and/or material.

`--instanceall` will create instances of all the geometry. This allows you to re-use the model with the smallest amount of drawcalls.

Expand Down Expand Up @@ -287,7 +286,7 @@ it('should have a scene with a blue mesh', async () => {
const { scene } = await new Promise((res) => loader.parse(data, '', res))
expect(() => scene.children.length).toEqual(1)
expect(() => scene.children[0].type).toEqual('mesh')
expect(() => scene.children[0].material.color).toEqual('blue')
expect(() => scene.children[0].material.color).toEqual('blue')
})
```

Expand Down
7 changes: 5 additions & 2 deletions src/utils/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ async function transform(file, output, config = {}) {
)
}

functions.push(
// Weld vertices
weld(),
)

if (config.simplify) {
functions.push(
// Weld vertices
weld({ tolerance: config.weld ?? 0.0001 / 2 }),
// Simplify meshes
simplify({ simplifier: MeshoptSimplifier, ratio: config.ratio ?? 0, error: config.error ?? 0.0001 })
)
Expand Down
31 changes: 8 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1094,43 +1094,28 @@
"@babel/helper-validator-identifier" "^7.24.7"
to-fast-properties "^2.0.0"

"@gltf-transform/[email protected]":
version "4.0.4"
resolved "https://registry.yarnpkg.com/@gltf-transform/core/-/core-4.0.4.tgz#71d9465fb12f656c97506fe85ef54e3ae96e927c"
integrity sha512-SsIL8vAMjXZBEJR3gI6OhGejK+ck4KKDSswYtShpXLylFAVke2EmITRA7CbzwmgZzbFc3a+appjhBhjRdb7v5A==
dependencies:
property-graph "^2.0.0"

"@gltf-transform/core@^4.0.4", "@gltf-transform/core@^4.0.8":
"@gltf-transform/[email protected]", "@gltf-transform/core@^4.0.8":
version "4.0.8"
resolved "https://registry.yarnpkg.com/@gltf-transform/core/-/core-4.0.8.tgz#cf5699cb05e62a33bb68dbf9e1438186f992cac5"
integrity sha512-8oSLSw+t+wxPvKC2qm0n3EOoR6Ql2DMuagimjWjGz8sC4MtCqbo6kS6dCeissYrkgP2fj/k8dzRWiWQZRatGMg==
dependencies:
property-graph "^2.0.0"

"@gltf-transform/[email protected]":
version "4.0.4"
resolved "https://registry.yarnpkg.com/@gltf-transform/extensions/-/extensions-4.0.4.tgz#f4cbd658969ca012f6a5c2e6565133f90d6bb0e4"
integrity sha512-hFdH9Rp7EeNKGjie+OSlw/OHGb10+AlN1e9VLWGzQjNc7Oco9+ZIja5pqEENxYw5WjOS6sX07E6eyOAZUcbF4A==
dependencies:
"@gltf-transform/core" "^4.0.4"
ktx-parse "^0.7.0"

"@gltf-transform/extensions@^4.0.4":
"@gltf-transform/[email protected]", "@gltf-transform/extensions@^4.0.8":
version "4.0.8"
resolved "https://registry.yarnpkg.com/@gltf-transform/extensions/-/extensions-4.0.8.tgz#4f099841df3b2f28a7974e048c7a65f9abb03618"
integrity sha512-ZbpSV+tHIIYZySwkt5/GLiTvfhvO/4ff7frSmdAG8nE3xRsLyxd2cuYcYHSqN7PP3Ch8NxLSBxvd9/MHNQmMYw==
dependencies:
"@gltf-transform/core" "^4.0.8"
ktx-parse "^0.7.0"

"@gltf-transform/[email protected].4":
version "4.0.4"
resolved "https://registry.yarnpkg.com/@gltf-transform/functions/-/functions-4.0.4.tgz#8e3b68a8b30a637fc1a0e4bc0e7038655e88d55c"
integrity sha512-9RLdwSqyBQGbY3dPq7JDXkGJfCo29X5MlXX8UJLjse8SBB53ketvJvcnKD2yEg6XiIso+ee126uB8MXdQuw4pg==
"@gltf-transform/[email protected].8":
version "4.0.8"
resolved "https://registry.yarnpkg.com/@gltf-transform/functions/-/functions-4.0.8.tgz#2b5d653e327d6c27c4011774561d4828b14f3956"
integrity sha512-v6yVtqh82iBoIz4PiEX9SWu9GEsrCyMclhzBMh6A6X2ezb823QBg1jOWdP+tB7KgetWZna4hALppwpsMQwS1ZQ==
dependencies:
"@gltf-transform/core" "^4.0.4"
"@gltf-transform/extensions" "^4.0.4"
"@gltf-transform/core" "^4.0.8"
"@gltf-transform/extensions" "^4.0.8"
ktx-parse "^0.7.0"
ndarray "^1.0.19"
ndarray-lanczos "^0.3.0"
Expand Down
Loading