File: draw.effect

package info (click to toggle)
obs-draw-dock 0.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 744 kB
  • sloc: cpp: 2,680; ansic: 887; sh: 259; makefile: 25
file content (446 lines) | stat: -rw-r--r-- 13,232 bytes parent folder | download
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
uniform float4x4 ViewProj;
uniform texture2d image;
uniform float2 uv_size;
uniform int draw_cursor;
uniform float2 uv_mouse;
uniform float2 uv_mouse_previous;
uniform float2 select_from;
uniform float2 select_to;
uniform float4 cursor_color;
uniform float cursor_size;
uniform texture2d cursor_image;
uniform int tool;
uniform texture2d tool_image;
uniform float4 tool_color;
uniform float tool_size;
uniform int tool_mode;
uniform bool shift_down;

sampler_state def_sampler {
	Filter   =
Linear;
	AddressU = Clamp;
	AddressV =
Clamp;
};

struct VertInOut
{
	float4 pos : POSITION;
	float2 uv : TEXCOORD0;
};

VertInOut VSDefault(VertInOut vert_in)
{
	VertInOut vert_out;
	vert_out.pos = mul(float4(vert_in.pos.xyz, 1.0), ViewProj);
	vert_out.uv = vert_in.uv;
	return vert_out;
}

float4 apply_color(float4 color, float4 orig)
{
	if (color.a == 0.0)
		return orig;
	if (color.a < 0.0)
		return float4(lerp(orig.rgb, color.rgb, (1.0 + color.a) / ((1.0 + color.a) + orig.a)), min((1.0 + color.a), orig.a));
		//return float4(0, 0, 0, 0);
		//return float4(orig.rgb * (1.0 + color.a), min((1.0 + color.a), orig.a));

	return float4(lerp(orig.rgb, color.rgb, color.a / (color.a + orig.a)), max(color.a, orig.a));
}

float4 draw_line(float2 coord, float2 from, float2 to, float4 color, float distance_factor, float4 orig)
{
	float d = distance(coord, to);
	float effective_alpha = 0.0;
	if (d <= tool_size)
	{
		effective_alpha = color.a - (color.a * (d / tool_size) * distance_factor);
	}
	if (from.x >= 0.0 && from.y >= 0.0 && (from.x != 0.0 || from.y != 0.0))
	{
		float2 lineDir = from - to;
		float2 perpDir = float2(lineDir.y, -lineDir.x);
		float2 dirToPt1 = to - coord;
		float ld = abs(dot(normalize(perpDir), dirToPt1));
		if (ld <= tool_size)
		{
			float dp = distance(coord, from);
			if (dp <= tool_size)
			{
				if (color.a < 0.0)
					effective_alpha = min(color.a - (color.a * (dp / tool_size) * distance_factor), effective_alpha);
				else
					effective_alpha = max(color.a - (color.a * (dp / tool_size) * distance_factor), effective_alpha);
			}
			float md = distance(from, to);
			if (dp < md && d < md)
			{
				if (color.a < 0.0)
					effective_alpha = min(color.a - (color.a * (ld / tool_size) * distance_factor), effective_alpha);
				else
					effective_alpha = max(color.a - (color.a * (ld / tool_size) * distance_factor), effective_alpha);
			}
		}
	}
	return apply_color(float4(color.rgb, effective_alpha), orig);
}

float4 draw_dot_line(float2 coord, float2 from, float2 to, float4 orig)
{
	float d = distance(coord, to);
	float effective_alpha = 0.0;
	float3 color = float3(1, 1, 1);
	if (d <= tool_size)
	{
		effective_alpha = 1.0;
	}
	else if (from.x >= 0.0 && from.y >= 0.0)
	{
		float2 lineDir = from - to;
		float2 perpDir = float2(lineDir.y, -lineDir.x);
		float2 dirToPt1 = to - coord;
		float ld = abs(dot(normalize(perpDir), dirToPt1));
		if (ld <= tool_size)
		{
			float dp = distance(coord, from);
			if (dp <= tool_size)
			{
				effective_alpha = max(1.0, effective_alpha);
			}
			else
			{
				float md = distance(from, to);
				if (dp < md && d < md)
				{
					effective_alpha = max(1.0, effective_alpha);

					float v = md - d;
					float v2 = v - (tool_size * 8.0) * floor(v / (tool_size * 8.0));
					if (d > dp && v2 >= tool_size * 4.0)
					{
						color = float3(0, 0, 0);
					}
					else
					{
						v = md - dp;
						v2 = v - (tool_size * 8.0) * floor(v / (tool_size * 8.0));
						if (d < dp && v2 >= tool_size * 4.0)
						{
							color = float3(0, 0, 0);
						}
					}
				}
			}
		}
	}
	return apply_color(float4(color, effective_alpha), orig);
}

float4 PSDraw(VertInOut vert_in) : TARGET
{
	float4 orig = image.Sample(def_sampler, vert_in.uv);
	float2 coord = vert_in.uv * uv_size;
	float effective_cursor_size = cursor_size <= 0.0f ? tool_size : cursor_size;
	if (draw_cursor == 1)
	{
		float d = distance(coord, uv_mouse);
		if (d < effective_cursor_size)
			return cursor_color;
	}
	else if (draw_cursor == 2)
	{
		if (abs(coord.x - uv_mouse.x) < effective_cursor_size && abs(coord.y - uv_mouse.y) < effective_cursor_size)
		{
			float2 cursor_pos = (coord - uv_mouse) / float2(effective_cursor_size * 2.0, effective_cursor_size * 2.0) + float2(0.5, 0.5);
			float4 cc = cursor_image.Sample(def_sampler, cursor_pos);
			if (cc.a > 0.0)
				return cc;
		}
	}
	if (tool_mode == 0)
	{ // tool up
		if (select_from.x != select_to.x || select_from.y != select_to.y)
		{
		
			if (tool == 8) // select rectangle
			{
				orig = draw_dot_line(coord, select_from, float2(select_from.x, select_to.y), orig);
				orig = draw_dot_line(coord, float2(select_from.x, select_to.y), select_to, orig);
				orig = draw_dot_line(coord, select_to, float2(select_to.x, select_from.y), orig);
				orig = draw_dot_line(coord, float2(select_to.x, select_from.y), select_from, orig);
			}
			else if (tool == 9) // select ellipse
			{
				float2 center = (select_from + select_to) / 2.0;
				float2 diff = abs(select_from - center);
				float2 inside = diff - tool_size;
				float2 outside = diff + tool_size;
				float2 temp = float2(pow(coord.x - center.x, 2), pow(coord.y - center.y, 2));
				if ((temp.x / pow(inside.x, 2.0)) + (temp.y / pow(inside.y, 2.0)) >= 1.0 && (temp.x / pow(outside.x, 2.0)) + (temp.y / pow(outside.y, 2.0)) <= 1.0)
				{
					float r = atan2(coord.y - center.y, coord.x - center.x) + 3.14;
					float v = r * 50.0 / 3.14;
					float v2 = v - 2.0 * floor(v / 2.0);
					if (v2 >= 1.0)
					{
						return apply_color(float4(0, 0, 0, 1), orig);
					}
					else
					{
						return apply_color(float4(1, 1, 1, 1), orig);
					}
				
				}
			}
		}
		return orig;
	}

	if (tool_mode == 2)
	{ // tool drag
		if (select_from.x != select_to.x || select_from.y != select_to.y)
		{
		
			if (tool == 8) // select rectangle
			{
				if (coord.x >= min(select_from.x, select_to.x) && coord.x <= max(select_from.x, select_to.x) && coord.y >= min(select_from.y, select_to.y) && coord.y <= max(select_from.y, select_to.y))
				{
					orig = float4(0, 0, 0, 0);
				}
				float2 diff = uv_mouse - uv_mouse_previous;
				if (coord.x >= min(select_from.x, select_to.x) + diff.x && coord.x <= max(select_from.x, select_to.x) + diff.x && coord.y >= min(select_from.y, select_to.y) + diff.y && coord.y <= max(select_from.y, select_to.y) + diff.y)
				{
					orig = apply_color(image.Sample(def_sampler, (coord - diff) / uv_size), orig);
				}
			}
			else if (tool == 9) // select ellipse
			{
				float2 center = (select_from + select_to) / 2.0;
				float2 diff = float2(pow(select_from.x - center.x, 2.0), pow(select_from.y - center.y, 2.0));
				float p = (pow(coord.x - center.x, 2) / diff.x) + (pow(coord.y - center.y, 2) / diff.y);
				if (p <= 1.0)
				{
					orig = float4(0, 0, 0, 0);
				}
				float2 diff2 = uv_mouse - uv_mouse_previous;
				p = (pow(coord.x - diff2.x - center.x, 2) / diff.x) + (pow(coord.y - diff2.y - center.y, 2) / diff.y);
				if (p <= 1.0)
				{
					orig = apply_color(image.Sample(def_sampler, (coord - diff2) / uv_size), orig);
				}
			}

		}
		return orig;
	}
	
	if (tool == 1) // pencil
	{
		return draw_line(coord, uv_mouse_previous, uv_mouse, tool_color, 0.0, orig);
	}
	else if (tool == 2) // brush
	{
		return draw_line(coord, uv_mouse_previous, uv_mouse, tool_color, 1.0, orig);
	}
	else if (tool == 3)//line
	{
		if (shift_down)
		{
			if (abs(uv_mouse_previous.x - uv_mouse.x) < abs(uv_mouse_previous.y - uv_mouse.y))
			{
				return draw_line(coord, uv_mouse_previous, float2(uv_mouse_previous.x, uv_mouse.y), tool_color, 0.0, orig);
			}
			else
			{
				return draw_line(coord, uv_mouse_previous, float2(uv_mouse.x, uv_mouse_previous.y), tool_color, 0.0, orig);
			}
		}
		return draw_line(coord, uv_mouse_previous, uv_mouse, tool_color, 0.0, orig);
	}
	else if (tool == 4) // rectangle outline
	{
		float2 from = uv_mouse_previous;
		float2 to = uv_mouse;
		if (shift_down)
		{
			if (abs(from.x - to.x) > abs(from.y - to.y))
			{
				to.x = from.x + (to.x - from.x) / abs(from.x - to.x) * abs(from.y - to.y);
			}
			else
			{
				to.y = from.y + (to.y - from.y) / abs(from.y - to.y) * abs(from.x - to.x);
			}
		}
		orig = draw_line(coord, from, float2(from.x, to.y), tool_color, 0.0, orig);
		orig = draw_line(coord, float2(from.x, to.y), to, tool_color, 0.0, orig);
		orig = draw_line(coord, to, float2(to.x, from.y), tool_color, 0.0, orig);
		orig = draw_line(coord, float2(to.x, from.y), from, tool_color, 0.0, orig);
		return orig;
	}
	else if (tool == 5) // rectangle
	{
		float2 from = uv_mouse_previous;
		float2 to = uv_mouse;
		if (shift_down)
		{
			if (abs(from.x - to.x) > abs(from.y - to.y))
			{
				to.x = from.x + (to.x - from.x) / abs(from.x - to.x) * abs(from.y - to.y);
			}
			else
			{
				to.y = from.y + (to.y - from.y) / abs(from.y - to.y) * abs(from.x - to.x);
			}
		}
		float2 min_mouse = min(to, from);
		float2 max_mouse = max(to, from);
		if (coord.x >= min_mouse.x && coord.x <= max_mouse.x && coord.y >= min_mouse.y && coord.y <= max_mouse.y)
			return apply_color(tool_color, orig);
	}
	else if (tool == 6) // ellipse outline
	{
		float2 from = uv_mouse_previous;
		float2 to = uv_mouse;
		if (shift_down)
		{
			if (abs(from.x - to.x) > abs(from.y - to.y))
			{
				to.x = from.x + (to.x - from.x) / abs(from.x - to.x) * abs(from.y - to.y);
			}
			else
			{
				to.y = from.y + (to.y - from.y) / abs(from.y - to.y) * abs(from.x - to.x);
			}
		}
		float2 center = (from + to) / 2.0;
		float2 diff = abs(from - center);
		float2 inside = diff - tool_size;
		float2 outside = diff + tool_size;
		float2 temp = float2(pow(coord.x - center.x, 2), pow(coord.y - center.y, 2));
		if ((temp.x / pow(inside.x, 2.0)) + (temp.y / pow(inside.y, 2.0)) >= 1.0 && (temp.x / pow(outside.x, 2.0)) + (temp.y / pow(outside.y, 2.0)) <= 1.0)
			return apply_color(tool_color, orig);
	}
	else if (tool == 7) // ellipse
	{
		float2 from = uv_mouse_previous;
		float2 to = uv_mouse;
		if (shift_down)
		{
			if (abs(from.x - to.x) > abs(from.y - to.y))
			{
				to.x = from.x + (to.x - from.x) / abs(from.x - to.x) * abs(from.y - to.y);
			}
			else
			{
				to.y = from.y + (to.y - from.y) / abs(from.y - to.y) * abs(from.x - to.x);
			}
		}
		float2 center = (from + to) / 2.0;
		float2 diff = float2(pow(from.x - center.x, 2.0), pow(from.y - center.y, 2.0));
		float p = (pow(coord.x - center.x, 2) / diff.x) + (pow(coord.y - center.y, 2) / diff.y);
		if (p <= 1.0)
			return apply_color(tool_color, orig);
	}
	else if (tool == 8) // select rectangle
	{
		float2 from = uv_mouse_previous;
		float2 to = uv_mouse;
		if (shift_down)
		{
			if (abs(from.x - to.x) > abs(from.y - to.y))
			{
				to.x = from.x + (to.x - from.x) / abs(from.x - to.x) * abs(from.y - to.y);
			}
			else
			{
				to.y = from.y + (to.y - from.y) / abs(from.y - to.y) * abs(from.x - to.x);
			}
		}
		orig = draw_dot_line(coord, from, float2(from.x, to.y), orig);
		orig = draw_dot_line(coord, float2(from.x, to.y), to, orig);
		orig = draw_dot_line(coord, to, float2(to.x, from.y), orig);
		orig = draw_dot_line(coord, float2(to.x, from.y), from, orig);
		return orig;
	}
	else if (tool == 9) // select ellipse
	{
		float2 from = uv_mouse_previous;
		float2 to = uv_mouse;
		if (shift_down)
		{
			if (abs(from.x - to.x) > abs(from.y - to.y))
			{
				to.x = from.x + (to.x - from.x) / abs(from.x - to.x) * abs(from.y - to.y);
			}
			else
			{
				to.y = from.y + (to.y - from.y) / abs(from.y - to.y) * abs(from.x - to.x);
			}
		}
		float2 center = (from + to) / 2.0;
		float2 diff = abs(from - center);
		float2 inside = diff - tool_size;
		float2 outside = diff + tool_size;
		float2 temp = float2(pow(coord.x - center.x, 2), pow(coord.y - center.y, 2));
		if ((temp.x / pow(inside.x, 2.0)) + (temp.y / pow(inside.y, 2.0)) >= 1.0 && (temp.x / pow(outside.x, 2.0)) + (temp.y / pow(outside.y, 2.0)) <= 1.0)
		{
			float r = atan2(coord.y - center.y, coord.x - center.x) + 3.14;
			float v = r * 50.0 / 3.14;
			float v2 = v - 2.0 * floor(v / 2.0);
			if (v2 >= 1.0)
			{
				return apply_color(float4(0, 0, 0, 1), orig);
			}
			else
			{
				return apply_color(float4(1, 1, 1, 1), orig);
			}
				
		}
	}
	else if (tool == 10) // stamp
	{
		if (coord.x >= uv_mouse.x - tool_size && coord.x <= uv_mouse.x + tool_size && coord.y >= uv_mouse.y - tool_size && coord.y <= uv_mouse.y + tool_size)
		{
			float2 uv = (coord - uv_mouse + float2(tool_size, tool_size)) / (tool_size * 2.0);
			return apply_color(tool_image.Sample(def_sampler, uv), orig);
		}
	}
	else if (tool == 11) // image
	{
		float2 from = uv_mouse_previous;
		float2 to = uv_mouse;
		if (shift_down)
		{
			if (abs(from.x - to.x) > abs(from.y - to.y))
			{
				to.x = from.x + (to.x - from.x) / abs(from.x - to.x) * abs(from.y - to.y);
			}
			else
			{
				to.y = from.y + (to.y - from.y) / abs(from.y - to.y) * abs(from.x - to.x);
			}
		}
		float2 min_mouse = min(to, from);
		float2 max_mouse = max(to, from);
		if (coord.x >= min_mouse.x && coord.x <= max_mouse.x && coord.y >= min_mouse.y && coord.y <= max_mouse.y)
		{
			float2 uv = (coord - from) / (to - from);
			return apply_color(tool_image.Sample(def_sampler, uv), orig);
		}
	}
	
	return orig;
}

technique Draw
{
	pass
	{
		vertex_shader = VSDefault(vert_in);
		pixel_shader = PSDraw(vert_in);
	}
}