From de8dc44f8a4b80872e5288adea4e5a9d6f1b6652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvar=20O=CC=88rn=20Unn=C3=BEo=CC=81rsson?= Date: Fri, 15 May 2020 18:55:33 +0200 Subject: [PATCH] Backport of #339 --- .../CHANGELOG.md | 1 + .../FBXMaterialDescriptionPreprocessor.cs | 6 +++-- ...ketchupMaterialDescriptionPostprocessor.cs | 4 ++- ...ThreeDSMaterialDescriptionPostprocessor.cs | 4 ++- .../Data/UniversalRenderPipelineAsset.cs | 13 ++++++++-- .../Runtime/ShaderUtils.cs | 26 +++++++++++++++++++ .../Tests/Editor/EditorTests.cs | 18 +++++++++++++ 7 files changed, 66 insertions(+), 6 deletions(-) diff --git a/com.unity.render-pipelines.universal/CHANGELOG.md b/com.unity.render-pipelines.universal/CHANGELOG.md index 450b2a61e9c..f4bd80277a6 100644 --- a/com.unity.render-pipelines.universal/CHANGELOG.md +++ b/com.unity.render-pipelines.universal/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed issues with NullReferenceException happening with URP shaders - Fixed an issue where the emission value in particle shaders would not update in the editor without entering playmode. - Fixed SceneView Draw Modes not being properly updated after opening new scene view panels or changing the editor layout. +- Fixed issue with Model Importer materials using the Legacy standard shader instead of URP's Lit shader when import happens at Editor startup. ## [8.1.0] - 2020-04-21 diff --git a/com.unity.render-pipelines.universal/Editor/AssetPostProcessors/FBXMaterialDescriptionPreprocessor.cs b/com.unity.render-pipelines.universal/Editor/AssetPostProcessors/FBXMaterialDescriptionPreprocessor.cs index 17d7cb1caf2..0fd765eaa2d 100644 --- a/com.unity.render-pipelines.universal/Editor/AssetPostProcessors/FBXMaterialDescriptionPreprocessor.cs +++ b/com.unity.render-pipelines.universal/Editor/AssetPostProcessors/FBXMaterialDescriptionPreprocessor.cs @@ -1,6 +1,7 @@ using System.IO; using UnityEditor.AssetImporters; using UnityEngine; +using UnityEngine.Rendering.Universal; namespace UnityEditor.Rendering.Universal { @@ -23,8 +24,9 @@ public void OnPreprocessMaterialDescription(MaterialDescription description, Mat var lowerCaseExtension = Path.GetExtension(assetPath).ToLower(); if (lowerCaseExtension != ".fbx" && lowerCaseExtension != ".obj" && lowerCaseExtension != ".blend" && lowerCaseExtension != ".mb" && lowerCaseExtension != ".ma" && lowerCaseExtension != ".max") return; - - var shader = Shader.Find("Universal Render Pipeline/Lit"); + + string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(ShaderPathID.Lit)); + var shader = AssetDatabase.LoadAssetAtPath(path); if (shader == null) return; diff --git a/com.unity.render-pipelines.universal/Editor/AssetPostProcessors/SketchupMaterialDescriptionPostprocessor.cs b/com.unity.render-pipelines.universal/Editor/AssetPostProcessors/SketchupMaterialDescriptionPostprocessor.cs index 3045e02db8e..51f52120a0b 100644 --- a/com.unity.render-pipelines.universal/Editor/AssetPostProcessors/SketchupMaterialDescriptionPostprocessor.cs +++ b/com.unity.render-pipelines.universal/Editor/AssetPostProcessors/SketchupMaterialDescriptionPostprocessor.cs @@ -1,6 +1,7 @@ using System.IO; using UnityEngine; using UnityEditor.AssetImporters; +using UnityEngine.Rendering.Universal; namespace UnityEditor.Rendering.Universal { @@ -25,7 +26,8 @@ public void OnPreprocessMaterialDescription(MaterialDescription description, Mat if (lowerCasePath != ".skp") return; - var shader = Shader.Find("Universal Render Pipeline/Lit"); + string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(ShaderPathID.Lit)); + var shader = AssetDatabase.LoadAssetAtPath(path); if (shader == null) return; material.shader = shader; diff --git a/com.unity.render-pipelines.universal/Editor/AssetPostProcessors/ThreeDSMaterialDescriptionPostprocessor.cs b/com.unity.render-pipelines.universal/Editor/AssetPostProcessors/ThreeDSMaterialDescriptionPostprocessor.cs index 6320dca1b72..5a51e477ed5 100644 --- a/com.unity.render-pipelines.universal/Editor/AssetPostProcessors/ThreeDSMaterialDescriptionPostprocessor.cs +++ b/com.unity.render-pipelines.universal/Editor/AssetPostProcessors/ThreeDSMaterialDescriptionPostprocessor.cs @@ -2,6 +2,7 @@ using System.IO; using UnityEngine; using UnityEditor.AssetImporters; +using UnityEngine.Rendering.Universal; namespace UnityEditor.Rendering.Universal { @@ -26,7 +27,8 @@ public void OnPreprocessMaterialDescription(MaterialDescription description, Mat if (lowerCasePath != ".3ds") return; - var shader = Shader.Find("Universal Render Pipeline/Lit"); + string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(ShaderPathID.Lit)); + var shader = AssetDatabase.LoadAssetAtPath(path); if (shader == null) return; material.shader = shader; diff --git a/com.unity.render-pipelines.universal/Runtime/Data/UniversalRenderPipelineAsset.cs b/com.unity.render-pipelines.universal/Runtime/Data/UniversalRenderPipelineAsset.cs index c5f5ceda457..ed9adb51771 100644 --- a/com.unity.render-pipelines.universal/Runtime/Data/UniversalRenderPipelineAsset.cs +++ b/com.unity.render-pipelines.universal/Runtime/Data/UniversalRenderPipelineAsset.cs @@ -4,8 +4,10 @@ using UnityEditor; using UnityEditor.ProjectWindowCallback; using System.IO; +using UnityEditorInternal; #endif using System.ComponentModel; +using System.Linq; namespace UnityEngine.Rendering.LWRP { @@ -255,9 +257,10 @@ UniversalRenderPipelineEditorResources editorResources { if (m_EditorResourcesAsset != null && !m_EditorResourcesAsset.Equals(null)) return m_EditorResourcesAsset; - + string resourcePath = AssetDatabase.GUIDToAssetPath(editorResourcesGUID); - m_EditorResourcesAsset = AssetDatabase.LoadAssetAtPath(resourcePath); + var objs = InternalEditorUtility.LoadSerializedFileAndForget(resourcePath); + m_EditorResourcesAsset = objs != null && objs.Length > 0 ? objs.First() as UniversalRenderPipelineEditorResources : null; return m_EditorResourcesAsset; } } @@ -674,6 +677,12 @@ public override Shader defaultShader if (defaultShader != null) return defaultShader; } + + if (m_DefaultShader == null) + { + string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(ShaderPathID.Lit)); + m_DefaultShader = AssetDatabase.LoadAssetAtPath(path); + } #endif if (m_DefaultShader == null) diff --git a/com.unity.render-pipelines.universal/Runtime/ShaderUtils.cs b/com.unity.render-pipelines.universal/Runtime/ShaderUtils.cs index 0ae2ecd96a8..01149ae8fae 100644 --- a/com.unity.render-pipelines.universal/Runtime/ShaderUtils.cs +++ b/com.unity.render-pipelines.universal/Runtime/ShaderUtils.cs @@ -53,5 +53,31 @@ public static bool IsLWShader(Shader shader) { return s_ShaderPaths.Contains(shader.name); } + +#if UNITY_EDITOR + static readonly string[] s_ShaderGUIDs = + { + "933532a4fcc9baf4fa0491de14d08ed7", + "8d2bb70cbf9db8d4da26e15b26e74248", + "650dd9526735d5b46b79224bc6e94025", + "69c1f799e772cb6438f56c23efccb782", + "b7839dad95683814aa64166edc107ae2", + "8516d7a69675844a7a0b7095af7c46af", + "0406db5a14f94604a8c57ccfbc9f3b46", + "0ca6dca7396eb48e5849247ffd444914", + }; + + internal static string GetShaderGUID(ShaderPathID id) + { + int index = (int)id; + if (index < 0 && index >= (int)ShaderPathID.Count) + { + Debug.LogError("Trying to access universal shader path out of bounds"); + return ""; + } + + return s_ShaderGUIDs[index]; + } +#endif } } diff --git a/com.unity.render-pipelines.universal/Tests/Editor/EditorTests.cs b/com.unity.render-pipelines.universal/Tests/Editor/EditorTests.cs index 9bfa9a37c7a..dc5e2bce579 100644 --- a/com.unity.render-pipelines.universal/Tests/Editor/EditorTests.cs +++ b/com.unity.render-pipelines.universal/Tests/Editor/EditorTests.cs @@ -64,6 +64,24 @@ public void ValidateBuiltinResourceFiles() Assert.IsFalse(string.IsNullOrEmpty(editorResourcesPath)); } + // Validate that ShaderUtils.GetShaderGUID results are valid and that ShaderUtils.GetShaderPath match shader names. + [TestCase(ShaderPathID.Lit)] + [TestCase(ShaderPathID.SimpleLit)] + [TestCase(ShaderPathID.Unlit)] + [TestCase(ShaderPathID.TerrainLit)] + [TestCase(ShaderPathID.ParticlesLit)] + [TestCase(ShaderPathID.ParticlesSimpleLit)] + [TestCase(ShaderPathID.ParticlesUnlit)] + [TestCase(ShaderPathID.BakedLit)] + public void ValidateShaderResources(ShaderPathID shaderPathID) + { + string path = AssetDatabase.GUIDToAssetPath(ShaderUtils.GetShaderGUID(shaderPathID)); + Assert.IsFalse(string.IsNullOrEmpty(path)); + + var shader = AssetDatabase.LoadAssetAtPath(path); + Assert.AreEqual(shader.name, ShaderUtils.GetShaderPath(shaderPathID)); + } + // When creating URP all required resources should be initialized. [Test] public void ValidateNewAssetResources()