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

Conversation

borisghidaglia
Copy link

@borisghidaglia borisghidaglia commented Jan 17, 2025

The idea is to leverage GLTFExporter

Copy link
Contributor


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@@ -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.

Comment on lines +61 to +74
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) => {
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.

Comment on lines +140 to +142
object.material = new THREE.MeshBasicMaterial({
...object.material,
map: texture
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 :)

@FarazzShaikh
Copy link
Owner

Hey! Thanks for the PR. After thinking over on this, it seems that the scope of this PR is beyond the scope of the core library. As such, it would be better suited to be an example usage of the library rather than a core feature.

Another point for this is that GLTFExporter is also consiered an addon by ThreeJS (imported from three/addons) rather than a core feature.

So, I think it would be best to persue this:

  1. Ether in a seperate repo as a standalone demo
  2. Or by splitting the examples directory into having 2 examples, the current one and this new one

If going with 1, please close this PR. I may persue option 1 in my own time too, if so, i will let you know

@borisghidaglia
Copy link
Author

Hey! Thanks for the PR. After thinking over on this, it seems that the scope of this PR is beyond the scope of the core library. As such, it would be better suited to be an example usage of the library rather than a core feature.

No worries!
I just opened it because you suggested the idea by email and told me you would be happy to accept such a PR.
If it's not the case anymore that's perfectly ok 🙏


Throwing out the idea but what would you think about creating a utils.ts which could be imported through three-shader-baker/utils?

I'm fine with doing option 1, but it seems to me that it would be such a thin layer on top of this lib that it could be kind of overkill.


Side note: any idea about what I'm doing wrong here? #5 (comment)

@FarazzShaikh
Copy link
Owner

Yeah sorry for the initial confusion. I think we can do a mix of approach 1 and 2 where you can replace the current example with this use case. I’d be happy to accept a PR for that

for the comment, yep that seems about right, keep in mind, in my email I outlined an approach that I haven’t tried yet so this may or may not work. We’ll need to find out

@FarazzShaikh
Copy link
Owner

The reason I’m hesitant to add it to the core of this library is that it’s an inherently hacky approach and would introduce some maintenance overhead if it doesn’t work for some folk or breaks when ThreeJS updates something. Since GLTFExporter is not a part of code ThreeJS, breaking changes are more likely within it

if left in userland, we wouldn’t have to scurry for a solution of something like that happens

@borisghidaglia
Copy link
Author

borisghidaglia commented Jan 18, 2025

Yeah sorry for the initial confusion

No worries again!

you can replace the current example with this use case

Maybe just extend it? I think it's nice to have the current example to showcase a simple use case. Wdyt?

this may or may not work

I can tell you for sure: it does not 😂. I ran it before submitting this draft.

We’ll need to find out

I'm going to investigate.

The reason I’m hesitant to add it [...]

Thanks for the explanations, got it!

@FarazzShaikh
Copy link
Owner

Extending it sounds good to me!

@borisghidaglia
Copy link
Author

borisghidaglia commented Jan 18, 2025

Great! On it!
I managed to export the scene but for now it's all black on gltf.pmnd.rs

@borisghidaglia
Copy link
Author

Can't manage do get around this.

Error: THREE.GLTFExporter: Invalid image type. Use HTMLImageElement, HTMLCanvasElement, ImageBitmap or OffscreenCanvas.

The THREE.WebGLRenderTarget returned by bake has a texture prop, which itself contains an image prop. This image prop is indeed not one of these types. Tried a few things but I clearly don't understand what I'm doing enough to progress efficiently.

Posting this here in case you would have any clue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants