UnityGLTF icon indicating copy to clipboard operation
UnityGLTF copied to clipboard

Issues building for runtime loading

Open andybak opened this issue 1 year ago • 3 comments

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.

andybak avatar May 06 '24 13:05 andybak

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.

hybridherbst avatar May 06 '24 14:05 hybridherbst

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

andybak avatar May 06 '24 17:05 andybak

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 avatar Aug 27 '24 14:08 pfcDorn

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 :)

pfcDorn avatar Oct 16 '24 07:10 pfcDorn

@pfcDorn I started building the .apk for Quest 3s using the preloaded UnityGLTFShaderVariantCollection, however the compilation is taking hours due to a huge number of variants. Is this normal behavior?

image

spirosperos avatar Dec 18 '24 18:12 spirosperos

You can also reduce the build time to strip more variants (see my previous post). Let's say you don't use BuiltIn RenderPipeline at all, then select the BuildIn Passes. Also when using URP und you are not using Deferred rendering, you can also strip this there. This should also reduce the build time. Other then that, for even more optimization, you need to create your own shader variant collection. But then you need to know exactly which shader variants you need at runtime.

pfcDorn avatar Feb 06 '25 06:02 pfcDorn

Is there any documentation on the shader stripping? I'm confused as to the relationship between the checkbox and the dropdown. I'd also like to make sure that selecting in the dropdown means "strip these" rather than "don't strip these" (the former seems like the obvious interpretation but it's worth double checking)

andybak avatar Feb 07 '25 17:02 andybak