KhronosGroup / UnityGLTF

Runtime glTF 2.0 Loader for Unity3D
MIT License
1.83k stars 489 forks source link

Issues building for runtime loading #736

Closed andybak closed 1 month ago

andybak commented 6 months ago

I'm hitting a problem with UnityGLTF that seems to be perfectly solved by this PR: https://github.com/KhronosGroup/UnityGLTF/pull/120

Except that code was removed ages ago - so I presume there's a better approach now.

The problem:

In a build UnityGLTF throws an exception on import because it says it's shaders are missing. So the obvious solution is to add them to Unity's "Always Include" list.

Except that gives this error when building for Android.

Error building Player: UnityGLTF/PBRGraph has too many Shader variants (1179648). To resolve this, go to Project Settings > Graphics and remove this Shader from the Always Included Shaders list. For more explicit control over how Unity includes specific Shader variants in a build, use a ShaderVariantCollection.

No idea if desktop builds work as they never seem to finish!

I don't really understand how I'm meant to use ShaderVariantCollection - how do I know what set of variants are needed? I'm aiming to support any GLTF that the user supplies.

For comparison GLTFast seems to handle this fine - it adds itself to the "Always Include" list and doesn't seem to generate a huge number of variants.

I've reproduced this in a minimal project that simply loads a single GLTF at runtime from the Streaming Assets folder.

My current plan is to add every model from https://github.com/KhronosGroup/glTF-Sample-Assets to the project and use this to generate a ShaderVariantCollection - but that seems a bizarre direction to be forced into.

I suspect I'm doing something stupid or unusual but I genuinely have no idea what it might be.

hybridherbst commented 6 months ago

Hey, which version if Unity are you using? There have been bugs in some 2021 + 2022 versions on Unity's side regarding shader variant stripping from Shader Graph, which resulted in far too many variants.

andybak commented 6 months ago

I tried 2021.3.30f1 and the 2022 LTS (2022.3.27f1)

The ShaderVariant thing seemed to work. I've no idea if the list it came up with is sufficient and I don't have time to test against a ton of sample GLTFs at the moment

pfcDorn commented 2 months ago

UnityGLTF currently supports way more material features than glTFast, so we end up with more shader variants at runtime.

We will likely have to either reduce the number of keywords (at some runtime performance cost for dynamic switching), or provide some means to choose which shader features are expected to work at runtime, or both.

pfcDorn commented 1 month ago

Hey, we now have two predefined ShaderVariantCollection with the latest update image One is for BuildIn RP, the other for URP and HDRP.

Also you can find in the Gltf Settings new Shader Pass Stripping options to reduce it even more: image

I will close this, when you have still issues with the shaders, please create a new issue. thanks :)