Skip to content

Commit

Permalink
Much better illumination
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMoroz committed Aug 22, 2019
1 parent 2fd0c1e commit 16fd44f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 33 deletions.
2 changes: 1 addition & 1 deletion game_folder/shaders/compute/Illumination_step.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void main() {
norm.xyz = normalize(norm.xyz);
pos.xyz += norm.xyz* 10*td*fovray;
illum.x = shadow_march(pos, normalize(vec4(LIGHT_DIRECTION,0)), MAX_DIST, 0.08);
pos.w = iMarbleRad/2;

//illum.y = ambient_occlusion(pos, norm);
}

Expand Down
4 changes: 2 additions & 2 deletions game_folder/shaders/compute/MRRM1.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ void main() {

fovray = 1.2*Camera.FOV/img_size.x;

ray_march(pos, dir, var, fovray, 5*fovray);
ray_march(pos, dir, var, fovray, 10*fovray);

vec4 pos1 = pos;

normarch(pos1);
//normarch(pos1);

//save the DE spheres
imageStore(DE_output, global_pos, pos);
Expand Down
6 changes: 3 additions & 3 deletions game_folder/shaders/compute/MRRM2.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
ivec2 prev_pos = min(ivec2((vec2(global_pos)/MRRM_step_scale) + 0.5),ivec2(pimg_size)-1);
//initialize the ray
vec4 sph = imageLoad(DE_input, prev_pos);
vec4 sph_norm = imageLoad(DE2_input, prev_pos);
//vec4 sph_norm = imageLoad(DE2_input, prev_pos);

#if(RBM1)
de_sph[local_indx.x][local_indx.y] = sph;
Expand All @@ -55,13 +55,13 @@ void main() {
float d = find_furthest_intersection(dir.xyz, pos.xyz, local_indx);
#else
float d = sphere_intersection(dir.xyz, pos.xyz, sph);
d = max(d, sphere_intersection(dir.xyz, pos.xyz, sph_norm));
//d = max(d, sphere_intersection(dir.xyz, pos.xyz, sph_norm));
#endif

pos.w = d;
var.w = 1;

fovray = 1*Camera.FOV/img_size.x;
fovray = 1.2*Camera.FOV/img_size.x;

ray_march(pos, dir, var, fovray, fovray);

Expand Down
10 changes: 6 additions & 4 deletions game_folder/shaders/compute/Shading_step.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ shared vec4 de_sph[group_size][group_size];
#include<camera.glsl>
#include<shading.glsl>

#define VIGNETTE_STRENGTH 0.2

///The second step of multi resolution ray marching

void main() {
Expand Down Expand Up @@ -43,13 +45,13 @@ void main() {
}
else
{
color = BACKGROUND_COLOR;
color = vec4(BACKGROUND_COLOR*BACKGROUND_COLOR*BACKGROUND_COLOR,0);
}

vec4 prev_color = imageLoad(color_HDR, global_pos);

color = prev_color*0.05 + 0.95*color; //a bit of blur

color = prev_color*Camera.mblur + (1-Camera.mblur)*color; //a bit of blur
float vignette = 1.0 - VIGNETTE_STRENGTH * length(vec2(global_pos)/img_size - 0.5);
imageStore(color_HDR, global_pos, color);
imageStore(color_output, global_pos, HDRmapping(color.xyz, Camera.exposure, 2.2));
imageStore(color_output, global_pos, HDRmapping(color.xyz, Camera.exposure, 2.2)*vignette);
}
4 changes: 2 additions & 2 deletions game_folder/shaders/compute/ray_marching.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#define MAX_DIST 50
#define MIN_DIST 1e-6
#define MAX_MARCHES 256
#define NORMARCHES 2
#define MAX_MARCHES 512
#define NORMARCHES 1


void ray_march(inout vec4 pos, inout vec4 dir, inout vec4 var, float fov, float d0)
Expand Down
77 changes: 56 additions & 21 deletions game_folder/shaders/compute/shading.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

#define PI 3.14159265
#define AMBIENT_MARCHES 8
#define BACKGROUND_COLOR 2*vec4(1,1,1,1)
#define AMBIENT_COLOR 2*vec4(1,1,1,1)

uniform vec3 BACKGROUND_COLOR;
uniform vec3 LIGHT_DIRECTION;
uniform float PBR_METALLIC;
uniform float PBR_ROUGHNESS;
Expand Down Expand Up @@ -66,26 +67,34 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)

float ambient_occlusion(in vec4 pos, in vec4 norm)
{
float dcoef = 0.005/iMarbleRad;
float occlusion_angle = 0;
float integral = 0;
float i_coef = 0;

//march in the direction of the normal
#pragma unroll
for(int i = 0; i < AMBIENT_MARCHES; i++)
if(pos.w > 0)
{
pos.w = iMarbleRad/2;
float dcoef = 0.005/iMarbleRad;
float occlusion_angle = 0;
float integral = 0;
float i_coef = 0;

//march in the direction of the normal
#pragma unroll
for(int i = 0; i < AMBIENT_MARCHES; i++)
{
norm.w += pos.w;
pos.xyz += pos.w*norm.xyz;
pos.w = DE(pos.xyz);
i_coef = 1/(dcoef*norm.w+1);//importance
occlusion_angle += i_coef*pos.w/norm.w;
integral += i_coef;
}

occlusion_angle /= integral; // average weighted by importance

return pow(occlusion_angle,2);
}
else
{
norm.w += pos.w;
pos.xyz += pos.w*norm.xyz;
pos.w = DE(pos.xyz);
i_coef = 1/(dcoef*norm.w+1);//importance
occlusion_angle += i_coef*pos.w/norm.w;
integral += i_coef;
return 1;
}

occlusion_angle /= integral; // average weighted by importance

return pow(occlusion_angle,2);
}

vec4 shading(in vec4 pos, in vec4 dir, float fov, float shadow)
Expand All @@ -100,9 +109,11 @@ vec4 shading(in vec4 pos, in vec4 dir, float fov, float shadow)
cpos = cpos - DE(cpos)*norm.xyz;
// cpos = cpos - DE(cpos)*norm.xyz;
vec3 albedo = COL(cpos).xyz;
albedo *= albedo;

//square because its a surface
vec4 ambient_color = BACKGROUND_COLOR*ambient_occlusion(pos, norm);
float ao = ambient_occlusion(pos, norm);
vec4 ambient_color = vec4(BACKGROUND_COLOR,1)*ao;

float metallic = PBR_METALLIC;
vec3 F0 = vec3(0.04);
Expand Down Expand Up @@ -141,7 +152,31 @@ vec4 shading(in vec4 pos, in vec4 dir, float fov, float shadow)
float roughness = PBR_ROUGHNESS;
vec3 L = normalize(LIGHT_DIRECTION);
vec3 H = normalize(V + L);
vec3 radiance = 10*LIGHT_COLOR*shadow;
vec3 radiance = 10*LIGHT_COLOR*shadow*(0.6+0.4*ao);

// cook-torrance brdf
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);

vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metallic;

vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
vec3 specular = numerator / max(denominator, 0.001);

// add to outgoing radiance Lo
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
}

{ //light reflection, GI imitation
float roughness = 0.6;
vec3 L = normalize(-LIGHT_DIRECTION);
vec3 H = normalize(V + L);
vec3 radiance = 6*LIGHT_COLOR*ao*(1-ao);

// cook-torrance brdf
float NDF = DistributionGGX(N, H, roughness);
Expand Down

0 comments on commit 16fd44f

Please sign in to comment.