UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

graphics

[GLTFast] GLB Models Show Magenta Materials

Solution

graphicsshader graphmathematicstexturesfile-import

Unity 2021.x - Unity 6.3.x

Published Mon, Mar 9

Issue

 When importing GLB models into an embedded Unity application using the GLTFast plugin, textures and materials fail to render, often appearing with a magenta color.

This issue specifically manifests when the Unity application is embedded within a C# WPF host, despite the same GLB loading process functioning correctly in a standalone environment. The data transfer mechanism via named pipes operates as expected, suggesting the problem lies within the Unity rendering pipeline during embedding.

Ensure all necessary GLTF shaders are explicitly added to the Always Included Shaders list within Project Settings to resolve missing texture issues with GLB models.

Explanation

The primary cause of magenta materials when loading GLB models via GLTFast in an embedded Unity application is shader stripping. Unity build processes, especially in specific configurations like embedded environments, remove shaders not directly referenced in active scenes.

  1. Navigate to Edit > Project Settings.
  2. Select the Graphics category.
  3. Locate the Always Included Shaders section.
  4. Increase the Size property to add new slots.
  5. Drag and drop all relevant GLTFast shaders from the GLTFast package Shaders folder into these slots.
  6. Alternatively, use the target icon next to each slot to search for and assign GLTFast shaders.
  7. Verify that every variant required by your models is explicitly part of the Always Included Shaders collection.

Additional Tips

  • Verify that the Graphics Tier settings match between the standalone and embedded environments.
  • Ensure that the Shader Variant Collection is not stripping variants required by the runtime hardware.
  • Only add necessary shaders to the Always Included Shaders to minimize the overall build size and runtime memory footprint.

Copy


using GLTFast;
using UnityEngine;

public class yourScript : MonoBehaviour
{
    public async void LoadFromPath(string path)
    {
        var settings = new ImportSettings
        {
            GenerateMipMaps = true,
            AnisotropicFilterLevel = 3
        };
        var gltf = new GltfImport();
        bool ok = await gltf.Load(path, settings);
        if (!ok)
        {
            Debug.LogError("Error loading glTF");
            return;
        }
        await gltf.InstantiateMainSceneAsync(transform);
    }
}

Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.