Skip to content

Commit

Permalink
live preview
Browse files Browse the repository at this point in the history
  • Loading branch information
z3y committed Sep 15, 2024
1 parent 4d20328 commit fb0d95d
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 49 deletions.
9 changes: 9 additions & 0 deletions Editor/Generation/PropertyDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,15 @@ public void UpdatePreviewMaterial()
return;
}
Material m = graphView.PreviewMaterial;
UpdatePreviewMaterial(m);

if (graphView.ImportedMaterial != null)
{
UpdatePreviewMaterial(graphView.ImportedMaterial);
}
}
public void UpdatePreviewMaterial(Material m)
{
string name = GetReferenceName(GenerationMode.Preview);
if (type == PropertyType.Float || type == PropertyType.Bool) m.SetFloat(name, FloatValue);
else if (type == PropertyType.Float2 || type == PropertyType.Float3 || type == PropertyType.Float4) m.SetVector(name, VectorValue);
Expand Down
20 changes: 17 additions & 3 deletions Editor/Generation/ShaderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ public class ShaderBuilder
{


public ShaderBuilder(GenerationMode generationMode, ShaderGraphView shaderGraphView, BuildTarget target = BuildTarget.StandaloneWindows64)
public ShaderBuilder(GenerationMode generationMode, ShaderGraphView shaderGraphView, BuildTarget target = BuildTarget.StandaloneWindows64, bool unlocked = false)
{
GenerationMode = generationMode;
ShaderGraphView = shaderGraphView;

var data = shaderGraphView.graphData;
shaderName = data.shaderName;
BuildTarget = target;
this.unlocked = unlocked;
}

private ShaderStringBuilder _sb;

public bool unlocked = false;
public BuildTarget BuildTarget { get; private set; }
public string shaderName;
public string fallback;
Expand Down Expand Up @@ -385,10 +386,23 @@ private void AppendProperties()
{
if (GenerationMode == GenerationMode.Preview)
{
var allProperties = passBuilders.SelectMany(x => x.properties);
var allProperties = passBuilders.SelectMany(x => x.properties).ToList();

if (unlocked)
{
foreach (var property in properties.Union(ShaderGraphView.graphData.properties).Union(subgraphProperties).Distinct())
{
if (property.ShouldDeclare())
_sb.AppendLine(property.GetPropertyDeclaration(GenerationMode.Final));
}
}

foreach (var property in allProperties)
{
if (unlocked && property.IsTextureType)
{
continue;
}
if (property.ShouldDeclare())
_sb.AppendLine(property.GetPropertyDeclaration(GenerationMode.Preview));
}
Expand Down
5 changes: 3 additions & 2 deletions Editor/Importer/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public override void OnImportAsset(AssetImportContext ctx)
//else if (graphView is null)
//{
var data = ReadGraphData(guid);
var graphView = new ShaderGraphView(null);
var graphView = new ShaderGraphView(null, assetPath);
data.PopulateGraph(graphView);
//}

var filename = Path.GetFileNameWithoutExtension(ctx.assetPath);
var builder = new ShaderBuilder(GenerationMode.Final, graphView, target);
bool unlocked = graphView.graphData.unlocked;
var builder = new ShaderBuilder(unlocked ? GenerationMode.Preview : GenerationMode.Final, graphView, target, unlocked);
if (string.IsNullOrEmpty(builder.shaderName) || builder.shaderName == "Default Shader")
{
builder.shaderName = "Graphlit/" + filename;
Expand Down
1 change: 1 addition & 0 deletions Editor/Serialization/GraphData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class GraphData
public OutlinePassMode outlinePass = OutlinePassMode.Disabled;
public bool stencil = false;
public VRCFallbackTags vrcFallbackTags = new VRCFallbackTags();
public bool unlocked = false;

//public List<SerializablePortDescriptor> subgraphOutputs = new();
//public List<SerializablePortDescriptor> subgraphInputs = new();
Expand Down
25 changes: 24 additions & 1 deletion Editor/ShaderGraphView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,25 @@ public class ShaderGraphView : GraphView
hideFlags = HideFlags.HideAndDontSave
};

public ShaderGraphView(ShaderGraphWindow editorWindow)
Material _importedMaterial;
public Material ImportedMaterial
{
get
{
if (_importedMaterial == null)
{
Debug.Log("search");
_importedMaterial = AssetDatabase.LoadAllAssetRepresentationsAtPath(_assetPath).OfType<Material>().FirstOrDefault();
}
return _importedMaterial;
}
}

string _assetPath;
public ShaderGraphView(ShaderGraphWindow editorWindow, string assetPath)
{
_editorWindow = editorWindow;
_assetPath = assetPath;
// manipulators
SetupZoom(0.1f, 10.0f);
this.AddManipulator(new ContentDragger());
Expand Down Expand Up @@ -60,6 +76,13 @@ public ShaderGraphView(ShaderGraphWindow editorWindow)

RegisterCallback<DragUpdatedEvent>(OnDragUpdated);
RegisterCallback<DragPerformEvent>(OnDragPerform);

if (!string.IsNullOrEmpty(assetPath))
{
var assets = AssetDatabase.LoadAllAssetRepresentationsAtPath(assetPath);
//i//mportedMaterial = assets.OfType<Material>().FirstOrDefault();
//Debug.Log(importedMaterial);
}
}

private GraphViewChange OnGraphViewChanged(GraphViewChange change)
Expand Down
55 changes: 42 additions & 13 deletions Editor/ShaderGraphWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

Expand All @@ -21,6 +22,8 @@ public class ShaderGraphWindow : EditorWindow

public void Initialize(string importerGuid, bool focus = true)
{
this.importerGuid = importerGuid;

//_importer = (ShaderGraphImporter)AssetImporter.GetAtPath(AssetDatabase.AssetPathToGUID(importerGuid));

AddStyleVariables();
Expand Down Expand Up @@ -54,7 +57,6 @@ public void Initialize(string importerGuid, bool focus = true)
};

editorInstances[importerGuid] = this;
this.importerGuid = importerGuid;
hasUnsavedChanges = false;

//rootVisualElement.Add(conainer);
Expand Down Expand Up @@ -92,11 +94,11 @@ private void OnDisable()

public void AddBar(VisualElement visualElement)
{
var toolbar = new VisualElement()
/*var toolbar = new VisualElement()
{
style = {
flexDirection = FlexDirection.Column,
width = 100,
width = 120,
backgroundColor = new Color(0.1f, 0.1f, 0.1f),
marginTop = 4,
marginLeft = 4
Expand All @@ -105,25 +107,39 @@ public void AddBar(VisualElement visualElement)
toolbar.style.SetBorderRadius(8);
toolbar.style.SetPadding(2);
toolbar.style.paddingTop = 4;
toolbar.style.paddingBottom = 4;
toolbar.style.paddingBottom = 4;*/

var toolbar = new Toolbar()
{
style = {
justifyContent = Justify.SpaceBetween,
alignItems = Align.Center,
height = 22,
}
};

var saveButton = new Button() { text = "Save", style = { height = 32 } };
var left = new VisualElement() { style = { flexDirection = FlexDirection.Row } };
toolbar.Add(left);
var right = new VisualElement() { style = { flexDirection = FlexDirection.Row } };
toolbar.Add(right);


var saveButton = new ToolbarButton() { text = "Save Asset", style = { marginRight = 4 } };
saveButton.clicked += SaveChanges;
toolbar.Add(saveButton);
left.Add(saveButton);


var pingAsset = new Button() { text = "Select Asset", style = { height = 24 } };
var pingAsset = new ToolbarButton() { text = "Select Asset", style = { marginRight = 4 } };
pingAsset.clicked += () =>
{
var assetPath = AssetDatabase.GUIDToAssetPath(importerGuid);
var obj = AssetDatabase.LoadAssetAtPath(assetPath, typeof(UnityEngine.Object));
//EditorGUIUtility.PingObject(obj);
EditorGUIUtility.PingObject(obj);
Selection.activeObject = obj;
};
toolbar.Add(pingAsset);
right.Add(pingAsset);

var selectMasterNode = new Button() { text = "Master Node", style = { height = 24 } };
var selectMasterNode = new ToolbarButton() { text = "Master Node", style = { marginRight = 4 } };
selectMasterNode.clicked += () =>
{
//var masterNode = graphView.graphElements.Where(x => x is TemplateOutput || x is SubgraphOutputNode).First();
Expand All @@ -139,7 +155,20 @@ public void AddBar(VisualElement visualElement)
masterNode.Focus();
}
};
toolbar.Add(selectMasterNode);
right.Add(selectMasterNode);

var unlocked = new Toggle("Live Preview") {
value = graphView.graphData.unlocked,
tooltip = "Temporarly convert constants to properties and update them live on the imported material",
};
var unlockedLabel = unlocked.Q<Label>();
unlockedLabel.style.minWidth = 60;

unlocked.RegisterValueChangedCallback(x =>
{
graphView.graphData.unlocked = x.newValue;
});
left.Add(unlocked);

visualElement.Add(toolbar);
}
Expand All @@ -149,7 +178,7 @@ private VisualElement GetNodePropertiesElement()
var properties = new VisualElement();
var style = properties.style;
style.width = 400;
style.paddingTop = 6;
style.paddingTop = 25;
//style.paddingLeft = 5;
//style.paddingRight = 6;

Expand All @@ -168,7 +197,7 @@ public void AddStyleVariables()

public void AddGraphView(VisualElement visualElement)
{
var graphView = new ShaderGraphView(this);
var graphView = new ShaderGraphView(this, AssetDatabase.GUIDToAssetPath(importerGuid));
graphView.StretchToParentSize();

var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>(ROOT + "Styles/GraphViewStyles.uss");
Expand Down
11 changes: 6 additions & 5 deletions Editor/ShaderNode/Nodes/Constants/BooleanConstantNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class BooleanConstantNode : ShaderNode
const int OUT = 0;
[SerializeField] private bool _value = false;

PropertyDescriptor _descriptor = new(PropertyType.Bool);
PropertyDescriptor _descriptor;
PropertyDescriptor Descriptor => _descriptor ??= new(PropertyType.Bool) { guid = viewDataKey };

public override bool DisablePreview => true;
public override void Initialize()
Expand All @@ -23,7 +24,7 @@ public override void Initialize()

onUpdatePreviewMaterial += (mat) =>
{
mat.SetFloat(_descriptor.GetReferenceName(GenerationMode.Preview), _value ? 1 : 0);
mat.SetFloat(Descriptor.GetReferenceName(GenerationMode.Preview), _value ? 1 : 0);
};

var f = new Toggle() { value = _value };
Expand All @@ -41,9 +42,9 @@ protected override void Generate(NodeVisitor visitor)
{
if (visitor.GenerationMode == GenerationMode.Preview)
{
_descriptor.FloatValue = _value ? 1 : 0;
visitor.AddProperty(_descriptor);
PortData[OUT] = new GeneratedPortData(new Bool(), _descriptor.GetReferenceName(GenerationMode.Preview));
Descriptor.FloatValue = _value ? 1 : 0;
visitor.AddProperty(Descriptor);
PortData[OUT] = new GeneratedPortData(new Bool(), Descriptor.GetReferenceName(GenerationMode.Preview));
}
else
{
Expand Down
11 changes: 6 additions & 5 deletions Editor/ShaderNode/Nodes/Constants/ColorConstantNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class ColorNode : ShaderNode
const int OUT = 0;
[SerializeField] private Vector4 _value = Vector4.one;

PropertyDescriptor _descriptor = new(PropertyType.Color);
PropertyDescriptor _descriptor;
PropertyDescriptor Descriptor => _descriptor ??= new(PropertyType.Color) { guid = viewDataKey };

public override bool DisablePreview => true;
public override void Initialize()
Expand All @@ -23,7 +24,7 @@ public override void Initialize()

onUpdatePreviewMaterial += (mat) =>
{
mat.SetColor(_descriptor.GetReferenceName(GenerationMode.Preview), _value);
mat.SetColor(Descriptor.GetReferenceName(GenerationMode.Preview), _value);
};

var f = new ColorField() { value = _value };
Expand All @@ -41,9 +42,9 @@ protected override void Generate(NodeVisitor visitor)
{
if (visitor.GenerationMode == GenerationMode.Preview)
{
_descriptor.VectorValue = _value;
visitor.AddProperty(_descriptor);
PortData[OUT] = new GeneratedPortData(new Float(4), _descriptor.GetReferenceName(GenerationMode.Preview));
Descriptor.VectorValue = _value;
visitor.AddProperty(Descriptor);
PortData[OUT] = new GeneratedPortData(new Float(4), Descriptor.GetReferenceName(GenerationMode.Preview));
}
else
{
Expand Down
11 changes: 6 additions & 5 deletions Editor/ShaderNode/Nodes/Constants/Float2ConstantNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class Float2Node : ShaderNode
const int OUT = 0;
[SerializeField] private Vector2 _value;

PropertyDescriptor _descriptor = new(PropertyType.Float2);
PropertyDescriptor _descriptor;
PropertyDescriptor Descriptor => _descriptor ??= new(PropertyType.Float2) { guid = viewDataKey };

public override bool DisablePreview => true;
public override void Initialize()
Expand All @@ -22,7 +23,7 @@ public override void Initialize()

onUpdatePreviewMaterial += (mat) =>
{
mat.SetVector(_descriptor.GetReferenceName(GenerationMode.Preview), _value);
mat.SetVector(Descriptor.GetReferenceName(GenerationMode.Preview), _value);
};

var f = new Vector2Field() { value = _value };
Expand All @@ -39,9 +40,9 @@ protected override void Generate(NodeVisitor visitor)
{
if (visitor.GenerationMode == GenerationMode.Preview)
{
_descriptor.VectorValue = _value;
visitor.AddProperty(_descriptor);
PortData[OUT] = new GeneratedPortData(new Float(2), _descriptor.GetReferenceName(GenerationMode.Preview));
Descriptor.VectorValue = _value;
visitor.AddProperty(Descriptor);
PortData[OUT] = new GeneratedPortData(new Float(2), Descriptor.GetReferenceName(GenerationMode.Preview));
}
else
{
Expand Down
11 changes: 6 additions & 5 deletions Editor/ShaderNode/Nodes/Constants/Float3ConstantNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class Float3Node : ShaderNode
const int OUT = 0;
[SerializeField] private Vector3 _value;

PropertyDescriptor _descriptor = new(PropertyType.Float3);
PropertyDescriptor _descriptor;
PropertyDescriptor Descriptor => _descriptor ??= new(PropertyType.Float3) { guid = viewDataKey };

public override bool DisablePreview => true;
public override void Initialize()
Expand All @@ -22,7 +23,7 @@ public override void Initialize()

onUpdatePreviewMaterial += (mat) =>
{
mat.SetVector(_descriptor.GetReferenceName(GenerationMode.Preview), _value);
mat.SetVector(Descriptor.GetReferenceName(GenerationMode.Preview), _value);
};

var f = new Vector3Field() { value = _value };
Expand All @@ -39,9 +40,9 @@ protected override void Generate(NodeVisitor visitor)
{
if (visitor.GenerationMode == GenerationMode.Preview)
{
_descriptor.VectorValue = _value;
visitor.AddProperty(_descriptor);
PortData[OUT] = new GeneratedPortData(new Float(3), _descriptor.GetReferenceName(GenerationMode.Preview));
Descriptor.VectorValue = _value;
visitor.AddProperty(Descriptor);
PortData[OUT] = new GeneratedPortData(new Float(3), Descriptor.GetReferenceName(GenerationMode.Preview));
}
else
{
Expand Down
Loading

0 comments on commit fb0d95d

Please sign in to comment.