0001: // -------------------------------------------------------------
0002: // レリーフテクスチャ
0003: // 
0004: // Copyright (c) 2003 IMAGIRE Takashi. All rights reserved.
0005: // -------------------------------------------------------------
0006: 
0007: // -------------------------------------------------------------
0008: // グローバル変数
0009: // -------------------------------------------------------------
0010: float4x4 mWVP;      // ローカルから射影空間への座標変換
0011: float4   vEyePos;   // 視点の位置
0012: float4   vLight;
0013: 
0014: // -------------------------------------------------------------
0015: // テクスチャ
0016: // -------------------------------------------------------------
0017: texture Depth;
0018: sampler DepthSamp = sampler_state
0019: {
0020:     Texture = <Depth>;
0021:     MinFilter = LINEAR;
0022:     MagFilter = LINEAR;
0023:     MipFilter = NONE;
0024: 
0025:     AddressU = Clamp;
0026:     AddressV = Clamp;
0027: };
0028: texture Color;
0029: sampler ColorSamp = sampler_state
0030: {
0031:     Texture = <Color>;
0032:     MinFilter = LINEAR;
0033:     MagFilter = LINEAR;
0034:     MipFilter = NONE;
0035: 
0036:     AddressU = Clamp;
0037:     AddressV = Clamp;
0038: };
0039: texture Normal;
0040: sampler NormalSamp = sampler_state
0041: {
0042:     Texture = <Normal>;
0043:     MinFilter = LINEAR;
0044:     MagFilter = LINEAR;
0045:     MipFilter = NONE;
0046: 
0047:     AddressU = Clamp;
0048:     AddressV = Clamp;
0049: };
0050: // -------------------------------------------------------------
0051: // 頂点シェーダからピクセルシェーダに渡すデータ
0052: // -------------------------------------------------------------
0053: struct VS_OUTPUT
0054: {
0055:     float4 Pos      : POSITION;
0056:     float4 Tex      : TEXCOORD0;
0057:     float4 Eye      : TEXCOORD1;
0058: };
0059: 
0060: // -------------------------------------------------------------
0061: // 頂点シェーダプログラム
0062: // -------------------------------------------------------------
0063: VS_OUTPUT VS (
0064:       float4 Pos    : POSITION          // モデルの頂点
0065:     , float4 Normal : NORMAL
0066:     , float4 Tex    : TEXCOORD0
0067: ){
0068:     VS_OUTPUT Out = (VS_OUTPUT)0;        // 出力データ
0069:     
0070:     // 位置座標
0071:     Out.Pos = mul( Pos, mWVP );
0072:     
0073:     
0074:     // 視線ベクトル
0075:     float4 e = vEyePos - Pos;
0076:     e *= 1.0/e.z;
0077:     e.y = -e.y;
0078:     Out.Eye = e;
0079: 
0080:     // テクスチャ座標
0081:     Out.Tex = Tex + e;
0082: 
0083:     return Out;
0084: }
0085: // -------------------------------------------------------------
0086: // ピクセルシェーダプログラム
0087: // -------------------------------------------------------------
0088: #if 0
0089: float4 PS( VS_OUTPUT In ) : COLOR
0090: {
0091:     float4 O = (float4)0;
0092:     
0093:     float4 col;
0094:     float4 num = 256.0f;
0095:     float2 dt = (1.0f/(num-1))*(In.Eye.xyz);
0096:     float  dd = 1.0/num;
0097:     float  d = 1.0000001 - dd;
0098:     float2 uv = In.Tex-In.Eye;
0099:     float2 st = 0;
0100:     bool ok = false;
0101:     float i;
0102:     
0103:     for ( i = 0; i < 1; i += 1.0f/num ) {
0104:         col = tex2D( DepthSamp, uv );
0105:         st = (d<col.x && !ok) ? uv : st;
0106:         ok = (d<col.x) ? true : ok;
0107:         uv += dt.xy;
0108:         d -= dd;
0109:     }
0110: 
0111:     // Lambert Diffuse
0112:     float3 N = 2.0f*tex2D( NormalSamp, st.xy).xyz - 1.0f;
0113:     float  NL = max(0,dot(N, vLight.xyz));
0114:     float4 color = tex2D( ColorSamp, st.xy);
0115:     
0116:     O.xyz = color * (NL + vLight.w);
0117:     O.w = (ok) ? 1 : 0;
0118:     
0119:     return O;
0120: }
0121: // -------------------------------------------------------------
0122: // テクニック
0123: // -------------------------------------------------------------
0124: technique TShader
0125: {
0126:     pass P0
0127:     {
0128:         // テクスチャ
0129:         Sampler[0] = (DepthSamp);
0130:         Sampler[1] = (ColorSamp);
0131:         Sampler[2] = (NormalSamp);
0132:         
0133:         // シェーダ
0134:         VertexShader = compile vs_1_1 VS();
0135:         PixelShader  = compile ps_2_sw PS();
0136:         
0137:         Cullmode=CCW;
0138: 
0139:         AlphaBlendEnable = True;
0140:         SrcBlend  = SRCALPHA;
0141:         DestBlend = INVSrcALPHA;
0142:     }
0143: }
0144: #else
0145: PIXELSHADER PS = asm
0146: {
0147:     ps_2_0
0148: 
0149:     // c0:ライトベクトル
0150:     def c1, 2.0, 1.0, 0.0, 0.000001
0151:     def c2, 0.1, 5, 100000, 99999// 1/num, 5, 100000 100000-1
0152:     
0153:     dcl t0.xy   // Depth
0154:     dcl t1.xy   // Eye
0155:     dcl_2d s0   // Depth
0156:     dcl_2d s1   // Color
0157:     dcl_2d s2   // Normal
0158:     
0159:     mul r11.xy, t1, -c2.x   // r0: dt = (-1.0f/num))*In.Eye;
0160:     mov r11.z,  c2.x
0161:     mov r10.xy, c2.z        // r10:st = 100000
0162:     mov r0.xy,  t0
0163:     mov r0.z,   c1.w
0164:     add r2.xyz, r11, r0
0165:     add r4.xyz, r11, r2
0166:     add r6.xyz, r11, r4
0167:     add r8.xyz, r11, r6
0168: 
0169:     texld r1, r0, s0
0170:     texld r3, r2, s0
0171:     texld r5, r4, s0
0172:     texld r7, r6, s0
0173:     texld r9, r8, s0
0174:     
0175:     add r0.w, r1.x, -r0.z       // r0.w = depth-uv.z
0176:     cmp r10.xy, r0.w, r0, r10   // st = (uv.z<=depth) ? uv : st;
0177:     add r2.w, r3.x, -r2.z       // r0.w = depth-uv.z
0178:     cmp r10.xy, r2.w, r2, r10   // st = (uv.z<=depth) ? uv : st;
0179:     add r4.w, r5.x, -r4.z       // r0.w = depth-uv.z
0180:     cmp r10.xy, r4.w, r4, r10   // st = (uv.z<=depth) ? uv : st;
0181:     add r6.w, r7.x, -r6.z       // r0.w = depth-uv.z
0182:     cmp r10.xy, r6.w, r6, r10   // st = (uv.z<=depth) ? uv : st;
0183:     add r8.w, r9.x, -r8.z       // r0.w = depth-uv.z
0184:     cmp r10.xy, r8.w, r8, r10   // st = (uv.z<=depth) ? uv : st;
0185: 
0186:     mul r11.xyz, r11, c2.y
0187:     add r0.xyz, r0, r11
0188:     add r2.xyz, r2, r11
0189:     add r4.xyz, r4, r11
0190:     add r6.xyz, r6, r11
0191:     add r8.xyz, r8, r11
0192: 
0193:     texld r1, r0, s0
0194:     texld r3, r2, s0
0195:     texld r5, r4, s0
0196:     texld r7, r6, s0
0197:     texld r9, r8, s0
0198:     
0199:     add r0.w, r1.x, -r0.z       // r0.w = depth-uv.z
0200:     cmp r10.xy, r0.w, r0, r10   // st = (uv.z<=depth) ? uv : st;
0201:     add r2.w, r3.x, -r2.z       // r0.w = depth-uv.z
0202:     cmp r10.xy, r2.w, r2, r10   // st = (uv.z<=depth) ? uv : st;
0203:     add r4.w, r5.x, -r4.z       // r0.w = depth-uv.z
0204:     cmp r10.xy, r4.w, r4, r10   // st = (uv.z<=depth) ? uv : st;
0205:     add r6.w, r7.x, -r6.z       // r0.w = depth-uv.z
0206:     cmp r10.xy, r6.w, r6, r10   // st = (uv.z<=depth) ? uv : st;
0207:     add r8.w, r9.x, -r8.z       // r0.w = depth-uv.z
0208:     cmp r10.xy, r8.w, r8, r10   // st = (uv.z<=depth) ? uv : st;
0209: 
0210:     // アルファ成分をST値から求める
0211:     add r0.w, c2.w, -r10.x
0212:     add r1.w, r1.w, -r1.w           // 定数レジスタ2つを引数にできないので0を作る
0213:     cmp_pp r11.w, r0.w, c1.y, r1.w  // r11.w = O.w = (st.x<=100000-1) ? 1 : 0;
0214: 
0215:     texld r0, r10, s1               // Color
0216:     texld r1, r10, s2               // Normal
0217: 
0218:     mad r1.xyz, c1.x, r1, -c1.y     // n=2*NormalMap-1
0219:     dp3 r1.w, r1, c0                // r1.w = NL
0220:     cmp r1.w, r1.w, r1.w, c1.z      // r1.w = max(0,NL)
0221:     add r1.w, r1.w, c0.w            // r1.w = max(0,NL) + amb
0222:     mul r11.xyz, r0, r1.w           // r11 = O.xyz = color*(max(0,NL) + amb)
0223: 
0224:     mov oC0, r11
0225: };
0226: // -------------------------------------------------------------
0227: // テクニック
0228: // -------------------------------------------------------------
0229: technique TShader
0230: {
0231:     pass P0
0232:     {
0233:         // テクスチャ
0234:         Sampler[0] = (DepthSamp);
0235:         Sampler[1] = (ColorSamp);
0236:         Sampler[2] = (NormalSamp);
0237:         
0238:         // シェーダ
0239:         VertexShader = compile vs_1_1 VS();
0240:         PixelShader  = <PS>;
0241:         PixelShaderConstantF[0]      = (vLight);
0242:         
0243:         Cullmode=CCW;
0244: 
0245:         AlphaBlendEnable = True;
0246:         SrcBlend  = SRCALPHA;
0247:         DestBlend = INVSrcALPHA;
0248:     }
0249: }
0250: #endif
0251: