Skip to content

Instantly share code, notes, and snippets.

@Kink3d
Created May 8, 2018 00:13
Show Gist options
  • Save Kink3d/c96673b8fe1aef537f25b0d82fb528b0 to your computer and use it in GitHub Desktop.
Save Kink3d/c96673b8fe1aef537f25b0d82fb528b0 to your computer and use it in GitHub Desktop.
A custom lighting node example for Shader Graph and Lightweight Render Pipeline
using UnityEngine;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph
{
[Title("Input", "Lighting", "Main Light")]
public class MainLightNode : AbstractMaterialNode, IGeneratesBodyCode
{
public MainLightNode()
{
name = "Main Light";
UpdateNodeAfterDeserialization();
}
public override bool hasPreview { get { return false; } }
const string kPositionSlotName = "Position";
const string kDirectionSlotName = "Direction";
const string kAttenuationSlotName = "Attenuation";
const string kColorSlotName = "Color";
public const int PositionSlotId = 0;
public const int DirectionSlotId = 1;
public const int AttenuationSlotId = 2;
public const int ColorSlotId = 3;
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new Vector3MaterialSlot(PositionSlotId, kPositionSlotName, kPositionSlotName, SlotType.Input, Vector3.zero));
AddSlot(new Vector3MaterialSlot(DirectionSlotId, kDirectionSlotName, kDirectionSlotName, SlotType.Output, Vector3.zero));
AddSlot(new Vector1MaterialSlot(AttenuationSlotId, kAttenuationSlotName, kAttenuationSlotName, SlotType.Output, 0));
AddSlot(new Vector3MaterialSlot(ColorSlotId, kColorSlotName, kColorSlotName, SlotType.Output, Vector3.zero));
RemoveSlotsNameNotMatching(new[] { PositionSlotId, DirectionSlotId, AttenuationSlotId, ColorSlotId });
}
public void GenerateNodeCode(ShaderGenerator visitor, GenerationMode generationMode)
{
ShaderStringBuilder sb = new ShaderStringBuilder();
if(generationMode != GenerationMode.Preview)
{
sb.AppendLine("Light {0}_Light = GetMainLight({1});", GetVariableNameForNode(), GetSlotValue(PositionSlotId, generationMode));
sb.AppendLine("{0} {1} = {2}_Light.direction;"
, FindOutputSlot<MaterialSlot>(DirectionSlotId).concreteValueType.ToString(precision)
, GetVariableNameForSlot(DirectionSlotId)
, GetVariableNameForNode());
sb.AppendLine("{0} {1} = {2}_Light.attenuation;"
, FindOutputSlot<MaterialSlot>(AttenuationSlotId).concreteValueType.ToString(precision)
, GetVariableNameForSlot(AttenuationSlotId)
, GetVariableNameForNode());
sb.AppendLine("{0} {1} = {2}_Light.color;"
, FindOutputSlot<MaterialSlot>(ColorSlotId).concreteValueType.ToString(precision)
, GetVariableNameForSlot(ColorSlotId)
, GetVariableNameForNode());
}
else
{
sb.AppendLine("{0} {1} = {2}3 (0.5, 0.5, -0.25);"
, FindOutputSlot<MaterialSlot>(DirectionSlotId).concreteValueType.ToString(precision)
, GetVariableNameForSlot(DirectionSlotId)
, precision);
sb.AppendLine("{0} {1} = 1;"
, FindOutputSlot<MaterialSlot>(AttenuationSlotId).concreteValueType.ToString(precision)
, GetVariableNameForSlot(AttenuationSlotId));
sb.AppendLine("{0} {1} = {2}3 (1, 1, 1);"
, FindOutputSlot<MaterialSlot>(ColorSlotId).concreteValueType.ToString(precision)
, GetVariableNameForSlot(ColorSlotId)
, precision);
}
visitor.AddShaderChunk(sb.ToString(), true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment