Guest User

Untitled

a guest
Feb 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor.ShaderGraph;
  5. using System.Reflection;
  6.  
  7. [Title("Custom", "Main Light")]
  8. public class MainLightNode : CodeFunctionNode
  9. {
  10. public override bool hasPreview { get { return false; } }
  11.  
  12. //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.
  13. private static string functionBodyForReals = @"{
  14. Light mainLight = GetMainLight();
  15. Color = mainLight.color;
  16. Direction = mainLight.direction;
  17. float4 shadowCoord;
  18. #ifdef LIGHTWEIGHT_SHADOWS_INCLUDED
  19. #if SHADOWS_SCREEN
  20. float4 clipPos = TransformWorldToHClip(WorldPos);
  21. shadowCoord = ComputeScreenPos(clipPos);
  22. #else
  23. shadowCoord = TransformWorldToShadowCoord(WorldPos);
  24. #endif
  25. #endif
  26. Attenuation = MainLightRealtimeShadow(shadowCoord);
  27. }";
  28. private static string functionBodyPreview = @"{
  29. Color = 1;
  30. Direction = float3(-0.5, .5, 0.5);
  31. Attenuation = 1;
  32. }";
  33.  
  34. private static bool isPreview;
  35.  
  36. private static string functionBody
  37. {
  38. get
  39. {
  40. if (isPreview)
  41. return functionBodyPreview;
  42. else
  43. return functionBodyForReals;
  44. }
  45. }
  46.  
  47.  
  48. public MainLightNode()
  49. {
  50. name = "Main Light";
  51. }
  52.  
  53. protected override MethodInfo GetFunctionToConvert()
  54. {
  55. return GetType().GetMethod("CustomFunction", BindingFlags.Static | BindingFlags.NonPublic);
  56. }
  57.  
  58.  
  59. public override void GenerateNodeFunction(FunctionRegistry registry, GraphContext graphContext, GenerationMode generationMode)
  60. {
  61. isPreview = generationMode == GenerationMode.Preview;
  62.  
  63. base.GenerateNodeFunction(registry, graphContext, generationMode);
  64. }
  65.  
  66. private static string CustomFunction(
  67. [Slot(0, Binding.None)] out Vector3 Direction,
  68. [Slot(1, Binding.None)] out Vector1 Attenuation,
  69. [Slot(2, Binding.None)] out Vector3 Color,
  70. [Slot(3, Binding.WorldSpacePosition)] Vector3 WorldPos)
  71. {
  72. Direction = Vector3.zero;
  73. Color = Vector3.zero;
  74.  
  75. return functionBody;
  76. }
  77. }
Add Comment
Please, Sign In to add comment