Skip to content

Commit

Permalink
pass branch node
Browse files Browse the repository at this point in the history
  • Loading branch information
z3y committed Sep 11, 2024
1 parent 19f6f62 commit 81f94a3
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 10 deletions.
100 changes: 100 additions & 0 deletions Editor/ShaderNode/Nodes/PassBranchNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Text;
using Graphlit.Nodes;
using Graphlit.Nodes.PortType;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace Graphlit
{
[NodeInfo("Utility/Pass Branch"), Serializable]
public class PassBranchNode : ShaderNode
{
const int TRUE = 1;
const int FALSE = 2;
const int OUT = 3;

[Flags]
public enum Pass {
Forward = 1 << 0,
ForwardAdd = 1 << 1,
ShadowCaster = 1 << 2,
Meta = 1 << 3
}

string FlagsToString(GenerationMode generationMode)
{
if (generationMode == GenerationMode.Preview)
{
return passFlags.HasFlag(Pass.Forward) ? "1" :"0";
}
var sb = new StringBuilder();
bool or = false;
if (passFlags.HasFlag(Pass.Forward))
{
or = true;
sb.Append("defined(UNITY_PASS_FORWARDBASE)");
}
if (passFlags.HasFlag(Pass.ForwardAdd))
{
if (or) { sb.Append(" || "); };
or = true;
sb.Append("defined(UNITY_PASS_FORWARDADD)");
}
if (passFlags.HasFlag(Pass.ShadowCaster))
{
if (or) { sb.Append(" || "); };
or = true;
sb.Append("defined(UNITY_PASS_SHADOWCASTER)");
}
if (passFlags.HasFlag(Pass.Meta))
{
if (or) { sb.Append(" || "); };
or = true;
sb.Append("defined(UNITY_PASS_META)");
}

if (!or)
{
return "0";
}

return sb.ToString();
}

[SerializeField] public Pass passFlags;

public override bool DisablePreview => true;
public override void Initialize()
{
AddPort(new(PortDirection.Input, new Float(1, true), TRUE, "True"));
AddPort(new(PortDirection.Input, new Float(1, true), FALSE, "False"));
AddPort(new(PortDirection.Output, new Float(1, true), OUT));

var e = new EnumFlagsField("", passFlags);
e.RegisterValueChangedCallback((e) =>
{
passFlags = (Pass)e.newValue;
GeneratePreviewForAffectedNodes();
});
extensionContainer.Add(e);
}


protected override void Generate(NodeVisitor visitor)
{
ChangeDimensions(OUT, ImplicitTruncation(TRUE, FALSE).dimensions);
SetVariable(OUT, UniqueVariable);

var data = PortData[OUT];
var type = (Float)data.Type;
visitor.AppendLine($"{PrecisionString(type.dimensions)} {data.Name};");
visitor.AppendLine($"#if {FlagsToString(visitor.GenerationMode)}");
visitor.AppendLine($"{data.Name} = {PortData[TRUE].Name};");
visitor.AppendLine("#else");
visitor.AppendLine($"{data.Name} = {PortData[FALSE].Name};");
visitor.AppendLine("#endif");
}
}
}
11 changes: 11 additions & 0 deletions Editor/ShaderNode/Nodes/PassBranchNode.cs.meta

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

8 changes: 0 additions & 8 deletions Shaders/Custom Lighting.meta

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.z3y.graphlit",
"version": "1.0.2",
"version": "1.0.3",
"unity": "2022.3",
"displayName": "Graphlit",
"hideInEditor": false,
Expand All @@ -16,4 +16,4 @@
"com.unity.render-pipelines.core": "14.0.8"
},
"description": "A Node shader editor"
}
}

0 comments on commit 81f94a3

Please sign in to comment.