DirectX9 SDK Samples(26) PixelMotionBlur Sample

动态模糊,一看到这个词我就想到了极品飞车和MineCraft的光影MOD。DX11貌似可以很好地支持动态模糊,但这个例子是基于DX9的,那DX9是怎么实现MotionBlur的呢?

SDK文档里面提到,实现动态模糊的其中一种方法是将场景用不同的Alpha通道渲染多遍。这个例子用的是另外一个方法,模仿现实中动态模糊出现的条件,记录像素的速度来实现动态模糊。

既然是Post-Process,那么多个RenderTarget一般都是需要的。RenderTarget需要三个纹理,一个用来渲染原来的场景,另外两个是浮点纹理,提供给PS来渲染每个像素的速度,其中一个记录上一帧的速度,另外一个记录当前帧的速度。也需要一个QUAD用来显示最后的纹理。

在Render函数主要完成了两件事:1.渲染场景和记录当前帧每个像素的速度,假如设备不支持同时渲染多个RenderTarget,那么就需要在两个Pass内完成上述渲染;2.完成Post-Process,对一个全屏的矩形(就是上面提到的QUAD)每个像素执行MB的PS。

那么该怎么计算速度呢?可以确定的是,这个计算必须在VS上进行。那么是不是保存所有点的上一次的坐标呢?明显不用,只需要保存上一次的WVP矩阵就可以了。

    // Transform from object space to homogeneous projection space
    vPosProjSpaceCurrent = mul(vPos, mWorldViewProjection);
    vPosProjSpaceLast = mul(vPos, mWorldViewProjectionLast);
    
    // Output the vetrex position in projection space
    Output.Position = vPosProjSpaceCurrent;

    // Convert to non-homogeneous points [-1,1] by dividing by w 
    vPosProjSpaceCurrent /= vPosProjSpaceCurrent.w;
    vPosProjSpaceLast /= vPosProjSpaceLast.w;
    
    // Vertex's velocity (in non-homogeneous projection space) is the position this frame minus 
    // its position last frame.  This information is stored in a texture coord.  The pixel shader 
    // will read the texture coordinate with a sampler and use it to output each pixel's velocity.
    float2 velocity = vPosProjSpaceCurrent - vPosProjSpaceLast;    
    
    // The velocity is now between (-2,2) so divide by 2 to get it to (-1,1)
    velocity /= 2.0f;   

    // Store the velocity in a texture coord
    Output.VelocityUV = velocity;
这一段HLSL代码正是完成了计算速度的工作。注意,在计算速度前需要先除以w值,具体我也不清楚为什么。

PS_OUTPUT WorldPixelShader( VS_OUTPUT In )
{ 
    PS_OUTPUT Output;
    Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;
    Output.PixelVelocity = float4(In.VelocityUV,1.0f,1.0f);
    
    // Using MRT, output 2 values in the pixel shader.    
    return Output;
}
PS不需要做什么,基本上输出值就好了。

接下来就是怎么样利用记录好的值来进行MotionBlur了。

    float curVelocitySqMag = curFramePixelVelocity.r * curFramePixelVelocity.r +
                             curFramePixelVelocity.g * curFramePixelVelocity.g;
    float lastVelocitySqMag = lastFramePixelVelocity.r * lastFramePixelVelocity.r +
                              lastFramePixelVelocity.g * lastFramePixelVelocity.g;
                                   
    if( lastVelocitySqMag > curVelocitySqMag )
    {
        pixelVelocity.x =  lastFramePixelVelocity.r * PixelBlurConst;   
        pixelVelocity.y = -lastFramePixelVelocity.g * PixelBlurConst;
    }
    else
    {
        pixelVelocity.x =  curFramePixelVelocity.r * PixelBlurConst;   
        pixelVelocity.y = -curFramePixelVelocity.g * PixelBlurConst;    
    }
    
    // For each sample, sum up each sample's color in "Blurred" and then divide
    // to average the color after all the samples are added.
    float3 Blurred = 0;    
    for(float i = 0; i < NumberOfPostProcessSamples; i++)
    {   
        // Sample texture in a new spot based on pixelVelocity vector 
        // and average it with the other samples        
        float2 lookup = pixelVelocity * i / NumberOfPostProcessSamples + OriginalUV;
        
        // Lookup the color at this new spot
        float4 Current = tex2D(RenderTargetSampler, lookup);
        
        // Add it with the other samples
        Blurred += Current.rgb;
    }
    
    // Return the average color of all the samples
    return float4(Blurred / NumberOfPostProcessSamples, 1.0f);
以上就是MB的HLSL代码,简单来说就是首先确定使用哪一个速度值,然后就是根据速度方向来进行采样,这样就产生了模糊效果。

使用这样的方法来模拟动态模糊有两个缺点,一个是在边缘会出现漏洞,第二个是当物体移动过快时模糊效果不对,后一个可以通过提高帧数解决。前一个如果对每一个像素周围的像素也进行采用的话,估计改善情况。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值