Skip to content
This repository was archived by the owner on Apr 26, 2023. It is now read-only.
483 changes: 108 additions & 375 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion glsl/clear.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
precision highp float;
precision highp int;

#define NUM_GBUFFERS 4
#define NUM_GBUFFERS 2

void main() {
for (int i = 0; i < NUM_GBUFFERS; i++) {
Expand Down
22 changes: 21 additions & 1 deletion glsl/copy.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,30 @@ precision highp int;
uniform sampler2D u_colmap;
uniform sampler2D u_normap;

uniform float u_specExp;
uniform float u_remove;

varying vec3 v_position;
varying vec3 v_normal;
varying vec2 v_uv;

vec3 applyNormalMap(vec3 geomnor, vec3 normap) {
normap = normap * 2.0 - 1.0;
vec3 up = normalize(vec3(0.001, 1, 0.001));
vec3 surftan = normalize(cross(geomnor, up));
vec3 surfbinor = cross(geomnor, surftan);
return normap.y * surftan + normap.x * surfbinor + normap.z * geomnor;
}

// http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
// http://stackoverflow.com/questions/30242013/glsl-compressing-packing-multiple-0-1-colours-var4-into-a-single-var4-variab
float packRGBA( vec4 rgba ) {
return dot( floor(rgba.rgb*100.0), vec3(1000.0*1000.0, 1000.0, 1.0) );
}

void main() {
// TODO: copy values into gl_FragData[0], [1], etc.
// Copy values into gl_FragData[0], [1], etc.
gl_FragData[0] = vec4(v_position, packRGBA(texture2D(u_colmap, v_uv)));
gl_FragData[1] = vec4(applyNormalMap(v_normal, texture2D(u_normap, v_uv).rgb), u_specExp);
//gl_FragData[2] = vec4(texture2D(u_colmap, v_uv).rgb, u_remove);
}
25 changes: 20 additions & 5 deletions glsl/deferred/ambient.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,40 @@
precision highp float;
precision highp int;

#define NUM_GBUFFERS 4
#define NUM_GBUFFERS 2

uniform sampler2D u_gbufs[NUM_GBUFFERS];
uniform sampler2D u_depth;

const vec4 SKY_COLOR = vec4(0.1, 0.14, 0.22, 0.1)*2.0;

varying vec2 v_uv;

// http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
// http://stackoverflow.com/questions/30242013/glsl-compressing-packing-multiple-0-1-colours-var4-into-a-single-var4-variab
vec4 unpackRGBA( float v ) {
float r = floor(v/1000.0/1000.0);
v-=(r*1000.0*1000.0);
float g = floor(v/1000.0);
v-=(g*1000.0);
float b = floor(v);
return vec4(r,g,b,v)/100.0;
}


void main() {
vec4 gb0 = texture2D(u_gbufs[0], v_uv);
vec4 gb1 = texture2D(u_gbufs[1], v_uv);
vec4 gb2 = texture2D(u_gbufs[2], v_uv);
vec4 gb3 = texture2D(u_gbufs[3], v_uv);
//vec4 gb2 = texture2D(u_gbufs[2], v_uv);
//vec4 gb3 = texture2D(u_gbufs[3], v_uv);
float depth = texture2D(u_depth, v_uv).x;
// TODO: Extract needed properties from the g-buffers into local variables

vec3 colmap = unpackRGBA(gb0.w).xyz; // The color map - unlit "albedo" (surface color)

if (depth == 1.0) {
gl_FragColor = vec4(0, 0, 0, 0); // set alpha to 0
return;
}

gl_FragColor = vec4(0.1, 0.1, 0.1, 1); // TODO: replace this
gl_FragColor = SKY_COLOR*vec4(colmap, 1.0);
}
61 changes: 56 additions & 5 deletions glsl/deferred/blinnphong-pointlight.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
precision highp float;
precision highp int;

#define NUM_GBUFFERS 4
#define NUM_GBUFFERS 2

uniform vec3 u_lightCol;
uniform vec3 u_lightPos;
uniform float u_lightRad;
uniform vec3 u_camPos;
uniform sampler2D u_gbufs[NUM_GBUFFERS];
uniform sampler2D u_depth;

uniform int u_toon;

varying vec2 v_uv;

vec3 applyNormalMap(vec3 geomnor, vec3 normap) {
Expand All @@ -20,13 +23,31 @@ vec3 applyNormalMap(vec3 geomnor, vec3 normap) {
return normap.y * surftan + normap.x * surfbinor + normap.z * geomnor;
}

// http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
// http://stackoverflow.com/questions/30242013/glsl-compressing-packing-multiple-0-1-colours-var4-into-a-single-var4-variab
vec4 unpackRGBA( float v ) {
float r = floor(v/1000.0/1000.0);
v-=(r*1000.0*1000.0);
float g = floor(v/1000.0);
v-=(g*1000.0);
float b = floor(v);
return vec4(r,g,b,v)/100.0;
}

void main() {
vec4 gb0 = texture2D(u_gbufs[0], v_uv);
vec4 gb1 = texture2D(u_gbufs[1], v_uv);
vec4 gb2 = texture2D(u_gbufs[2], v_uv);
vec4 gb3 = texture2D(u_gbufs[3], v_uv);
//vec4 gb2 = texture2D(u_gbufs[2], v_uv);
//vec4 gb3 = texture2D(u_gbufs[3], v_uv);
float depth = texture2D(u_depth, v_uv).x;
// TODO: Extract needed properties from the g-buffers into local variables
// Extract needed properties from the g-buffers into local variables
vec3 pos = gb0.xyz; // World-space position
//vec3 geomnor = gb1.xyz; // Normals of the geometry as defined, without normal mapping
vec3 colmap = unpackRGBA(gb0.w).xyz; // The color map - unlit "albedo" (surface color)
vec3 nor = gb1.xyz; // The raw normal map (normals relative to the surface they're on)
//vec3 nor = applyNormalMap(geomnor, normap); // The true normals as we want to light them - with the normal map applied to the geometry normals (applyNormalMap above)
float specExp = gb1.w;
//float removeChannel = gb2.w;

// If nothing was rendered to this pixel, set alpha to 0 so that the
// postprocessing step can render the sky color.
Expand All @@ -35,5 +56,35 @@ void main() {
return;
}

gl_FragColor = vec4(0, 0, 1, 1); // TODO: perform lighting calculations
float dist = length(pos-u_lightPos);
if (dist > u_lightRad){
gl_FragColor = vec4(0, 0, 0, 0);
return;
}

// Camera at (0,0,0)
vec3 V = normalize(u_camPos-pos);
vec3 L = normalize(u_lightPos-pos);
vec3 H = normalize(L+V);

//float specExp = 10.0;
float diffIntense = max(dot(nor, L), 0.0);
float specIntense = pow(max(dot(nor, H), 0.0), specExp);
float falloff = (1.0-dist/u_lightRad)/pow(dist/u_lightRad, 0.7);
/*
if (removeChannel == 0.0) colmap.x = 0.0;
if (removeChannel == 1.0) colmap.y = 0.0;
if (removeChannel == 2.0) colmap.z = 0.0;
*/
// Toon ramping
// Concept: http://prideout.net/blog/?p=22#toon
if (u_toon == 1){
float steps = 4.0;
diffIntense = ceil(diffIntense*steps)/steps;
specIntense = ceil(specIntense*steps)/steps;
falloff = ceil(falloff*steps)/steps;
gl_FragColor = vec4(falloff*(diffIntense*colmap*u_lightCol+specIntense*vec3(1.0)), falloff);
} else {
gl_FragColor = vec4(falloff*(diffIntense*colmap*u_lightCol+specIntense*vec3(1.0)), falloff);
}
}
37 changes: 25 additions & 12 deletions glsl/deferred/debug.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
precision highp float;
precision highp int;

#define NUM_GBUFFERS 4
#define NUM_GBUFFERS 2

uniform int u_debug;
uniform sampler2D u_gbufs[NUM_GBUFFERS];
Expand All @@ -20,33 +20,46 @@ vec3 applyNormalMap(vec3 geomnor, vec3 normap) {
return normap.y * surftan + normap.x * surfbinor + normap.z * geomnor;
}

// http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
// http://stackoverflow.com/questions/30242013/glsl-compressing-packing-multiple-0-1-colours-var4-into-a-single-var4-variab
vec4 unpackRGBA( float v ) {
float r = floor(v/1000.0/1000.0);
v-=(r*1000.0*1000.0);
float g = floor(v/1000.0);
v-=(g*1000.0);
float b = floor(v);
return vec4(r,g,b,v)/100.0;
}

void main() {
vec4 gb0 = texture2D(u_gbufs[0], v_uv);
vec4 gb1 = texture2D(u_gbufs[1], v_uv);
vec4 gb2 = texture2D(u_gbufs[2], v_uv);
vec4 gb3 = texture2D(u_gbufs[3], v_uv);
//vec4 gb2 = texture2D(u_gbufs[2], v_uv);
//vec4 gb3 = texture2D(u_gbufs[3], v_uv);
float depth = texture2D(u_depth, v_uv).x;
// TODO: Extract needed properties from the g-buffers into local variables
// Extract needed properties from the g-buffers into local variables
// These definitions are suggested for starting out, but you will probably want to change them.
vec3 pos; // World-space position
vec3 geomnor; // Normals of the geometry as defined, without normal mapping
vec3 colmap; // The color map - unlit "albedo" (surface color)
vec3 normap; // The raw normal map (normals relative to the surface they're on)
vec3 nor; // The true normals as we want to light them - with the normal map applied to the geometry normals (applyNormalMap above)
vec3 pos = gb0.xyz; // World-space position
//vec3 geomnor = gb1.xyz; // Normals of the geometry as defined, without normal mapping
vec3 colmap = unpackRGBA(gb0.w).xyz; // The color map - unlit "albedo" (surface color)
//vec3 normap = gb3.xyz; // The raw normal map (normals relative to the surface they're on)
//vec3 nor = applyNormalMap(geomnor, normap); // The true normals as we want to light them - with the normal map applied to the geometry normals (applyNormalMap above)
vec3 nor = gb1.xyz;

if (u_debug == 0) {
gl_FragColor = vec4(vec3(depth), 1.0);
} else if (u_debug == 1) {
gl_FragColor = vec4(abs(pos) * 0.1, 1.0);
} else if (u_debug == 2) {
gl_FragColor = vec4(abs(geomnor), 1.0);
//gl_FragColor = vec4(abs(geomnor), 1.0);
gl_FragColor = vec4(abs(nor), 1.0);
} else if (u_debug == 3) {
gl_FragColor = vec4(colmap, 1.0);
} else if (u_debug == 4) {
} /*else if (u_debug == 4) {
gl_FragColor = vec4(normap, 1.0);
} else if (u_debug == 5) {
gl_FragColor = vec4(abs(nor), 1.0);
} else {
}*/ else {
gl_FragColor = vec4(1, 0, 1, 1);
}
}
20 changes: 20 additions & 0 deletions glsl/post/direct.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#version 100
precision highp float;
precision highp int;

uniform sampler2D u_color;
uniform vec2 u_screen_inv;

varying vec2 v_uv;

const vec4 SKY_COLOR = vec4(0.01, 0.14, 0.42, 1.0);

void main() {
vec4 color = texture2D(u_color, v_uv);

if (color.a == 0.0) {
color = SKY_COLOR;
}

gl_FragColor = color;
}
20 changes: 17 additions & 3 deletions glsl/post/one.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,32 @@ precision highp float;
precision highp int;

uniform sampler2D u_color;
uniform vec2 u_screen_inv;

varying vec2 v_uv;

const vec4 SKY_COLOR = vec4(0.01, 0.14, 0.42, 1.0);

// Gaussian
const vec3 G = vec3(0.399, 0.242, 0.054);

void main() {
vec4 color = texture2D(u_color, v_uv);

if (color.a == 0.0) {
gl_FragColor = SKY_COLOR;
return;
color = SKY_COLOR;
}

gl_FragColor = color;
vec4 L2 = texture2D(u_color,vec2(v_uv.x-u_screen_inv.x*2.0,v_uv.y));
vec4 L1 = texture2D(u_color,vec2(v_uv.x-u_screen_inv.x*1.0,v_uv.y));
vec4 R1 = texture2D(u_color,vec2(v_uv.x+u_screen_inv.x*1.0,v_uv.y));
vec4 R2 = texture2D(u_color,vec2(v_uv.x+u_screen_inv.x*2.0,v_uv.y));

color = color*color.a;
L2 = L2*L2.a;
L1 = L1*L1.a;
R1 = R1*R1.a;
R2 = R2*R2.a;

gl_FragColor = L2*G.z+L1*G.y+color*G.x+R1*G.y+R2*G.z;
}
38 changes: 38 additions & 0 deletions glsl/post/toon1.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#version 100
precision highp float;
precision highp int;

uniform sampler2D u_color;
uniform vec2 u_screen_inv;

varying vec2 v_uv;

const vec4 SKY_COLOR = vec4(0.01, 0.14, 0.42, 1.0);

void main() {
vec4 color = texture2D(u_color, v_uv);

// https://en.wikipedia.org/wiki/Sobel_operator

vec4 L1 = texture2D(u_color,vec2(
v_uv.x-u_screen_inv.x,
v_uv.y));
vec4 L1T = texture2D(u_color,vec2(
v_uv.x-u_screen_inv.x,
v_uv.y-u_screen_inv.y));
vec4 L1B = texture2D(u_color,vec2(
v_uv.x-u_screen_inv.x,
v_uv.y+u_screen_inv.y));

vec4 R1 = texture2D(u_color,vec2(
v_uv.x+u_screen_inv.x,
v_uv.y));
vec4 R1T = texture2D(u_color,vec2(
v_uv.x+u_screen_inv.x,
v_uv.y-u_screen_inv.y));
vec4 R1B = texture2D(u_color,vec2(
v_uv.x+u_screen_inv.x,
v_uv.y+u_screen_inv.y));

gl_FragColor = -L1*1.0-L1T*1.0-L1B*1.0+R1*1.0+R1T*1.0+R1B*1.0;
}
49 changes: 49 additions & 0 deletions glsl/post/toon2.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#version 100
precision highp float;
precision highp int;

uniform sampler2D u_color;
uniform sampler2D o_color;
uniform vec2 u_screen_inv;

varying vec2 v_uv;

void main() {
vec4 color = texture2D(u_color, v_uv);
vec4 color_o = texture2D(o_color, v_uv);

vec4 T1 = texture2D(u_color,vec2(
v_uv.x,
v_uv.y-u_screen_inv.y));
vec4 T1L = texture2D(u_color,vec2(
v_uv.x-u_screen_inv.x,
v_uv.y-u_screen_inv.y));
vec4 T1R = texture2D(u_color,vec2(
v_uv.x+u_screen_inv.x,
v_uv.y-u_screen_inv.y));

vec4 B1 = texture2D(u_color,vec2(
v_uv.x,
v_uv.y+u_screen_inv.y));
vec4 B1L = texture2D(u_color,vec2(
v_uv.x-u_screen_inv.x,
v_uv.y+u_screen_inv.y));
vec4 B1R = texture2D(u_color,vec2(
v_uv.x+u_screen_inv.x,
v_uv.y+u_screen_inv.y));

vec4 blend = -T1*1.0-T1L*1.0-T1R*1.0+B1*1.0+B1L*1.0+B1R*1.0;

// Edge ramping
blend = vec4(vec3(max(max(blend.x, blend.y), blend.z)), 1);

if (blend.x <= 0.3){
blend = vec4(vec3(0.0), 1);
} else {
blend = vec4(1.0);
}

blend = (1.0-blend) * color_o;

gl_FragColor = blend;
}
Loading