0001: // -------------------------------------------------------------
0002: // インクレンダリング
0003: // 
0004: // Copyright (c) 2003 IMAGIRE Takashi. All rights reserved.
0005: // -------------------------------------------------------------
0006: 
0007: // -------------------------------------------------------------
0008: // グローバル変数
0009: // -------------------------------------------------------------
0010: 
0011: float  MAP_WIDTH;   // SAT の幅
0012: float  MAP_HEIGHT;  // SAT の高さ
0013: 
0014: float SIZE = 40.0f; // ぼかす量
0015: float INV_SIZE2=1.0f/(40.0f*40.0f); // ぼかす量の逆2乗
0016: 
0017: // -------------------------------------------------------------
0018: // テクスチャ
0019: // -------------------------------------------------------------
0020: texture SrcMap;
0021: sampler SrcSamp = sampler_state
0022: {
0023:     Texture = <SrcMap>;
0024:     MinFilter = POINT;
0025:     MagFilter = POINT;
0026:     MipFilter = NONE;
0027: 
0028:     AddressU = Clamp;
0029:     AddressV = Clamp;
0030: };
0031: // -------------------------------------------------------------
0032: texture WeightMap;
0033: sampler WeightSamp = sampler_state
0034: {
0035:     Texture = <WeightMap>;
0036:     MinFilter = POINT;
0037:     MagFilter = POINT;
0038:     MipFilter = NONE;
0039: 
0040:     AddressU = Clamp;
0041:     AddressV = Clamp;
0042: };
0043: // -------------------------------------------------------------
0044: // 頂点シェーダからピクセルシェーダに渡すデータ
0045: // -------------------------------------------------------------
0046: struct VS_OUTPUT
0047: {
0048:     float4 Pos          : POSITION;
0049:     float2 ofset        : COLOR0;
0050:     float2 Tex0         : TEXCOORD0;
0051:     float2 Tex1         : TEXCOORD1;
0052:     float2 Tex2         : TEXCOORD2;
0053:     float2 Tex3         : TEXCOORD3;
0054: };
0055: 
0056: // -------------------------------------------------------------
0057: // -------------------------------------------------------------
0058: // エリア総和テーブルの作成
0059: // -------------------------------------------------------------
0060: // -------------------------------------------------------------
0061: 
0062: // -------------------------------------------------------------
0063: // 頂点シェーダプログラム
0064: // -------------------------------------------------------------
0065: VS_OUTPUT VS_sat_x (
0066:       float4 Pos    : POSITION,          // モデルの頂点
0067:       float4 Tex    : TEXCOORD0          // テクスチャ座標
0068: ){
0069:     VS_OUTPUT Out = (VS_OUTPUT)0;        // 出力データ
0070:     
0071:     // 位置座標
0072:     Out.Pos = Pos;
0073:     
0074:     // テクスチャ座標// 中心
0075:     Out.Tex0 = Tex + float2( 0.5f/MAP_WIDTH, 0.5f/MAP_HEIGHT );
0076:     Out.Tex1 = Tex + float2(-0.5f/MAP_WIDTH, 0.5f/MAP_HEIGHT );// 左
0077:     
0078:     return Out;
0079: }
0080: // -------------------------------------------------------------
0081: // ピクセルシェーダプログラム
0082: // -------------------------------------------------------------
0083: float4 PS_sat_x(VS_OUTPUT In) : COLOR
0084: {   
0085:     float4 Color;
0086:     float4 weight = tex2D( WeightSamp, In.Tex0 );
0087:     
0088:     Color  = tex2D( SrcSamp, In.Tex0 ) * weight
0089:            + tex2D( SrcSamp, In.Tex1 );
0090:            
0091:     return Color;
0092: }
0093: 
0094: // -------------------------------------------------------------
0095: // 頂点シェーダプログラム
0096: // -------------------------------------------------------------
0097: VS_OUTPUT VS_sat_y (
0098:       float4 Pos    : POSITION,          // モデルの頂点
0099:       float4 Tex    : TEXCOORD0          // テクスチャ座標
0100: ){
0101:     VS_OUTPUT Out = (VS_OUTPUT)0;        // 出力データ
0102:     
0103:     // 位置座標
0104:     Out.Pos = Pos;
0105:     
0106:     // テクスチャ座標
0107:     Out.Tex0 = Tex + float2( 0.5f/MAP_WIDTH, 0.5f/MAP_HEIGHT );
0108:     Out.Tex1 = Tex + float2( 0.5f/MAP_WIDTH,-0.5f/MAP_HEIGHT );// 上
0109:     
0110:     return Out;
0111: }
0112: 
0113: // -------------------------------------------------------------
0114: // ピクセルシェーダプログラム
0115: // -------------------------------------------------------------
0116: float4 PS_sat_y(VS_OUTPUT In) : COLOR
0117: {   
0118:     float4 Color;
0119:     
0120:     Color  = tex2D( SrcSamp, In.Tex0 )
0121:            + tex2D( SrcSamp, In.Tex1 );
0122:            
0123:     return Color;
0124: }
0125: 
0126: // -------------------------------------------------------------
0127: // -------------------------------------------------------------
0128: // 最終出力
0129: // -------------------------------------------------------------
0130: // -------------------------------------------------------------
0131: 
0132: // -------------------------------------------------------------
0133: // 頂点シェーダプログラム
0134: // -------------------------------------------------------------
0135: VS_OUTPUT VS_out (
0136:       float4 Pos    : POSITION,          // モデルの頂点
0137:       float4 Tex    : TEXCOORD0          // テクスチャ座標
0138: ){
0139:     VS_OUTPUT Out = (VS_OUTPUT)0;        // 出力データ
0140:     
0141:     // 位置座標
0142:     Out.Pos = Pos;
0143:     
0144:     Out.Tex0 = Tex + float2( (0.5f-0.5f*SIZE)/MAP_WIDTH, (0.5f-0.5f*SIZE)/MAP_HEIGHT );
0145:     Out.Tex1 = Tex + float2( (0.5f-0.5f*SIZE)/MAP_WIDTH, (0.5f+0.5f*SIZE)/MAP_HEIGHT );
0146:     Out.Tex2 = Tex + float2( (0.5f+0.5f*SIZE)/MAP_WIDTH, (0.5f-0.5f*SIZE)/MAP_HEIGHT );
0147:     Out.Tex3 = Tex + float2( (0.5f+0.5f*SIZE)/MAP_WIDTH, (0.5f+0.5f*SIZE)/MAP_HEIGHT );
0148:     
0149:     return Out;
0150: }
0151: 
0152: // -------------------------------------------------------------
0153: // ピクセルシェーダプログラム
0154: // -------------------------------------------------------------
0155: float4 PS_out(VS_OUTPUT In) : COLOR
0156: {   
0157:     float4 Color;
0158:     float  weight;
0159:     
0160:     // 重みの総和を求める
0161:     weight =  tex2D( SrcSamp, In.Tex0 ).a
0162:             - tex2D( SrcSamp, In.Tex1 ).a
0163:             - tex2D( SrcSamp, In.Tex2 ).a
0164:             + tex2D( SrcSamp, In.Tex3 ).a;
0165:             
0166:     // SATの値を重みの総和で割って、色を求める
0167:     Color  =( tex2D( SrcSamp, In.Tex0 )
0168:             - tex2D( SrcSamp, In.Tex1 )
0169:             - tex2D( SrcSamp, In.Tex2 )
0170:             + tex2D( SrcSamp, In.Tex3 ))/weight;
0171: 
0172:     return Color;
0173: }
0174: 
0175: // -------------------------------------------------------------
0176: // テクニック
0177: // -------------------------------------------------------------
0178: technique TShader
0179: {
0180:     pass P0
0181:     {
0182:         // -----------------------------------------------------
0183:         // X軸ぼかし
0184:         // -----------------------------------------------------
0185:         VertexShader = compile vs_1_1 VS_sat_x();
0186:         PixelShader  = compile ps_2_0 PS_sat_x();
0187:     }
0188:     pass P1
0189:     {
0190:         // -----------------------------------------------------
0191:         // Y軸ぼかし
0192:         // -----------------------------------------------------
0193:         VertexShader = compile vs_1_1 VS_sat_y();
0194:         PixelShader  = compile ps_2_0 PS_sat_y();
0195:     }
0196:     pass P2
0197:     {
0198:         // -----------------------------------------------------
0199:         // 出力
0200:         // -----------------------------------------------------
0201:         VertexShader = compile vs_1_1 VS_out();
0202:         PixelShader  = compile ps_2_0 PS_out();
0203:     }
0204: }
0205: