0001: // -------------------------------------------------------------
0002: // レリーフテクスチャ
0003: // 
0004: // Copyright (c) 2003 IMAGIRE Takashi. All rights reserved.
0005: // -------------------------------------------------------------
0006: 
0007: // -------------------------------------------------------------
0008: // グローバル変数
0009: // -------------------------------------------------------------
0010: float4x4 mWVP;      // ローカルから射影空間への座標変換
0011: float4   vEyePos;   // 視点の位置
0012: 
0013: // -------------------------------------------------------------
0014: // テクスチャ
0015: // -------------------------------------------------------------
0016: texture Tex;
0017: sampler TexSamp = sampler_state
0018: {
0019:     Texture = <Tex>;
0020:     MinFilter = POINT;
0021:     MagFilter = POINT;
0022:     MipFilter = NONE;
0023: 
0024:     AddressU = Clamp;
0025:     AddressV = Clamp;
0026: };
0027: texture Color;
0028: sampler ColorSamp = sampler_state
0029: {
0030:     Texture = <Color>;
0031:     MinFilter = POINT;
0032:     MagFilter = POINT;
0033:     MipFilter = NONE;
0034: 
0035:     AddressU = Clamp;
0036:     AddressV = Clamp;
0037: };
0038: // -------------------------------------------------------------
0039: // 頂点シェーダからピクセルシェーダに渡すデータ
0040: // -------------------------------------------------------------
0041: struct VS_OUTPUT
0042: {
0043:     float4 Pos      : POSITION;
0044:     float4 Tex      : TEXCOORD0;
0045:     float4 Eye      : TEXCOORD1;
0046:     float4 Normal   : TEXCOORD2;
0047: };
0048: 
0049: // -------------------------------------------------------------
0050: // 頂点シェーダプログラム
0051: // -------------------------------------------------------------
0052: VS_OUTPUT VS (
0053:       float4 Pos    : POSITION          // モデルの頂点
0054:     , float4 Normal : NORMAL
0055:     , float4 Tex    : TEXCOORD0
0056: ){
0057:     VS_OUTPUT Out = (VS_OUTPUT)0;        // 出力データ
0058:     
0059:     // 位置座標
0060:     Out.Pos = mul( Pos, mWVP );
0061:     
0062:     // テクスチャ座標
0063:     Out.Tex = Tex;
0064:     
0065:     // 視線ベクトル
0066:     Out.Eye = vEyePos - Pos;
0067:     Out.Eye *= 0.2f/Out.Eye.z;
0068:     Out.Eye.y = -Out.Eye.y;
0069:     
0070:     // 光源ベクトル
0071:     Out.Normal = Normal;
0072: 
0073:     return Out;
0074: }
0075: // -------------------------------------------------------------
0076: // ピクセルシェーダプログラム
0077: // -------------------------------------------------------------
0078: float4 PS( VS_OUTPUT In ) : COLOR
0079: {
0080:     float4 O = (float4)0;
0081:     
0082:     float4 col;
0083:     float4 num = 9.0f;
0084:     float2 dt = (1.0f/(num-1))*(In.Eye.xyz);
0085:     float  dd = 1.0/num;
0086:     float  d = 1.0001 - dd;
0087:     float2 uv = In.Tex.xy;
0088:     float2 st = 0;
0089:     bool ok = false;
0090:     
0091:     col = tex2D( TexSamp, uv ); // 1
0092:     st = (d<col.x && !ok) ? uv : st;
0093:     ok = (d<col.x) ? true : ok;
0094:     uv += dt.xy;
0095:     d -= dd;
0096: 
0097:     col = tex2D( TexSamp, uv ); // 2
0098:     st = (d<col.x && !ok) ? uv : st;
0099:     ok = (d<col.x) ? true : ok;
0100:     uv += dt.xy;
0101:     d -= dd;
0102: 
0103:     col = tex2D( TexSamp, uv ); // 3
0104:     st = (d<col.x && !ok) ? uv : st;
0105:     ok = (d<col.x) ? true : ok;
0106:     uv += dt.xy;
0107:     d -= dd;
0108: 
0109:     col = tex2D( TexSamp, uv ); // 4
0110:     st = (d<col.x && !ok) ? uv : st;
0111:     ok = (d<col.x) ? true : ok;
0112:     uv += dt.xy;
0113:     d -= dd;
0114: 
0115:     col = tex2D( TexSamp, uv ); // 5
0116:     st = (d<col.x && !ok) ? uv : st;
0117:     ok = (d<col.x) ? true : ok;
0118:     uv += dt.xy;
0119:     d -= dd;
0120: 
0121:     col = tex2D( TexSamp, uv ); // 6
0122:     st = (d<col.x && !ok) ? uv : st;
0123:     ok = (d<col.x) ? true : ok;
0124:     uv += dt.xy;
0125:     d -= dd;
0126: 
0127:     col = tex2D( TexSamp, uv ); // 7
0128:     st = (d<col.x && !ok) ? uv : st;
0129:     ok = (d<col.x) ? true : ok;
0130:     uv += dt.xy;
0131:     d -= dd;
0132: 
0133:     col = tex2D( TexSamp, uv ); // 8
0134:     st = (d<col.x && !ok) ? uv : st;
0135:     ok = (d<col.x) ? true : ok;
0136:     uv += dt.xy;
0137:     d -= dd;
0138: 
0139:     col = tex2D( TexSamp, uv ); // 9
0140:     st = (d<col.x && !ok) ? uv : st;
0141:     ok = (d<col.x) ? true : ok;
0142: 
0143:     O = (ok)?tex2D( ColorSamp, st):0.0f;
0144: 
0145:     return O;
0146: }
0147: // -------------------------------------------------------------
0148: // テクニック
0149: // -------------------------------------------------------------
0150: technique TShader
0151: {
0152:     pass P0
0153:     {
0154:         // テクスチャ
0155:         Sampler[0] = (TexSamp);
0156:         Sampler[1] = (ColorSamp);
0157:         
0158:         // シェーダ
0159:         VertexShader = compile vs_1_1 VS();
0160:         PixelShader  = compile ps_2_0 PS();
0161:         
0162:         Cullmode=None;
0163: 
0164: #if 1
0165:         AlphaBlendEnable = True;
0166:         SrcBlend  = SRCALPHA;
0167:         DestBlend = INVSrcALPHA;
0168: #endif
0169:     }
0170: }
0171: