forked from Syomus/ProceduralToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.cginc
154 lines (126 loc) · 3.43 KB
/
Common.cginc
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef PROCEDURAL_TOOLKIT_COMMON_INCLUDED
#define PROCEDURAL_TOOLKIT_COMMON_INCLUDED
//
// Collection of shaping and debug functions
//
#include "UnityCG.cginc"
//
// Step functions
//
float CalculateScreenAA()
{
return length(float2(_ScreenParams.z - 1.0, _ScreenParams.w - 1.0));
}
float SmoothStep0(float x, float aa)
{
return smoothstep(0.0, aa, x);
}
float SmoothStep0(float x)
{
return smoothstep(0.0, CalculateScreenAA(), x);
}
float InverseSmoothStep0(float x, float aa)
{
return smoothstep(aa, 0.0, x);
}
float InverseSmoothStep0(float x)
{
return smoothstep(CalculateScreenAA(), 0.0, x);
}
float RectangleStep(float from, float x)
{
return (x > from) ? 1.0 : 0.0;
}
float LineStep(float from, float to, float x)
{
return saturate((x - from)/(to - from));
}
//
// Wave functions
//
float TriangleWave(float x)
{
return 2.0*abs(frac(x + 0.5) - 0.5);
}
//
// Pulse functions
//
float RectanglePulse(float x, float from, float to)
{
return (x > from && x < to) ? 1.0 : 0.0;
}
float TrianglePulse(float x, float from, float to)
{
return max(1.0 - abs(((x - from)*2.0)/(to - from) - 1.0), 0.0);
}
float SawPulse(float x, float from, float to)
{
return LineStep(from, to, x) - step(to, x);
}
float LinePulse(float x, float fromA, float fromB, float toA, float toB)
{
return LineStep(fromA, fromB, x) - LineStep(toA, toB, x);
}
//
// Debug
//
float DebugValuePattern(float value, float offset, float aa)
{
value = frac(value);
return smoothstep(offset, offset + aa, value)*smoothstep(1.0 - offset, 1.0 - offset - aa, value);
}
float DebugValuePatternSimple(float value)
{
float aa = 0.005;
float pattern1 = DebugValuePattern(value, aa*0.5, aa);
float pattern2 = DebugValuePattern(value*2.0, aa*0.1, aa*2.0);
float pattern10 = DebugValuePattern(value*10.0, -aa*10.0, aa*20.0);
return pattern1*pattern2*pattern10;
}
float DebugValuePattern(float value)
{
float aa = 0.005;
float pattern100 = DebugValuePattern(value*100.0, -aa*50.0, aa*100.0);
return DebugValuePatternSimple(value)*pattern100;
}
float4 DebugValueColor(float value)
{
float offset = sign(value)*0.3;
float4 nearColor = float4(0.4 + offset, 0.35, 0.4 - offset, 1.0);
float4 farColor = float4(0.55 + offset, 0.65, 0.55 - offset, 1.0);
float4 color = lerp(nearColor, farColor, saturate(abs(value*10.0)));
#ifndef UNITY_COLORSPACE_GAMMA
color.rgb = GammaToLinearSpace(color.rgb);
#endif
return color;
}
float4 DebugValueColor(float2 value)
{
float offsetX = sign(value.x)*0.3;
float offsetY = step(value.y, 0.0)*0.25;
float4 nearColor = float4(0.4 + offsetX, 0.35 - offsetY, 0.4 - offsetX, 1.0);
float4 farColor = float4(0.55 + offsetX, 0.65 - offsetY, 0.55 - offsetX, 1.0);
float t = 1.0 - length(min((abs(value) - float2(0.1, 0.1))*10.0, float2(0.0, 0.0)));
float4 color = lerp(nearColor, farColor, t);
#ifndef UNITY_COLORSPACE_GAMMA
color.rgb = GammaToLinearSpace(color.rgb);
#endif
return color;
}
float4 DebugValueSimple(float value)
{
return DebugValuePatternSimple(value)*DebugValueColor(value);
}
float4 DebugValue(float value)
{
return DebugValuePattern(value)*DebugValueColor(value);
}
float4 DebugValueSimple(float2 value)
{
return DebugValuePatternSimple(value.x)*DebugValuePatternSimple(value.y)*DebugValueColor(value);
}
float4 DebugValue(float2 value)
{
return DebugValuePattern(value.x)*DebugValuePattern(value.y)*DebugValueColor(value);
}
#endif