0001: // ------------------------------------------------------------
0002: // 重心計算
0003: // 
0004: // Copyright (c) 2004 IMAGIRE Takashi. All rights reserved.
0005: // ------------------------------------------------------------
0006: 
0007: float inv_size = 1/256;
0008: 
0009: // ------------------------------------------------------------
0010: // Textures(テクスチャ)
0011: // ------------------------------------------------------------
0012: texture LinearTex;
0013: sampler LinearSamp = sampler_state
0014: {
0015:     Texture = <LinearTex>;
0016:     MinFilter = LINEAR;
0017:     MagFilter = LINEAR;
0018:     MipFilter = NONE;
0019: 
0020:     AddressU = Clamp;
0021:     AddressV = Clamp;
0022: };
0023: // ------------------------------------------------------------
0024: texture PointTex;
0025: sampler PointSamp = sampler_state
0026: {
0027:     Texture = <PointTex>;
0028:     MinFilter = POINT;
0029:     MagFilter = POINT;
0030:     MipFilter = NONE;
0031: 
0032:     AddressU = Clamp;
0033:     AddressV = Clamp;
0034: };
0035: 
0036: // ------------------------------------------------------------
0037: // 加重平均を出力する
0038: // ------------------------------------------------------------
0039: struct FRAGMENT0
0040: {
0041:     float4 Pos          : POSITION;
0042:     float2 Tex          : TEXCOORD0;
0043:     float4 XY           : TEXCOORD1;
0044: };
0045: // ------------------------------------------------------------
0046: FRAGMENT0 VS0 (
0047:       float4 Pos    : POSITION           // (モデルの頂点)
0048:     , float2 Tex    : TEXCOORD0          // (テクスチャ座標)
0049:       )
0050: {
0051:     FRAGMENT0 Out = (FRAGMENT0)0;
0052:     
0053:     // Position(位置座標)
0054:     Out.Pos = Pos;
0055:     
0056:     // [0,1]にした位置
0057:     Out.XY = 1;
0058:     Out.XY.x = (  0.5 * Pos.x + 0.5);
0059:     Out.XY.y = (- 0.5 * Pos.y + 0.5);
0060:     
0061:     // テクスチャ座標
0062:     Out.Tex = Tex;
0063:     
0064:     return Out;
0065: }
0066: // ------------------------------------------------------------
0067: float4 PS0 (FRAGMENT0 In) : COLOR
0068: {
0069:     float4 Out;
0070:     
0071:     Out = tex2D( LinearSamp, In.Tex ).x * In.XY;
0072:     
0073:     return Out;
0074: }
0075: 
0076: 
0077: 
0078: // ------------------------------------------------------------
0079: // 平均操作
0080: // ------------------------------------------------------------
0081: struct FRAGMENT1
0082: {
0083:     float4 Pos          : POSITION;
0084:     float2 Tex0         : TEXCOORD0;
0085:     float2 Tex1         : TEXCOORD1;
0086:     float2 Tex2         : TEXCOORD2;
0087:     float2 Tex3         : TEXCOORD3;
0088:     float2 Tex4         : TEXCOORD4;
0089:     float2 Tex5         : TEXCOORD5;
0090:     float2 Tex6         : TEXCOORD6;
0091:     float2 Tex7         : TEXCOORD7;
0092: };
0093: // ------------------------------------------------------------
0094: FRAGMENT1 VS1 (
0095:       float4 Pos    : POSITION           // (モデルの頂点)
0096:     , float2 Tex    : TEXCOORD0          // (テクスチャ座標)
0097:       )
0098: {
0099:     FRAGMENT1 Out = (FRAGMENT1)0;
0100:     
0101:     // Position(位置座標)
0102:     Out.Pos = Pos;
0103:     
0104:     // テクスチャ座標
0105:     Out.Tex0 = Tex + float2( 0.5*inv_size, 0.5*inv_size );
0106:     Out.Tex1 = Tex + float2( 1.5*inv_size, 0.5*inv_size );
0107:     Out.Tex2 = Tex + float2( 2.5*inv_size, 0.5*inv_size );
0108:     Out.Tex3 = Tex + float2( 3.5*inv_size, 0.5*inv_size );
0109:     Out.Tex4 = Tex + float2( 0.5*inv_size, 1.5*inv_size );
0110:     Out.Tex5 = Tex + float2( 1.5*inv_size, 1.5*inv_size );
0111:     Out.Tex6 = Tex + float2( 2.5*inv_size, 1.5*inv_size );
0112:     Out.Tex7 = Tex + float2( 3.5*inv_size, 1.5*inv_size );
0113:     
0114:     return Out;
0115: }
0116: // ------------------------------------------------------------
0117: float4 PS1 (FRAGMENT1 In) : COLOR
0118: {
0119:     float4 Out;
0120:     
0121:     Out = tex2D( PointSamp, In.Tex0 )
0122:         + tex2D( PointSamp, In.Tex1 )
0123:         + tex2D( PointSamp, In.Tex2 )
0124:         + tex2D( PointSamp, In.Tex3 )
0125:         + tex2D( PointSamp, In.Tex4 )
0126:         + tex2D( PointSamp, In.Tex5 )
0127:         + tex2D( PointSamp, In.Tex6 )
0128:         + tex2D( PointSamp, In.Tex7 )
0129:         + tex2D( PointSamp, In.Tex0 + float2( 0.0, 2.0*inv_size ) )
0130:         + tex2D( PointSamp, In.Tex1 + float2( 0.0, 2.0*inv_size ) )
0131:         + tex2D( PointSamp, In.Tex2 + float2( 0.0, 2.0*inv_size ) )
0132:         + tex2D( PointSamp, In.Tex3 + float2( 0.0, 2.0*inv_size ) )
0133:         + tex2D( PointSamp, In.Tex4 + float2( 0.0, 2.0*inv_size ) )
0134:         + tex2D( PointSamp, In.Tex5 + float2( 0.0, 2.0*inv_size ) )
0135:         + tex2D( PointSamp, In.Tex6 + float2( 0.0, 2.0*inv_size ) )
0136:         + tex2D( PointSamp, In.Tex7 + float2( 0.0, 2.0*inv_size ) );
0137:     
0138:     return Out/16;
0139: }
0140: 
0141: 
0142: // ------------------------------------------------------------
0143: // ターゲットを動かす
0144: // ------------------------------------------------------------
0145: struct FRAGMENT2
0146: {
0147:     float4 Pos          : POSITION;
0148:     float2 Tex          : TEXCOORD0;
0149: };
0150: // ------------------------------------------------------------
0151: float4 PS2 (FRAGMENT2 In) : COLOR
0152: {
0153:     float4 Out;
0154:     
0155:     float4 pos =  tex2D( PointSamp, float2(0.5,0.5) );
0156:     pos /= pos.w;
0157:     
0158:     pos *= 512/32;
0159: 
0160:     Out =  tex2D( LinearSamp, In.Tex - pos.xy );
0161: 
0162: //  clip( Out.x-0.5 );// 黒抜き(効率悪い)
0163:     
0164:     return Out;
0165: }
0166: 
0167: 
0168: 
0169: 
0170: 
0171: // ------------------------------------------------------------
0172: // テクニック
0173: // ------------------------------------------------------------
0174: technique TShader
0175: {
0176:     pass P0
0177:     {
0178:         VertexShader = compile vs_1_1 VS0();
0179:         PixelShader  = compile ps_2_0 PS0();
0180:     }
0181:     
0182:     pass P1
0183:     {
0184:         VertexShader = compile vs_1_1 VS1();
0185:         PixelShader  = compile ps_2_0 PS1();
0186:     }
0187:     
0188:     pass P2
0189:     {
0190:         PixelShader  = compile ps_2_0 PS2();
0191:     }
0192: }
0193: