Skip to content

Commit

Permalink
Lens flare with occlusion testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
luboslenco committed Apr 3, 2016
1 parent 36bd1f4 commit 358693f
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 55 deletions.
Binary file added Assets/checker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions Sources/cycles/Root.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Root {
Eg.addNodeTrait(sceneNode, physics);

App.requestRender(render);

// untyped loadScript('hdrload.js', null);
// var hdrbin = kha.Assets.blobs.test_hdr.bytes.getData();
// var hdrimage:Dynamic = null;
// untyped __js__("hdrimage = parseHdr(hdrbin);");
}

function render(g:kha.graphics4.Graphics) {
Expand Down
3 changes: 2 additions & 1 deletion raw/blur_pass/blur_pass.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ in vec2 texCoord;
// const float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216);

void main() {
vec2 step = dir / vec2(400, 300);
// vec2 step = dir / vec2(400, 300);
vec2 step = dir / vec2(960, 540);
// vec2 step = dir / vec2(800, 600);

// vec3 result = texture(tex, texCoord).rgb * weight[0];
Expand Down
76 changes: 67 additions & 9 deletions raw/dof_pass/dof_pass.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,34 @@ uniform sampler2D gbuffer1;
uniform sampler2D gbuffer2;
uniform vec3 eye;

uniform vec3 light;
uniform mat4 V;
uniform mat4 P;

in vec2 texCoord;

const float focus_depth = 0.5;

const float vignout = 1.6; // vignetting outer border
const float vignout = 1.8; // vignetting outer border
const float vignin = 0.0; // vignetting inner border
const float vignfade = 90.0; // f-stops till vignete fades
const float fstop = 20; // f-stop value

// const vec3 fogColor = vec3(0.5, 0.6, 0.7);
const float aspectRatio = 800.0 / 600.0;

const vec3 fogColor = vec3(0.5, 0.6, 0.7);
// const float b = 0.01;
// const float c = 0.1;
const float b = 1.0;
const float c = 1.0;

// vec3 applyFog(vec3 rgb, // original color of the pixel
// float distance, // camera to point distance
// vec3 rayOri, // camera position
// vec3 rayDir) { // camera to point vector
// float fogAmount = c * exp(-rayOri.y * b) * (1.0 - exp(-distance * rayDir.y * b)) / rayDir.y;
// return mix(rgb, fogColor, fogAmount);
// }
vec3 applyFog(vec3 rgb, // original color of the pixel
float distance, // camera to point distance
vec3 rayOri, // camera position
vec3 rayDir) { // camera to point vector
float fogAmount = c * exp(-rayOri.y * b) * (1.0 - exp(-distance * rayDir.y * b)) / rayDir.y;
return mix(rgb, fogColor, fogAmount);
}
// vec3 applyFog(vec3 rgb, // original color of the pixel
// float distance) { // camera to point distance
// float fogAmount = 1.0 - exp(-distance * b);
Expand All @@ -56,7 +64,42 @@ vec4 sampleBox(float size) {
return color;
}

float linearize(float depth, float znear, float zfar) {
return -zfar * znear / (depth * (zfar - znear) - zfar);
}

// Based on lense flare implementation by musk
// https://www.shadertoy.com/view/4sX3Rs
vec3 lensflare(vec2 uv, vec2 pos) {
vec2 uvd = uv * (length(uv));
float f2 = max(1.0/(1.0+32.0*pow(length(uvd+0.8*pos),2.0)),0.0)*0.25;
float f22 = max(1.0/(1.0+32.0*pow(length(uvd+0.85*pos),2.0)),0.0)*0.23;
float f23 = max(1.0/(1.0+32.0*pow(length(uvd+0.9*pos),2.0)),0.0)*0.21;

vec2 uvx = mix(uv, uvd, -0.5);
float f4 = max(0.01-pow(length(uvx+0.4*pos),2.4),0.0)*6.0;
float f42 = max(0.01-pow(length(uvx+0.45*pos),2.4),0.0)*5.0;
float f43 = max(0.01-pow(length(uvx+0.5*pos),2.4),0.0)*3.0;

uvx = mix(uv, uvd, -0.4);
float f5 = max(0.01-pow(length(uvx+0.2*pos),5.5),0.0)*2.0;
float f52 = max(0.01-pow(length(uvx+0.4*pos),5.5),0.0)*2.0;
float f53 = max(0.01-pow(length(uvx+0.6*pos),5.5),0.0)*2.0;

uvx = mix(uv, uvd, -0.5);
float f6 = max(0.01-pow(length(uvx-0.3*pos),1.6),0.0)*6.0;
float f62 = max(0.01-pow(length(uvx-0.325*pos),1.6),0.0)*3.0;
float f63 = max(0.01-pow(length(uvx-0.35*pos),1.6),0.0)*5.0;

vec3 c = vec3(0.0);
c.r += f2 + f4 + f5 + f6;
c.g += f22 + f42 + f52 + f62;
c.b += f23 + f43 + f53 + f63;
return c;
}

void main() {
// Blur
float depth = texture(gbuffer0, texCoord).a;
float blur_amount = abs(depth - focus_depth);
if (depth < depth - focus_depth) {
Expand All @@ -77,6 +120,21 @@ void main() {
// col.rgb = applyFog(col.rgb, dist, eye, dir);
// col.rgb = applyFog(col.rgb, dist);

// Lens flare
// vec4 lndc = P * V * vec4(light, 1.0);
// lndc.xy /= lndc.w;

// float lightDistance = distance(eye, light);
// vec2 lss = lndc.xy * 0.5 + 0.5;
// float lssdepth = linearize(texture(gbuffer0, lss).a, 0.1, 1000.0);

// if (lssdepth >= lightDistance) {
// vec2 lensuv = (texCoord - 0.5) * 2.0;
// lensuv.x *= aspectRatio;
// vec3 lensflarecol = vec3(1.4, 1.2, 1.0) * lensflare(lensuv, lndc.xy);
// col.rgb += lensflarecol;
// }

// Vignetting
col *= vignette();

Expand Down
12 changes: 12 additions & 0 deletions raw/dof_pass/dof_pass.shader.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
{
"id": "eye",
"link": "_cameraPosition"
},
{
"id": "light",
"link": "_lightPosition"
},
{
"id": "V",
"link": "_viewMatrix"
},
{
"id": "P",
"link": "_projectionMatrix"
}
],
"texture_params": [],
Expand Down
88 changes: 44 additions & 44 deletions raw/env_map/env_map.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,60 +12,60 @@ in vec2 pos;
out vec3 normal;
// out vec2 texCoord;

mat3 transpose_(mat3 m) {
return mat3(m[0][0], m[1][0], m[2][0],
m[0][1], m[1][1], m[2][1],
m[0][2], m[1][2], m[2][2]);
}
// mat3 transpose_(mat3 m) {
// return mat3(m[0][0], m[1][0], m[2][0],
// m[0][1], m[1][1], m[2][1],
// m[0][2], m[1][2], m[2][2]);
// }

mat4 inverse_(mat4 m) {
float
a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3],
a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3],
a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3],
a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3],
// mat4 inverse_(mat4 m) {
// float
// a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3],
// a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3],
// a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3],
// a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3],

b00 = a00 * a11 - a01 * a10,
b01 = a00 * a12 - a02 * a10,
b02 = a00 * a13 - a03 * a10,
b03 = a01 * a12 - a02 * a11,
b04 = a01 * a13 - a03 * a11,
b05 = a02 * a13 - a03 * a12,
b06 = a20 * a31 - a21 * a30,
b07 = a20 * a32 - a22 * a30,
b08 = a20 * a33 - a23 * a30,
b09 = a21 * a32 - a22 * a31,
b10 = a21 * a33 - a23 * a31,
b11 = a22 * a33 - a23 * a32,
// b00 = a00 * a11 - a01 * a10,
// b01 = a00 * a12 - a02 * a10,
// b02 = a00 * a13 - a03 * a10,
// b03 = a01 * a12 - a02 * a11,
// b04 = a01 * a13 - a03 * a11,
// b05 = a02 * a13 - a03 * a12,
// b06 = a20 * a31 - a21 * a30,
// b07 = a20 * a32 - a22 * a30,
// b08 = a20 * a33 - a23 * a30,
// b09 = a21 * a32 - a22 * a31,
// b10 = a21 * a33 - a23 * a31,
// b11 = a22 * a33 - a23 * a32,

det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
// det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;

return mat4(
a11 * b11 - a12 * b10 + a13 * b09,
a02 * b10 - a01 * b11 - a03 * b09,
a31 * b05 - a32 * b04 + a33 * b03,
a22 * b04 - a21 * b05 - a23 * b03,
a12 * b08 - a10 * b11 - a13 * b07,
a00 * b11 - a02 * b08 + a03 * b07,
a32 * b02 - a30 * b05 - a33 * b01,
a20 * b05 - a22 * b02 + a23 * b01,
a10 * b10 - a11 * b08 + a13 * b06,
a01 * b08 - a00 * b10 - a03 * b06,
a30 * b04 - a31 * b02 + a33 * b00,
a21 * b02 - a20 * b04 - a23 * b00,
a11 * b07 - a10 * b09 - a12 * b06,
a00 * b09 - a01 * b07 + a02 * b06,
a31 * b01 - a30 * b03 - a32 * b00,
a20 * b03 - a21 * b01 + a22 * b00) / det;
}
// return mat4(
// a11 * b11 - a12 * b10 + a13 * b09,
// a02 * b10 - a01 * b11 - a03 * b09,
// a31 * b05 - a32 * b04 + a33 * b03,
// a22 * b04 - a21 * b05 - a23 * b03,
// a12 * b08 - a10 * b11 - a13 * b07,
// a00 * b11 - a02 * b08 + a03 * b07,
// a32 * b02 - a30 * b05 - a33 * b01,
// a20 * b05 - a22 * b02 + a23 * b01,
// a10 * b10 - a11 * b08 + a13 * b06,
// a01 * b08 - a00 * b10 - a03 * b06,
// a30 * b04 - a31 * b02 + a33 * b00,
// a21 * b02 - a20 * b04 - a23 * b00,
// a11 * b07 - a10 * b09 - a12 * b06,
// a00 * b09 - a01 * b07 + a02 * b06,
// a31 * b01 - a30 * b03 - a32 * b00,
// a20 * b03 - a21 * b01 + a22 * b00) / det;
// }

// const vec2 madd = vec2(0.5, 0.5);

void main() {

mat4 invP = inverse_(P);
mat4 invP = inverse(P);
// mat4 invP = inverse(P);
mat3 invMV = transpose_(mat3(V));
mat3 invMV = transpose(mat3(V));

vec4 p = vec4(pos.xy, 0.0, 1.0);
vec3 unprojected = (invP * p).xyz;
Expand Down
3 changes: 2 additions & 1 deletion raw/ssao_pass/ssao_pass.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ uniform sampler2D gbuffer2;
uniform sampler2D snoise;

const float PI = 3.1415926535;
const vec2 screenSize = vec2(800.0, 600.0);
// const vec2 screenSize = vec2(800.0, 600.0);
const vec2 screenSize = vec2(1920.0, 1080.0);
const float aoSize = 0.6;//0.43;
const int kernelSize = 8;
const float strength = 0.8;//0.55;
Expand Down

0 comments on commit 358693f

Please sign in to comment.