forked from przemyslawzaworski/Unity3D-CG-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphotoshop_blends.shader
291 lines (252 loc) · 6.42 KB
/
photoshop_blends.shader
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//useful links:
//https://www.shadertoy.com/view/XdS3RW
//see also: https://docs.gimp.org/en/gimp-concepts-layer-modes.html
Shader "Photoshop Blends"
{
Properties
{
_source ("Upper Layer", 2D) = "white" {}
_destination ("Lower Layer", 2D) = "white" {}
number("Mode 0-24", Int) = 0
}
Subshader
{
Pass
{
CGPROGRAM
#pragma vertex vertex_shader
#pragma fragment pixel_shader
#pragma target 3.0
#include "UnityCG.cginc"
struct custom_type
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uv1 : TEXCOORD1;
};
sampler2D _destination,_source;
int number;
float4 _source_ST;
float4 _destination_ST;
float3 darken( float3 s, float3 d )
{
return min(s,d);
}
float3 multiply( float3 s, float3 d )
{
return s*d;
}
float3 colorBurn( float3 s, float3 d )
{
return 1.0 - (1.0 - d) / s;
}
float3 linearBurn( float3 s, float3 d )
{
return s + d - 1.0;
}
float3 darkerColor( float3 s, float3 d )
{
return (s.x + s.y + s.z < d.x + d.y + d.z) ? s : d;
}
float3 lighten( float3 s, float3 d )
{
return max(s,d);
}
float3 screen( float3 s, float3 d )
{
return s + d - s * d;
}
float3 colorDodge( float3 s, float3 d )
{
return d / (1.0 - s);
}
float3 linearDodge( float3 s, float3 d )
{
return s + d;
}
float3 lighterColor( float3 s, float3 d )
{
return (s.x + s.y + s.z > d.x + d.y + d.z) ? s : d;
}
float overlay( float s, float d )
{
return (d < 0.5) ? 2.0 * s * d : 1.0 - 2.0 * (1.0 - s) * (1.0 - d);
}
float3 overlay( float3 s, float3 d )
{
float3 c;
c.x = overlay(s.x,d.x);
c.y = overlay(s.y,d.y);
c.z = overlay(s.z,d.z);
return c;
}
float softLight( float s, float d )
{
return (s < 0.5) ? d - (1.0 - 2.0 * s) * d * (1.0 - d)
: (d < 0.25) ? d + (2.0 * s - 1.0) * d * ((16.0 * d - 12.0) * d + 3.0)
: d + (2.0 * s - 1.0) * (sqrt(d) - d);
}
float3 softLight( float3 s, float3 d )
{
float3 c;
c.x = softLight(s.x,d.x);
c.y = softLight(s.y,d.y);
c.z = softLight(s.z,d.z);
return c;
}
float hardLight( float s, float d )
{
return (s < 0.5) ? 2.0 * s * d : 1.0 - 2.0 * (1.0 - s) * (1.0 - d);
}
float3 hardLight( float3 s, float3 d )
{
float3 c;
c.x = hardLight(s.x,d.x);
c.y = hardLight(s.y,d.y);
c.z = hardLight(s.z,d.z);
return c;
}
float vividLight( float s, float d )
{
return (s < 0.5) ? 1.0 - (1.0 - d) / (2.0 * s) : d / (2.0 * (1.0 - s));
}
float3 vividLight( float3 s, float3 d )
{
float3 c;
c.x = vividLight(s.x,d.x);
c.y = vividLight(s.y,d.y);
c.z = vividLight(s.z,d.z);
return c;
}
float3 linearLight( float3 s, float3 d )
{
return 2.0 * s + d - 1.0;
}
float pinLight( float s, float d )
{
return (2.0 * s - 1.0 > d) ? 2.0 * s - 1.0 : (s < 0.5 * d) ? 2.0 * s : d;
}
float3 pinLight( float3 s, float3 d )
{
float3 c;
c.x = pinLight(s.x,d.x);
c.y = pinLight(s.y,d.y);
c.z = pinLight(s.z,d.z);
return c;
}
float3 hardlerp( float3 s, float3 d )
{
return floor(s + d);
}
float3 difference( float3 s, float3 d )
{
return abs(d - s);
}
float3 exclusion( float3 s, float3 d )
{
return s + d - 2.0 * s * d;
}
float3 subtract( float3 s, float3 d )
{
return s - d;
}
float3 divide( float3 s, float3 d )
{
return s / d;
}
float3 rgb2hsv(float3 c)
{
float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
float3 hsv2rgb(float3 c)
{
float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * lerp(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
float3 hue( float3 s, float3 d )
{
d = rgb2hsv(d);
d.x = rgb2hsv(s).x;
return hsv2rgb(d);
}
float3 color( float3 s, float3 d )
{
s = rgb2hsv(s);
s.z = rgb2hsv(d).z;
return hsv2rgb(s);
}
float3 saturation( float3 s, float3 d )
{
d = rgb2hsv(d);
d.y = rgb2hsv(s).y;
return hsv2rgb(d);
}
float3 luminosity( float3 s, float3 d )
{
float dLum = dot(d, float3(0.3, 0.59, 0.11));
float sLum = dot(s, float3(0.3, 0.59, 0.11));
float lum = sLum - dLum;
float3 c = d + lum;
float minC = min(min(c.x, c.y), c.z);
float maxC = max(max(c.x, c.y), c.z);
if(minC < 0.0) return sLum + ((c - sLum) * sLum) / (sLum - minC);
else if(maxC > 1.0) return sLum + ((c - sLum) * (1.0 - sLum)) / (maxC - sLum);
else return c;
}
float3 blend( float3 s, float3 d, int id )
{
if(id==0) return darken(s,d);
if(id==1) return multiply(s,d);
if(id==2) return colorBurn(s,d);
if(id==3) return linearBurn(s,d);
if(id==4) return darkerColor(s,d);
if(id==5) return lighten(s,d);
if(id==6) return screen(s,d);
if(id==7) return colorDodge(s,d);
if(id==8) return linearDodge(s,d);
if(id==9) return lighterColor(s,d);
if(id==10) return overlay(s,d);
if(id==11) return softLight(s,d);
if(id==12) return hardLight(s,d);
if(id==13) return vividLight(s,d);
if(id==14) return linearLight(s,d);
if(id==15) return pinLight(s,d);
if(id==16) return hardlerp(s,d);
if(id==17) return difference(s,d);
if(id==18) return exclusion(s,d);
if(id==19) return subtract(s,d);
if(id==20) return divide(s,d);
if(id==21) return hue(s,d);
if(id==22) return color(s,d);
if(id==23) return saturation(s,d);
if(id==24) return luminosity(s,d);
return float3(0,0,0);
}
custom_type vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0, float2 uv1:TEXCOORD1)
{
custom_type vs;
vs.vertex = UnityObjectToClipPos(vertex);
vs.uv = TRANSFORM_TEX(uv, _source);
vs.uv1 = TRANSFORM_TEX(uv, _destination);
return vs;
}
float4 pixel_shader (custom_type ps) : SV_TARGET
{
float2 uv = ps.uv.xy;
float2 uv1 = ps.uv1.xy;
int id =number;
float3 s = tex2D(_source, uv).xyz;
float3 d = tex2D(_destination, uv1).xyz;
float3 c = blend(s,d,id);
return float4(c,1.0);
}
ENDCG
}
}
}