-
Notifications
You must be signed in to change notification settings - Fork 0
/
GradientCompute.compute
48 lines (37 loc) · 1.05 KB
/
GradientCompute.compute
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel GradientGen
struct GradientNode
{
float4 Color;
float Percent;
float ID;
};
StructuredBuffer<GradientNode> _GradientNodes;
float2 _Size;
int _NodeCount;
RWTexture2D<float4> _Result;
float Remap(float input, float oldMin, float oldMax, float newMin, float newMax)
{
return newMin + (input - oldMin) * (newMax - newMin) / (oldMax - oldMin);
}
[numthreads(32,1,1)]
void GradientGen (uint3 id : SV_DispatchThreadID)
{
if (_NodeCount == 0) return;
float percent = saturate(id.x / _Size.x);
float4 color = _GradientNodes[0].Color;
float prev = _GradientNodes[0].Percent;
for (int j = 1; j < _NodeCount; j++)
{
float curr = _GradientNodes[j].Percent;
float t = saturate(Remap(percent, prev, curr, 0, 1));
color = lerp(color, _GradientNodes[j].Color, t);
prev = curr;
}
float2 uv = float2(id.x, 0);
for (int i = 0; i < _Size.y; i++)
{
uv.y = i;
_Result[uv] = color;
}
}