Full featured deferred lighting.
This commit is contained in:
parent
dc2d85f446
commit
bbda9e9c88
|
@ -29,6 +29,7 @@ uniform float metalness;
|
|||
#endif
|
||||
|
||||
in vec3 position;
|
||||
in vec4 mvpposition;
|
||||
#ifdef _AMTex
|
||||
in vec2 texCoord;
|
||||
#endif
|
||||
|
@ -73,10 +74,14 @@ void main() {
|
|||
#ifdef _OMTex
|
||||
float occlusion = texture(som, texCoord).r;
|
||||
#else
|
||||
float occlusion = 0.0;
|
||||
float occlusion = 1.0;
|
||||
#endif
|
||||
|
||||
gl_FragData[0] = vec4(position.xyz, metalness);
|
||||
float depth = mvpposition.z / mvpposition.w;
|
||||
|
||||
// occlusion
|
||||
|
||||
gl_FragData[0] = vec4(position.xyz, depth);
|
||||
gl_FragData[1] = vec4(normal.xyz, roughness);
|
||||
gl_FragData[2] = vec4(baseColor.rgb, occlusion);
|
||||
gl_FragData[2] = vec4(baseColor.rgb, metalness);
|
||||
}
|
||||
|
|
|
@ -40,6 +40,7 @@ uniform float skinBones[50 * 12];
|
|||
#endif
|
||||
|
||||
out vec3 position;
|
||||
out vec4 mvpposition;
|
||||
#ifdef _AMTex
|
||||
out vec2 texCoord;
|
||||
#endif
|
||||
|
@ -131,6 +132,7 @@ void main() {
|
|||
|
||||
vec3 mPos = vec4(M * sPos).xyz;
|
||||
position = mPos;
|
||||
mvpposition = gl_Position;
|
||||
lightDir = light - mPos;
|
||||
eyeDir = eye - mPos;
|
||||
|
||||
|
|
|
@ -4,21 +4,126 @@
|
|||
precision mediump float;
|
||||
#endif
|
||||
|
||||
uniform sampler2D gbuffer0; // Positions, metalness
|
||||
uniform sampler2D gbuffer1; // Normals, roughness
|
||||
uniform sampler2D gbuffer2; // Base color, occlusion
|
||||
#define PI 3.1415926535
|
||||
#define TwoPI (2.0 * PI)
|
||||
|
||||
uniform sampler2D gbuffer0;
|
||||
uniform sampler2D gbuffer1;
|
||||
uniform sampler2D gbuffer2;
|
||||
|
||||
uniform sampler2D shadowMap;
|
||||
uniform sampler2D senvmapRadiance;
|
||||
uniform sampler2D senvmapIrradiance;
|
||||
uniform sampler2D senvmapBrdf;
|
||||
|
||||
uniform vec3 light;
|
||||
uniform vec3 eye;
|
||||
|
||||
in vec2 texCoord;
|
||||
|
||||
void main() {
|
||||
|
||||
vec3 normal = texture(gbuffer1, texCoord).rgb * 2.0 - 1.0;
|
||||
vec3 baseColor = texture(gbuffer2, texCoord).rgb;
|
||||
|
||||
gl_FragColor = vec4(baseColor, 1.0);
|
||||
vec2 envMapEquirect(vec3 normal) {
|
||||
float phi = acos(normal.z);
|
||||
float theta = atan(-normal.y, normal.x) + PI;
|
||||
return vec2(theta / TwoPI, phi / PI);
|
||||
}
|
||||
|
||||
float getMipLevelFromRoughness(float roughness) {
|
||||
// First mipmap level = roughness 0, last = roughness = 1
|
||||
// 6 mipmaps + base
|
||||
return roughness * 7.0;
|
||||
}
|
||||
|
||||
vec3 surfaceAlbedo(vec3 baseColor, float metalness) {
|
||||
return mix(baseColor, vec3(0.0), metalness);
|
||||
}
|
||||
|
||||
vec3 surfaceF0(vec3 baseColor, float metalness) {
|
||||
return mix(vec3(0.04), baseColor, metalness);
|
||||
}
|
||||
|
||||
vec3 f_schlick(vec3 f0, float vh) {
|
||||
return f0 + (1.0-f0)*exp2((-5.55473 * vh - 6.98316)*vh);
|
||||
}
|
||||
|
||||
float v_smithschlick(float nl, float nv, float a) {
|
||||
return 1.0 / ( (nl*(1.0-a)+a) * (nv*(1.0-a)+a) );
|
||||
}
|
||||
|
||||
float d_ggx(float nh, float a) {
|
||||
float a2 = a*a;
|
||||
float denom = pow(nh*nh * (a2-1.0) + 1.0, 2.0);
|
||||
return a2 * (1.0 / 3.1415926535) / denom;
|
||||
}
|
||||
|
||||
vec3 specularBRDF(vec3 f0, float roughness, float nl, float nh, float nv, float vh, float lh) {
|
||||
float a = roughness * roughness;
|
||||
return d_ggx(nh, a) * clamp(v_smithschlick(nl, nv, a), 0.0, 1.0) * f_schlick(f0, vh) / 4.0;
|
||||
//return vec3(LightingFuncGGX_OPT3(nl, lh, nh, roughness, f0[0]));
|
||||
}
|
||||
|
||||
vec3 lambert(vec3 albedo, float nl) {
|
||||
return albedo * max(0.0, nl);
|
||||
}
|
||||
|
||||
vec3 diffuseBRDF(vec3 albedo, float roughness, float nv, float nl, float vh, float lv) {
|
||||
return lambert(albedo, nl);
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
||||
vec4 g0 = texture(gbuffer0, texCoord); // Positions, depth
|
||||
float depth = g0.a;
|
||||
if (depth >= 1.0) discard;
|
||||
|
||||
vec4 g1 = texture(gbuffer1, texCoord); // Normals, roughness
|
||||
vec4 g2 = texture(gbuffer2, texCoord); // Base color, metalness
|
||||
|
||||
vec3 p = g0.rgb;
|
||||
vec3 n = g1.rgb;// * 2.0 - 1.0;
|
||||
//n = normalize(n);
|
||||
vec3 baseColor = g2.rgb;
|
||||
|
||||
float roughness = g1.a;
|
||||
float metalness = g2.a;
|
||||
// float occlusion = g2.a;
|
||||
|
||||
vec3 lightDir = light - p.xyz;
|
||||
vec3 eyeDir = eye - p.xyz;
|
||||
vec3 l = normalize(lightDir);
|
||||
vec3 v = normalize(eyeDir);
|
||||
vec3 h = normalize(v + l);
|
||||
|
||||
float dotNL = max(dot(n, l), 0.0);
|
||||
float dotNV = max(dot(n, v), 0.0);
|
||||
float dotNH = max(dot(n, h), 0.0);
|
||||
float dotVH = max(dot(v, h), 0.0);
|
||||
float dotLV = max(dot(l, v), 0.0);
|
||||
float dotLH = max(dot(l, h), 0.0);
|
||||
|
||||
vec3 albedo = surfaceAlbedo(baseColor, metalness);
|
||||
vec3 f0 = surfaceF0(baseColor, metalness);
|
||||
|
||||
float visibility = 1.0;
|
||||
|
||||
// Direct
|
||||
vec3 direct = diffuseBRDF(albedo, roughness, dotNV, dotNL, dotVH, dotLV) + specularBRDF(f0, roughness, dotNL, dotNH, dotNV, dotVH, dotLH);
|
||||
|
||||
// Indirect
|
||||
vec3 indirectDiffuse = texture(senvmapIrradiance, envMapEquirect(n)).rgb;
|
||||
indirectDiffuse = pow(indirectDiffuse, vec3(2.2)) * albedo;
|
||||
|
||||
vec3 reflectionWorld = reflect(-v, n);
|
||||
float lod = getMipLevelFromRoughness(roughness);// + 1.0;
|
||||
vec3 prefilteredColor = textureLod(senvmapRadiance, envMapEquirect(reflectionWorld), lod).rgb;
|
||||
prefilteredColor = pow(prefilteredColor, vec3(2.2));
|
||||
|
||||
vec2 envBRDF = texture(senvmapBrdf, vec2(roughness, 1.0 - dotNV)).xy;
|
||||
vec3 indirectSpecular = prefilteredColor * (f0 * envBRDF.x + envBRDF.y);
|
||||
vec3 indirect = indirectDiffuse + indirectSpecular;
|
||||
|
||||
vec4 outColor = vec4(vec3(direct * visibility + indirect), 1.0);
|
||||
|
||||
// outColor.rgb *= occlusion;
|
||||
|
||||
gl_FragColor = vec4(pow(outColor.rgb, vec3(1.0 / 2.2)), outColor.a);
|
||||
}
|
||||
|
|
|
@ -25,6 +25,14 @@
|
|||
}
|
||||
],
|
||||
"links": [
|
||||
{
|
||||
"id": "light",
|
||||
"link": "_lightPosition"
|
||||
},
|
||||
{
|
||||
"id": "eye",
|
||||
"link": "_cameraPosition"
|
||||
}
|
||||
],
|
||||
"vertex_shader": "deferred_light.vert.glsl",
|
||||
"fragment_shader": "deferred_light.frag.glsl"
|
||||
|
|
Loading…
Reference in a new issue