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"
|
|
|
|
// octahedronWrap()
|
|
|
|
// unpackFloat()
|
|
|
|
#include "../std/math.glsl"
|
|
|
|
// envMapEquirect()
|
|
|
|
#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() {
|
2016-08-29 09:56:34 +02:00
|
|
|
vec4 g0 = texture(gbuffer0, texCoord); // Normal.xy, metallic/roughness, mask
|
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-02-22 15:50:19 +01:00
|
|
|
vec4 g1 = texture(gbuffer1, texCoord); // Basecolor.rgb, occlusion
|
|
|
|
vec3 albedo = surfaceAlbedo(g1.rgb, metrough.x); // g1.rgb - basecolor
|
|
|
|
|
2016-08-09 23:51:40 +02:00
|
|
|
#ifdef _Rad
|
|
|
|
float depth = texture(gbufferD, texCoord).r * 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
|
|
|
|
vec3 indirectDiffuse = indirectDiffuseLight(n, p / voxelgiDimensions.x);
|
|
|
|
|
|
|
|
vec3 reflectWorld = reflect(-v, n);
|
|
|
|
vec3 indirectSpecular = traceSpecularVoxelCone(p / voxelgiDimensions.x, reflectWorld, n, metrough.y * 10.0);
|
|
|
|
indirectSpecular *= f0 * envBRDF.x + envBRDF.y;
|
|
|
|
|
|
|
|
fragColor.rgb = indirectDiffuse * 0.1 * albedo + indirectSpecular;
|
|
|
|
fragColor.rgb *= texture(ssaotex, texCoord).r;
|
|
|
|
|
|
|
|
// if (opacity < 1.0) fragColor.rgb = mix(indirectRefractiveLight(-v), fragColor.rgb); // Transparency
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Envmap
|
2017-01-23 20:41:45 +01:00
|
|
|
#ifdef _Irr
|
2017-02-22 15:50:19 +01:00
|
|
|
fragColor.rgb = shIrradiance(n, 2.2) / PI;
|
2017-01-23 20:41:45 +01:00
|
|
|
#else
|
2017-02-22 15:50:19 +01:00
|
|
|
fragColor.rgb = 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-02-22 15:50:19 +01:00
|
|
|
fragColor.rgb = pow(fragColor.rgb, vec3(2.2));
|
2016-08-09 23:51:40 +02:00
|
|
|
#ifdef _Rad
|
|
|
|
prefilteredColor = pow(prefilteredColor, vec3(2.2));
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2017-02-22 15:50:19 +01:00
|
|
|
fragColor.rgb *= albedo;
|
2016-08-09 23:51:40 +02:00
|
|
|
|
2017-02-22 15:50:19 +01:00
|
|
|
#ifdef _Rad // Indirect specular
|
|
|
|
fragColor.rgb += prefilteredColor * (f0 * envBRDF.x + envBRDF.y);
|
2016-08-09 23:51:40 +02:00
|
|
|
#endif
|
|
|
|
|
2017-02-22 15:50:19 +01:00
|
|
|
fragColor.rgb = fragColor.rgb * envmapStrength;// * lightColor;
|
|
|
|
fragColor.rgb = fragColor.rgb * g1.a; // Occlusion
|
2016-08-09 23:51:40 +02:00
|
|
|
|
|
|
|
#ifdef _SSAO
|
2017-02-22 15:50:19 +01:00
|
|
|
fragColor.rgb *= texture(ssaotex, texCoord).r; // SSAO
|
2016-08-09 23:51:40 +02:00
|
|
|
#endif
|
|
|
|
}
|