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

Enable GLTF export with baked-in textures #5

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@types/node": "^22.5.5",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/three": "0.168.0",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to add it in order to make onDone and onError types below work, but without success.

"@vitejs/plugin-react": "^4.3.1",
"eslint": "^9.9.0",
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
Expand All @@ -41,4 +42,4 @@
"vite": "^5.4.1",
"vite-plugin-svgr": "^4.2.0"
}
}
}
44 changes: 44 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as THREE from "three";
import {
AccumulativeShadows,
Bounds,
Expand All @@ -15,6 +16,7 @@ import {
useShaderBaker
} from "three-shader-baker/react";

import { ShaderBaker as ShaderBakerImpl } from "three-shader-baker";
import { useEffect, useState } from "react";

import { EffectComposer } from "@react-three/postprocessing";
Expand All @@ -24,6 +26,8 @@ import { Clothes } from "./Clothes";
import { N8AO } from "./N8AO";
import { TextureViewer3D } from "./TextureViewer3D";

const sb = new ShaderBakerImpl();

function Thing() {
const { bake } = useShaderBaker();
const [seed, setSeed] = useState(0);
Expand All @@ -48,9 +52,49 @@ function Thing() {
}

export default function App() {
const [scene, setScene] = useState<THREE.Scene>(null);
const [gl, setGl] = useState<THREE.WebGLRenderer>(null);

function downloadGLTF() {
const exporter = sb.exportAsBakedGLTF(scene, gl);
exporter({
onDone: (gltf) => {
const blob = new Blob([JSON.stringify(gltf)], {
type: "application/json"
});
const url = URL.createObjectURL(blob);

const link = document.createElement("a");
link.href = url;
link.download = "scene.gltf";
link.click();

URL.revokeObjectURL(url);
},
onError: (error) => {
Comment on lines +61 to +74
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, the params of onDone and onError are inferred as any. Seems that it's because GLTFExporter type is inferred as any in the first place.

console.log(error);
}
});
}

return (
<>
<button
onClick={downloadGLTF}
style={{
position: "absolute",
top: "10px",
right: "10px",
zIndex: 1000
}}
>
Export
</button>
<Canvas
onCreated={({ scene, gl }) => {
setScene(scene);
setGl(gl);
}}
shadows
camera={{
position: [-2, 1, 5]
Expand Down
28 changes: 28 additions & 0 deletions package/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as THREE from "three";
import { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter";

import { DilationMaterial } from "./DilationMaterial";
import { FullScreenQuad } from "./FullScreenQuad";

Expand Down Expand Up @@ -119,6 +121,32 @@ export class ShaderBaker {

return targetFbo;
}

exportAsBakedGLTF(
scene: THREE.Scene,
gl: THREE.WebGLRenderer,
options?: BakeOptions
): ({
onDone,
onError
}: {
onDone: Parameters<InstanceType<typeof GLTFExporter>["parse"]>[1]; // onDone param
onError: Parameters<InstanceType<typeof GLTFExporter>["parse"]>[2]; // onError param
}) => any {
scene.traverse((object) => {
console.log(object);
if (object instanceof THREE.Mesh) {
const texture = this.bake(gl, object, options).texture;
object.material = new THREE.MeshBasicMaterial({
...object.material,
map: texture
Comment on lines +140 to +142
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I think I'm applying the texture wrong.
I'm a Three.js noob tbh, any clue is welcome :)

});
}
});

return ({ onDone, onError }) =>
new GLTFExporter().parse(scene, onDone, onError);
}
}

export * from "./utils";
22 changes: 11 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1909,26 +1909,26 @@
resolved "https://registry.yarnpkg.com/@types/stats.js/-/stats.js-0.17.3.tgz#705446e12ce0fad618557dd88236f51148b7a935"
integrity sha512-pXNfAD3KHOdif9EQXZ9deK82HVNaXP5ZIF5RP2QG6OQFNTaY2YIetfrE9t528vEreGQvEPRDDc8muaoYeK0SxQ==

"@types/three@^0.163.0":
version "0.163.0"
resolved "https://registry.yarnpkg.com/@types/three/-/three-0.163.0.tgz#96f5440fcd39452d2c84dfe0c9b7a9cf0247b9e6"
integrity sha512-uIdDhsXRpQiBUkflBS/i1l3JX14fW6Ot9csed60nfbZNXHDTRsnV2xnTVwXcgbvTiboAR4IW+t+lTL5f1rqIqA==
"@types/three@0.168.0", "@types/three@^0.168.0":
version "0.168.0"
resolved "https://registry.yarnpkg.com/@types/three/-/three-0.168.0.tgz#510420c4bbee7937bbbcdfbc5dc31160771eaef8"
integrity sha512-qAGLGzbaYgkkonOBfwOr+TZpOskPfFjrDAj801WQSVkUz0/D9zwir4vhruJ/CC/GteywzR9pqeVVfs5th/2oKw==
dependencies:
"@tweenjs/tween.js" "~23.1.1"
"@tweenjs/tween.js" "~23.1.3"
"@types/stats.js" "*"
"@types/webxr" "*"
"@webgpu/types" "*"
fflate "~0.8.2"
meshoptimizer "~0.18.1"

"@types/three@^0.168.0":
version "0.168.0"
resolved "https://registry.yarnpkg.com/@types/three/-/three-0.168.0.tgz#510420c4bbee7937bbbcdfbc5dc31160771eaef8"
integrity sha512-qAGLGzbaYgkkonOBfwOr+TZpOskPfFjrDAj801WQSVkUz0/D9zwir4vhruJ/CC/GteywzR9pqeVVfs5th/2oKw==
"@types/three@^0.163.0":
version "0.163.0"
resolved "https://registry.yarnpkg.com/@types/three/-/three-0.163.0.tgz#96f5440fcd39452d2c84dfe0c9b7a9cf0247b9e6"
integrity sha512-uIdDhsXRpQiBUkflBS/i1l3JX14fW6Ot9csed60nfbZNXHDTRsnV2xnTVwXcgbvTiboAR4IW+t+lTL5f1rqIqA==
dependencies:
"@tweenjs/tween.js" "~23.1.3"
"@tweenjs/tween.js" "~23.1.1"
"@types/stats.js" "*"
"@types/webxr" "*"
"@webgpu/types" "*"
fflate "~0.8.2"
meshoptimizer "~0.18.1"

Expand Down
Loading