Skip to content

Generating invalid materials on the first few frames #2740

@redwyre

Description

@redwyre

Environments (please complete the following information):

  • UniVRM version: 0.130.1
  • Unity version: Unity-6000.2.9
  • OS: Windows 11

Describe the bug

  • Try to import a glTF file in Start in a URP project
  • The model is magenta because it is using built-in shaders

When importing a glTF file at runtime on the first frame, the default MaterialDescriptorGeneratorUtility.GetValidGltfMaterialDescriptorGenerator() uses RenderPipelineUtility.GetRenderPipelineType() which is not guaranteed to be valid for around 4 frames: https://docs.unity3d.com/6000.2/Documentation/ScriptReference/Rendering.RenderPipelineManager-currentPipeline.html. This results in it thinking it's running in the built in pipeline and choosing those shaders.

public class Test : MonoBehaviour
{
    public string FilePath;
    GltfData data;
    GameObject go;
    public void Start()
    {
        data = new AutoGltfFileParser(FilePath).Parse();

        using (var context = new ImporterContext(data))
        {
            var loaded = context.Load();
            loaded.ShowMeshes();
            go = loaded.Root;
            go.transform.SetParent(this.transform, false);
        }
    }
}

The fix looks like it should just be using GraphicsSettings.currentRenderPipeline instead.

A work around for this is to provide the URP material generator directly:

...
using (var context = new ImporterContext(data, materialGenerator: new UrpGltfMaterialDescriptorGenerator()))
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions