1#version 120
2// Compatibility #ifdefs needed for parameters
3#ifdef GL_ES
4#define COMPAT_PRECISION mediump
5#else
6#define COMPAT_PRECISION
7#endif
8
9// Parameter lines go here:
10#pragma parameter RETRO_PIXEL_SIZE "Retro Pixel Size" 0.84 0.0 1.0 0.01
11#ifdef PARAMETER_UNIFORM
12// All parameter floats need to have COMPAT_PRECISION in front of them
13uniform COMPAT_PRECISION float RETRO_PIXEL_SIZE;
14#else
15#define RETRO_PIXEL_SIZE 0.84
16#endif
17
18#if defined(VERTEX)
19
20#if __VERSION__ >= 130
21#define COMPAT_VARYING out
22#define COMPAT_ATTRIBUTE in
23#define COMPAT_TEXTURE texture
24#else
25#define COMPAT_VARYING varying
26#define COMPAT_ATTRIBUTE attribute
27#define COMPAT_TEXTURE texture2D
28#endif
29
30#ifdef GL_ES
31#define COMPAT_PRECISION mediump
32#else
33#define COMPAT_PRECISION
34#endif
35
36COMPAT_ATTRIBUTE vec4 VertexCoord;
37COMPAT_ATTRIBUTE vec4 COLOR;
38COMPAT_ATTRIBUTE vec4 TexCoord;
39COMPAT_VARYING vec4 COL0;
40COMPAT_VARYING vec4 TEX0;
41// out variables go here as COMPAT_VARYING whatever
42
43vec4 _oPosition1;
44uniform mat4 MVPMatrix;
45uniform COMPAT_PRECISION int FrameDirection;
46uniform COMPAT_PRECISION int FrameCount;
47uniform COMPAT_PRECISION vec2 OutputSize;
48uniform COMPAT_PRECISION vec2 TextureSize;
49uniform COMPAT_PRECISION vec2 InputSize;
50
51// compatibility #defines
52#define vTexCoord TEX0.xy
53#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
54#define OutSize vec4(OutputSize, 1.0 / OutputSize)
55
56void main()
57{
58    gl_Position = MVPMatrix * VertexCoord;
59    TEX0.xy = VertexCoord.xy;
60// Paste vertex contents here:
61}
62
63#elif defined(FRAGMENT)
64
65#if __VERSION__ >= 130
66#define COMPAT_VARYING in
67#define COMPAT_TEXTURE texture
68out vec4 FragColor;
69#else
70#define COMPAT_VARYING varying
71#define FragColor gl_FragColor
72#define COMPAT_TEXTURE texture2D
73#endif
74
75#ifdef GL_ES
76#ifdef GL_FRAGMENT_PRECISION_HIGH
77precision highp float;
78#else
79precision mediump float;
80#endif
81#define COMPAT_PRECISION mediump
82#else
83#define COMPAT_PRECISION
84#endif
85
86uniform COMPAT_PRECISION int FrameDirection;
87uniform COMPAT_PRECISION int FrameCount;
88uniform COMPAT_PRECISION vec2 OutputSize;
89uniform COMPAT_PRECISION vec2 TextureSize;
90uniform COMPAT_PRECISION vec2 InputSize;
91uniform sampler2D Texture;
92COMPAT_VARYING vec4 TEX0;
93// in variables go here as COMPAT_VARYING whatever
94
95// compatibility #defines
96#define Source Texture
97#define vTexCoord TEX0.xy
98
99#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
100#define OutSize vec4(OutputSize, 1.0 / OutputSize)
101
102// delete all 'params.' or 'registers.' or whatever in the fragment
103float iGlobalTime = float(FrameCount)*0.025;
104vec2 iResolution = OutputSize.xy;
105
106float sphere
107	(vec3 ray, vec3 dir, vec3 center, float radius, vec3 color, inout vec3 nml, inout vec3 mat, float closestHit)
108{
109	vec3 rc = ray-center;
110	float c = dot(rc, rc) - (radius*radius);
111	float b = dot(dir, rc);
112	float d = b*b - c;
113	float t = -b - sqrt(abs(d));
114	float st = step(0.0, min(t,d)) * step(t, closestHit);
115	closestHit = mix(closestHit, t, st);
116	nml = mix(nml, (center-(ray+dir*t)) / radius, st);
117	mat = mix(mat, color, st);
118	return closestHit;
119}
120
121float scene(float t, vec3 ro, vec3 rd, inout vec3 nml, inout vec3 mat, float dist)
122{
123	dist = sphere(ro, rd, vec3(0.0), 1.0, vec3(0.5, 0.8, 1.0), nml, mat, dist);
124	dist = sphere(ro, rd,
125				  vec3(sin(t*3.0)*3.0, cos(t*3.0)*3.0, cos(t)*8.0),
126				  1.5, vec3(1.0, 0.8, 1.0),
127				  nml, mat, dist);
128	dist = sphere(ro, rd,
129				  vec3(sin(t*3.0)*-3.0, cos(t*3.0)*-3.0, sin(t)*8.0),
130				  1.5, vec3(0.5, 0.8, 0.5),
131				  nml, mat, dist);
132	return dist;
133}
134
135vec3 background(float t, vec3 rd)
136{
137	vec3 sunColor = vec3(2.0, 1.6, 1.0);
138	vec3 skyColor = vec3(0.5, 0.6, 0.7);
139	vec3 sunDir = normalize(vec3(sin(t), sin(t*1.2), cos(t)));
140	return
141		pow(max(0.0, dot(sunDir, rd)), 128.0)*sunColor +
142		0.2*pow(max(0.0, dot(sunDir, rd)), 2.0)*sunColor +
143		pow(max(0.0, -dot(vec3(0.0, 1.0, 0.0), rd)), 1.0)*(1.0-skyColor) +
144		pow(max(0.0, dot(vec3(0.0, 1.0, 0.0), rd)), 1.0)*skyColor;
145}
146
147void mainImage( out vec4 fragColor, in vec2 fragCoord )
148{
149	vec2 uv =
150		(-1.0 + 2.0*fragCoord.xy / iResolution.xy) *
151		vec2(iResolution.x/iResolution.y, 1.0);
152	vec3 light = vec3(0.0); // How much light hits the eye through the ray.
153
154	float epsilon = 0.001;
155	float maxDist = 1e5;
156
157	const int mblur_count = 3;  // How many motion blur rays we trace.
158	const int bounce_count = 3; // How many scene rays we trace.
159
160	float exposureTime = 1.0/15.0;
161	vec2 tuv = vec2(float(mblur_count), 1.0)*(fragCoord.xy / 256.0);
162
163	for (int j=0; j<mblur_count; j++) {
164		float t = iGlobalTime + exposureTime*((float(j)+2.0*(0.5))/float(mblur_count));
165		vec3 ro = vec3(0.0, 0.0, -6.0);     // Ray origin.
166		vec3 rd = normalize(vec3(uv, 1.0)); // Ray direction.
167		vec3 transmit = vec3(1.0);          // How much light the ray lets through.
168
169		for (int i=0; i<bounce_count; i++) {
170			vec3 mat, nml;
171			float dist = scene(t, ro, rd, nml, mat, maxDist);
172			if (dist < maxDist) { // Object hit.
173				transmit *= mat;       // Make the ray more opaque.
174				ro += rd*dist;         // Move the ray to the hit point.
175				rd = reflect(rd, nml); // Reflect the ray.
176				// Move the ray off the surface to avoid hitting the same point twice.
177				ro += rd*epsilon;
178			} else { // Background hit.
179				// Put the background light through the ray
180				// and add it to the light seen by the eye.
181				light += transmit * background(t,rd);
182				break; // Don't bounce off the background.
183			}
184		}
185	}
186	light /= float(mblur_count);
187	fragColor = vec4(light, 1.0);
188}
189
190 void main(void)
191{
192  //just some shit to wrap shadertoy's stuff
193  vec2 FragCoord = vTexCoord.xy*OutputSize.xy;
194  mainImage(FragColor,FragCoord);
195}
196#endif
197