GLSLBackground_shaders.cs 5.94 KB
Newer Older
pcy's avatar
pcy committed
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
namespace glsltest {
	public partial class GLSLBackground {
		// simple passthru vertex shader
		readonly static string vsh =
"#version 330 core\n"+
"\n"+
"in vec3 in_pos;\n"+
"in vec4 in_col;\n"+
"in vec2 in_texcoord;\n"+
"\n"+
"out vec2 out_texcoord;\n"+
"\n"+
"void main() {\n"+
"	gl_Position = vec4(in_pos.x, in_pos.y, in_pos.z, 1.0);\n"+
"	out_texcoord = in_texcoord;\n"+
"}\n";

		// default Bonzomatic fragment shader (see https://github.com/Gargaj/Bonzomatic )
		readonly static string fsh_bonzo =
"#version 330 core\n"+
"\n"+
"uniform float fGlobalTime;\n"+
"uniform vec2 v2Resolution;\n"+
"\n"+
"layout(location = 0) out vec4 C;\n"+
"\n"+
"vec4 plas(float time, vec2 v) {\n"+
"	float c = 0.5 + sin(v.x * 10.0) + cos(sin(time + v.y) * 20.0);\n"+
"	return vec4(sin(c * 0.2 + cos(time)), c * 0.15, cos(c * 0.1 + time / .4) * .25, 1.0);\n"+
"}\n"+
"\n"+
"void main() {\n"+
"	float pi = 4.0*atan(1.0);\n"+
"	vec2 uv = vec2(gl_FragCoord.x/v2Resolution.x, gl_FragCoord.y/v2Resolution.y);\n"+
"	uv -= 0.5;\n"+
"	uv /= vec2(v2Resolution.y/v2Resolution.x, 1.);\n"+
"\n"+
"	vec2 m;\n"+
"	m.x = atan(uv.x / uv.y) / pi;\n"+
"	m.y = 1 / length(uv) * .2;\n"+
"	float d = m.y;\n"+
"	m.x += sin(fGlobalTime) * 0.1;\n"+
"	m.y += fGlobalTime * 0.25;\n"+
"\n"+
"	vec4 t = plas(fGlobalTime, m * pi) / d;\n"+
"	C = clamp(t, 0.0, 1.0);\n"+
"}\n";

		// a raymarched scene! (no I'm not going to explain how it works)
		// greets to iq, mercury, shane, and noby alkama blackle #lsc halcy
		// yx evvvvil nusan and countless others
		readonly static string fsh_raymarch = @"
#version 410 core

uniform float fGlobalTime; // in seconds
uniform vec2 v2Resolution; // viewport resolution (in pixels)

layout(location = 0) out vec4 out_color;

// global raymarcher structure (probably?) based on something Shane wrote, iirc

#define PI 4.0*atan(1.0)

const float EPS = 0.005;
const int MAX_ITER = 128;

// stolen from hg_sdf
float pMod1(inout float p,float s){
	float hs=s*0.5;
	float c=floor((p+hs)/s);
	p=mod(p+hs,s)-hs;
	return c;
}
float pModPolar(inout vec2 p, float repetitions) {
	float angle = 2*PI/repetitions;
	float a = atan(p.y, p.x) + angle/2.;
	float r = length(p);
	float c = floor(a/angle);
	a = mod(a,angle) - angle/2.;
	p = vec2(cos(a), sin(a))*r;
	if (abs(c) >= (repetitions/2)) c = abs(c);
	return c;
}

float bigegg(vec3 p) {
	p.xz /= 1.4;
	return length(p)-1.3;
}
float smallspheres(vec3 p) {
	pModPolar(p.xz,16.);
	p.x-=1;
	float c = abs(pMod1(p.y,0.5));
	if (c==0) {
		p.x-=2;
	}else p.x-=1/(c*0.7);
	return length(p)-0.15*(1+sin(fGlobalTime)*0.5);
}

float scene(in vec3 p) {
	p.xz=vec2(p.x*cos(fGlobalTime*0.2)+p.z*sin(fGlobalTime*0.2),
			  p.z*cos(fGlobalTime*0.2)-p.x*sin(fGlobalTime*0.2));
	return min(smallspheres(p),bigegg(p));
}

vec3 getNormal(in vec3 p) {
	return normalize(vec3(
		scene(vec3(p.x+EPS,p.y,p.z))-scene(vec3(p.x-EPS,p.y,p.z)),
		scene(vec3(p.x,p.y+EPS,p.z))-scene(vec3(p.x,p.y-EPS,p.z)),
		scene(vec3(p.x,p.y,p.z+EPS))-scene(vec3(p.x,p.y,p.z-EPS))
	));
}

float march_basic(vec3 o, vec3 d, float t_min, float t_max, out vec3 pos, int mxit) {
	float t = t_min;
	for (int i = 0; i < mxit; ++i) {
		pos = o + d * t;
		float rad = scene(pos);

		t+=rad;
		if (rad < EPS || t > t_max) {
			return t;
		}
	}

	return t_max*2;
}

float march_hg(vec3 o, vec3 d, float t_min, float t_max, out vec3 pos) {
	float O = 1.2;
	float t = t_min;
	float cerr = 1e9;
	float ct = t_min;
	float prad = 0.;
	float sl = 0.;
	float sg = scene(o) < 0. ? -1. : 1.;
	float pxrad=0.5/length(v2Resolution);
	for (int i = 0; i < MAX_ITER; ++i) {
		pos =o+d*t;
		float sr = sg * scene(pos);
		float rr = abs(sr);

		bool sfail = O > 1. && (rr+prad) < sl;
		if (sfail) {
			sl -= O*sl;
			O=1;
		}else {
			sl=sr*O;
		}

		prad = rr;
		float err = rr/t;

		if (!sfail && err < cerr) {
			ct = t;
			cerr = err;
		}
		if (!sfail && err < pxrad || t > t_max) {
			break;
		}
		t+=sl;
	}

	if (t>t_max||cerr>pxrad)return t_max*2.;
	return ct;
}

vec3 envcol(vec3 n) {
	float x = normalize(n).y;
	x = (x+1)*0.5;
	return mix(vec3(0.1,0.1,0.25),vec3(0.4,0.6,0.9), smoothstep(0.,1.,smoothstep(0.,1.,smoothstep(0.,1.,x))));
}

vec3 shadeAt(vec3 p, vec3 n, vec3 camPos, vec3 obj){
	vec3 ret = vec3(0);

	for (int i=0;i<3;++i){
	vec3 lp;// = vec3(2,1,-2);//vec3(2*sin(fGlobalTime*0.5), 0.0, 2*cos(fGlobalTime*0.5));
		if (i==0)lp=vec3(2,1,-2);
		else if (i==1)lp=vec3(-2,1,-2);
		else lp=vec3(2,-1,-2);
	vec3 ld = lp-p;
	vec3 lcolor = vec3(1.);

	float len = length(ld);
	ld /= len;
	float lightAtten = min(1.0 / (0.25*len*len), 1.0);

	vec3 ref = reflect(-ld, n);
	float ambient = .8;
	float specularPower = 64.0;
	float diffuse = max( 0.0, dot(n, ld) );
	float specular = max( 0.0, dot(ref, normalize(camPos-p)));
	specular = pow(specular, specularPower);

	ret+= (obj*(diffuse*0.5+ambient)+specular*0.5)*lcolor*lightAtten/3.;
	}
	return ret;
}

void main(void) {
	vec2 aspect = vec2(v2Resolution.x/v2Resolution.y, 1.0);
	vec2 screenCoords = (2.0*gl_FragCoord.xy/v2Resolution.xy - 1.0)*aspect;

	vec3 lookAt = vec3(0.,0.,0.);
	vec3 camPos = vec3(0., -0.25, -5.);

	vec3 forward = normalize(lookAt-camPos);
	vec3 right = vec3(forward.z, 0., -forward.x);
	vec3 up = normalize(cross(forward,right));

	float FOV = 0.5;

	vec3 ro = camPos;
	vec2 rdo = screenCoords;
	float cth=0.25;
	rdo=vec2(rdo.x*cos(cth)+rdo.y*sin(cth),rdo.y*cos(cth)-rdo.x*sin(cth))*FOV;
	vec3 rd = normalize(forward + rdo.x*right + rdo.y*up);

	vec3 bgcolor = envcol(rd);

	const float clipNear = 0.0;
	const float clipFar = 10.0;
	vec3 sp = vec3(0.);
	float dist = march_hg(ro, rd, clipNear, clipFar, sp);
	if (dist >= clipFar) {
		out_color = vec4(bgcolor, 1.0);
		return;
	}

	vec3 norm = getNormal(sp);

	vec3 sceneColor = vec3(0.0);

	vec3 objColor = envcol(norm);
	vec3 newn=norm,newp=sp+norm*0.1;
	for (int iii=0;iii < 2; ++iii) {
	float d = march_basic(newp, newn, 0., 10., newp, 10);
	if (d < 10.){
		newn= getNormal(newp);
		objColor = shadeAt(newp, newn, camPos, objColor);
		newp+=newn*0.1;
	}}
	sceneColor = shadeAt(sp, norm, camPos, objColor);

	out_color = vec4(clamp(sceneColor, 0.0, 1.0), 1.0);
}
";
	}
}