2016-08-09 23:51:40 +02:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
#ifdef GL_ES
|
|
|
|
precision mediump float;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../compiled.glsl"
|
2016-10-17 17:39:40 +02:00
|
|
|
#include "../std/gbuffer.glsl"
|
|
|
|
#include "../std/math.glsl"
|
|
|
|
#include "../std/brdf.glsl"
|
2017-01-23 20:41:45 +01:00
|
|
|
#ifdef _Irr
|
|
|
|
#include "../std/shirr.glsl"
|
|
|
|
#endif
|
2017-02-22 15:50:19 +01:00
|
|
|
#ifdef _VoxelGI
|
|
|
|
#include "../std/conetrace.glsl"
|
|
|
|
#endif
|
2016-08-09 23:51:40 +02:00
|
|
|
|
|
|
|
uniform sampler2D gbufferD;
|
|
|
|
uniform sampler2D gbuffer0;
|
|
|
|
uniform sampler2D gbuffer1;
|
|
|
|
|
2017-02-22 15:50:19 +01:00
|
|
|
#ifdef _VoxelGI
|
|
|
|
//!uniform sampler3D voxels;
|
|
|
|
#endif
|
|
|
|
|
2016-08-09 23:51:40 +02:00
|
|
|
uniform float envmapStrength;
|
2017-01-23 20:41:45 +01:00
|
|
|
#ifdef _Irr
|
2017-02-07 11:50:21 +01:00
|
|
|
//!uniform vec4 shirr[7];
|
2017-01-23 20:41:45 +01:00
|
|
|
#endif
|
2016-08-09 23:51:40 +02:00
|
|
|
#ifdef _Rad
|
|
|
|
uniform sampler2D senvmapRadiance;
|
|
|
|
uniform sampler2D senvmapBrdf;
|
|
|
|
uniform int envmapNumMipmaps;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _SSAO
|
|
|
|
uniform sampler2D ssaotex;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _Rad
|
|
|
|
uniform vec3 eye;
|
|
|
|
uniform vec3 eyeLook;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
in vec2 texCoord;
|
|
|
|
#ifdef _Rad
|
|
|
|
in vec3 viewRay;
|
|
|
|
#endif
|
2016-10-12 17:52:27 +02:00
|
|
|
out vec4 fragColor;
|
2016-08-09 23:51:40 +02:00
|
|
|
|
|
|
|
void main() {
|
2017-05-23 15:01:56 +02:00
|
|
|
vec4 g0 = texture(gbuffer0, texCoord); // Normal.xy, metallic/roughness, depth
|
2016-08-09 23:51:40 +02:00
|
|
|
|
|
|
|
vec3 n;
|
|
|
|
n.z = 1.0 - abs(g0.x) - abs(g0.y);
|
|
|
|
n.xy = n.z >= 0.0 ? g0.xy : octahedronWrap(g0.xy);
|
|
|
|
n = normalize(n);
|
|
|
|
|
2016-08-29 09:56:34 +02:00
|
|
|
vec2 metrough = unpackFloat(g0.b);
|
2016-08-09 23:51:40 +02:00
|
|
|
|
2017-03-11 01:50:47 +01:00
|
|
|
vec4 g1 = texture(gbuffer1, texCoord); // Basecolor.rgb,
|
2017-02-22 15:50:19 +01:00
|
|
|
vec3 albedo = surfaceAlbedo(g1.rgb, metrough.x); // g1.rgb - basecolor
|
|
|
|
|
2016-08-09 23:51:40 +02:00
|
|
|
#ifdef _Rad
|
2017-05-31 10:43:57 +02:00
|
|
|
// TODO: Firefox throws transform loop error even when no depth write is performed
|
|
|
|
// float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0;
|
|
|
|
float depth = (1.0 - g0.a) * 2.0 - 1.0;
|
2016-10-17 17:39:40 +02:00
|
|
|
vec3 p = getPos(eye, eyeLook, viewRay, depth);
|
2016-08-09 23:51:40 +02:00
|
|
|
vec3 v = normalize(eye - p.xyz);
|
2017-02-22 15:50:19 +01:00
|
|
|
|
|
|
|
float dotNV = max(dot(n, v), 0.0);
|
|
|
|
vec3 f0 = surfaceF0(g1.rgb, metrough.x);
|
|
|
|
vec2 envBRDF = texture(senvmapBrdf, vec2(metrough.y, 1.0 - dotNV)).xy;
|
2016-08-09 23:51:40 +02:00
|
|
|
#endif
|
2016-10-09 16:06:18 +02:00
|
|
|
|
2017-02-22 15:50:19 +01:00
|
|
|
#ifdef _VoxelGI
|
2017-05-20 19:07:15 +02:00
|
|
|
vec3 wpos = p / voxelgiDimensions.x;
|
|
|
|
vec4 indirectDiffuse = indirectDiffuseLight(n, wpos);
|
2017-05-17 23:02:36 +02:00
|
|
|
|
2017-02-22 15:50:19 +01:00
|
|
|
vec3 reflectWorld = reflect(-v, n);
|
2017-05-20 19:07:15 +02:00
|
|
|
vec3 indirectSpecular = traceSpecularVoxelCone(wpos, reflectWorld, n, metrough.y * 10.0);
|
2017-02-22 15:50:19 +01:00
|
|
|
indirectSpecular *= f0 * envBRDF.x + envBRDF.y;
|
|
|
|
|
2017-05-20 19:07:15 +02:00
|
|
|
fragColor.rgb = indirectDiffuse.rgb * voxelgiDiff * g1.rgb + indirectSpecular * voxelgiSpec;
|
|
|
|
float occ = 1.0 - indirectDiffuse.a * 0.25 * voxelgiOcc;
|
2017-05-17 23:02:36 +02:00
|
|
|
fragColor.rgb *= occ;
|
|
|
|
|
2017-05-19 00:59:01 +02:00
|
|
|
#ifdef _SSAO
|
|
|
|
fragColor.rgb *= texture(ssaotex, texCoord).r * 0.5 + 0.5;
|
|
|
|
#endif
|
2017-02-22 15:50:19 +01:00
|
|
|
|
2017-05-20 19:07:15 +02:00
|
|
|
// float opacity = g1.a;
|
|
|
|
// if (opacity < 1.0) fragColor.rgb = mix(indirectRefractiveLight(-v, n, vec3(1.0), opacity, wpos), fragColor.rgb, opacity);
|
|
|
|
|
2017-03-29 14:32:44 +02:00
|
|
|
// return;
|
2017-02-22 15:50:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Envmap
|
2017-01-23 20:41:45 +01:00
|
|
|
#ifdef _Irr
|
2017-03-12 17:29:22 +01:00
|
|
|
vec3 envl = shIrradiance(n, 2.2) / PI;
|
2017-01-23 20:41:45 +01:00
|
|
|
#else
|
2017-03-12 17:29:22 +01:00
|
|
|
vec3 envl = vec3(1.0);
|
2017-01-23 20:41:45 +01:00
|
|
|
#endif
|
|
|
|
|
2017-01-02 22:46:18 +01:00
|
|
|
#ifdef _Rad
|
|
|
|
vec3 reflectionWorld = reflect(-v, n);
|
|
|
|
float lod = getMipFromRoughness(metrough.y, envmapNumMipmaps);
|
|
|
|
vec3 prefilteredColor = textureLod(senvmapRadiance, envMapEquirect(reflectionWorld), lod).rgb;
|
2016-08-09 23:51:40 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _EnvLDR
|
2017-03-12 17:29:22 +01:00
|
|
|
envl.rgb = pow(envl.rgb, vec3(2.2));
|
2016-08-09 23:51:40 +02:00
|
|
|
#ifdef _Rad
|
|
|
|
prefilteredColor = pow(prefilteredColor, vec3(2.2));
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2017-03-12 17:29:22 +01:00
|
|
|
envl.rgb *= albedo;
|
2016-08-09 23:51:40 +02:00
|
|
|
|
2017-02-22 15:50:19 +01:00
|
|
|
#ifdef _Rad // Indirect specular
|
2017-03-12 17:29:22 +01:00
|
|
|
envl.rgb += prefilteredColor * (f0 * envBRDF.x + envBRDF.y);
|
2016-08-09 23:51:40 +02:00
|
|
|
#endif
|
|
|
|
|
2017-05-23 15:01:56 +02:00
|
|
|
#ifdef _SSS
|
|
|
|
envl.rgb *= envmapStrength * fract(g1.a);
|
|
|
|
#else
|
2017-03-28 14:30:51 +02:00
|
|
|
envl.rgb *= envmapStrength * g1.a; // Occlusion
|
2017-05-23 15:01:56 +02:00
|
|
|
#endif
|
2016-08-09 23:51:40 +02:00
|
|
|
|
|
|
|
#ifdef _SSAO
|
2017-03-12 17:29:22 +01:00
|
|
|
envl.rgb *= texture(ssaotex, texCoord).r; // SSAO
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _VoxelGI
|
2017-05-17 23:02:36 +02:00
|
|
|
fragColor.rgb += envl * voxelgiEnv * occ;
|
2017-03-12 17:29:22 +01:00
|
|
|
#else
|
|
|
|
fragColor.rgb = envl;
|
2016-08-09 23:51:40 +02:00
|
|
|
#endif
|
|
|
|
}
|