Files
gitea-docker/desktop/src-common/advent/screens/shader.clj
2017-06-01 08:05:02 -07:00

132 lines
3.1 KiB
Clojure

(ns advent.screens.shader
(:require [advent.utils :as utils]))
(println "loading " *ns*)
(def v-shader (constantly "attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoords;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
gl_Position = u_projTrans * a_position;
}"))
(def mobile-pix-shader
"
#ifdef GL_ES
#define LOWP lowp
#define HIGHP highp
precision mediump float;
#else
#define LOWP
#define HIGHP
#endif
varying vec4 v_color;
varying HIGHP vec2 v_texCoords;
uniform HIGHP float multiply_amount;
uniform HIGHP float hue_amount;
uniform sampler2D u_texture;
vec4 toGrayscale(in vec4 color)
{
float average = (color.r + color.g + color.b) / 3.0;
return vec4(average, average, average, color.a);
}
vec4 colorize(in vec4 grayscale, in vec4 color)
{
return (grayscale * color);
}
void main ()
{
vec4 scaledColor = texture2D(u_texture, v_texCoords);
vec4 grayscale = toGrayscale(scaledColor);
vec4 colorizedOutput = mix(scaledColor, colorize(grayscale, v_color.rgba), hue_amount);
vec4 multiplied = mix(colorizedOutput.rgba, v_color.rgba * scaledColor.rgba, multiply_amount);
gl_FragColor = vec4(multiplied.rgb, scaledColor.a * v_color.a);
}
"
)
(def desktop-pix-shader
"
#ifdef GL_ES
#define LOWP lowp
#define HIGHP highp
precision mediump float;
#else
#define LOWP
#define HIGHP
#endif
varying vec4 v_color;
varying HIGHP vec2 v_texCoords;
uniform HIGHP float multiply_amount;
uniform HIGHP float hue_amount;
uniform sampler2D u_texture;
vec4 toGrayscale(in vec4 color)
{
float average = (color.r + color.g + color.b) / 3.0;
return vec4(average, average, average, color.a);
}
vec4 colorize(in vec4 grayscale, in vec4 color)
{
return (grayscale * color);
}
void main ()
{
HIGHP vec2 sz = vec2 (2048.0, 2048.0);
HIGHP vec3 step = vec3 (1.0 / 4.0, 1.0 / 4.0, 0.0);
HIGHP vec2 tex_pixel = sz * v_texCoords - step.xy / 2.0;
HIGHP vec2 corner = floor (tex_pixel) + 1.0;
HIGHP vec2 frac = min ((corner - tex_pixel) * vec2 (4.0, 4.0), vec2 (1.0, 1.0));
HIGHP vec4 c1 = texture2D (u_texture, (floor (tex_pixel + step.zz) + 0.5) / sz);
HIGHP vec4 c2 = texture2D (u_texture, (floor (tex_pixel + step.xz) + 0.5) / sz);
HIGHP vec4 c3 = texture2D (u_texture, (floor (tex_pixel + step.zy) + 0.5) / sz);
HIGHP vec4 c4 = texture2D (u_texture, (floor (tex_pixel + step.xy) + 0.5) / sz);
c1 *= frac.x * frac.y;
c2 *= (1.0 - frac.x) * frac.y;
c3 *= frac.x * (1.0 - frac.y);
c4 *= (1.0 - frac.x) * (1.0 - frac.y);
vec4 scaledColor = (c1 + c2 + c3 + c4);
vec4 grayscale = toGrayscale(scaledColor);
vec4 colorizedOutput = mix(scaledColor, colorize(grayscale, v_color.rgba), hue_amount);
vec4 multiplied = mix(colorizedOutput.rgba, v_color.rgba * scaledColor.rgba, multiply_amount);
gl_FragColor = vec4(multiplied.rgb, scaledColor.a * v_color.a);
}
")
(defn pix-shader []
(if utils/mobile?
mobile-pix-shader
desktop-pix-shader))