A game about forced loneliness, made by TACStudios
1using System.Reflection;
2using UnityEngine;
3
4namespace UnityEditor.ShaderGraph
5{
6 [Title("Input", "Gradient", "Sample Gradient")]
7 class SampleGradient : CodeFunctionNode
8 {
9 public override int latestVersion => 1;
10 public SampleGradient()
11 {
12 name = "Sample Gradient";
13 }
14
15 public override bool hasPreview
16 {
17 get { return true; }
18 }
19
20 protected override MethodInfo GetFunctionToConvert()
21 {
22 switch (sgVersion)
23 {
24 case 0:
25 return GetType().GetMethod("Unity_SampleGradientV0", BindingFlags.Static | BindingFlags.NonPublic);
26 case 1:
27 default:
28 return GetType().GetMethod("Unity_SampleGradientV1", BindingFlags.Static | BindingFlags.NonPublic);
29 }
30 }
31
32 static string Unity_SampleGradientV0(
33 [Slot(0, Binding.None)] Gradient Gradient,
34 [Slot(1, Binding.None)] Vector1 Time,
35 [Slot(2, Binding.None)] out Vector4 Out)
36 {
37 Out = Vector4.zero;
38 return
39@"
40{
41 $precision3 color = Gradient.colors[0].rgb;
42 [unroll]
43 for (int c = 1; c < Gradient.colorsLength; c++)
44 {
45 $precision colorPos = saturate((Time - Gradient.colors[c - 1].w) / (Gradient.colors[c].w - Gradient.colors[c - 1].w)) * step(c, Gradient.colorsLength - 1);
46 color = lerp(color, Gradient.colors[c].rgb, lerp(colorPos, step(0.01, colorPos), Gradient.type));
47 }
48#ifndef UNITY_COLORSPACE_GAMMA
49 color = SRGBToLinear(color);
50#endif
51 $precision alpha = Gradient.alphas[0].x;
52 [unroll]
53 for (int a = 1; a < Gradient.alphasLength; a++)
54 {
55 $precision alphaPos = saturate((Time - Gradient.alphas[a - 1].y) / (Gradient.alphas[a].y - Gradient.alphas[a - 1].y)) * step(a, Gradient.alphasLength - 1);
56 alpha = lerp(alpha, Gradient.alphas[a].x, lerp(alphaPos, step(0.01, alphaPos), Gradient.type));
57 }
58 Out = $precision4(color, alpha);
59}
60";
61 }
62
63 static string Unity_SampleGradientV1(
64 [Slot(0, Binding.None)] Gradient Gradient,
65 [Slot(1, Binding.None)] Vector1 Time,
66 [Slot(2, Binding.None)] out Vector4 Out)
67 {
68 Out = Vector4.zero;
69 return
70@"
71{
72 // convert to OkLab if we need perceptual color space.
73 $precision3 color = lerp(Gradient.colors[0].rgb, LinearToOklab(Gradient.colors[0].rgb), Gradient.type == 2);
74
75 [unroll]
76 for (int c = 1; c < Gradient.colorsLength; c++)
77 {
78 $precision colorPos = saturate((Time - Gradient.colors[c - 1].w) / (Gradient.colors[c].w - Gradient.colors[c - 1].w)) * step(c, Gradient.colorsLength - 1);
79 $precision3 color2 = lerp(Gradient.colors[c].rgb, LinearToOklab(Gradient.colors[c].rgb), Gradient.type == 2);
80 color = lerp(color, color2, lerp(colorPos, step(0.01, colorPos), Gradient.type % 2)); // grad.type == 1 is fixed, 0 and 2 are blends.
81 }
82 color = lerp(color, OklabToLinear(color), Gradient.type == 2);
83
84#ifdef UNITY_COLORSPACE_GAMMA
85 color = LinearToSRGB(color);
86#endif
87
88 $precision alpha = Gradient.alphas[0].x;
89 [unroll]
90 for (int a = 1; a < Gradient.alphasLength; a++)
91 {
92 $precision alphaPos = saturate((Time - Gradient.alphas[a - 1].y) / (Gradient.alphas[a].y - Gradient.alphas[a - 1].y)) * step(a, Gradient.alphasLength - 1);
93 alpha = lerp(alpha, Gradient.alphas[a].x, lerp(alphaPos, step(0.01, alphaPos), Gradient.type % 2));
94 }
95
96 Out = $precision4(color, alpha);
97}
98";
99 }
100 }
101}