KhronosGroup / glTF-Blender-IO

Blender glTF 2.0 importer and exporter
https://docs.blender.org/manual/en/latest/addons/import_export/scene_gltf2.html
Apache License 2.0
1.49k stars 317 forks source link

Support alpha transparency workflow that allows preview in Blender renderer #144

Closed dsinni closed 5 years ago

dsinni commented 5 years ago

I'm noticing that in Blender 2.80, adjusting the Principle BSDF's Base Color A (alpha) value and setting the material's Blending Mode to Alpha Blend does work on export, but there doesn't appear to be a way to effectively preview the alpha levels in the Blender renderer(s) using that method, so I have to keep checking after export.

Would it maybe make more sense, if possible, to support using the Principled BSDF + Transparent BSDF through a Mix Shader and adjusting the alpha level with the Mix Shader's Factor property?

If I'm not mistaken, this is a pretty common node configuration: image

I'm no Blender guru, so I may be missing a small detail, but the ability to accurately preview the result in the renderer before export would be very useful.

dsinni commented 5 years ago

When using the above configuration, it also appears that, while a bit noisy, Blend Mode: Alpha Hashed gives a more accurate representation of the settings thanAlpha Blend does, asAlpha Blend shows transparency regardless of alpha value.

With Base Color's alpha set to 1.0 and Mix Shader's Factor set to 1.0, the object should be completely opaque.

These can be seen in both Eevee Rendered and LookDev views:

Alpha Blend: image

Alpha Hashed: image

emackey commented 5 years ago

Unfortunately, many (but not all) realtime engines suffer the Alpha Blend problem in your screenshot above, where they are at a loss to correctly depth-order the blended opaque polygons. So for these engines, Alpha Blend is a closer approximation to the final glTF.

I think I mostly like the node structure you've got above, with some caveats: It looks like low-alpha materials will start to blend with pure white, per the transparent node, which glTF will not do. Also, to import a glTF with this node structure, one would need to take the imported base color texturemap, run it through "Separate RGBA", feed the A over to your nodes above, then run "Combine RGB" to put the remaining 3 channels back together, before feeding into Principled BSDF's base color input. That's a lot of complexity just to move the alpha channel elsewhere, but, I don't know a better way.

dsinni commented 5 years ago

Thanks for the reply, @emackey.

I can appreciate not wanting to add unnecessary complexity, but I believe it's important for users to have a pretty good idea of what they'll be exporting before they export, given that they work within glTF's limitations.

I just wanted to point out that using the already supported method of adjusting the Base Color Alpha, and using Alpha Blend results in instant, unvarying transparency not only in the real-time engine, but also in a render, regardless of alpha value. This only gives users the option to see the material as either completely opaque or semi-transparent, depending on the Blend Mode; there is no variation with Alpha Blend from what I can tell.

So you can compare to my earlier screen shots using the other method, see actual (F12) renders below using a simple (supported) node setup similar to this: image

EEVEE render with Alpha Blend and Base Color alpha of 0 (should be completely transparent): image

EEVEE render with Alpha Blend and Base Color alpha of 1 (should be completely opaque): image

While these settings may export with variation, as you can see, they are exactly the same in Blender. And I swear that I didn't cheat; these are actually different screen shots. :)

I'm not sure of the best way; I can only say that the example node setup with Transparency BSDF + Alpha Hashed gave a better representation within Blender. You folks are the wizards, so I just wanted to share my experiences in hopes that you could work your magic for the best UX possible.

I think I mostly like the node structure you've got above, with some caveats: It looks like low-alpha materials will start to blend with pure white, per the transparent node, which glTF will not do.

I'm not sure I was experiencing this. Would the order of the shaders in the Mix Shader have any effect on that?

donmccurdy commented 5 years ago

Do we think this is just a bug in the 2.8 beta? I’m surprised alpha blend is not affected by the alpha value in the blender viewport...

donmccurdy commented 5 years ago

Per https://developer.blender.org/T52189, the ideal outcome in my opinion would be a fix landing in Blender 2.8. Mixing with transparent nodes is less ideal (see https://developer.blender.org/T51408).

julienduroure commented 5 years ago

See #247 for importer part

donmccurdy commented 5 years ago

At this point it looks likely and preferable that Blender will fix the transparency workflow. Closing this issue for now, we can revisit if needed.

xCyborg commented 5 years ago

This is still a huge issue, I can't export and alpha blended model in glTF or even FBX, the sorting is all messed up, tried all options it's just impossible. Should be fixed ASAP!

donmccurdy commented 5 years ago

@xCyborg we already support exporting alpha blended models in glTF. If you're having trouble getting this to work, please review the docs and if it's not working, file a new issue including a .blend file so we can reproduce your issue.

xCyborg commented 5 years ago

EEVEE render with Alpha Blend and Base Color alpha of 1 (should be completely opaque):

@donmccurdy That's the problem, it's still rendering really weird even if opacity is 1.

donmccurdy commented 5 years ago

"the sorting is all messed up"

Perfect transparent sorting is a Hard Problem in all realtime engines; this is not something an exporter can fix for you... if you would like to share an example we may be able to advise how to work around it.

xCyborg commented 5 years ago

@donmccurdy Here download this glb file and open it in any glTF viewer, I'm using Windows' built in 3D Viewer. https://1drv.ms/u/s!Ato44WJy5busskzlSlyq_fI_uJuJ

xCyborg commented 5 years ago

@donmccurdy Here is the source blend file.

https://1drv.ms/u/s!Ato44WJy5bussk0DKaXSocyyuPer

image

xCyborg commented 5 years ago

@donmccurdy It's also exporting the vertex color data as base color distorting the diffuse color, I'm not sure if it's the exporter or the 3d viewer.

emackey commented 5 years ago

Depth-sorting of alpha blended polygons has been a well-known and well-studied problem for multiple decades of realtime 3D graphics. It's got nothing to do with the glTF or FBX exporters. Find a way to use non-alpha-blended materials for the opaque stuff. Use opaque objects to block multiple layers of translucency from stacking up. Or use a path tracer. Or use OIT. etc.

xCyborg commented 5 years ago

@donmccurdy You mean I must use different materials for the opaque parts? And very simple translucent part geometry? But in realtime 3D engines like Unity I can use Alpha-blended materials just like any opaque materials with no sorting problems at all. I still don't understand though why parts that aren't supposed to be blended are blended and rendered as transparent.

emackey commented 5 years ago

You mean I must use different materials for the opaque parts?

Yes, exactly. Complex and robust game engines like Unity may offer ways to support it, but simpler realtime engines will struggle with opaque parts incorrectly marked as translucent.

This has nothing to do with file formats or exporters, so this GitHub issue tracker is not really the correct forum for asking for help on this topic. You could try searching for "alpha blend depth sorting" or similar online.