Skip to content

Commit

Permalink
Diffusion Profile and Material references in HDRP materials are now c…
Browse files Browse the repository at this point in the history
…orrectly exported to unity packages. (#136)

* Added external references inside a material to diffusion profiles and materials in order to handle Material export to a package correctly.

* Update changelog

* Removed useless test

* Updated changelog message to add more info
  • Loading branch information
JulienIgnace-Unity committed Apr 20, 2020
1 parent 3b168b4 commit 7c3662d
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 7 deletions.
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 @@ -548,6 +548,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed shader warning on Xbox for ResolveStencilBuffer.compute.
- Fixed PBR shader ZTest rendering in deferred.
- Replaced commands incompatible with async compute in light list build process.
- 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
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,13 @@ 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);
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,15 @@ 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);
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);
}
}
}

0 comments on commit 7c3662d

Please sign in to comment.