From 361823b8ef9db8dfd08bdfb81c3270563b5805cd Mon Sep 17 00:00:00 2001 From: Julien Ignace Date: Wed, 15 Apr 2020 17:35:32 +0200 Subject: [PATCH 1/4] Added external references inside a material to diffusion profiles and materials in order to handle Material export to a package correctly. --- .../DiffusionProfileMaterialUI.cs | 12 +++- .../Material/MaterialExternalReferences.cs | 70 +++++++++++++++++++ .../MaterialExternalReferences.cs.meta | 11 +++ .../Editor/Material/PBR/HDPBRLit.cs | 4 +- .../Material/UIBlocks/LayerListUIBlock.cs | 12 +++- .../UIBlocks/LitSurfaceInputsUIBlock.cs | 2 +- .../Material/UIBlocks/ShaderGraphUIBlock.cs | 4 +- 7 files changed, 108 insertions(+), 7 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 8e793ffd2d4..5ebe1d51237 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 @@ -8,7 +8,7 @@ class HDPBRLitGUI : ShaderGUI public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props) { materialEditor.PropertiesDefaultGUI(props); - + EmissionUIBlock.BakedEmissionEnabledProperty(materialEditor); // Make sure all selected materials are initialized. @@ -40,7 +40,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 12b758968ec..5111809ebd0 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 @@ -97,7 +97,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) @@ -219,7 +219,7 @@ void DrawShadowMatteToggle() void DrawDiffusionProfileUI() { if (DiffusionProfileMaterialUI.IsSupported(materialEditor)) - DiffusionProfileMaterialUI.OnGUI(FindProperty("_DiffusionProfileAsset"), FindProperty("_DiffusionProfileHash")); + DiffusionProfileMaterialUI.OnGUI(materialEditor, FindProperty("_DiffusionProfileAsset"), FindProperty("_DiffusionProfileHash"), 0); } } } From bbb7001415187d92912c7d20930d0023d17f2e6f Mon Sep 17 00:00:00 2001 From: Julien Ignace Date: Wed, 15 Apr 2020 17:36:32 +0200 Subject: [PATCH 2/4] 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 38855887f58..147e0d9b587 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -528,6 +528,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed MSAA depth resolve when there is no motion vectors - Fixed various object leaks in HDRP. - Fixed compile error with XR SubsystemManager. +- Diffusion Profile and Material references in HDRP materials are now correctly exported to unity packages. ### Changed - Color buffer pyramid is not allocated anymore if neither refraction nor distortion are enabled From 2b6bee407425ae3a8c07a5de1b78388e17f5f99d Mon Sep 17 00:00:00 2001 From: Julien Ignace Date: Wed, 15 Apr 2020 17:42:52 +0200 Subject: [PATCH 3/4] Removed useless test --- .../Material/DiffusionProfile/DiffusionProfileMaterialUI.cs | 5 +---- .../Editor/Material/UIBlocks/LayerListUIBlock.cs | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) 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 b879b15367f..5e179d4d04d 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 @@ -47,10 +47,7 @@ public static void OnGUI(MaterialEditor materialEditor, MaterialProperty diffusi foreach (var target in materialEditor.targets) { MaterialExternalReferences matExternalRefs = MaterialExternalReferences.GetMaterialExternalReferences(target as Material); - if (matExternalRefs != null) - { - matExternalRefs.SetDiffusionProfileReference(profileIndex, diffusionProfile); - } + matExternalRefs.SetDiffusionProfileReference(profileIndex, diffusionProfile); } } 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 2db7c6e96b5..caf388419b6 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 @@ -159,10 +159,7 @@ void DrawLayerListGUI() foreach (var target in materialEditor.targets) { MaterialExternalReferences matExternalRefs = MaterialExternalReferences.GetMaterialExternalReferences(target as Material); - if (matExternalRefs != null) - { - matExternalRefs.SetMaterialReference(layerIndex, m_MaterialLayers[layerIndex]); - } + matExternalRefs.SetMaterialReference(layerIndex, m_MaterialLayers[layerIndex]); } } From 030ba09ea5bf6b6575a370a5a5a4301fe88a69ba Mon Sep 17 00:00:00 2001 From: JulienIgnace-Unity Date: Thu, 16 Apr 2020 13:02:27 +0200 Subject: [PATCH 4/4] Updated changelog message to add more info --- com.unity.render-pipelines.high-definition/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index 147e0d9b587..c16b70de8eb 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -528,7 +528,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed MSAA depth resolve when there is no motion vectors - Fixed various object leaks in HDRP. - Fixed compile error with XR SubsystemManager. -- Diffusion Profile and Material references in HDRP materials are now correctly exported to unity packages. +- 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 - Color buffer pyramid is not allocated anymore if neither refraction nor distortion are enabled