30350n / pcb2blender

KiCad to Blender 3D model workflow
GNU General Public License v3.0
516 stars 11 forks source link

How to export glTF 2.0 #28

Open N0cynym opened 1 year ago

N0cynym commented 1 year ago

I just have a question:

How can I export from my blender project a .glb with rendered surface texture?

I want to create a file for https://modelviewer.dev/editor/

Thanks for this tool πŸ™

30350n commented 1 year ago

That's not easily possible atm, alteast not with the current Cycles shaders. The 3D (deprecated) material mode might work, but won't look nearly as good. Solutions for this have been discussed before in #8 and also #24 kinda.

N0cynym commented 1 year ago

@30350n did you test how it looks like if you render top and bottom image and put it as image texture on the surface of the blank imported glb body?

30350n commented 1 year ago

No, but what your suggesting is more or less a very naive version of texture baking. The problem is that this doesn't really work (or atleast isn't optimal) with the pcb mesh the addon produces. I spent some time on working around this on the baking branch, but don't have time to further persue this atm.

N0cynym commented 8 months ago

Dear @30350n,

I hope you started well into the new year!?

Do you have any news/updates about this feature?

30350n commented 8 months ago

I hope you started well into the new year!?

Yes, thanks, hope you did too.

Do you have any news/updates about this feature?

No, not really and it wouldn't really be the main priority either. The main thing that needs to be done is a rework of all the materials, to improve performance (and visuals). Though this could also atleast simplify the export problem ...

Regardless, I can't really invest the time that would take atm.

N0cynym commented 8 months ago

Thank you very much for your time and for updating this amazing project. πŸ‘

Well for now I'll see how I can embed a VRML export from KiCad to my web presentation. But I think using a glTF 2.0 export with your material finish in a model viewer would be much nicer ☺️

crazy-hair commented 1 month ago

You can bake the textures out with the Blender passes (the ones you will need are Diffuse Color, Glossy Color, Roughness, Normal, and optionally Ambient Occlusion) onto a secondary UV map (you can use the integrated Smart UV Project function for this, it doesn't need to be fancy). Unfortunately Blender doesn't support exporting with the KHR_materials_pbrSpecularGlossiness extension though, so you'll have to export it as glTF and then edit the JSON with the proper specular setup: Code_2024-07-31_00-40-54_c You can find more documentation on the extension here.

If you want the solder mask to show up in the normal map, you'll need to bake the normal map pass (and AO, if you want it to show up there as well) with a modified shader setup like this: image (Blender doesn't show past 3 digits but the Bump node's distance field is set to 0.0001).

Attached is an example glTF file: glTF_compressed.zip

Make sure that the viewer you're using has support for SpecularGlossiness though, it looks like \<model-viewer> might not support it. I know Babylon.js and PlayCanvas do.

TLDR: Not impossible, but certainly not a single button press. If you're familiar with Blender and the glTF standard it shouldn't be too difficult though.

crazy-hair commented 1 month ago

Small update: It appears that with the Blender 4.2 update, you can export spec-gloss materials with a node setup like this: blender_2024-08-10_20-26-05 It works by setting the IOR to a very high index and then plugging the specular texture into the specular tint input. It actually doesn't utilize the KHR_materials_pbrSpecularGlossiness extension at all, instead just using KHR_materials_ior and KHR_materials_specular extensions, both of which appear to be supported by <model-viewer>. Code_2024-08-10_20-52-57

With manual glTF editing out of the way, the only hurdle to exporting as glTF is now just baking the materials down to textures, and it's not that hard if you understand how rendering in Blender works. Here's an example glB file for reference: compressed.zip

@N0cynym If you still need a pcb3d file converted to glTF, I can take a crack at it if you want to send it my way.

30350n commented 1 month ago

Thanks a lot for looking into this and for sharing your findings! Ideally this would probably be automated, either by selecting a special "GLTF Export" material mode in the importer or by having a special "Export PCB to GLTF" operator somewhere, which could then also handle the baking as well as possibleGLTF post processing.

Regarding the "baking" part of this, I tried looking into this quite a while ago. WIP is available on the baking branch.

crazy-hair commented 1 month ago

It would probably be easiest to have a button labeled something like Bake to exportable mesh in the sidebar that clones the object, applies all the needed actions (make single user, apply modifiers, create a secondary UV map, auto-unwrap, etc,) and then bakes the materials to it. It would leave the user with a secondary mesh in the viewport that can be exported with the native glTF exporter. I unfortunately am not a dev, so I'm not really able to automate anything or make a PR, and I know you're busy with other things so I'm not expecting this to get implemented anytime soon, but on the face of it, it should be pretty doable.