1: 
   2: //-----------------------------------------------------------------------------
   3: // Global variables
   4: //-----------------------------------------------------------------------------
   5: float4 MaterialAmbientColor = {0.3, 0.3, 0.3, 1.0};
   6: float4 MaterialDiffuseColor = {0.7, 0.7, 0.7, 0.0};
   7: 
   8: #define LIGHT_DIR  0.0,10.0,0.0
   9: float3 LightDir = normalize(float3(LIGHT_DIR));
  10: float4 LightAmbient = { 1.0f, 1.0f, 1.0f, 1.0f };    // ambient
  11: float4 LightDiffuse = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse
  12: 
  13: float4x4 mWorld;
  14: float4x4 mWorldViewProjection;
  15: 
  16: float4 weight;
  17: float coeff_l;
  18: static const float MAP_WIDTH  = 512;
  19: static const float MAP_HEIGHT = 512;
  20: float4 vBias = {2/MAP_WIDTH, 4/MAP_WIDTH, 6/MAP_WIDTH, 8/MAP_WIDTH};
  21: float4 hBias = {2/MAP_HEIGHT,4/MAP_HEIGHT,6/MAP_HEIGHT,8/MAP_HEIGHT};
  22: 
  23: //-----------------------------------------------------------------------------
  24: // Texture samplers
  25: //-----------------------------------------------------------------------------
  26: texture RenderTargetTexture;
  27: sampler RenderTargetSampler = 
  28: sampler_state
  29: {
  30:     Texture = <RenderTargetTexture>;
  31:     MinFilter = LINEAR;
  32:     MagFilter = LINEAR;
  33: 
  34:     AddressU = Clamp;
  35:     AddressV = Clamp;
  36: };
  37: 
  38: texture MeshTexture;
  39: sampler MeshTextureSampler = 
  40: sampler_state
  41: {
  42:     Texture = <MeshTexture>;
  43:     MinFilter = LINEAR;
  44:     MagFilter = LINEAR;
  45:     MipFilter = NONE;
  46: 
  47:     AddressU = Clamp;
  48:     AddressV = Clamp;
  49: };
  50: 
  51: //-----------------------------------------------------------------------------
  52: //-----------------------------------------------------------------------------
  53: float3 GetDiffuse(float3 normalW )
  54: {
  55:     return MaterialDiffuseColor * LightDiffuse * max(0,dot(normalW, LightDir))
  56:            + MaterialAmbientColor * LightAmbient;
  57: }
  58: 
  59: 
  60: //-----------------------------------------------------------------------------
  61: // Vertex shader output structure
  62: //-----------------------------------------------------------------------------
  63: struct VS_OUTPUT
  64: {
  65:     float4 Position : POSITION;
  66:     float2 TextureUV : TEXCOORD0;
  67:     float4 Diffuse : TEXCOORD1;
  68: };
  69: struct PS_OUTPUT
  70: {
  71:     float4 Color : Color0;
  72: };
  73: 
  74: 
  75: //-----------------------------------------------------------------------------
  76: // Name: SceneVertexShader
  77: // Type: Vertex shader                                      
  78: // Desc: In addition to standard transform and lighting, it calculates the blur
  79: //       factor of the vertex and outputs this as a texture coord.
  80: //-----------------------------------------------------------------------------
  81: VS_OUTPUT SceneVertexShader( float4 vPos : POSITION, 
  82:                              float3 vNormal : NORMAL,
  83:                              float2 vTexCoord0 : TEXCOORD0 )
  84: {
  85:     VS_OUTPUT Output;
  86:     float3 vViewPosition;
  87:     float3 vWorldNormal;
  88:     float  fBlurFactor;
  89:   
  90:     // tranform vertex position into screen space
  91:     Output.Position = mul(vPos, mWorldViewProjection);
  92:     
  93:     // Compute simple lighting equation
  94:     vWorldNormal = mul(vNormal, (float3x3)mWorld);       
  95:     Output.Diffuse = float4(GetDiffuse(vWorldNormal), 1.0);
  96:     
  97:     // Just copy the texture coordinate through
  98:     Output.TextureUV.xy = vTexCoord0;
  99:     
 100:     return Output;
 101: }
 102: 
 103: 
 104: //-----------------------------------------------------------------------------
 105: // Name: ScenePixelShader
 106: // Type: Pixel shader
 107: // Desc: This shader simply outputs the pixel's color 
 108: //-----------------------------------------------------------------------------
 109: PS_OUTPUT ScenePixelShader( VS_OUTPUT In )
 110: {
 111:     PS_OUTPUT Output;
 112: 
 113:     Output.Color = tex2D(MeshTextureSampler, In.TextureUV.xy) * In.Diffuse;
 114:     Output.Color.a = 1.0f;
 115:     
 116:     return Output;
 117: }
 118: 
 119: 
 120: //-----------------------------------------------------------------------------
 121: // Name: TechScene
 122: // Type: Technique                                     
 123: // Desc: Renders the scene's color to the render target and stores the
 124: //       depth information as a blur factor in the alpha channel.
 125: //-----------------------------------------------------------------------------
 126: technique TechScene
 127: {
 128:     pass P0
 129:     {        
 130:         VertexShader = compile vs_1_1 SceneVertexShader();
 131:         PixelShader  = compile ps_2_0 ScenePixelShader();
 132:     }
 133: }
 134: 
 135: 
 136: 
 137: 
 138: //-----------------------------------------------------------------------------
 139: // Name: RenderGaussX
 140: //-----------------------------------------------------------------------------
 141: float4 RenderGaussX( in float2 OriginalUV : TEXCOORD0 ) : COLOR 
 142: {
 143:      float4 Color;
 144:      
 145:      half4 t00 = tex2D( MeshTextureSampler, OriginalUV );
 146:      half4 t10 = tex2D( MeshTextureSampler, OriginalUV + half2(vBias.x, 0));
 147:      half4 t11 = tex2D( MeshTextureSampler, OriginalUV - half2(vBias.x, 0));
 148:      half4 t20 = tex2D( MeshTextureSampler, OriginalUV + half2(vBias.y, 0));
 149:      half4 t21 = tex2D( MeshTextureSampler, OriginalUV - half2(vBias.y, 0));
 150:      half4 t30 = tex2D( MeshTextureSampler, OriginalUV + half2(vBias.z, 0));
 151:      half4 t31 = tex2D( MeshTextureSampler, OriginalUV - half2(vBias.z, 0));
 152:      half4 t40 = tex2D( MeshTextureSampler, OriginalUV + half2(vBias.w, 0));
 153:      half4 t41 = tex2D( MeshTextureSampler, OriginalUV - half2(vBias.w, 0));
 154:      
 155:      // 中心との色の差
 156:      half3 d10 = t10.rgb - t00.rgb;
 157:      half3 d20 = t20.rgb - t00.rgb;
 158:      half3 d30 = t30.rgb - t00.rgb;
 159:      half3 d40 = t40.rgb - t00.rgb;
 160:      half3 d11 = t11.rgb - t00.rgb;
 161:      half3 d21 = t21.rgb - t00.rgb;
 162:      half3 d31 = t31.rgb - t00.rgb;
 163:      half3 d41 = t41.rgb - t00.rgb;
 164: 
 165:      // 中心との色の強さの差の2乗
 166:      half4 l0, l1;
 167:      l0.x = dot(d10, d10);
 168:      l0.y = dot(d20, d20);
 169:      l0.z = dot(d30, d30);
 170:      l0.w = dot(d40, d40);
 171:      l1.x = dot(d11, d11);
 172:      l1.y = dot(d21, d21);
 173:      l1.z = dot(d31, d31);
 174:      l1.w = dot(d41, d41);
 175:      
 176:      l0 = weight * exp(coeff_l * l0);
 177:      l1 = weight * exp(coeff_l * l1);
 178: 
 179:      Color  = t00;
 180:      Color += l0.x * t10 + l1.x * t11;
 181:      Color += l0.y * t20 + l1.y * t21;
 182:      Color += l0.z * t30 + l1.z * t31;
 183:      Color += l0.w * t40 + l1.w * t41;
 184:      
 185:      return Color / 4;// 全部足すと精度が足りないようなので、適当に割っとく
 186: }
 187: 
 188: 
 189: technique TechGaussX
 190: {
 191:     pass P0
 192:     {        
 193:         PixelShader = compile ps_2_0 RenderGaussX();
 194:     }
 195: }
 196: //-----------------------------------------------------------------------------
 197: // Name: RenderGaussX
 198: //-----------------------------------------------------------------------------
 199: float4 RenderGaussY( in float2 OriginalUV : TEXCOORD0 ) : COLOR 
 200: {
 201:      float4 Color;
 202:      
 203:      half4 t00 = tex2D( MeshTextureSampler, OriginalUV );
 204:      half4 t10 = tex2D( MeshTextureSampler, OriginalUV + half2(0, hBias.x));
 205:      half4 t11 = tex2D( MeshTextureSampler, OriginalUV - half2(0, hBias.x));
 206:      half4 t20 = tex2D( MeshTextureSampler, OriginalUV + half2(0, hBias.y));
 207:      half4 t21 = tex2D( MeshTextureSampler, OriginalUV - half2(0, hBias.y));
 208:      half4 t30 = tex2D( MeshTextureSampler, OriginalUV + half2(0, hBias.z));
 209:      half4 t31 = tex2D( MeshTextureSampler, OriginalUV - half2(0, hBias.z));
 210:      half4 t40 = tex2D( MeshTextureSampler, OriginalUV + half2(0, hBias.w));
 211:      half4 t41 = tex2D( MeshTextureSampler, OriginalUV - half2(0, hBias.w));
 212:      
 213:      // 中心との色の差
 214:      half3 d10 = t10.rgb - t00.rgb;
 215:      half3 d20 = t20.rgb - t00.rgb;
 216:      half3 d30 = t30.rgb - t00.rgb;
 217:      half3 d40 = t40.rgb - t00.rgb;
 218:      half3 d11 = t11.rgb - t00.rgb;
 219:      half3 d21 = t21.rgb - t00.rgb;
 220:      half3 d31 = t31.rgb - t00.rgb;
 221:      half3 d41 = t41.rgb - t00.rgb;
 222: 
 223:      // 中心との色の強さの差の2乗
 224:      half4 l0, l1;
 225:      l0.x = dot(d10, d10);
 226:      l0.y = dot(d20, d20);
 227:      l0.z = dot(d30, d30);
 228:      l0.w = dot(d40, d40);
 229:      l1.x = dot(d11, d11);
 230:      l1.y = dot(d21, d21);
 231:      l1.z = dot(d31, d31);
 232:      l1.w = dot(d41, d41);
 233:      
 234:      l0 = weight * exp(coeff_l * l0);
 235:      l1 = weight * exp(coeff_l * l1);
 236: 
 237:      Color  = t00;
 238:      Color += l0.x * t10 + l1.x * t11;
 239:      Color += l0.y * t20 + l1.y * t21;
 240:      Color += l0.z * t30 + l1.z * t31;
 241:      Color += l0.w * t40 + l1.w * t41;
 242:      
 243:      return Color/Color.a;
 244: }
 245: 
 246: 
 247: technique TechGaussY
 248: {
 249:     pass P0
 250:     {        
 251:         PixelShader = compile ps_2_0 RenderGaussY();
 252:     }
 253: }
 254: 
 255: 
 256: 
 257: float4 FinalPS( in float2 OriginalUV : TEXCOORD0 ) : COLOR 
 258: {
 259: 	return tex2D(MeshTextureSampler, OriginalUV);
 260: }
 261: 
 262: 
 263: //-----------------------------------------------------------------------------
 264: // Name: TechFinal
 265: // Type: Technique
 266: //-----------------------------------------------------------------------------
 267: technique TechFinal
 268: {
 269:     pass P0
 270:     {        
 271:         PixelShader = compile ps_1_1 FinalPS();
 272:         
 273:         ZEnable = false;
 274:         AlphaBlendEnable = false;
 275:     }
 276: }