forked from baldavenger/DCTLs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrain_Blend.dctl
82 lines (65 loc) · 2.02 KB
/
Grain_Blend.dctl
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
// Grain Blend DCTL
#define RGB_GRAIN // alternative monochromatic
#define SOFTLIGHT // alternative is overlay blend
#define INTENSITY 12 // grain contrast
__DEVICE__ float fract(float A)
{
float B;
B = A - _floor(A);
return B;
}
__DEVICE__ float rand(float x)
{
return fract(_sinf(x) * 100000.0f);
}
__DEVICE__ float intensity(float grain, float contrast)
{
return (grain - 0.5f) * contrast + 0.5f;
}
__DEVICE__ float softLight(float col, float grain)
{
return grain < 0.5f ? 2.0f * col * grain + (col * col) * (1.0f - 2.0f * grain) : 2.0f * col * (1.0f - grain) + _sqrtf(col) * (2.0f * grain - 1.0f);
}
__DEVICE__ float3 softLight(float3 col, float3 grain)
{
return make_float3(softLight(col.x, grain.x), softLight(col.y, grain.y), softLight(col.z, grain.z));
}
__DEVICE__ float overlay(float col, float grain)
{
return col < 0.5f ? (2 * col * grain) : (1 - 2 * (1 - col) * (1 - grain));
}
__DEVICE__ float3 overlay(float3 col, float3 grain)
{
return make_float3(overlay(col.x, grain.x), overlay(col.y, grain.y), overlay(col.z, grain.z));
}
__DEVICE__ float3 transform(int p_Width, int p_Height, int p_X, int p_Y, __TEXTURE__ p_TexR, __TEXTURE__ p_TexG, __TEXTURE__ p_TexB)
{
float3 col = make_float3(_tex2D(p_TexR, p_X, p_Y), _tex2D(p_TexG, p_X, p_Y), _tex2D(p_TexB, p_X, p_Y));
float contrast = (float)INTENSITY * 0.01f;
float iTime = _tex2D(p_TexR, 500, 500) * 20.0f;
float X = (float)p_X + 1.0f;
float Y = (float)(p_Height - p_Y) + 1.0f;
float2 resolution = make_float2((float)p_Width, (float)p_Height);
float2 position = make_float2(X/resolution.x, Y/resolution.y);
float x = position.x * position.y * 100000.0f + iTime;
float f = fract(x);
float i = _floor(x);
x *= 100.0f;
float3 grain;
grain.x = rand(x);
grain.x = intensity(grain.x, contrast);
#ifdef RGB_GRAIN
grain.y = rand(x * iTime);
grain.y = intensity(grain.y, contrast);
grain.z = rand(x + iTime);
grain.z = intensity(grain.z, contrast);
#else
grain.z = grain.y = grain.x;
#endif
#ifdef SOFTLIGHT
col = softLight(col, grain);
#else
col = overlay(col, grain);
#endif
return col;
}