From d7ca74e5089551e6afdfd52d123c6deef6857d1c Mon Sep 17 00:00:00 2001 From: Julien Ignace Date: Wed, 15 Apr 2020 17:35:32 +0200 Subject: [PATCH 1/2] Added external references inside a material to diffusion profiles and materials in order to handle Material export to a package correctly. # Conflicts: # com.unity.render-pipelines.high-definition/Editor/Material/PBR/HDPBRLit.cs --- .../DiffusionProfileMaterialUI.cs | 12 +++- .../Material/MaterialExternalReferences.cs | 70 +++++++++++++++++++ .../MaterialExternalReferences.cs.meta | 11 +++ .../Editor/Material/PBR/HDPBRLit.cs | 3 +- .../Material/UIBlocks/LayerListUIBlock.cs | 12 +++- .../UIBlocks/LitSurfaceInputsUIBlock.cs | 2 +- .../Material/UIBlocks/ShaderGraphUIBlock.cs | 4 +- 7 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 com.unity.render-pipelines.high-definition/Editor/Material/MaterialExternalReferences.cs create mode 100644 com.unity.render-pipelines.high-definition/Editor/Material/MaterialExternalReferences.cs.meta diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/DiffusionProfile/DiffusionProfileMaterialUI.cs b/com.unity.render-pipelines.high-definition/Editor/Material/DiffusionProfile/DiffusionProfileMaterialUI.cs index 726379b1193..b879b15367f 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/DiffusionProfile/DiffusionProfileMaterialUI.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/DiffusionProfile/DiffusionProfileMaterialUI.cs @@ -18,7 +18,7 @@ public static bool IsSupported(MaterialEditor materialEditor) }); } - public static void OnGUI(MaterialProperty diffusionProfileAsset, MaterialProperty diffusionProfileHash) + public static void OnGUI(MaterialEditor materialEditor, MaterialProperty diffusionProfileAsset, MaterialProperty diffusionProfileHash, int profileIndex) { // We can't cache these fields because of several edge cases like undo/redo or pressing escape in the object picker string guid = HDUtils.ConvertVector4ToGUID(diffusionProfileAsset.vectorValue); @@ -42,6 +42,16 @@ public static void OnGUI(MaterialProperty diffusionProfileAsset, MaterialPropert // encode back GUID and it's hash diffusionProfileAsset.vectorValue = newGuid; diffusionProfileHash.floatValue = hash; + + // Update external reference. + foreach (var target in materialEditor.targets) + { + MaterialExternalReferences matExternalRefs = MaterialExternalReferences.GetMaterialExternalReferences(target as Material); + if (matExternalRefs != null) + { + matExternalRefs.SetDiffusionProfileReference(profileIndex, diffusionProfile); + } + } } DrawDiffusionProfileWarning(diffusionProfile); diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/MaterialExternalReferences.cs b/com.unity.render-pipelines.high-definition/Editor/Material/MaterialExternalReferences.cs new file mode 100644 index 00000000000..4466369d138 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Editor/Material/MaterialExternalReferences.cs @@ -0,0 +1,70 @@ +using UnityEngine; +using UnityEngine.Rendering.HighDefinition; + +namespace UnityEditor.Rendering.HighDefinition +{ + // This class only purpose is to be used as a sub-asset to a material and store references to other assets. + // The goal is to be able to export the material as a package and not miss those referenced assets. + class MaterialExternalReferences : ScriptableObject + { + [SerializeField] + DiffusionProfileSettings[] m_DiffusionProfileReferences = new DiffusionProfileSettings[0]; + [SerializeField] + Material[] m_MaterialReferences = new Material[0]; + + public void SetDiffusionProfileReference(int index, DiffusionProfileSettings profile) + { + if (index >= m_DiffusionProfileReferences.Length) + { + var newList = new DiffusionProfileSettings[index + 1]; + for (int i = 0; i < m_DiffusionProfileReferences.Length; ++i) + newList[i] = m_DiffusionProfileReferences[i]; + + m_DiffusionProfileReferences = newList; + } + + m_DiffusionProfileReferences[index] = profile; + EditorUtility.SetDirty(this); + } + + public void SetMaterialReference(int index, Material mat) + { + if (index >= m_MaterialReferences.Length) + { + var newList = new Material[index + 1]; + for (int i = 0; i < m_MaterialReferences.Length; ++i) + newList[i] = m_MaterialReferences[i]; + + m_MaterialReferences = newList; + } + + m_MaterialReferences[index] = mat; + EditorUtility.SetDirty(this); + } + + public static MaterialExternalReferences GetMaterialExternalReferences(Material material) + { + var subAssets = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(material)); + MaterialExternalReferences matExternalRefs = null; + foreach (var subAsset in subAssets) + { + if (subAsset.GetType() == typeof(MaterialExternalReferences)) + { + matExternalRefs = subAsset as MaterialExternalReferences; + break; + } + } + + if (matExternalRefs == null) + { + matExternalRefs = CreateInstance(); + matExternalRefs.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector | HideFlags.NotEditable; + AssetDatabase.AddObjectToAsset(matExternalRefs, material); + EditorUtility.SetDirty(matExternalRefs); + EditorUtility.SetDirty(material); + } + + return matExternalRefs; + } + } +} diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/MaterialExternalReferences.cs.meta b/com.unity.render-pipelines.high-definition/Editor/Material/MaterialExternalReferences.cs.meta new file mode 100644 index 00000000000..30af94f1846 --- /dev/null +++ b/com.unity.render-pipelines.high-definition/Editor/Material/MaterialExternalReferences.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: aa486462e6be1764e89c788ba30e61f7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/PBR/HDPBRLit.cs b/com.unity.render-pipelines.high-definition/Editor/Material/PBR/HDPBRLit.cs index 7fe5e89df34..81c2edf1c96 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/PBR/HDPBRLit.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/PBR/HDPBRLit.cs @@ -13,6 +13,7 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro materialEditor.LightmapEmissionFlagsProperty(MaterialEditor.kMiniTextureFieldLabelIndentLevel, true, true); } + // Make sure all selected materials are initialized. string materialTag = "MotionVector"; foreach (var obj in materialEditor.targets) @@ -42,7 +43,7 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro } if (DiffusionProfileMaterialUI.IsSupported(materialEditor)) - DiffusionProfileMaterialUI.OnGUI(FindProperty("_DiffusionProfileAsset", props), FindProperty("_DiffusionProfileHash", props)); + DiffusionProfileMaterialUI.OnGUI(materialEditor, FindProperty("_DiffusionProfileAsset", props), FindProperty("_DiffusionProfileHash", props), 0); } } } diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/LayerListUIBlock.cs b/com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/LayerListUIBlock.cs index 2053093ebfe..2db7c6e96b5 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/LayerListUIBlock.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/LayerListUIBlock.cs @@ -154,8 +154,18 @@ void DrawLayerListGUI() Undo.RecordObjects(new UnityEngine.Object[] { material, m_MaterialImporter }, "Change layer material"); LayeredLitGUI.SynchronizeLayerProperties(material, m_MaterialLayers, layerIndex, true); layersChanged = true; + + // Update external reference. + foreach (var target in materialEditor.targets) + { + MaterialExternalReferences matExternalRefs = MaterialExternalReferences.GetMaterialExternalReferences(target as Material); + if (matExternalRefs != null) + { + matExternalRefs.SetMaterialReference(layerIndex, m_MaterialLayers[layerIndex]); + } + } } - + EditorGUI.DrawRect(colorRect, kLayerColors[layerIndex]); m_WithUV[layerIndex] = EditorGUI.Toggle(uvRect, m_WithUV[layerIndex]); diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/LitSurfaceInputsUIBlock.cs b/com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/LitSurfaceInputsUIBlock.cs index 2dfda3f8b14..004228f7c63 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/LitSurfaceInputsUIBlock.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/LitSurfaceInputsUIBlock.cs @@ -557,7 +557,7 @@ void ShaderSSSAndTransmissionInputGUI() if (hdPipeline == null) return; - DiffusionProfileMaterialUI.OnGUI(diffusionProfileAsset[m_LayerIndex], diffusionProfileHash[m_LayerIndex]); + DiffusionProfileMaterialUI.OnGUI(materialEditor, diffusionProfileAsset[m_LayerIndex], diffusionProfileHash[m_LayerIndex], m_LayerIndex); // TODO: does not work with multi-selection if ((int)materialID.floatValue == (int)MaterialId.LitSSS && materials[0].GetSurfaceType() != SurfaceType.Transparent) diff --git a/com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/ShaderGraphUIBlock.cs b/com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/ShaderGraphUIBlock.cs index 0648baabce4..a7befa8a171 100644 --- a/com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/ShaderGraphUIBlock.cs +++ b/com.unity.render-pipelines.high-definition/Editor/Material/UIBlocks/ShaderGraphUIBlock.cs @@ -96,7 +96,7 @@ void DrawShaderGraphGUI() // Filter out properties we don't want to draw: PropertiesDefaultGUI(properties); - // If we change a property in a shadergraph, we trigger a material keyword reset + // If we change a property in a shadergraph, we trigger a material keyword reset if (CheckPropertyChanged(properties)) { foreach (var material in materials) @@ -221,7 +221,7 @@ void DrawShadowMatteToggle() void DrawDiffusionProfileUI() { if (DiffusionProfileMaterialUI.IsSupported(materialEditor)) - DiffusionProfileMaterialUI.OnGUI(FindProperty("_DiffusionProfileAsset"), FindProperty("_DiffusionProfileHash")); + DiffusionProfileMaterialUI.OnGUI(materialEditor, FindProperty("_DiffusionProfileAsset"), FindProperty("_DiffusionProfileHash"), 0); } } } From 4f5b426fe770d90bf486933534ae13f229ba7cb8 Mon Sep 17 00:00:00 2001 From: Julien Ignace Date: Mon, 20 Apr 2020 10:00:32 +0200 Subject: [PATCH 2/2] Update changelog --- com.unity.render-pipelines.high-definition/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index 6d5ec57997d..823168e0026 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed NaN which can appear with real time reflection and inf value - Fixed warning about missing bound decal buffer - Fix black screen in XR when HDRP package is present but not used. +- Diffusion Profile and Material references in HDRP materials are now correctly exported to unity packages. Note that the diffusion profile or the material references need to be edited once before this can work properly. ### Changed - Rejecting history for ray traced reflections based on a threshold evaluated on the neighborhood of the sampled history.