shader学习笔记——运动模糊(motion blur)

运动模糊(motion blur

运动模糊效果的工作流程

 

1.先将场景渲染到一个RenderTarget1
2.
RenderTarget1跟上一帧渲染的结果进行混合, 并输出到RenderTarget2
3.
RenderTarget2输出到屏幕, 并将其保留到下一帧进行混合

ScreenAlignedQuad:一个正方形网格

 

 

Pass0的设置和代码:

RenderTarget 开启颜色清除和深度缓冲清除。

Vertex Shader代码:

float4x4 matViewProjection;

struct VS_INPUT

{

   float4 Position : POSITION0;

   float2 Txr1   : TEXCOORD0;

  

};

struct VS_OUTPUT

{

   float4 Position : POSITION0;

   float2 Txr1   : TEXCOORD0;

  

};

VS_OUTPUT vs_main( VS_INPUT Input )

{

   VS_OUTPUT Output;

 

   Output.Position = mul( Input.Position, matViewProjection );//位置

   Output.Txr1 = Input.Txr1;//获取纹理

  

   return( Output );

  

}

Pixel Shader代码:

sampler Texture0;

float4 ps_main(

   float4 inDiffuse: COLOR0,

   float2 inTxr1: TEXCOORD0

) : COLOR0

{

   //  Output color:

   return tex2D(Texture0,inTxr1); //纹理映射

}

 

Blur1的设置和代码:

 

 

 

RenderState设置

D3DCULLMODE – D3DCULL_NONE,

D3DRS_ZENABLE – D3DZB_FALSE,

D3DRS_ZWRITEENABLE – FALSE.

Texture0设置:

 

 

Vertex Shader代码:

float4x4 matViewProjection;

struct VS_INPUT

{

   float4 Position : POSITION0;

};

struct VS_OUTPUT

{

   float4 Position : POSITION0;

   float2 texCoord : TEXCOORD0;

};

 

VS_OUTPUT vs_main( VS_INPUT Input )

{

   VS_OUTPUT Output;

   // Simply output the position without transforming it

   Output.Position = float4(Input.Position.xy,0,1);

   // Texture coordinates are setup so that the full texture

   // is mapped completeley onto the screen

   Output.texCoord.x = 0.5 * (1 + Input.Position.x);

   Output.texCoord.y = 0.5 * (1 - Input.Position.y);

   return( Output );

}

Pixel Shader 代码:

float4 blurFactor;

sampler Texture0;

sampler Texture1;

float4 ps_main(float2 texCoord: TEXCOORD0) : COLOR0

{

   float4 col1 = tex2D(Texture0, texCoord);

   float4 col2 = tex2D(Texture1, texCoord);

 

   return lerp(col1,col2,blurFactor);

}

 

 

 

Present的设置和代码:

 

 

Texture0设置

 

 

Vertex Shader代码:

float4x4 matViewProjection;

 

struct VS_INPUT

{

   float4 Position : POSITION0;

};

 

struct VS_OUTPUT

{

   float4 Position : POSITION0;

   float2 texCoord : TEXCOORD0;

};

 

VS_OUTPUT vs_main( VS_INPUT Input )

{

   VS_OUTPUT Output;

   Output.Position = float4(Input.Position.xy,0,1);

   Output.texCoord.x = 0.5 * (1 + Input.Position.x);

   Output.texCoord.y = 0.5 * (1 - Input.Position.y);

   return( Output );

}

Pixel Shader代码:

sampler Texture0;

float4 ps_main(float2 texCoord: TEXCOORD0) : COLOR0

{

   // Simply read the temporary texture and send

   // the color to the output without manipulating it

   return tex2D(Texture0,  texCoord);

}

效果:

 

你可能感兴趣的:(游戏开发)