Skip to content

[Backport 8.x.x] Diffusion Profile and Material references in HDRP materials are now correctly exported to unity packages. #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed an issue that was collapsing the volume components in the HDRP default settings
- Fixed warning about missing bound decal buffer
- Fixed the debug exposure mode for display sky reflection and debug view baked lighting
- 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MaterialExternalReferences>();
matExternalRefs.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector | HideFlags.NotEditable;
AssetDatabase.AddObjectToAsset(matExternalRefs, material);
EditorUtility.SetDirty(matExternalRefs);
EditorUtility.SetDirty(material);
}

return matExternalRefs;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
}