Skip to content

Generate a material as a subasset for ShaderGraphs #5795

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 6 commits into from
Oct 19, 2021
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.shadergraph/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Adding ability to automatically cast Bools to Vector types in ShaderGraph [1359160]
- Added ShaderGraph import warning to old nodes and properties, and ability to dismiss the warning if old behavior is desired.
- Added normal transforms to the Transform node
- Added an automatically generated material subasset on ShaderGraphs.

### Changed
- Changed the title suffix on old nodes and properties rom "Deprecated" to "Legacy".
Expand Down
19 changes: 19 additions & 0 deletions com.unity.shadergraph/Editor/Data/Implementation/GraphObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ public GraphData graph
}
}

Material m_MaterialArtifact;

/// <summary>
/// The Material artifact generated by the import process.
/// Every modification to this material will be lost when the graph is saved.
/// for.
/// </summary>
public Material materialArtifact
{
get
{
if (m_MaterialArtifact == null)
m_MaterialArtifact = AssetDatabase.LoadAssetAtPath<Material>(AssetDatabase.GUIDToAssetPath(AssetGuid));
return m_MaterialArtifact;
}
}

// this value stores whether an undo operation has been registered (which indicates a change has been made to the graph)
// and is used to trigger the MaterialGraphEditWindow to update it's title
public bool isDirty
Expand All @@ -85,6 +102,8 @@ public bool isDirty
public virtual void RegisterCompleteObjectUndo(string actionName)
{
Undo.RegisterCompleteObjectUndo(this, actionName);
if (materialArtifact)
Undo.RecordObject(m_MaterialArtifact, actionName);
m_SerializedVersion++;
m_DeserializedVersion++;
m_IsDirty = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,48 @@ void ChangePropertyValue(GraphData graphData)
AssertHelpers.IsNotNull(shaderInputReference, "ShaderPropertyReference is null while carrying out ChangePropertyValueAction");
// The Undos are currently handled in ShaderInputPropertyDrawer but we want to move that out from there and handle here
//graphData.owner.RegisterCompleteObjectUndo("Change Property Value");
var material = graphData.owner.materialArtifact;
switch (shaderInputReference)
{
case BooleanShaderProperty booleanProperty:
booleanProperty.value = ((ToggleData)newShaderInputValue).isOn;
if (material) material.SetFloat(shaderInputReference.referenceName, booleanProperty.value ? 1.0f : 0.0f);
break;
case Vector1ShaderProperty vector1Property:
vector1Property.value = (float)newShaderInputValue;
if (material) material.SetFloat(shaderInputReference.referenceName, vector1Property.value);
break;
case Vector2ShaderProperty vector2Property:
vector2Property.value = (Vector2)newShaderInputValue;
if (material) material.SetVector(shaderInputReference.referenceName, vector2Property.value);
break;
case Vector3ShaderProperty vector3Property:
vector3Property.value = (Vector3)newShaderInputValue;
if (material) material.SetVector(shaderInputReference.referenceName, vector3Property.value);
break;
case Vector4ShaderProperty vector4Property:
vector4Property.value = (Vector4)newShaderInputValue;
if (material) material.SetVector(shaderInputReference.referenceName, vector4Property.value);
break;
case ColorShaderProperty colorProperty:
colorProperty.value = (Color)newShaderInputValue;
if (material) material.SetColor(shaderInputReference.referenceName, colorProperty.value);
break;
case Texture2DShaderProperty texture2DProperty:
texture2DProperty.value.texture = (Texture)newShaderInputValue;
if (material) material.SetTexture(shaderInputReference.referenceName, texture2DProperty.value.texture);
break;
case Texture2DArrayShaderProperty texture2DArrayProperty:
texture2DArrayProperty.value.textureArray = (Texture2DArray)newShaderInputValue;
if (material) material.SetTexture(shaderInputReference.referenceName, texture2DArrayProperty.value.textureArray);
break;
case Texture3DShaderProperty texture3DProperty:
texture3DProperty.value.texture = (Texture3D)newShaderInputValue;
if (material) material.SetTexture(shaderInputReference.referenceName, texture3DProperty.value.texture);
break;
case CubemapShaderProperty cubemapProperty:
cubemapProperty.value.cubemap = (Cubemap)newShaderInputValue;
if (material) material.SetTexture(shaderInputReference.referenceName, cubemapProperty.value.cubemap);
break;
case Matrix2ShaderProperty matrix2Property:
matrix2Property.value = (Matrix4x4)newShaderInputValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,11 @@ void BuildBooleanKeywordField(PropertySheet propertySheet, ShaderKeyword keyword
{
this._preChangeValueCallback("Change property value");
keyword.value = newValue.isOn ? 1 : 0;
if (graphData.owner.materialArtifact)
{
graphData.owner.materialArtifact.SetFloat(keyword.referenceName, keyword.value);
MaterialEditor.ApplyMaterialPropertyDrawers(graphData.owner.materialArtifact);
}
this._postChangeValueCallback(false, ModificationScope.Graph);
},
new ToggleData(keyword.value == 1),
Expand All @@ -1345,6 +1350,11 @@ void BuildEnumKeywordField(PropertySheet propertySheet, ShaderKeyword keyword)
{
this._preChangeValueCallback("Change Keyword Value");
keyword.value = field.index;
if (graphData.owner.materialArtifact)
{
graphData.owner.materialArtifact.SetFloat(keyword.referenceName, field.index);
MaterialEditor.ApplyMaterialPropertyDrawers(graphData.owner.materialArtifact);
}
this._postChangeValueCallback(false, ModificationScope.Graph);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,14 @@ void OnDestroy()
Undo.ClearUndo(graphObject);
}

// Discard any unsaved modification on the generated material
if (graphObject && graphObject.materialArtifact && EditorUtility.IsDirty(graphObject.materialArtifact))
{
var material = new Material(AssetDatabase.LoadAssetAtPath<Shader>(AssetDatabase.GUIDToAssetPath(graphObject.AssetGuid)));
graphObject.materialArtifact.CopyPropertiesFromMaterial(material);
CoreUtils.Destroy(material);
}

graphObject = null;
graphEditorView = null;

Expand Down
6 changes: 6 additions & 0 deletions com.unity.shadergraph/Editor/Importers/ShaderGraphImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ public override void OnImportAsset(AssetImportContext ctx)
}
#endif

if (shader != null)
{
Material material = new Material(shader) { name = Path.GetFileNameWithoutExtension(path) + " Material" };
ctx.AddObjectToAsset("Material", material);
}

Texture2D texture = Resources.Load<Texture2D>("Icons/sg_graph_icon");
ctx.AddObjectToAsset("MainAsset", mainObject, texture);
ctx.SetMainObject(mainObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace UnityEditor.ShaderGraph
class ShaderGraphImporterEditor : ScriptedImporterEditor
{
protected override bool needsApplyRevert => false;
MaterialEditor materialEditor = null;

public override void OnInspectorGUI()
{
Expand Down Expand Up @@ -115,6 +116,30 @@ GraphData GetGraphData(AssetImporter importer)
}

ApplyRevertGUI();

if (materialEditor)
{
EditorGUILayout.Space();
materialEditor.DrawHeader();
using (new EditorGUI.DisabledGroupScope(true))
materialEditor.OnInspectorGUI();
}
}

public override void OnEnable()
{
base.OnEnable();
AssetImporter importer = target as AssetImporter;
var material = AssetDatabase.LoadAssetAtPath<Material>(importer.assetPath);
if (material)
materialEditor = (MaterialEditor)CreateEditor(material);
}

public override void OnDisable()
{
base.OnDisable();
if (materialEditor != null)
DestroyImmediate(materialEditor);
}

internal static bool ShowGraphEditWindow(string path)
Expand Down