armory/Shaders/motion_blur_pass/motion_blur_pass.frag.glsl

101 lines
2.2 KiB
Plaintext
Raw Normal View History

2016-03-23 15:09:12 +01:00
// Based on GPU Gems 3
// http://http.developer.nvidia.com/GPUGems3/gpugems3_ch27.html
2016-03-23 00:17:21 +01:00
#version 450
#include "compiled.inc"
2017-12-13 14:21:42 +01:00
#include "std/gbuffer.glsl"
2016-07-10 00:51:39 +02:00
2016-05-13 00:08:11 +02:00
uniform sampler2D gbufferD;
2016-03-23 00:17:21 +01:00
uniform sampler2D gbuffer0;
uniform sampler2D tex;
uniform mat4 prevVP;
uniform vec3 eye;
uniform vec3 eyeLook;
2017-11-04 18:35:34 +01:00
uniform vec2 cameraProj;
2017-12-26 01:58:40 +01:00
uniform float frameScale;
2016-03-23 00:17:21 +01:00
in vec2 texCoord;
2016-08-07 23:12:14 +02:00
in vec3 viewRay;
2016-10-12 17:52:27 +02:00
out vec4 fragColor;
2016-03-23 00:17:21 +01:00
vec2 getVelocity(vec2 coord, float depth) {
vec4 currentPos = vec4(coord.xy * 2.0 - 1.0, depth, 1.0);
2017-11-04 18:35:34 +01:00
vec4 worldPos = vec4(getPos(eye, eyeLook, viewRay, depth, cameraProj), 1.0);
2016-03-23 15:09:12 +01:00
vec4 previousPos = prevVP * worldPos;
previousPos /= previousPos.w;
vec2 velocity = (currentPos - previousPos).xy / 40.0;
return velocity;
}
2016-04-02 18:19:52 +02:00
void main() {
2017-03-12 17:29:22 +01:00
fragColor.rgb = texture(tex, texCoord).rgb;
2016-04-02 18:19:52 +02:00
// Do not blur masked objects
if (texture(gbuffer0, texCoord).a == 1.0) {
2016-04-02 18:19:52 +02:00
return;
}
2016-05-13 00:08:11 +02:00
float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0;
if (depth == 1.0) {
return;
}
2016-03-23 15:09:12 +01:00
2017-12-26 01:58:40 +01:00
float blurScale = motionBlurIntensity * frameScale;
2016-07-31 11:49:55 +02:00
vec2 velocity = getVelocity(texCoord, depth) * blurScale;
2016-03-23 15:09:12 +01:00
vec2 offset = texCoord;
2016-04-02 18:19:52 +02:00
int processed = 1;
2016-03-23 15:09:12 +01:00
// for(int i = 1; i < samples; ++i) {
2016-04-02 18:19:52 +02:00
offset += velocity;
if (texture(gbuffer0, offset).a != 1.0) {
2017-03-12 17:29:22 +01:00
fragColor.rgb += texture(tex, offset).rgb;
2016-04-02 18:19:52 +02:00
processed++;
}
2016-03-23 00:17:21 +01:00
2016-04-02 18:19:52 +02:00
offset += velocity;
if (texture(gbuffer0, offset).a != 1.0) {
2017-03-12 17:29:22 +01:00
fragColor.rgb += texture(tex, offset).rgb;
2016-04-02 18:19:52 +02:00
processed++;
}
2016-03-23 15:09:12 +01:00
2016-04-02 18:19:52 +02:00
offset += velocity;
if (texture(gbuffer0, offset).a != 1.0) {
2017-03-12 17:29:22 +01:00
fragColor.rgb += texture(tex, offset).rgb;
2016-04-02 18:19:52 +02:00
processed++;
}
2016-03-23 15:09:12 +01:00
2016-04-02 18:19:52 +02:00
offset += velocity;
if (texture(gbuffer0, offset).a != 1.0) {
2017-03-12 17:29:22 +01:00
fragColor.rgb += texture(tex, offset).rgb;
2016-04-02 18:19:52 +02:00
processed++;
}
2016-03-23 15:09:12 +01:00
2016-04-02 18:19:52 +02:00
offset += velocity;
if (texture(gbuffer0, offset).a != 1.0) {
2017-03-12 17:29:22 +01:00
fragColor.rgb += texture(tex, offset).rgb;
2016-04-02 18:19:52 +02:00
processed++;
}
2016-03-23 15:09:12 +01:00
2016-04-02 18:19:52 +02:00
offset += velocity;
if (texture(gbuffer0, offset).a != 1.0) {
2017-03-12 17:29:22 +01:00
fragColor.rgb += texture(tex, offset).rgb;
2016-04-02 18:19:52 +02:00
processed++;
}
2016-03-23 15:09:12 +01:00
2016-04-02 18:19:52 +02:00
offset += velocity;
if (texture(gbuffer0, offset).a != 1.0) {
2017-03-12 17:29:22 +01:00
fragColor.rgb += texture(tex, offset).rgb;
2016-04-02 18:19:52 +02:00
processed++;
}
2016-03-23 15:09:12 +01:00
2016-04-02 18:19:52 +02:00
offset += velocity;
if (texture(gbuffer0, offset).a != 1.0) {
2017-03-12 17:29:22 +01:00
fragColor.rgb += texture(tex, offset).rgb;
2016-04-02 18:19:52 +02:00
processed++;
}
2016-03-23 15:09:12 +01:00
// }
2017-03-12 17:29:22 +01:00
fragColor.rgb /= processed;
2016-03-23 00:17:21 +01:00
}