armory/raw/motion_blur_pass/motion_blur_pass.frag.glsl

82 lines
2.1 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
uniform sampler2D gbuffer0;
uniform sampler2D gbuffer1;
uniform sampler2D gbuffer2;
uniform sampler2D tex;
uniform mat4 prevVP;
2016-03-23 15:09:12 +01:00
uniform mat4 invVP;
2016-03-23 00:17:21 +01:00
in vec2 texCoord;
2016-03-23 15:09:12 +01:00
vec2 getVelocity(vec2 texCoord, float depth) {
// Get the depth buffer value at this pixel
2016-03-26 12:53:25 +01:00
float zOverW = depth; // * 2.0 - 1.0
2016-03-23 15:09:12 +01:00
// H is the viewport position at this pixel in the range -1 to 1
vec4 H = vec4(texCoord.x * 2.0 - 1.0, (texCoord.y) * 2.0 - 1.0, zOverW, 1.0);
// Transform by the view-projection inverse
vec4 D = invVP * H;
// Divide by w to get the world position
vec4 worldPos = D / D.w;
2016-03-23 00:17:21 +01:00
2016-03-23 15:09:12 +01:00
// Current viewport position
vec4 currentPos = H;
// Use the world position, and transform by the previous view-projection matrix
vec4 previousPos = prevVP * worldPos;
previousPos /= previousPos.w;
// Use this frame's position and last frame's to compute the pixel velocity
vec2 velocity = (currentPos - previousPos).xy / 40.0;
return velocity;
}
void main() {
2016-03-23 00:17:21 +01:00
vec4 g0 = texture(gbuffer0, texCoord);
float depth = g0.a;
2016-03-23 15:09:12 +01:00
float blurScale = 1.0; //currentFps / targeFps;
vec2 velocity = getVelocity(texCoord, depth) * blurScale;
2016-03-23 00:17:21 +01:00
2016-03-23 15:09:12 +01:00
const int samples = 8;
vec4 color = texture(tex, texCoord);
vec2 offset = texCoord;
// for(int i = 1; i < samples; ++i) {
// Sample the color buffer along the velocity vector
// offset += velocity;
// color += texture(tex, offset);
offset += velocity;
color += texture(tex, offset);
2016-03-23 00:17:21 +01:00
2016-03-23 15:09:12 +01:00
offset += velocity;
color += texture(tex, offset);
offset += velocity;
color += texture(tex, offset);
offset += velocity;
color += texture(tex, offset);
offset += velocity;
color += texture(tex, offset);
offset += velocity;
color += texture(tex, offset);
offset += velocity;
color += texture(tex, offset);
offset += velocity;
color += texture(tex, offset);
// }
2016-03-23 00:17:21 +01:00
2016-03-23 15:09:12 +01:00
vec4 finalColor = color / samples;
gl_FragColor = finalColor;
2016-03-23 00:17:21 +01:00
}