我正在尝试实现基于运动模糊的后期处理运动矢量。它有点“有效”......
我遇到了两个主要问题:
- 如果物体和相机都没有移动,则绘图会产生空白屏幕。
- 沿着屏幕的边界存在对象的扭曲。
这是 2 pass 方法: pass#1 - 将对象渲染到 FBO texture.pass#2 - 渲染到全屏四边形并将 MB 应用于前一个 pass 输出。
第二遍的顶点着色器:
void main(void){
// transform previous and current position to eye space
vec4 P = MODEL_VIEW_MATRIX * position;
vec4 Pprev = prevModelView * position;
vec3 motionVector = P.xyz - Pprev.xyz;
// calculate window space motion vector
P = PROJ * MODEL_VIEW_MATRIX * position;
Pprev = PROJ * prevModelView * position;
Pprev = mix(P, Pprev, blurScale);
// choose previous or current position based on dot product between motion vector
// and normal
vec3 N = mat3(MODEL_VIEW_MATRIX) *normal;
bool flag = dot(motionVector, N) > 0;
vec4 Pstretch = flag ? P : Pprev;
gl_Position = position;
// do divide by W -> NDC coordinates
P.xyz = P.xyz / P.w;
Pprev.xyz = Pprev.xyz / Pprev.w;
// calculate window space velocity
vec3 dP = (P.xyz - Pprev.xyz) * halfWindowSize.xyz;
float len = length (dP)/(halfWindowSize[0]*2);
len = clamp(len, 0., 1.);
dP = normalize(dP);
dP.z = len;
out_velocity = dP;
}
第二遍的片段着色器:
void main(void){
float w = 1.0 / samples;
vec4 a=vec4(0.);
vec2 velocity = out_velocity.xy * blurScale * .05 * out_velocity.z;
ivec2 tsize = textureSize(COLOR_MAP_0, 0);
vec2 screntc = gl_FragCoord.xy * (1.0 / vec2(tsize));
// sample into scene texture along motion vector
for(float i=0; i<samples; i+=1)
{
float t = i / (samples-1);
a = a + texture(COLOR_MAP_0, vec2(screntc) + velocity * t ) * w;
}
colorOut = a;
}
这些着色器有什么问题?