prefrontalcortex / UnityGLTF

Runtime GLTF Loader for Unity3D
MIT License
163 stars 41 forks source link

ShaderNotFoundException #51

Closed davidyrgilbert closed 2 years ago

davidyrgilbert commented 2 years ago

Hello, I'm getting an issue when loading a project that uses imported gltfs/glbs for the first time. The full error is as following:

ShaderNotFoundException: UnityGLTF/PBRGraph not found. Did you forget to add it to the build?
UnityGLTF.PBRGraphMap..ctor (System.String shaderName) (at Library/PackageCache/org.khronos.unitygltf@a72508a846/Runtime/Scripts/UniformMaps/PBRGraphMap.cs:33)
UnityGLTF.PBRGraphMap..ctor () (at Library/PackageCache/org.khronos.unitygltf@a72508a846/Runtime/Scripts/UniformMaps/PBRGraphMap.cs:11)
UnityGLTF.GLTFSceneImporter.ConstructMaterial (GLTF.Schema.GLTFMaterial def, System.Int32 materialIndex) (at Library/PackageCache/org.khronos.unitygltf@a72508a846/Runtime/Scripts/SceneImporter/ImporterMaterials.cs:58)
UnityGLTF.GLTFSceneImporter.ConstructMesh (GLTF.Schema.GLTFMesh mesh, System.Int32 meshIndex, System.Threading.CancellationToken cancellationToken) (at Library/PackageCache/org.khronos.unitygltf@a72508a846/Runtime/Scripts/SceneImporter/ImporterMeshes.cs:85)
UnityGLTF.GLTFSceneImporter.ConstructNode (GLTF.Schema.Node node, System.Int32 nodeIndex, System.Threading.CancellationToken cancellationToken) (at Library/PackageCache/org.khronos.unitygltf@a72508a846/Runtime/Scripts/GLTFSceneImporter.cs:729)
UnityGLTF.GLTFSceneImporter.GetNode (System.Int32 nodeId, System.Threading.CancellationToken cancellationToken) (at Library/PackageCache/org.khronos.unitygltf@a72508a846/Runtime/Scripts/GLTFSceneImporter.cs:642)
UnityGLTF.GLTFSceneImporter.ConstructScene (GLTF.Schema.GLTFScene scene, System.Boolean showSceneObj, System.Threading.CancellationToken cancellationToken) (at Library/PackageCache/org.khronos.unitygltf@a72508a846/Runtime/Scripts/GLTFSceneImporter.cs:965)
UnityGLTF.GLTFSceneImporter._LoadScene (System.Int32 sceneIndex, System.Boolean showSceneObj, System.Threading.CancellationToken cancellationToken) (at Library/PackageCache/org.khronos.unitygltf@a72508a846/Runtime/Scripts/GLTFSceneImporter.cs:528)
UnityGLTF.GLTFSceneImporter.LoadSceneAsync (System.Int32 sceneIndex, System.Boolean showSceneObj, System.Action`2[T1,T2] onLoadComplete, System.Threading.CancellationToken cancellationToken, System.IProgress`1[T] progress) (at Library/PackageCache/org.khronos.unitygltf@a72508a846/Runtime/Scripts/GLTFSceneImporter.cs:366)
Rethrow as AggregateException: One or more errors occurred. (UnityGLTF/PBRGraph not found. Did you forget to add it to the build?)
System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Threading.Tasks.Task.Wait () (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
UnityGLTF.GLTFImporter.CreateGLTFScene (System.String projectFilePath) (at Library/PackageCache/org.khronos.unitygltf@a72508a846/Editor/Scripts/GLTFImporter.cs:423)
UnityGLTF.GLTFImporter.OnImportAsset (UnityEditor.AssetImporters.AssetImportContext ctx) (at Library/PackageCache/org.khronos.unitygltf@a72508a846/Editor/Scripts/GLTFImporter.cs:365)
UnityEditor.AssetImporters.ScriptedImporter.GenerateAssetData (UnityEditor.AssetImporters.AssetImportContext ctx) (at <1f0be198f5164d2489de92f22c998266>:0)

I thought the recent commits that always ensured the shaders are loaded by the asset database fixes this, but unfortunately it did not. I'm using Unity 2021.3.5 with URP and the latest dev branch here.

Doing a reimport all (or just reimporting the broken meshes) fixes it, but every time someone clones a fresh project, the error pops up.

hybridherbst commented 2 years ago

Hi and thanks for the report! Yes, the AssetDatabase.LoadAsset calls in 1.10.1-pre should indeed prevent this. Could you try reverting to the version before (1.10.0-pre) that used GatherDependenciesFromSourceAsset?

I'll add another in-editor workaround in the place where PBRGraph is using Shader.Load - my assumption was that AssetDatabase.LoadAsset followed by Shader.Load would always succeed but seems that's not the case (it worked in my tests though).

davidyrgilbert commented 2 years ago

Thanks for the quick response! I've had a "hack" in there before this version as well, from way back from the origial repo actually. The thing that at the moment works if I call AssetDatabase.LoadAsset directly, and save the result in the shader variable.

        Shader s = null;
#if UNITY_EDITOR
        var path = "Packages/org.khronos.unitygltf/Runtime/Shaders/ShaderGraph/PBRGraph.shadergraph";
        s = AssetDatabase.LoadAssetAtPath<Shader>(path);
        if(s == null)
        {
            s = Shader.Find(shaderName);
        }
#else
        s = Shader.Find(shaderName);
#endif

That's pretty ugly but did the trick, but is obviously not applicable in general. I haven't yet tried it in a brand new project, maybe some settings or so are broken on my end? It almost feels like it's trying to import things before actually setting up the importer correctly the first time the project loads.

hybridherbst commented 2 years ago

Thanks - that's exactly what I'll add, just with the more stable GUID instead of the path :)

davidyrgilbert commented 2 years ago

Perfect, thanks a lot!

hybridherbst commented 2 years ago

Should be fixed on latest dev - would be great if you try!

davidyrgilbert commented 2 years ago

Should be fixed on latest dev - would be great if you try!

Tried it yesterday and didn't find any issues anymore, thanks a lot!