Skip to content

Instantly share code, notes, and snippets.

@Derulan
Last active August 19, 2020 04:01
Show Gist options
  • Save Derulan/23df32f5794c82cecbff911984116a66 to your computer and use it in GitHub Desktop.
Save Derulan/23df32f5794c82cecbff911984116a66 to your computer and use it in GitHub Desktop.
Modified Light Node (OG by @CiroContns on twitter) for Shader Graph 4.9.0
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.ShaderGraph;
using System.Reflection;
[Title("Custom", "Main Light")]
public class MainLightNode : CodeFunctionNode
{
public override bool hasPreview { get { return false; } }
//None of this is mine. It was created by @CiroContns on twitter. He uses an older version of shadergraph, so all I did was update it. This will eventually become outdatded.
private static string functionBodyForReals = @"{
Light mainLight = GetMainLight();
Color = mainLight.color;
Direction = mainLight.direction;
float4 shadowCoord;
#ifdef LIGHTWEIGHT_SHADOWS_INCLUDED
#if SHADOWS_SCREEN
float4 clipPos = TransformWorldToHClip(WorldPos);
shadowCoord = ComputeScreenPos(clipPos);
#else
shadowCoord = TransformWorldToShadowCoord(WorldPos);
#endif
#endif
Attenuation = MainLightRealtimeShadow(shadowCoord);
}";
private static string functionBodyPreview = @"{
Color = 1;
Direction = float3(-0.5, .5, 0.5);
Attenuation = 1;
}";
private static bool isPreview;
private static string functionBody
{
get
{
if (isPreview)
return functionBodyPreview;
else
return functionBodyForReals;
}
}
public MainLightNode()
{
name = "Main Light";
}
protected override MethodInfo GetFunctionToConvert()
{
return GetType().GetMethod("CustomFunction", BindingFlags.Static | BindingFlags.NonPublic);
}
public override void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
{
isPreview = generationMode == GenerationMode.Preview;
base.GenerateNodeFunction(registry, graphContext, generationMode);
}
private static string CustomFunction(
[Slot(0, Binding.None)] out Vector3 Direction,
[Slot(1, Binding.None)] out Vector1 Attenuation,
[Slot(2, Binding.None)] out Vector3 Color,
[Slot(3, Binding.WorldSpacePosition)] Vector3 WorldPos)
{
Direction = Vector3.zero;
Color = Vector3.zero;
return functionBody;
}
}
@AcelisWeaven
Copy link

If anyone uses Unity 2019.1+ and can't use this custom node anymore, here's a replacement posted by @toothbrush on the Unity forums. Shader preview won't work, sadly, but it'll work in-game.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment