armory/raw/motion_blur_pass/motion_blur_pass.frag.glsl

120 lines
2.7 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
#ifdef GL_ES
precision mediump float;
#endif
2016-05-13 00:08:11 +02:00
uniform sampler2D gbufferD;
2016-03-23 00:17:21 +01:00
uniform sampler2D gbuffer0;
uniform sampler2D gbuffer1;
2016-03-23 00:17:21 +01:00
uniform sampler2D tex;
uniform mat4 prevVP;
2016-03-23 15:09:12 +01:00
uniform mat4 invVP;
uniform vec3 eye;
uniform vec3 eyeLook;
2016-03-23 00:17:21 +01:00
in vec3 viewRay;
2016-03-23 00:17:21 +01:00
in vec2 texCoord;
2016-04-02 18:19:52 +02:00
const int samples = 8;
vec3 getPos(float depth, vec2 coord) {
vec3 vray = normalize(viewRay);
const float znear = 0.1;
const float zfar = 1000.0;
const float projectionA = zfar / (zfar - znear);
const float projectionB = (-zfar * znear) / (zfar - znear);
float linearDepth = projectionB / (depth * 0.5 + 0.5 - projectionA);
float viewZDist = dot(eyeLook, vray);
vec3 wposition = eye + vray * (linearDepth / viewZDist);
return wposition;
}
vec2 getVelocity(vec2 coord, float depth) {
vec4 currentPos = vec4(coord.xy * 2.0 - 1.0, depth, 1.0);
vec4 worldPos = vec4(getPos(depth, coord), 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() {
vec4 color = texture(tex, texCoord);
// Do not blur masked objects
if (texture(gbuffer0, texCoord).a == 1.0) {
2016-04-02 18:19:52 +02:00
gl_FragColor = color;
return;
}
2016-05-13 00:08:11 +02:00
// float depth = 1.0 - texture(gbuffer0, texCoord).a;
float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0;
// if (depth == 0.0) {
if (depth == 1.0) {
gl_FragColor = color;
return;
}
2016-03-23 15:09:12 +01:00
float blurScale = 1.0; //currentFps / targeFps;
2016-04-02 18:19:52 +02:00
vec2 velocity = getVelocity(texCoord, depth) * blurScale * (-1.0);
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) {
2016-04-02 18:19:52 +02:00
color += texture(tex, offset);
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) {
2016-04-02 18:19:52 +02:00
color += texture(tex, offset);
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) {
2016-04-02 18:19:52 +02:00
color += texture(tex, offset);
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) {
2016-04-02 18:19:52 +02:00
color += texture(tex, offset);
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) {
2016-04-02 18:19:52 +02:00
color += texture(tex, offset);
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) {
2016-04-02 18:19:52 +02:00
color += texture(tex, offset);
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) {
2016-04-02 18:19:52 +02:00
color += texture(tex, offset);
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) {
2016-04-02 18:19:52 +02:00
color += texture(tex, offset);
processed++;
}
2016-03-23 15:09:12 +01:00
// }
2016-04-02 18:19:52 +02:00
vec4 finalColor = color / processed;
2016-03-23 15:09:12 +01:00
gl_FragColor = finalColor;
2016-03-23 00:17:21 +01:00
}