armory/Shaders/deferred/mesh.frag.glsl

150 lines
3 KiB
Plaintext
Raw Normal View History

2016-03-18 23:02:00 +01:00
#version 450
#ifdef GL_ES
precision mediump float;
#endif
2016-10-17 17:39:40 +02:00
#include "../std/gbuffer.glsl"
// octahedronWrap()
// packFloat()
2016-08-07 23:12:14 +02:00
uniform float mask;
2016-08-25 00:26:01 +02:00
#ifdef _BaseTex
uniform sampler2D sbase;
2016-03-18 23:02:00 +01:00
#endif
2016-08-25 00:26:01 +02:00
#ifdef _NorTex
2016-06-18 13:58:12 +02:00
uniform sampler2D snormal;
2016-03-18 23:02:00 +01:00
#endif
2016-08-25 00:26:01 +02:00
#ifdef _NorStr
2016-08-04 22:38:56 +02:00
uniform float normalStrength;
#endif
2016-08-25 00:26:01 +02:00
#ifdef _OccTex
uniform sampler2D socclusion;
2016-06-21 13:29:27 +02:00
#else
uniform float occlusion;
2016-03-18 23:02:00 +01:00
#endif
2016-08-25 00:26:01 +02:00
#ifdef _RoughTex
uniform sampler2D srough;
2016-03-18 23:02:00 +01:00
#else
2016-06-18 13:58:12 +02:00
uniform float roughness;
2016-03-18 23:02:00 +01:00
#endif
2016-08-25 00:26:01 +02:00
#ifdef _RoughStr
2016-08-04 22:38:56 +02:00
uniform float roughnessStrength;
#endif
2016-08-25 00:26:01 +02:00
#ifdef _MetTex
uniform sampler2D smetal;
2016-03-18 23:02:00 +01:00
#else
2016-06-18 13:58:12 +02:00
uniform float metalness;
#endif
#ifdef _Probes
2016-06-18 13:58:12 +02:00
uniform int probeID;
uniform float probeBlending;
uniform float probeStrength;
2016-06-18 13:58:12 +02:00
uniform vec3 probeVolumeCenter;
uniform vec3 probeVolumeSize;
2016-06-07 09:38:49 +02:00
#endif
2016-08-07 23:12:14 +02:00
in vec4 matColor;
2016-06-21 13:29:27 +02:00
#ifdef _Tex
2016-06-18 13:58:12 +02:00
in vec2 texCoord;
2016-03-18 23:02:00 +01:00
#endif
2016-08-25 00:26:01 +02:00
#ifdef _NorTex
2016-06-18 13:58:12 +02:00
in mat3 TBN;
2016-03-18 23:02:00 +01:00
#else
2016-06-18 13:58:12 +02:00
in vec3 normal;
#endif
#ifdef _Probes
2016-08-25 00:26:01 +02:00
in vec4 wpos;
2016-06-07 09:38:49 +02:00
#endif
2016-07-31 23:25:33 +02:00
#ifdef _Veloc
2016-08-25 00:26:01 +02:00
in vec4 wvppos;
in vec4 prevwvppos;
2016-07-31 23:25:33 +02:00
#endif
2016-06-07 09:38:49 +02:00
2016-08-13 20:38:45 +02:00
#ifdef _Veloc
2016-10-12 17:52:27 +02:00
out vec4[3] fragColor;
2016-08-13 20:38:45 +02:00
#else
2016-10-12 17:52:27 +02:00
out vec4[2] fragColor;
2016-08-13 20:38:45 +02:00
#endif
#ifdef _Probes
2016-06-07 09:38:49 +02:00
float distanceBox(vec3 point, vec3 center, vec3 halfExtents) {
vec3 d = abs(point - center) - halfExtents;
2016-06-07 09:38:49 +02:00
return min(max(d.x, max(d.y, d.z)), 0.0) + length(max(d, 0.0));
}
#endif
2016-03-18 23:02:00 +01:00
void main() {
2016-06-21 13:29:27 +02:00
#ifdef _Tex
2016-06-18 13:58:12 +02:00
vec2 newCoord = texCoord;
#endif
2016-08-25 00:26:01 +02:00
#ifdef _NorTex
2016-06-18 13:58:12 +02:00
vec3 n = (texture(snormal, newCoord).rgb * 2.0 - 1.0);
2016-03-18 23:02:00 +01:00
n = normalize(TBN * normalize(n));
#else
vec3 n = normalize(normal);
#endif
2016-08-25 00:26:01 +02:00
#ifdef _NorStr
2016-08-04 22:38:56 +02:00
n *= normalStrength;
#endif
2016-03-18 23:02:00 +01:00
2016-05-18 18:55:53 +02:00
vec3 baseColor = matColor.rgb;
2016-08-25 00:26:01 +02:00
#ifdef _BaseTex
vec4 texel = texture(sbase, newCoord);
2016-03-18 23:02:00 +01:00
#ifdef _AlphaTest
if(texel.a < 0.4)
discard;
#endif
2016-06-18 13:58:12 +02:00
texel.rgb = pow(texel.rgb, vec3(2.2)); // Variant 1
2016-05-18 18:55:53 +02:00
baseColor *= texel.rgb;
2016-03-18 23:02:00 +01:00
#endif
2016-06-18 13:58:12 +02:00
// baseColor = pow(baseColor, vec3(2.2)); // Variant 2
2016-03-18 23:02:00 +01:00
2016-08-25 00:26:01 +02:00
#ifdef _MetTex
float metalness = texture(smetal, newCoord).r;
2016-03-18 23:02:00 +01:00
#endif
2016-08-25 00:26:01 +02:00
#ifdef _RoughTex
float roughness = texture(srough, newCoord).r;
2016-03-18 23:02:00 +01:00
#endif
2016-08-25 00:26:01 +02:00
#ifdef _RoughStr
2016-08-04 22:38:56 +02:00
roughness *= roughnessStrength;
#endif
2016-03-18 23:02:00 +01:00
2016-08-25 00:26:01 +02:00
#ifdef _OccTex
float occ = texture(socclusion, newCoord).r;
2016-03-18 23:02:00 +01:00
#else
2016-06-21 13:29:27 +02:00
float occ = occlusion;
2016-03-18 23:02:00 +01:00
#endif
2016-08-07 01:43:21 +02:00
// Pack normal
2016-04-16 18:57:32 +02:00
n /= (abs(n.x) + abs(n.y) + abs(n.z));
n.xy = n.z >= 0.0 ? n.xy : octahedronWrap(n.xy);
2016-06-18 13:58:12 +02:00
#ifdef _Probes
float mask_probe;
if (probeID > 0) { // Non-global probe attached
// Distance of vertex located inside probe to probe bounds
2016-08-25 00:26:01 +02:00
float dist = distanceBox(wpos.xyz, probeVolumeCenter, probeVolumeSize);
if (dist > 0) mask_probe = 0;
else {
// Blend local probe with global probe
const float eps = 0.00001;
float clampres = clamp(probeBlending + dist, 0.0, 1.0 - eps);
mask_probe = probeID + clampres;
}
2016-06-07 09:38:49 +02:00
}
2016-10-12 17:52:27 +02:00
fragColor[0] = vec4(n.xy, packFloat(metalness, roughness), mask_probe);
2016-06-07 09:38:49 +02:00
#else
2016-10-12 17:52:27 +02:00
fragColor[0] = vec4(n.xy, packFloat(metalness, roughness), mask);
2016-06-07 09:38:49 +02:00
#endif
2016-10-12 17:52:27 +02:00
fragColor[1] = vec4(baseColor.rgb, occ);
2016-07-31 23:25:33 +02:00
#ifdef _Veloc
2016-08-25 00:26:01 +02:00
vec2 posa = (wvppos.xy / wvppos.w) * 0.5 + 0.5;
vec2 posb = (prevwvppos.xy / prevwvppos.w) * 0.5 + 0.5;
2016-10-12 17:52:27 +02:00
fragColor[2].rg = vec2(posa - posb);
2016-07-31 23:25:33 +02:00
#endif
2016-03-18 23:02:00 +01:00
}