0001: // ------------------------------------------------------------
0002: // 水平線マップ
0003: // 
0004: // Copyright (c) 2003 IMAGIRE Takashi. All rights reserved.
0005: // ------------------------------------------------------------
0006: 
0007: // ------------------------------------------------------------
0008: // グローバル変数
0009: // ------------------------------------------------------------
0010: float4x4 mWVP;
0011: float4   vCol;
0012: float4   vLightDir; // ライトの方向
0013: 
0014: // ------------------------------------------------------------
0015: // テクスチャ
0016: // ------------------------------------------------------------
0017: texture SrcTex;
0018: sampler SrcSamp = sampler_state
0019: {
0020:     Texture = <SrcTex>;
0021:     MinFilter = LINEAR;
0022:     MagFilter = LINEAR;
0023:     MipFilter = NONE;
0024: 
0025:     AddressU = Clamp;
0026:     AddressV = Clamp;
0027: };
0028: // ------------------------------------------------------------
0029: texture CoverTex0;
0030: sampler CoverSamp0 = sampler_state
0031: {
0032:     Texture = <CoverTex0>;
0033:     MinFilter = LINEAR;
0034:     MagFilter = LINEAR;
0035:     MipFilter = NONE;
0036: 
0037:     AddressU = Wrap;
0038:     AddressV = Wrap;
0039: };
0040: // ------------------------------------------------------------
0041: texture CoverTex1;
0042: sampler CoverSamp1 = sampler_state
0043: {
0044:     Texture = <CoverTex1>;
0045:     MinFilter = LINEAR;
0046:     MagFilter = LINEAR;
0047:     MipFilter = NONE;
0048: 
0049:     AddressU = Wrap;
0050:     AddressV = Wrap;
0051: };
0052: // ------------------------------------------------------------
0053: texture CoverTex2;
0054: sampler CoverSamp2 = sampler_state
0055: {
0056:     Texture = <CoverTex2>;
0057:     MinFilter = LINEAR;
0058:     MagFilter = LINEAR;
0059:     MipFilter = NONE;
0060: 
0061:     AddressU = Wrap;
0062:     AddressV = Wrap;
0063: };
0064: // ------------------------------------------------------------
0065: texture CoverTex3;
0066: sampler CoverSamp3 = sampler_state
0067: {
0068:     Texture = <CoverTex3>;
0069:     MinFilter = LINEAR;
0070:     MagFilter = LINEAR;
0071:     MipFilter = NONE;
0072: 
0073:     AddressU = Wrap;
0074:     AddressV = Wrap;
0075: };
0076: // ------------------------------------------------------------
0077: texture CoverTex4;
0078: sampler CoverSamp4 = sampler_state
0079: {
0080:     Texture = <CoverTex4>;
0081:     MinFilter = LINEAR;
0082:     MagFilter = LINEAR;
0083:     MipFilter = NONE;
0084: 
0085:     AddressU = Wrap;
0086:     AddressV = Wrap;
0087: };
0088: // ------------------------------------------------------------
0089: texture CoverTex5;
0090: sampler CoverSamp5 = sampler_state
0091: {
0092:     Texture = <CoverTex5>;
0093:     MinFilter = LINEAR;
0094:     MagFilter = LINEAR;
0095:     MipFilter = NONE;
0096: 
0097:     AddressU = Wrap;
0098:     AddressV = Wrap;
0099: };
0100: // ------------------------------------------------------------
0101: texture CoverTex6;
0102: sampler CoverSamp6 = sampler_state
0103: {
0104:     Texture = <CoverTex6>;
0105:     MinFilter = LINEAR;
0106:     MagFilter = LINEAR;
0107:     MipFilter = NONE;
0108: 
0109:     AddressU = Wrap;
0110:     AddressV = Wrap;
0111: };
0112: // ------------------------------------------------------------
0113: texture CoverTex7;
0114: sampler CoverSamp7 = sampler_state
0115: {
0116:     Texture = <CoverTex7>;
0117:     MinFilter = LINEAR;
0118:     MagFilter = LINEAR;
0119:     MipFilter = NONE;
0120: 
0121:     AddressU = Wrap;
0122:     AddressV = Wrap;
0123: };
0124: // ------------------------------------------------------------
0125: // 頂点シェーダからピクセルシェーダに渡すデータ
0126: // ------------------------------------------------------------
0127: struct VS_OUTPUT
0128: {
0129:     float4 Pos          : POSITION;
0130:     float4 Color        : COLOR0;
0131:     float2 Tex0         : TEXCOORD0;
0132:     float2 Tex1         : TEXCOORD1;
0133: };
0134: 
0135: // ------------------------------------------------------------
0136: // 頂点シェーダプログラム
0137: // ------------------------------------------------------------
0138: VS_OUTPUT VS (
0139:       float4 Pos    : POSITION           // モデルの頂点
0140:      ,float4 Normal : NORMAL             // 法線ベクトル
0141:      ,float4 Tex0   : TEXCOORD0          // テクスチャ座標
0142: ){
0143:     VS_OUTPUT Out = (VS_OUTPUT)0;        // 出力データ
0144:     
0145:     float4 pos = mul( Pos, mWVP );
0146:     
0147:     // 位置座標
0148:     Out.Pos = pos;
0149:     
0150:     Out.Tex0 = Tex0;
0151:     
0152:     return Out;
0153: }
0154: // ------------------------------------------------------------
0155: // ピクセルシェーダプログラム
0156: // ------------------------------------------------------------
0157: float4 vWeight[8];
0158: float4 PS (VS_OUTPUT In) : COLOR
0159: {
0160:     float4 Out;
0161:     
0162:     // 遮蔽量
0163:     float Cover = dot(vWeight[0], tex2D( CoverSamp0, In.Tex0 ));
0164:           Cover+= dot(vWeight[1], tex2D( CoverSamp1, In.Tex0 ));
0165:           Cover+= dot(vWeight[2], tex2D( CoverSamp2, In.Tex0 ));
0166:           Cover+= dot(vWeight[3], tex2D( CoverSamp3, In.Tex0 ));
0167:           Cover+= dot(vWeight[4], tex2D( CoverSamp4, In.Tex0 ));
0168:           Cover+= dot(vWeight[5], tex2D( CoverSamp5, In.Tex0 ));
0169:           Cover+= dot(vWeight[6], tex2D( CoverSamp6, In.Tex0 ));
0170:           Cover+= dot(vWeight[7], tex2D( CoverSamp7, In.Tex0 ));
0171:           
0172:     // 色
0173:     Out = tex2D( SrcSamp, In.Tex0 ) *(Cover+0.3f);
0174: 
0175:     return Out;
0176: }
0177: // ------------------------------------------------------------
0178: // 高さマップ作成頂点シェーダプログラム
0179: // ------------------------------------------------------------
0180: VS_OUTPUT VS_Height (
0181:       float4 Pos    : POSITION           // モデルの頂点
0182:     , float4 Normal : NORMAL             // 法線ベクトル
0183:     , float4 Tex0   : TEXCOORD0          // テクスチャ座標
0184: ){
0185:     VS_OUTPUT Out = (VS_OUTPUT)0;        // 出力データ
0186:     
0187:     float4 pos = mul( Pos, mWVP );
0188:     
0189:     // 位置座標
0190:     Out.Pos = 2.0 * Tex0 - 1.0;
0191:     Out.Pos.y *= -1;
0192:     Out.Pos.z = 0.5;
0193:     Out.Pos.w = 1;
0194:     
0195:     // 色
0196:     Out.Color = Pos.y;
0197: 
0198:     return Out;
0199: }
0200: 
0201: // ------------------------------------------------------------
0202: // 高さマップ作成ピクセルシェーダプログラム
0203: // ------------------------------------------------------------
0204: float4 PS_Height (VS_OUTPUT In) : COLOR
0205: {
0206:     float4 Out;
0207:     
0208:     // レンダリングターゲット
0209:     Out = In.Color;
0210: 
0211:     return Out;
0212: }
0213: // ------------------------------------------------------------
0214: // 遮蔽マップ作成頂点シェーダプログラム
0215: // ------------------------------------------------------------
0216: float4 vOffset;
0217: VS_OUTPUT VS_Cover (
0218:       float4 Pos    : POSITION           // モデルの頂点
0219:     , float2 Tex0   : TEXCOORD0          // テクスチャ座標
0220: ){
0221:     VS_OUTPUT Out = (VS_OUTPUT)0;        // 出力データ
0222:     
0223:     // 頂点座標 
0224:     Out.Pos = Pos;
0225:     
0226:     // 色
0227:     Out.Tex0 = Tex0;
0228:     Out.Tex1 = Tex0+vOffset.xy;
0229: 
0230:     return Out;
0231: }
0232: 
0233: // ------------------------------------------------------------
0234: // 遮蔽マップ作成ピクセルシェーダプログラム
0235: // ------------------------------------------------------------
0236: float  fRayHeight;
0237: float4 PS_Cover(VS_OUTPUT In) : COLOR
0238: {
0239:     float4 Out;
0240:     
0241:     float4 center = tex2D( SrcSamp, In.Tex0 );
0242:     float4 offset = tex2D( SrcSamp, In.Tex1 );
0243: 
0244:     Out = (center+fRayHeight < offset) ? 0 : 1;
0245:     
0246:     return Out;
0247: }
0248: 
0249: // ------------------------------------------------------------
0250: // テクニック
0251: // ------------------------------------------------------------
0252: technique TShader
0253: {
0254:     pass P0 // 高さマップ
0255:     {
0256:         VertexShader = compile vs_1_1 VS_Height();
0257:         PixelShader  = compile ps_2_0 PS_Height();
0258:     }
0259:     pass P1 // 遮蔽マップ
0260:     {
0261:         VertexShader = compile vs_1_1 VS_Cover();
0262:         PixelShader  = compile ps_2_0 PS_Cover();
0263:     }
0264:     pass P2 // 照明計算なし
0265:     {
0266:         VertexShader = compile vs_1_1 VS();
0267:         PixelShader  = compile ps_2_0 PS();
0268:     }
0269: }
0270: