0001: 
0002: //-----------------------------------------------------------------------------
0003: // Global variables
0004: //-----------------------------------------------------------------------------
0005: float4 MaterialAmbientColor = {0.3, 0.3, 0.3, 1.0};
0006: float4 MaterialDiffuseColor = {0.7, 0.7, 0.7, 0.0};
0007: 
0008: #define LIGHT_DIR  0.0,10.0,0.0
0009: float3 LightDir = normalize(float3(LIGHT_DIR));
0010: float4 LightAmbient = { 1.0f, 1.0f, 1.0f, 1.0f };    // ambient
0011: float4 LightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse
0012: 
0013: float4x4 mWorld;
0014: float4x4 mWorldView;
0015: float4x4 mWorldViewProjection;
0016: 
0017: float weight[4];
0018: static const float MAP_WIDTH  = 1024;
0019: static const float MAP_HEIGHT = 1024;
0020: float4 vBias = {2/MAP_WIDTH, 4/MAP_WIDTH, 6/MAP_WIDTH, 8/MAP_WIDTH};
0021: float4 hBias = {2/MAP_HEIGHT,4/MAP_HEIGHT,6/MAP_HEIGHT,8/MAP_HEIGHT};
0022: 
0023: float fIntensity = 1.0;
0024: 
0025: float z_max ;
0026: float z_min;
0027: 
0028: //-----------------------------------------------------------------------------
0029: // Texture samplers
0030: //-----------------------------------------------------------------------------
0031: texture RenderTargetTexture;
0032: sampler RenderTargetSampler = 
0033: sampler_state
0034: {
0035:     Texture = <RenderTargetTexture>;
0036:     MinFilter = LINEAR;
0037:     MagFilter = LINEAR;
0038: 
0039:     AddressU = Clamp;
0040:     AddressV = Clamp;
0041: };
0042: 
0043: texture MeshTexture;
0044: sampler MeshTextureSampler = 
0045: sampler_state
0046: {
0047:     Texture = <MeshTexture>;
0048:     MinFilter = LINEAR;
0049:     MagFilter = LINEAR;
0050:     MipFilter = NONE;
0051: 
0052:     AddressU = Clamp;
0053:     AddressV = Clamp;
0054: };
0055: 
0056: 
0057: texture DepthRenderTarget;
0058: sampler DepthSampler = 
0059: sampler_state
0060: {
0061:     Texture = <DepthRenderTarget>;
0062:     MinFilter = LINEAR;
0063:     MagFilter = LINEAR;
0064:     MipFilter = NONE;
0065: 
0066:     AddressU = Clamp;
0067:     AddressV = Clamp;
0068: };
0069: 
0070: 
0071: 
0072: 
0073: 
0074: 
0075: //-----------------------------------------------------------------------------
0076: //-----------------------------------------------------------------------------
0077: float3 GetDiffuse(float3 normalW )
0078: {
0079:     return MaterialDiffuseColor * LightDiffuse * max(0,dot(normalW, LightDir))
0080:            + MaterialAmbientColor * LightAmbient;
0081: }
0082: 
0083: 
0084: //-----------------------------------------------------------------------------
0085: // Vertex shader output structure
0086: //-----------------------------------------------------------------------------
0087: struct VS_OUTPUT
0088: {
0089:     float4 Position : POSITION;
0090:     float3 TextureUV : TEXCOORD0;
0091:     float4 Diffuse : TEXCOORD1;
0092: };
0093: struct PS_OUTPUT
0094: {
0095:     float4 Color : Color0;
0096:     float4 Depth : Color1;
0097: };
0098: 
0099: 
0100: //-----------------------------------------------------------------------------
0101: // Name: SceneVertexShader
0102: // Type: Vertex shader                                      
0103: // Desc: In addition to standard transform and lighting, it calculates the blur
0104: //       factor of the vertex and outputs this as a texture coord.
0105: //-----------------------------------------------------------------------------
0106: VS_OUTPUT SceneVertexShader( float4 vPos : POSITION, 
0107:                              float3 vNormal : NORMAL,
0108:                              float2 vTexCoord0 : TEXCOORD0 )
0109: {
0110:     VS_OUTPUT Output;
0111:     float3 vViewPosition;
0112:     float3 vWorldNormal;
0113:     float  fBlurFactor;
0114:   
0115:     // tranform vertex position into screen space
0116:     Output.Position = mul(vPos, mWorldViewProjection);
0117:     
0118:     // Compute simple lighting equation
0119:     vWorldNormal = mul(vNormal, (float3x3)mWorld);       
0120:     Output.Diffuse = float4(GetDiffuse(vWorldNormal), 1.0);
0121:     
0122:     // Just copy the texture coordinate through
0123:     Output.TextureUV.xy = vTexCoord0;
0124:     
0125:     // 深度情報をテクスチャ座標に埋め込む
0126:     Output.TextureUV.z = (mul(vPos, mWorldView).z - z_min)/(z_max-z_min);
0127: 
0128:     return Output;
0129: }
0130: 
0131: 
0132: //-----------------------------------------------------------------------------
0133: // Name: ScenePixelShader
0134: // Type: Pixel shader
0135: // Desc: This shader simply outputs the pixel's color 
0136: //-----------------------------------------------------------------------------
0137: PS_OUTPUT ScenePixelShader( VS_OUTPUT In )
0138: {
0139:     PS_OUTPUT Output;
0140: 
0141:     Output.Color = tex2D(MeshTextureSampler, In.TextureUV.xy) * In.Diffuse;
0142:     Output.Depth = In.TextureUV.z;
0143: 
0144:     return Output;
0145: }
0146: 
0147: 
0148: //-----------------------------------------------------------------------------
0149: // Name: TechScene
0150: // Type: Technique                                     
0151: // Desc: Renders the scene's color to the render target and stores the
0152: //       depth information as a blur factor in the alpha channel.
0153: //-----------------------------------------------------------------------------
0154: technique TechScene
0155: {
0156:     pass P0
0157:     {        
0158:         VertexShader = compile vs_1_1 SceneVertexShader();
0159:         PixelShader  = compile ps_2_0 ScenePixelShader();
0160:     }
0161: }
0162: 
0163: 
0164: 
0165: 
0166: //-----------------------------------------------------------------------------
0167: // Name: RenderGaussX
0168: //-----------------------------------------------------------------------------
0169: float4 RenderGaussX( in float2 OriginalUV : TEXCOORD0 ) : COLOR 
0170: {
0171:      float4 Color;
0172:      
0173:      Color  = weight[0] *  tex2D( MeshTextureSampler, OriginalUV );
0174:      Color += weight[1]
0175:       * (tex2D( MeshTextureSampler, OriginalUV + float2( vBias.x, 0 ) )
0176:       +  tex2D( MeshTextureSampler, OriginalUV - float2( vBias.x, 0 ) ));
0177:      Color += weight[2]
0178:       * (tex2D( MeshTextureSampler, OriginalUV + float2( vBias.y, 0 ) )
0179:       +  tex2D( MeshTextureSampler, OriginalUV - float2( vBias.y, 0 ) ));
0180:      Color += weight[3]
0181:       * (tex2D( MeshTextureSampler, OriginalUV + float2( vBias.z, 0 ) )
0182:       +  tex2D( MeshTextureSampler, OriginalUV - float2( vBias.z, 0 ) ));
0183:      
0184:      return Color;
0185: }
0186: 
0187: 
0188: technique TechGaussX
0189: {
0190:     pass P0
0191:     {        
0192:         PixelShader = compile ps_2_0 RenderGaussX();
0193:     }
0194: }
0195: //-----------------------------------------------------------------------------
0196: // Name: RenderGaussX
0197: //-----------------------------------------------------------------------------
0198: float4 RenderGaussY( in float2 OriginalUV : TEXCOORD0 ) : COLOR 
0199: {
0200:      float4 Color;
0201: 
0202:      Color  = weight[0] *  tex2D( MeshTextureSampler, OriginalUV );
0203:      Color += weight[1]
0204:       * (tex2D( MeshTextureSampler, OriginalUV + float2( 0, hBias.x) )
0205:       +  tex2D( MeshTextureSampler, OriginalUV - float2( 0, hBias.x) ));
0206:      Color += weight[2]
0207:       * (tex2D( MeshTextureSampler, OriginalUV + float2( 0, hBias.y) )
0208:       +  tex2D( MeshTextureSampler, OriginalUV - float2( 0, hBias.y) ));
0209:      Color += weight[3]
0210:       * (tex2D( MeshTextureSampler, OriginalUV + float2( 0, hBias.z) )
0211:       +  tex2D( MeshTextureSampler, OriginalUV - float2( 0, hBias.z) ));
0212:      
0213:      return Color;
0214: }
0215: 
0216: 
0217: technique TechGaussY
0218: {
0219:     pass P0
0220:     {        
0221:         PixelShader = compile ps_2_0 RenderGaussY();
0222:     }
0223: }
0224: 
0225: 
0226: 
0227: float4 FinalPS( in float2 OriginalUV : TEXCOORD0 ) : COLOR 
0228: {
0229:     float4 color = tex2D(RenderTargetSampler, OriginalUV);
0230:     float  depth      = tex2D(DepthSampler, OriginalUV).r;
0231:     float  depth_blur = tex2D(MeshTextureSampler, OriginalUV).r;
0232: 
0233:     float diff = depth - depth_blur;
0234:     diff = max(0, diff);
0235: 
0236:     return color * (1 - fIntensity * diff);
0237: }
0238: 
0239: 
0240: //-----------------------------------------------------------------------------
0241: // Name: TechFishEye
0242: // Type: Technique
0243: //-----------------------------------------------------------------------------
0244: technique TechFinal
0245: {
0246:     pass P0
0247:     {        
0248:         PixelShader = compile ps_2_0 FinalPS();
0249:         
0250:         ZEnable = false;
0251:         AlphaBlendEnable = false;
0252:     }
0253: }
0254: