[GLTFast] GLB Models Show Magenta Materials
Solution
Unity 2021.x - Unity 6.3.x
Published Mon, Mar 9
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.
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.
- Navigate to Edit > Project Settings.
- Select the
Graphicscategory. - Locate the Always Included Shaders section.
- Increase the Size property to add new slots.
- Drag and drop all relevant
GLTFastshaders from theGLTFastpackage Shaders folder into these slots. - Alternatively, use the target icon next to each slot to search for and assign
GLTFastshaders. - Verify that every variant required by your models is explicitly part of the Always Included Shaders collection.
Additional Tips
- Verify that the
Graphics Tiersettings match between the standalone and embedded environments. - Ensure that the
Shader Variant Collectionis 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.
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.