-
Notifications
You must be signed in to change notification settings - Fork 36
/
damask.hlsl
212 lines (168 loc) · 5.17 KB
/
damask.hlsl
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
#define WINDOWS_TERMINAL
Texture2D shaderTexture;
SamplerState samplerState;
// --------------------
#if defined(WINDOWS_TERMINAL)
cbuffer PixelShaderSettings {
float Time;
float Scale;
float2 Resolution;
float4 Background;
};
#define TIME Time
#define RESOLUTION Resolution
#else
float time;
float2 resolution;
#define TIME time
#define RESOLUTION resolution
#endif
// --------------------
// --------------------
// GLSL => HLSL adapters
#define vec2 float2
#define vec3 float3
#define vec4 float4
#define mat2 float2x2
#define mat3 float3x3
#define fract frac
#define mix lerp
float mod(float x, float y) {
return x - y * floor(x/y);
}
vec2 mod(vec2 x, vec2 y) {
return x - y * floor(x/y);
}
static const vec2 unit2 = vec2(1.0, 1.0);
static const vec3 unit3 = vec3(1.0, 1.0, 1.0);
static const vec4 unit4 = vec4(1.0, 1.0, 1.0, 1.0);
// --------------------
#define PI 3.141592654
#define PI_2 (0.5*PI)
#define TAU (2.0*PI)
#define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a))
static const float hf = 0.015;
// License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488
static const vec4 hsv2rgb_K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 hsv2rgb(vec3 c) {
vec3 p = abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www);
return c.z * mix(hsv2rgb_K.xxx, clamp(p - hsv2rgb_K.xxx, 0.0, 1.0), c.y);
}
// License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488
// Macro version of above to enable compile-time constants
#define HSV2RGB(c) (c.z * mix(hsv2rgb_K.xxx, clamp(abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www) - hsv2rgb_K.xxx, 0.0, 1.0), c.y))
// License: Unknown, author: nmz (twitter: @stormoid), found: https://www.shadertoy.com/view/NdfyRM
vec3 sRGB(vec3 t) {
return mix(1.055*pow(t, unit3*(1./2.4)) - 0.055, 12.92*t, step(t, unit3*(0.0031308)));
}
// License: Unknown, author: Matt Taylor (https://github.com/64), found: https://64.github.io/tonemapping/
vec3 aces_approx(vec3 v) {
v = max(v, 0.0);
v *= 0.6f;
float a = 2.51f;
float b = 0.03f;
float c = 2.43f;
float d = 0.59f;
float e = 0.14f;
return clamp((v*(a*v+b))/(v*(c*v+d)+e), 0.0f, 1.0f);
}
// License: Unknown, author: Unknown, found: don't remember
float tanh_approx(float x) {
// return tanh(x);
float x2 = x*x;
return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0);
}
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/smin/smin.htm
float pmin(float a, float b, float k) {
float h = clamp(0.5+0.5*(b-a)/k, 0.0, 1.0);
return mix(b, a, h) - k*h*(1.0-h);
}
float pabs(float a, float k) {
return -pmin(a, -a, k);
}
float height(vec2 p) {
float tm = TIME;
const float xm = 0.5*0.005123;
float ym = mix(0.125, 0.25, 0.5-0.5*cos(TAU*TIME/600.0));
p *= 0.4;
float d = length(p);
p = mul(ROT(0.05*tm-2.0*d), p);
float c = 1E6;
float x = pow(d, 0.1);
float y = atan2(p.x, p.y) / TAU;
for (float i = 0.; i < 3.; ++i) {
float v = length(fract(vec2(x - tm*i*xm, fract(y + i*ym)*.5)*20.)*2.-1.);
c = pmin(c, v, 0.125);
}
float h = (-hf+hf*(pabs(tanh_approx(5.5*d-80.*c*c*d*d*(.55-d))-0.25*d, 0.25)));
return h;
}
vec3 normal(vec2 p) {
vec2 v;
vec2 w;
vec2 e = vec2(4.0/RESOLUTION.y, 0);
vec3 n;
n.x = height(p + e.xy) - height(p - e.xy);
n.y = -2.0*e.x;
n.z = height(p + e.yx) - height(p - e.yx);
return normalize(n);
}
vec3 effect(vec2 p) {
const float ss = 1.25;
const float hh = 1.95;
const vec3 lp1 = -vec3(1.0 , hh, -1.0)*vec3(ss, 1.0, ss);
const vec3 lp2 = -vec3(-1.0, hh, -1.0)*vec3(ss, 1.0, ss);
const vec3 lcol1 = HSV2RGB(vec3(0.30, 0.35, 2.0));
const vec3 lcol2 = HSV2RGB(vec3(0.57, 0.6 , 2.0));
const vec3 mat = HSV2RGB(vec3(0.55, 0.9, 0.05));
const float spe = 16.0;
float h = height(p);
vec3 n = normal(p);
vec3 ro = vec3(0.0, 8.0, 0.0);
vec3 pp = vec3(p.x, 0.0, p.y);
vec3 po = vec3(p.x, 0.0, p.y);
vec3 rd = normalize(ro - po);
vec3 ld1 = normalize(lp1 - po);
vec3 ld2 = normalize(lp2 - po);
float diff1 = max(dot(n, ld1), 0.0);
float diff2 = max(dot(n, ld2), 0.0);
vec3 rn = n;
vec3 ref = reflect(rd, rn);
float ref1 = max(dot(ref, ld1), 0.0);
float ref2 = max(dot(ref, ld2), 0.0);
float fre = 1.0+dot(n, rd);
float dm = tanh_approx(abs(h)*120.0);
float rm = dm;
dm *= dm;
vec3 lpow1 = dm*mat*lcol1;
vec3 lpow2 = dm*mat*lcol2;
vec3 col = unit3*(0.0);
col += diff1*diff1*lpow1;
col += diff2*diff2*lpow2;
col += rm*pow(ref1, spe)*lcol1;
col += rm*pow(ref2, spe)*lcol2;
return col;
}
//
// PS_OUTPUT ps_main(in PS_INPUT In)
#if defined(WINDOWS_TERMINAL)
float4 main(float4 pos : SV_POSITION, float2 tex : TEXCOORD) : SV_TARGET
#else
float4 ps_main(float4 pos : SV_POSITION, float2 tex : TEXCOORD) : SV_TARGET
#endif
{
vec2 q = tex;
vec2 p = -1.0 + 2.0*q;
#if defined(WINDOWS_TERMINAL)
p.y = -p.y;
#endif
p.x *= RESOLUTION.x/RESOLUTION.y;
vec3 col = effect(p);
col = aces_approx(col);
col = sRGB(col);
vec4 fg = shaderTexture.Sample(samplerState, q);
vec4 sh = shaderTexture.Sample(samplerState, q-2.0*unit2/RESOLUTION.xy);
col = mix(col, 0.0*unit3, sh.w);
col = mix(col, fg.xyz, fg.w);
return vec4(col, 1.0);
}