1// Compatibility #ifdefs needed for parameters
2#ifdef GL_ES
3#define COMPAT_PRECISION mediump
4#else
5#define COMPAT_PRECISION
6#endif
7
8// Parameter lines go here:
9#pragma parameter RETRO_PIXEL_SIZE "Retro Pixel Size" 0.84 0.0 1.0 0.01
10#ifdef PARAMETER_UNIFORM
11// All parameter floats need to have COMPAT_PRECISION in front of them
12uniform COMPAT_PRECISION float RETRO_PIXEL_SIZE;
13#else
14#define RETRO_PIXEL_SIZE 0.84
15#endif
16
17#if defined(VERTEX)
18
19#if __VERSION__ >= 130
20#define COMPAT_VARYING out
21#define COMPAT_ATTRIBUTE in
22#define COMPAT_TEXTURE texture
23#else
24#define COMPAT_VARYING varying
25#define COMPAT_ATTRIBUTE attribute
26#define COMPAT_TEXTURE texture2D
27#endif
28
29#ifdef GL_ES
30#define COMPAT_PRECISION mediump
31#else
32#define COMPAT_PRECISION
33#endif
34
35COMPAT_ATTRIBUTE vec4 VertexCoord;
36COMPAT_ATTRIBUTE vec4 COLOR;
37COMPAT_ATTRIBUTE vec4 TexCoord;
38COMPAT_VARYING vec4 COL0;
39COMPAT_VARYING vec4 TEX0;
40// out variables go here as COMPAT_VARYING whatever
41
42vec4 _oPosition1;
43uniform mat4 MVPMatrix;
44uniform COMPAT_PRECISION int FrameDirection;
45uniform COMPAT_PRECISION int FrameCount;
46uniform COMPAT_PRECISION vec2 OutputSize;
47uniform COMPAT_PRECISION vec2 TextureSize;
48uniform COMPAT_PRECISION vec2 InputSize;
49
50// compatibility #defines
51#define vTexCoord TEX0.xy
52#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
53#define OutSize vec4(OutputSize, 1.0 / OutputSize)
54
55void main()
56{
57    gl_Position = MVPMatrix * VertexCoord;
58    TEX0.xy = VertexCoord.xy;
59// Paste vertex contents here:
60}
61
62#elif defined(FRAGMENT)
63
64#if __VERSION__ >= 130
65#define COMPAT_VARYING in
66#define COMPAT_TEXTURE texture
67out vec4 FragColor;
68#else
69#define COMPAT_VARYING varying
70#define FragColor gl_FragColor
71#define COMPAT_TEXTURE texture2D
72#endif
73
74#ifdef GL_ES
75#ifdef GL_FRAGMENT_PRECISION_HIGH
76precision highp float;
77#else
78precision mediump float;
79#endif
80#define COMPAT_PRECISION mediump
81#else
82#define COMPAT_PRECISION
83#endif
84
85uniform COMPAT_PRECISION int FrameDirection;
86uniform COMPAT_PRECISION int FrameCount;
87uniform COMPAT_PRECISION vec2 OutputSize;
88uniform COMPAT_PRECISION vec2 TextureSize;
89uniform COMPAT_PRECISION vec2 InputSize;
90uniform sampler2D Texture;
91COMPAT_VARYING vec4 TEX0;
92// in variables go here as COMPAT_VARYING whatever
93
94// compatibility #defines
95#define Source Texture
96#define vTexCoord TEX0.xy
97
98#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
99#define OutSize vec4(OutputSize, 1.0 / OutputSize)
100
101// delete all 'params.' or 'registers.' or whatever in the fragment
102float iGlobalTime = float(FrameCount)*0.025;
103vec2 iResolution = OutputSize.xy;
104
105// Somewhere in 1993 -  nimitz - 2014-10-10
106// https://www.shadertoy.com/view/Md2XDD
107
108// Experiments in making low poly terrain.
109
110//Somewhere in 1993 by nimitz (twitter: @stormoid)
111
112#define PALETTE 6.8
113
114//3 to 5 works best
115#define TERRAIN_COMPLEXITY 4.
116#define ITR 100
117#define FAR 700.
118#define time mod(iGlobalTime,500.)
119
120mat2 mm2(in float a){float c = cos(a), s = sin(a);return mat2(c,-s,s,c);}
121float smoothfloor(const in float x, const in float w)
122{
123    return floor(x)+smoothstep(w, 1.-w,fract(x));
124}
125
126vec3 enpos()
127{
128    return vec3(sin(time)*100.+50.,sin(time)*30.+30.,300.+sin(time*.9+sin(time*0.88+0.2))*100.);
129}
130
131//--------------------------------------------------------
132//---------------------------HUD--------------------------
133//--------------------------------------------------------
134
135float square(in vec2 p){ return max(abs(p.x),abs(p.y));}
136float loz(in vec2 p){ return abs(p.x)+abs(p.y);}
137
138//from Dave (https://www.shadertoy.com/view/4djSRW)
139vec2 hash2(float p)
140{
141	vec2 p2  = fract(p * vec2(5.3983, 5.4427));
142    p2 += dot(p2.yx, p2.xy +  vec2(21.5351, 14.3137));
143	return fract(vec2(p2.x * p2.y * 95.4337, p2.x * p2.y * 97.597));
144}
145
146float line( in vec2 a, in vec2 b, in vec2 p )
147{
148	vec2 pa = p - a, ba = b - a;
149	float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
150	return length( pa - ba*h );
151}
152
153float crosshair(in vec2 p , in float tk, in float rt)
154{
155    float d = abs(p.x)+abs(p.y);
156    float a = atan(p.y,p.x);
157    float rz = smoothstep(0.03*tk,.04*tk,abs(d-0.5));
158    d = sin(a*3.+1.59-time*3.5-rt);
159    rz += smoothstep(0.0,.07*tk,d);
160    return rz;
161}
162
163//inspired by otaviogood "runes" (https://www.shadertoy.com/view/MsXSRn)
164float text2(in vec2 p)
165{
166    p = (p+vec2(1.75,-.8))*7.;
167    p.x *= 1.5;
168    float sd = floor(time*8.);
169    vec2 p1 = vec2(0), p2 = hash2(sd);
170    float d= 1.;
171    vec2 fl = vec2(2.,2.);
172    for(float i=0.;i<7.;i++)
173    {
174    	if(hash2(sd+i+10.).x<0.3)continue;
175        p1 = hash2(i+sd);
176    	p2 = hash2(i+sd+1.);
177		p1 = (floor(p1*fl) + .5)/fl;
178		p2 = (floor(p2*fl) + .5)/fl;
179        if (p1 == p2) p2 = vec2(.5);
180    	d = min(line(p1, p2, p), d);
181        p1 = p2;
182    	p2 = hash2(i+sd+3.);
183		p2 = (floor(p2*fl) + .5)/fl;
184    	d = min(line(p1, p2, p), d);
185        p1 = p2;
186    	p2 = hash2(i+sd+5.);
187		p2 = (floor(p2*fl) + .5)/fl;
188        if (p1 == p2)
189        {
190            p2 = hash2(i+sd+7.);
191			p2 = (floor(p2*fl) + .5)/fl;
192        }
193    	d = min(line(p1,p2,p),d);
194        p.x -= .8;
195    }
196
197    d = smoothstep(0.03, .08,d);
198    return d;
199}
200
201vec3 makeHud(in vec2 p, in float seek)
202{
203    float sk1 = smoothstep(0.99, 1., seek);
204    float sk2 = step(1.-sk1, .5);
205    //lens deformation
206    float ll = abs(p.x)+abs(p.y)*0.25;
207    p *= ll * -.3+1.29;
208    p *= 2.;
209    vec3 col = vec3(0);
210    float d= 1.;
211    //crosshairs
212    float rz = crosshair(p*1.1, .9,1.+sk1);
213    rz = min(rz,crosshair(p*2.7,2., -time*6.5-1.1-sk1));
214    //minimap (top right)
215    float d2 = square(p+vec2(-1.45, -0.67))+0.02;
216    d = smoothstep(0.3,0.31,d2);
217    d = max(d,smoothstep(0.35,.55,min(sin(p.x*80.+1.9),sin(p.y*80.+time*15.))+1.4));
218    d = min(d,smoothstep(0.002,0.009,abs(d2-0.3)));
219    vec3 enp = enpos()/1000.;
220    enp.z = 1.-enp.z;
221    float en = smoothstep(0.025, 0.033, loz(enp.xz+p-vec2(1.47, 1.4))) ;
222    en += mod(floor(time*2.5), 2.);
223    d = min(d,en);
224    rz = min(d,rz);
225    //text (top left)
226    rz= min(rz,text2(p));
227    //altitude bars
228    d = min(rz,sin(p.y*100.+sin(time)*20.)*3.+3.);
229    d2 = max(d,(p.x+0.59)*200.);
230    d2 = max(d2,-(p.x+0.66)*200.);
231    float d3 = max(d,(p.x-0.66)*200.);
232    d3 = max(d3,-(p.x-.59)*200.);
233    d2 = min(d2,d3);
234    d2 += smoothstep(0.59, .6, -p.y);
235    d2 += smoothstep(0.59, .6, p.y);
236    rz = min(rz,d2);
237    //bottom left "status"
238    float num = mod(floor(time*12.),12.);
239    vec2 p2 = p+vec2(-1.32,.94);
240    d = 1.;
241    for(float i=0.;i<5.;i++)
242    {
243        d = min(d,length(p2)+float(num==i));
244    	p2.x -= 0.1;
245    }
246    d = smoothstep(0.023,.03,d);
247    rz = min(d,rz);
248
249    vec3 hcol = (sin(vec3(0.35,0.4,0.48)*(3.35)*PALETTE)*0.5+.5);
250    hcol.gb -= sk2;
251    hcol.r += sk2;
252    return hcol*(1.-rz);
253}
254
255//--------------------------------------------------------
256//--------------------------------------------------------
257//--------------------------------------------------------
258
259float tri(in float x)
260{
261    return abs(fract(x)-0.5);
262}
263
264mat2 m2 = mat2( 0.80,  0.60, -0.60,  0.80 );
265float tnoise(in vec2 p)
266{
267    p*=.008;
268    float z=2.;
269	float rz = 0.;
270	for (float i= 1.;i < TERRAIN_COMPLEXITY;i++ )
271	{
272        rz+= tri(p.x+tri(p.y*1.))/z;
273		z = z*2.;
274		p = p*1.8;
275        p*= m2;
276	}
277	return rz*9.;
278}
279
280float oct(in vec3 p){ return dot(vec3(0.5773),abs(p));}
281vec2 ou( vec2 d1, vec2 d2 ){return (d1.x<d2.x) ? d1 : d2;}
282
283vec3 roty(vec3 p, float a)
284{
285    float s = sin(a), c = cos(a);
286    return vec3(c*p.x + s*p.z, p.y, -s*p.x + c*p.z);
287}
288
289vec2 map(vec3 p)
290{
291    //terrain
292    vec2 d = vec2(6.*tnoise(p.xz)+p.y+20.+(tri(p.z*0.001)-0.4)*22.,1.);
293    //xlog(x) seems to work nicely for a valley
294    d.x -= abs(p.x*0.5*log(abs(p.x)))*0.05-8.;
295    //flat water
296    d = ou(d,vec2(p.y+30., 2.));
297    //"enemy"
298    vec3 enp = enpos();
299    enp.z += time*50.;
300    d = ou(d,vec2((oct(roty(p-enp, time*2.5))-6.)*0.66,8.));
301
302	return d;
303}
304
305vec2 march(in vec3 ro, in vec3 rd)
306{
307	float precis = .1;
308    float h=precis*2.0;
309    float d = 0.;
310    float c = 1.;
311    for( int i=0; i<ITR; i++ )
312    {
313        if( abs(h)<precis || d>FAR ) break;
314        d += h;
315	    vec2 res = map(ro+rd*d);
316        h = res.x*1.4;
317        c = res.y;
318    }
319	return vec2(d,c);
320}
321
322vec3 normal(const in vec3 p)
323{
324    vec2 e = vec2(-1., 1.)*.1;
325	return normalize(e.yxx*map(p + e.yxx).x + e.xxy*map(p + e.xxy).x +
326					 e.xyx*map(p + e.xyx).x + e.yyy*map(p + e.yyy).x );
327}
328
329//(from eiffie, who thought it was from iq, dont know who actually wrote it)
330float segm(vec3 ro, vec3 rd, vec3 p1, vec3 p2)
331{
332	vec3 p = p1-ro;
333	vec3 di = p2-ro-p;
334	float proj = dot(rd, di);
335	float m = clamp((dot(rd,p)*proj-dot(p,di))/(dot(di,di)-proj*proj), 0., 1.);
336	p += di*m;
337	p = dot(p, rd)*rd-p;
338    return smoothstep(0.9985,.999,1.-dot(p,p));
339}
340
341void mainImage( out vec4 fragColor, in vec2 fragCoord )
342{
343	vec2 p = fragCoord.xy/iResolution.xy-0.5;
344    vec2 bp = p+0.5;
345	p.x*=iResolution.x/iResolution.y;
346	vec2 um = vec2(0);
347    um.x = 0.5+(smoothstep(-2.,2.,sin(time*.7-0.1))-0.5)*.1;
348    um.y = sin(time+1.)*0.02;
349
350    //camera
351    vec3 ro = vec3((smoothstep(-2., 2., sin(time*0.7+1.57))-0.5)*50., sin(time)*5.-1., time*50.);
352    um.x *= 3.;
353    vec3 eye = normalize(vec3(cos(um.x),um.y*5.,sin(um.x)));
354    vec3 right = normalize(vec3(cos(um.x+1.5708),0.,sin(um.x+1.5708)));
355    mat2 ori = mm2( smoothstep(-.5,.5,sin(time*0.7+0.78))-.5 + smoothfloor(time*0.04,.45)*6.28 );
356    right.xy *= ori;
357    vec3 up = normalize(cross(right,eye));
358	vec3 rd=normalize((p.x*right+p.y*up)*.75+eye);
359
360    vec3 bg = sin(vec3(0.35,0.4,0.48)*11.3*PALETTE)*0.5+.5;
361    vec3 col = bg*floor(-rd.y*50.+6.)*0.06;
362
363    //march
364	vec2 rz = march(ro,rd);
365    if ( rz.x < FAR )
366    {
367        vec3 pos = ro+rz.x*rd;
368        vec3 nor = normal( pos );
369        vec3 ligt = normalize(vec3(-.7,0.2, 0.1));
370        float dif = clamp(dot(nor, ligt), 0., 1.);
371        float fre = pow(clamp(1. + dot(nor, rd), 0., 1.), 2.);
372        if (rz.y == 1.)
373        {
374            float mx = abs(pos.x*.1)-10.;
375            mx = smoothstep(-20.,10.,mx);
376            col = mix(vec3(0.,0.37,0),vec3(0.2,.17,0.15),mx);
377        }
378        else
379            col = sin(vec3(0.35,0.4,0.48)*rz.y*PALETTE)*0.5+.55;
380        col = col*dif + col*0.4 + .3*fre*col;
381    }
382
383    //lasers
384    vec3 enp =enpos();
385    enp.z += time*50.;
386    vec3 rn = enp - ro;
387    float tgt = dot(eye, normalize(rn));
388    if (tgt > .997)
389    {
390        vec3 ray1 = vec3(0.7, 1., -1);
391        vec3 ray2 = vec3(-0.7, 1., -1);
392        ray1.xy *= ori; ray2.xy *= ori;
393        float lz = segm(ro,rd,ro-ray1,up*0.5+ro+(eye-ray1*0.01)*30.);
394        lz += segm(ro,rd,ro-ray2,up*.5+ro+(eye-ray2*0.01)*30.);
395        float sw = mod(floor(time*20.),2.);
396        lz *= sw;
397        col = col*(1.-smoothstep(0.0,1.,lz))+lz*vec3(1.,0.,0.);
398        //hit (cant really have explosions since I don't have a function for hit times)
399        if (tgt > .999)
400        {
401            vec2 d = hash2(time);
402            rd.xy += d*0.03;
403            rn.xy += d*10.;
404            float s = sw*smoothstep(0.9998, .9999,dot(rd,normalize(rn)));
405            col = col*(1.-smoothstep(0., 1., s))+s*vec3(1.-d.x, .0, 0.1);
406        }
407    }
408
409    //hud
410    float lk = 0.;
411    if (tgt > .99)lk = 4.;
412    vec3 hud = makeHud(p,tgt);
413    col = col*(1.-smoothstep(0., 1., hud.y+hud.x+hud.z))+hud;
414    //scanlines
415    col *= (sin(p.y*1.3*iResolution.x)*0.15)*(sin(p.y*10.+time*410.)*0.4)+1.;
416
417	fragColor = vec4( col, 1.0 );
418}
419
420 void main(void)
421{
422  //just some shit to wrap shadertoy's stuff
423  vec2 FragCoord = vTexCoord.xy*OutputSize.xy;
424  mainImage(FragColor,FragCoord);
425}
426#endif
427