Skip to content
This repository was archived by the owner on Apr 26, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
452 changes: 55 additions & 397 deletions README.md

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions glsl/copy.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ precision highp int;

uniform sampler2D u_colmap;
uniform sampler2D u_normap;
uniform float u_specCoff;

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

void main() {
// TODO: copy values into gl_FragData[0], [1], etc.
gl_FragData[0]=vec4(v_position,1);
gl_FragData[1]=vec4(v_normal,1);
gl_FragData[2]=texture2D(u_colmap, v_uv);
gl_FragData[3]=texture2D(u_normap, v_uv);
}
3 changes: 3 additions & 0 deletions glsl/copy.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ precision highp float;
precision highp int;

uniform mat4 u_cameraMat;
uniform vec3 u_camPos;

attribute vec3 a_position;
attribute vec3 a_normal;
attribute vec2 a_uv;

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

void main() {
gl_Position = u_cameraMat * vec4(a_position, 1.0);
v_position = a_position;
c_position = u_camPos;
v_normal = a_normal;
v_uv = a_uv;
}
45 changes: 41 additions & 4 deletions glsl/deferred/ambient.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,60 @@ precision highp int;

#define NUM_GBUFFERS 4

uniform int u_enableToon;
uniform vec3 u_camPos;
uniform sampler2D u_gbufs[NUM_GBUFFERS];
uniform sampler2D u_depth;

varying vec2 v_uv;

float contourStep(float angle){
if(angle<0.0) return 0.0;
angle/=0.1;
//if(angle<1.0) return 0.0;
if(angle<2.0) return 0.0;
//if(angle<3.0) return 0.2;
if(angle<4.0) return 0.25;
//if(angle<5.0) return 0.4;
if(angle<6.0) return 0.5;
//if(angle<7.0) return 0.6;
if(angle<8.0) return 0.75;
//if(angle<9.0) return 0.8;
return 1.0;
}

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;
}

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);
float depth = texture2D(u_depth, v_uv).x;
// TODO: Extract needed properties from the g-buffers into local variables
float depth = texture2D(u_depth, v_uv).x;

if (depth == 1.0) {
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
if(u_enableToon==1){
vec3 geomnor=gb1.xyz; // Normals of the geometry as defined, without normal mapping
vec3 colmap=gb2.xyz; // The color map - unlit "albedo" (surface color)
vec3 normap=gb3.xyz;
vec3 nor=applyNormalMap(geomnor,normap);
vec3 cameraDir=normalize(u_camPos-gb0.xyz);
float angle=dot(nor,cameraDir);
angle=contourStep(angle);
gl_FragColor=vec4(angle,angle,angle,1);
}
else{
vec3 colmap=gb2.xyz;
gl_FragColor = 0.2*gb2;
}
}
25 changes: 24 additions & 1 deletion glsl/deferred/blinnphong-pointlight.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ precision highp int;

#define NUM_GBUFFERS 4

uniform int u_debugScissor;
uniform vec3 u_lightCol;
uniform vec3 u_lightPos;
uniform vec3 u_camPos;
uniform float u_specCoff;
uniform float u_lightRad;
uniform sampler2D u_gbufs[NUM_GBUFFERS];
uniform sampler2D u_depth;
Expand Down Expand Up @@ -35,5 +38,25 @@ void main() {
return;
}

gl_FragColor = vec4(0, 0, 1, 1); // TODO: perform lighting calculations
vec3 geomnor=gb1.xyz; // Normals of the geometry as defined, without normal mapping
vec3 colmap=gb2.xyz; // The color map - unlit "albedo" (surface color)
vec3 normap=gb3.xyz;
vec3 nor=applyNormalMap(geomnor,normap);
vec3 cameraDir=normalize(u_camPos-gb0.xyz);
vec3 lightDir=normalize(gb0.xyz-u_lightPos);
vec3 ref=normalize(lightDir-2.0*nor*dot(lightDir,nor));

vec3 diff=u_lightCol*dot(nor,cameraDir);
vec3 spec=u_lightCol*pow(max(0.0,dot(ref,cameraDir)),u_specCoff);

vec3 color=0.5*diff+0.5*spec;

float len=length(u_lightPos-gb0.xyz);
if(u_debugScissor==1&&len<2.0*u_lightRad){
gl_FragColor=vec4(color+vec3(0.2,0,0),1);
}
else if(len<u_lightRad){
gl_FragColor=vec4(color,1)*(u_lightRad-len)/u_lightRad;
}
else gl_FragColor=vec4(0,0,0,1);
}
10 changes: 5 additions & 5 deletions glsl/deferred/debug.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ void main() {
float depth = texture2D(u_depth, v_uv).x;
// TODO: 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=vec3(gb0.x,gb0.y,gb0.z); // World-space position
vec3 geomnor=vec3(gb1.x,gb1.y,gb1.z); // Normals of the geometry as defined, without normal mapping
vec3 colmap=vec3(gb2.x,gb2.y,gb2.z); // The color map - unlit "albedo" (surface color)
vec3 normap=vec3(gb3.x,gb3.y,gb3.z); // 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)

if (u_debug == 0) {
gl_FragColor = vec4(vec3(depth), 1.0);
Expand Down
66 changes: 65 additions & 1 deletion glsl/post/one.frag.glsl
Original file line number Diff line number Diff line change
@@ -1,16 +1,80 @@
#version 100
#define COFF 0.8
#define N 32.0
precision highp float;
precision highp int;

uniform mat4 u_previousMat;
uniform mat4 u_currentMat;
uniform int u_mode;
uniform sampler2D u_color;
uniform sampler2D u_depth;

varying vec2 v_uv;

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

vec4 getBlur(vec2 v_uv,sampler2D u_gbuf){//old method
vec4 result=texture2D(u_gbuf,v_uv);
vec4 v00,v01,v02,v10,v11,v12,v20,v21,v22;
v00=texture2D(u_gbuf,vec2(v_uv.x-0.001,v_uv.y+0.001))*1.2;
v01=texture2D(u_gbuf,vec2(v_uv.x,v_uv.y+0.001))*1.2;
v02=texture2D(u_gbuf,vec2(v_uv.x+0.001,v_uv.y+0.001))*1.2;
v10=texture2D(u_gbuf,vec2(v_uv.x-0.001,v_uv.y))*1.2;
v11=result;
v12=texture2D(u_gbuf,vec2(v_uv.x+0.001,v_uv.y))*1.2;
v20=texture2D(u_gbuf,vec2(v_uv.x-0.001,v_uv.y-0.001))*1.2;
v21=texture2D(u_gbuf,vec2(v_uv.x,v_uv.y-0.001))*1.2;
v22=texture2D(u_gbuf,vec2(v_uv.x+0.001,v_uv.y-0.001))*1.2;
if(length(v00.xyz)<0.5) v00=result;
if(length(v01.xyz)<0.5) v01=result;
if(length(v02.xyz)<0.5) v02=result;
if(length(v10.xyz)<0.5) v10=result;
if(length(v12.xyz)<0.5) v12=result;
if(length(v20.xyz)<0.5) v20=result;
if(length(v21.xyz)<0.5) v21=result;
if(length(v22.xyz)<0.5) v22=result;
result=1.0/16.0*(v00+v02+v20+v22)+2.0/16.0*(v01+v10+v12+v21)+4.0/16.0*v11;
return result;
}

vec4 accumulateX(vec2 v_uv,sampler2D u_gbuf){
vec4 result=texture2D(u_gbuf,v_uv);
for(float i=1.0;i<=N;++i){
vec4 v=pow(COFF,i)*texture2D(u_gbuf,vec2(v_uv.x+0.005*i,v_uv.y));
if(length(v.xyz)>0.8)
result+=v;
else result+=pow(COFF,i)*texture2D(u_gbuf,v_uv);
}
for(float i=1.0;i<=N;++i){
vec4 v=pow(COFF,i)*texture2D(u_gbuf,vec2(v_uv.x-0.005*i,v_uv.y));
if(length(v.xyz)>0.8)
result+=v;
else result+=pow(COFF,i)*texture2D(u_gbuf,v_uv);
}
return result;
}

void main() {
vec4 color = texture2D(u_color, v_uv);
float depth = texture2D(u_depth, v_uv).x;
vec4 H = vec4(v_uv.x*2.0-1.0,(1.0-v_uv.y)*2.0-1.0,depth,1.0);
vec4 D = u_currentMat*H;
vec4 worldPos=D/D.w;
vec4 currentPos=H;
vec4 previousPos=u_previousMat*worldPos;
previousPos=previousPos/previousPos.w;
vec2 v=(currentPos.xy - previousPos.xy)/2.0;

vec4 color1,color2,color;
if(u_mode==0) color1 = texture2D(u_color, v_uv);
else color1 = accumulateX(v_uv,u_color);

v=0.05*v+1.0*v_uv;
if(u_mode==0) color2 = texture2D(u_color, v);
else color2 = accumulateX(v,u_color);

color=color1+color2;
color/=2.0;
if (color.a == 0.0) {
gl_FragColor = SKY_COLOR;
return;
Expand Down
51 changes: 51 additions & 0 deletions glsl/post/two.frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#version 100
#define N 32.0
#define COFF 0.8
precision highp float;
precision highp int;

uniform mat4 u_previousMat;
uniform mat4 u_currentMat;
uniform int u_mode;
uniform sampler2D u_color;
uniform sampler2D u_depth;

varying vec2 v_uv;

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

vec4 accumulateY(vec2 v_uv,sampler2D u_gbuf){
vec4 result=texture2D(u_gbuf,v_uv);
for(float i=1.0;i<=N;++i){
vec4 v=pow(COFF,i)*texture2D(u_gbuf,vec2(v_uv.x,v_uv.y+0.005*i));
if(length(v.xyz)>0.8)
result+=v;
else result+=pow(COFF,i)*texture2D(u_gbuf,v_uv);
}
for(float i=1.0;i<=N;++i){
vec4 v=pow(COFF,i)*texture2D(u_gbuf,vec2(v_uv.x,v_uv.y-0.005*i));
if(length(v.xyz)>0.8)
result+=v;
else result+=pow(COFF,i)*texture2D(u_gbuf,v_uv);
}
return result;
}

float getNum(){
float result=1.0+2.0*(1.0-pow(COFF,N));
result=result+2.0*result*(1.0-pow(COFF,N));
return result;
}

void main() {
vec4 color;
color=accumulateY(v_uv,u_color);
color/=getNum();

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

gl_FragColor = 1.0*color;
}
Binary file added img/ana_bloom1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/ana_bloom2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/ana_scissor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/blur.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/debug1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/debug2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/debug3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/debug4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/debug5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/debug6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/rep1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/rep2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/rep3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/rep4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/rep5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/scissor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
(Disable before measuring performance.)
</div>
<div id="dlbutton">
<button type="button" action="" onclick="downloadCanvas();">Screenshot</button>
<button type="button" action="" title="Debug mode only" disabled onclick="downloadCanvas();">Screenshot</button>
</div>
<div id="alertcontainer">
<div id="alertbox"><pre id="alerttext"></pre></div>
Expand Down
Loading