summaryrefslogtreecommitdiff
path: root/src/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'src/shaders')
-rw-r--r--src/shaders/lake.frag92
-rw-r--r--src/shaders/land.frag72
-rw-r--r--src/shaders/land.vert16
-rw-r--r--src/shaders/stars.frag5
-rw-r--r--src/shaders/stars.lua77
-rw-r--r--src/shaders/stars.vert30
6 files changed, 238 insertions, 54 deletions
diff --git a/src/shaders/lake.frag b/src/shaders/lake.frag
index d45917e..c1c23f0 100644
--- a/src/shaders/lake.frag
+++ b/src/shaders/lake.frag
@@ -1,5 +1,93 @@
precision mediump float;
-uniform vec4 color;
+uniform vec4 black;
+uniform vec4 outline;
+uniform vec4 highlight;
+uniform vec4 foreground;
+uniform vec4 midground;
+uniform vec4 shadow;
+uniform vec4 background;
+uniform vec4 lamp1; //vec3 position, float strength
+uniform vec4 lamp2;
+uniform vec4 lamp3; // max 3 lamps per shaded vertex
+uniform vec4 lamp4;
+uniform vec4 lamp5;
+uniform vec4 lamp6;
+uniform vec4 lamp7;
+uniform vec4 lamp8;
+uniform float streamer; // turn off the noise in the light
+uniform float time; //used for noise
+uniform sampler2D atlas;
+uniform sampler2D previous;
+uniform float nlamps;
+varying vec2 worldxy;
+varying vec2 norm;
+
+// Author @patriciogv - 2015
+float random (vec2 st) {
+ return fract(
+ sin(
+ dot(st.xy,vec2(12.9898,78.233))
+ ) *
+ 43758.5453123
+ );
+}
+
+// stolen from https://www.shadertoy.com/view/Msf3WH
+float noise( in vec2 p )
+{
+ const float K1 = 0.366025404; // (sqrt(3)-1)/2;
+ const float K2 = 0.211324865; // (3-sqrt(3))/6;
+
+ vec2 i = floor( p + (p.x+p.y)*K1 );
+ vec2 a = p - i + (i.x+i.y)*K2;
+ float m = step(a.y,a.x);
+ vec2 o = vec2(m,1.0-m);
+ vec2 b = a - o + K2;
+ vec2 c = a - 1.0 + 2.0*K2;
+ vec3 h = max( 0.5-vec3(dot(a,a), dot(b,b), dot(c,c) ), 0.0 );
+ vec3 n = h*h*h*h*vec3( dot(a,random(i+0.0)), dot(b,random(i+o)), dot(c,random(i+1.0)));
+ return dot( n, vec3(70.0) );
+}
+
void main() {
- gl_FragColor = color;
+ vec4 coord = gl_FragCoord + vec4(worldxy * 256., 0, 0);
+ /*
+ coord.x -= worldxy.x;
+ coord.y -= worldxy.y;
+ */
+ //coord = coord / 1000.;
+ // calculate color at this pixel
+ vec4 normal = texture2D(atlas, norm);
+ float color = 0.;
+ vec4 lamp1_norm = lamp1 * 256.;
+ color += lamp1_norm.w - distance(lamp1_norm.xy - worldxy, coord.xy);
+ color = max(color,(lamp2.w * 256.) - distance((lamp2.xy * 256.) - worldxy, coord.xy));
+ color = max(color,(lamp3.w * 256.) - distance((lamp3.xy * 256.) - worldxy, coord.xy));
+ // divide to get a normalized color
+ //color /= (256. * max(max(lamp1.w, lamp2.w), lamp3.w));
+ color /= 256. * 5.;
+ //color = sqrt(color / 2046.);
+ // Sett the normal texture under our lamplight
+ color = dot(vec4(color),normal) / 1.;
+ // make the colors fuzzy near the border (or don't if we're streaming)
+ float rng = random(vec2(coord.x, coord.y) + vec2(color, time));
+ color -= (pow(rng / 1.3, 10. * color)) * streamer;
+ // add noise to the water
+ /* */
+ if(color > 1.)
+ gl_FragColor = highlight * normal.a;
+ else if(color > 0.8)
+ gl_FragColor = foreground * normal.a;
+ else if(color > 0.6)
+ gl_FragColor = midground * normal.a;
+ else if(color > 0.4)
+ gl_FragColor = background * normal.a;
+ else if(color > 0.2)
+ gl_FragColor = shadow * normal.a;
+ else
+ gl_FragColor = black * normal.a;
+ /*
+ gl_FragColor = normal* vec4(color , color, color,1.);
+ */
+ //gl_FragColor = normal* vec4(color , color / 10., color / 1024.,1.);
}
diff --git a/src/shaders/land.frag b/src/shaders/land.frag
index 3c23990..1eb8d93 100644
--- a/src/shaders/land.frag
+++ b/src/shaders/land.frag
@@ -9,8 +9,18 @@ uniform vec4 background;
uniform vec4 lamp1; //vec3 position, float strength
uniform vec4 lamp2;
uniform vec4 lamp3; // max 3 lamps per shaded vertex
+uniform vec4 lamp4;
+uniform vec4 lamp5;
+uniform vec4 lamp6;
+uniform vec4 lamp7;
+uniform vec4 lamp8;
+uniform float streamer; // turn off the noise in the light
+uniform float time; //used for noise
+uniform sampler2D atlas;
+uniform float nlamps;
+uniform float water;
varying vec2 worldxy;
-varying mat4 pre;
+varying vec2 norm;
// Author @patriciogv - 2015
float random (vec2 st) {
@@ -22,6 +32,28 @@ float random (vec2 st) {
);
}
+// stolen from https://www.shadertoy.com/view/Msf3WH
+vec2 hash( vec2 p ) // replace this by something better
+{
+ p = vec2( dot(p,vec2(127.1,311.7)), dot(p,vec2(269.5,183.3)) );
+ return -1.0 + 2.0*fract(sin(p)*43758.5453123);
+}
+float noise( in vec2 p )
+{
+ const float K1 = 0.366025404; // (sqrt(3)-1)/2;
+ const float K2 = 0.211324865; // (3-sqrt(3))/6;
+
+ vec2 i = floor( p + (p.x+p.y)*K1 );
+ vec2 a = p - i + (i.x+i.y)*K2;
+ float m = step(a.y,a.x);
+ vec2 o = vec2(m,1.0-m);
+ vec2 b = a - o + K2;
+ vec2 c = a - 1.0 + 2.0*K2;
+ vec3 h = max( 0.5-vec3(dot(a,a), dot(b,b), dot(c,c) ), 0.0 );
+ vec3 n = h*h*h*h*vec3( dot(a,hash(i+0.0)), dot(b,hash(i+o)), dot(c,hash(i+1.0)));
+ return dot( n, vec3(70.0) );
+}
+
void main() {
vec4 coord = gl_FragCoord + vec4(worldxy * 256., 0, 0);
/*
@@ -29,22 +61,40 @@ void main() {
coord.y -= worldxy.y;
*/
//coord = coord / 1000.;
+ // calculate color at this pixel
+ vec4 normal = texture2D(atlas, norm);
float color = 0.;
- vec2 lamppos = (lamp1.xy * 256.) - (worldxy * 1.);
- color += (lamp1.w * 256.) - distance((lamp1.xy * 256.) - worldxy, coord.xy);
- /*
+ vec4 lamp1_norm = lamp1 * 256.;
+ color += lamp1_norm.w - distance(lamp1_norm.xy - worldxy, coord.xy);
+ color = max(color,(lamp2.w * 256.) - distance((lamp2.xy * 256.) - worldxy, coord.xy));
+ color = max(color,(lamp3.w * 256.) - distance((lamp3.xy * 256.) - worldxy, coord.xy));
+ // divide to get a normalized color
+ //color /= (256. * max(max(lamp1.w, lamp2.w), lamp3.w));
+ color /= 256. * 5.;
+ //color = sqrt(color / 2046.);
+ // see the normal texture under the color
+ color = dot(vec4(color),normal) / 1.;
+ // make the colors fuzzy near the border (or don't if we're streaming)
+ float rng = random(vec2(coord.x, coord.y) + vec2(color, time));
+ color -= (pow(rng / 1.3, 10. * color)) * streamer;
+ // add noise to water
+ if(water > 1.)
+ color += (noise(coord.xy + vec2(time, time)) - 0.0) * 0.1;
+ /* */
if(color > 1.)
- gl_FragColor = highlight;
+ gl_FragColor = highlight * normal.a;
else if(color > 0.8)
- gl_FragColor = foreground;
+ gl_FragColor = foreground * normal.a;
else if(color > 0.6)
- gl_FragColor = midground;
+ gl_FragColor = midground * normal.a;
else if(color > 0.4)
- gl_FragColor = background;
+ gl_FragColor = background * normal.a;
else if(color > 0.2)
- gl_FragColor = shadow;
+ gl_FragColor = shadow * normal.a;
else
- gl_FragColor = black;
+ gl_FragColor = black * normal.a;
+ /*
+ gl_FragColor = normal* vec4(color , color, color,1.);
*/
- gl_FragColor = vec4(color / (256. * lamp1.w), color / (256. * lamp1.w), color / (256. * lamp1.w),1.);
+ //gl_FragColor = normal* vec4(color , color / 10., color / 1024.,1.);
}
diff --git a/src/shaders/land.vert b/src/shaders/land.vert
index 2004ab3..1c9b9f3 100644
--- a/src/shaders/land.vert
+++ b/src/shaders/land.vert
@@ -1,14 +1,26 @@
precision highp float;
attribute vec3 land;
-uniform float time; //used for noise
+attribute vec2 landnormal;
+uniform float rot;
uniform float world_x;
uniform float world_y;
uniform mat4 MV;
uniform mat4 P;
varying vec2 worldxy;
varying mat4 pre;
+varying vec2 norm;
void main() {
+ norm = landnormal;
+ mat2 rotate = mat2(
+ cos(rot), -sin(rot),
+ sin(rot), cos(rot)
+ );
worldxy = vec2(world_x, world_y);
pre = P * MV;
- gl_Position = P * MV * vec4(land.x - world_x, land.y - world_y, 0., 1.0);
+ vec2 local = (land.xy - worldxy) * rotate;
+ float z_scale = 0.5;
+ // clamp so that everything becomes orthographic once we move away
+ float xoff = clamp(land.z * local.x * z_scale, -0.5, 0.5);
+ float yoff = clamp(land.z * local.y * z_scale, -0.5, 0.5);
+ gl_Position = P * MV * vec4(local.xy - vec2(xoff, yoff), land.z, 1.0);
}
diff --git a/src/shaders/stars.frag b/src/shaders/stars.frag
new file mode 100644
index 0000000..d45917e
--- /dev/null
+++ b/src/shaders/stars.frag
@@ -0,0 +1,5 @@
+precision mediump float;
+uniform vec4 color;
+void main() {
+ gl_FragColor = color;
+}
diff --git a/src/shaders/stars.lua b/src/shaders/stars.lua
index 8d54b16..3e4138e 100644
--- a/src/shaders/stars.lua
+++ b/src/shaders/stars.lua
@@ -1,57 +1,43 @@
local win = require("window")
local color = require("color")
local world = require("world")
-local numstars = 400 -- we might have as many as 4 over
+local shim = require("shader_shim")
+local numstars = 500 -- we might have as many as 4 over
local genned_stars = 0
local period_x = 3
local period_y = 3
local stars = {}
-while genned_stars < numstars do
+local tries = 0
+aspect = win.width / win.height
+while genned_stars < numstars and tries < 100000 do
local rngx = math.random()
local xpos = rngx * win.width --* (period_x - 1)
local rngy = math.random()
local ypos = rngy * win.height --* (period_y - 1)
local blinks = math.random() > 0.3 and (math.random() * 2 * math.pi) or 0
- stars[#stars+1] = vec3(xpos, ypos, blinks)
- genned_stars = genned_stars + 1
- if xpos < win.width then
- -- duplicate on the last screen
- stars[#stars+1] = vec3(xpos + (win.width * (period_x-2)), ypos, blinks)
- genned_stars = genned_stars + 1
- end
- if ypos < win.height then
- stars[#stars+1] = vec3(xpos, ypos + (win.height * (period_y-2)), blinks)
- genned_stars = genned_stars + 1
- end
- if xpos < win.width and ypos < win.height then
- stars[#stars+1] = vec3(xpos + (win.width * (period_x-2)), ypos+(win.height * (period_y-2)),blinks)
+ --if math.distance(vec2(rngx,rngy), vec2(0.53,0.5)) > 0.5 then
+ local off = vec2(math.abs(rngx - 0.50) * aspect, math.abs(rngy-0.5))
+ if math.length(off) > 0.5 then
+ stars[#stars+1] = vec3(xpos, ypos, blinks)
genned_stars = genned_stars + 1
+ if xpos < win.width then
+ -- duplicate on the last screen
+ stars[#stars+1] = vec3(xpos + (win.width * (period_x-2)), ypos, blinks)
+ genned_stars = genned_stars + 1
+ end
+ if ypos < win.height then
+ stars[#stars+1] = vec3(xpos, ypos + (win.height * (period_y-2)), blinks)
+ genned_stars = genned_stars + 1
+ end
+ if xpos < win.width and ypos < win.height then
+ stars[#stars+1] = vec3(xpos + (win.width * (period_x-2)), ypos+(win.height * (period_y-2)),blinks)
+ genned_stars = genned_stars + 1
+ end
end
+ tries = tries + 1
end
-local node = am.use_program(am.program([[
- precision highp float;
- attribute vec3 stars;
- uniform float time;
- uniform float world_x;
- uniform float world_y;
- uniform float world_x_period;
- uniform float world_y_period;
- uniform mat4 MV;
- uniform mat4 P;
- void main() {
- float world_x_off = mod(world_x, world_x_period);
- float world_y_off = mod(world_y, world_y_period);
- gl_Position = P * MV * vec4(stars.x - world_x_off, stars.y - world_y_off, 0., 1.0);
- float intensity = sin(stars.z + time) * cos(time) + 1.;
- gl_PointSize = pow(intensity, 2.) * stars.z * 0.3;
- }
- ]],[[
- precision mediump float;
- uniform vec4 color;
- void main() {
- gl_FragColor = color;
- }
-]]))
+assert(genned_stars == numstars, "Failed to generate stars")
+local node = am.blend("premult") ^ shim.stars
^ am.bind({
MV = mat4(
1, 0, 0, 0,
@@ -66,11 +52,24 @@ local node = am.use_program(am.program([[
world_y = am.current_time(),
world_y_period = (period_y - 2) * win.height,
time = am.current_time(),
+ lamp1 = vec3(0),
+ lamp2 = vec3(0),
+ lamp3 = vec3(0),
+ lamp4 = vec3(0),
+ lamp5 = vec3(0),
+ lamp6 = vec3(0),
+ lamp7 = vec3(0),
+ lamp8 = vec3(0)
})
^ am.draw("points")
node:action(function(self)
self("bind").time = am.current_time()
self("bind").world_x = world.world_x
self("bind").world_y = world.world_y
+ local lamps = world.level.lamps_on_screen()
+ for i,v in pairs(lamps) do
+ print("Setting lamp", i, "to", v)
+ self("bind")["lamp" .. tostring(i)] = v
+ end
end)
return node
diff --git a/src/shaders/stars.vert b/src/shaders/stars.vert
new file mode 100644
index 0000000..8737482
--- /dev/null
+++ b/src/shaders/stars.vert
@@ -0,0 +1,30 @@
+precision highp float;
+attribute vec3 stars;
+uniform float time;
+uniform float world_x;
+uniform float world_y;
+uniform float world_x_period;
+uniform float world_y_period;
+uniform vec4 lamp1;
+uniform vec4 lamp2;
+uniform vec4 lamp3;
+uniform vec4 lamp4;
+uniform vec4 lamp5;
+uniform vec4 lamp6;
+uniform vec4 lamp7;
+uniform vec4 lamp8;
+uniform mat4 MV;
+uniform mat4 P;
+void main() {
+ float world_x_off = mod(world_x, world_x_period);
+ float world_y_off = mod(world_y, world_y_period);
+ vec4 pos = P * MV * vec4(stars.x - world_x_off, stars.y - world_y_off, -0.1, 1.0);
+ gl_Position = pos;
+ float intensity = sin(stars.z + time) * cos(time) + 1.;
+ /*
+ if(distance(pos.xyz, lamp1.xyz) < 80.)
+ intensity = 0.;
+ */
+ gl_PointSize = pow(intensity, 2.) * stars.z * 0.3;
+ //gl_PointSize = distance(pos.xyz, lamp1.xyz);
+}