-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import * as THREE from "three"; | ||
import { | ||
AccumulativeShadows, | ||
Bounds, | ||
|
@@ -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"; | ||
|
@@ -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); | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, the params of |
||
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] | ||
|
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"; | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I think I'm applying the texture wrong. |
||
}); | ||
} | ||
}); | ||
|
||
return ({ onDone, onError }) => | ||
new GLTFExporter().parse(scene, onDone, onError); | ||
} | ||
} | ||
|
||
export * from "./utils"; |
There was a problem hiding this comment.
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
andonError
types below work, but without success.