0001: // -------------------------------------------------------------
0002: // GPU particle
0003: // 
0004: // Copyright (c) 2002-2003 IMAGIRE Takashi. All rights reserved.
0005: // -------------------------------------------------------------
0006: 
0007: // -------------------------------------------------------------
0008: // グローバル変数
0009: // -------------------------------------------------------------
0010: float4x4 mWVP;          // 変換行列
0011: float4   vVector;       // 位置とかの適当なベクトル
0012: 
0013: texture DecaleMap;
0014: sampler DecaleSamp = sampler_state
0015: {
0016:     Texture = <DecaleMap>;
0017:     MinFilter = LINEAR;
0018:     MagFilter = LINEAR;
0019:     MipFilter = NONE;
0020: 
0021:     AddressU = Clamp;
0022:     AddressV = Clamp;
0023: };
0024: texture ParticleMap;
0025: sampler ParticleSamp = sampler_state
0026: {
0027:     Texture = <ParticleMap>;
0028:     MinFilter = POINT;
0029:     MagFilter = POINT;
0030:     MipFilter = NONE;
0031: 
0032:     AddressU = Clamp;
0033:     AddressV = Clamp;
0034: };
0035: 
0036: // -------------------------------------------------------------
0037: // 頂点シェーダ出力データ
0038: // -------------------------------------------------------------
0039: struct VS_OUTPUT
0040: {
0041:     float4 Pos      : POSITION;
0042:     float4 Color    : COLOR0;
0043: };
0044: // -------------------------------------------------------------
0045: struct INIT_VS_OUTPUT
0046: {
0047:     float4 Pos      : POSITION;
0048: };
0049: // -------------------------------------------------------------
0050: struct PARTICLE_VS_OUTPUT
0051: {
0052:     float4 Pos      : POSITION;
0053:     float2 x        : TEXCOORD0;
0054:     float2 dx       : TEXCOORD1;
0055: };
0056: // -------------------------------------------------------------
0057: struct REFLECT_VS_OUTPUT
0058: {
0059:     float4 Pos      : POSITION;
0060:     float2 x        : TEXCOORD0;
0061:     float2 v        : TEXCOORD1;
0062: };
0063: 
0064: // -------------------------------------------------------------
0065: // パーティクルの描画
0066: // 
0067: //  c0 - c3 - 座標変換
0068: //  c12     - (0.0, 0.5, 1.0, 2.0)
0069: // -------------------------------------------------------------
0070: VertexShader DisplacementVS = asm
0071: {
0072:     vs_1_1
0073:     
0074:     dcl_position0 v0
0075:     dcl_normal0   v1
0076:     dcl_texcoord0 v2
0077:     dcl_sample0   v3
0078:     
0079:     def c12, 0.0f ,0.5f, 1.0f, 2.0f
0080:     
0081:     mad r0, v3, c12.w, -c12.z
0082:     add r0, v0, r0      // ディスプレースメントの量を頂点に追加
0083:     mov r0.w, v0.w      // w=1
0084:     m4x4 oPos, r0, c0   // スクリーン座標へ
0085:     
0086:     mov oT0, v2         // テクスチャ座標
0087: };
0088: // -------------------------------------------------------------
0089: PIXELSHADER DisplacementPS = asm
0090: {
0091:     ps.1.1
0092:     
0093:     tex t0
0094:     
0095:     mov r0, t0
0096: };
0097: // -------------------------------------------------------------
0098: // パーティクルマップの初期化
0099: // -------------------------------------------------------------
0100: INIT_VS_OUTPUT InitVS (
0101:       float4 Pos    : POSITION
0102: ){
0103:     INIT_VS_OUTPUT Out = (INIT_VS_OUTPUT)0;
0104:     
0105:     Out.Pos = Pos;
0106:     
0107:     return Out;
0108: }
0109: // -------------------------------------------------------------
0110: float4 InitPS(INIT_VS_OUTPUT In) : COLOR
0111: {   
0112:     return vVector;
0113: }
0114: 
0115: 
0116: // -------------------------------------------------------------
0117: // パーティクルマップの更新
0118: // -------------------------------------------------------------
0119: float4 ParticlePS(PARTICLE_VS_OUTPUT In) : COLOR
0120: {   
0121:     float4 Color;
0122:     
0123:     Color  =  tex2D( ParticleSamp, In.x )
0124:             + tex2D( ParticleSamp, In.dx )
0125:             - 0.502;// 0.5を中央にする。0.002は???
0126:     return Color;
0127: }
0128: 
0129: 
0130: // -------------------------------------------------------------
0131: // 跳ね返り(速度)
0132: // -------------------------------------------------------------
0133: float4 ReflectVPS ( REFLECT_VS_OUTPUT In ) : COLOR0
0134: {
0135:     float4 x = 2.0f*tex2D( ParticleSamp, In.x )-1.0f;
0136:     float4 v = 2.0f*tex2D( ParticleSamp, In.v )-1.0f;
0137:     
0138:     if(x.x<-1.0f || +1.0f<x.x) v.x = -v.x;
0139:     if(x.y<-1.0f || +1.0f<x.y) v.y = -0.8f*v.y;
0140:     if(x.z<-1.0f || +1.0f<x.z) v.z = -v.z;
0141:     
0142:     return 0.5f*v + 0.5f;
0143: }
0144: // -------------------------------------------------------------
0145: // 跳ね返り(位置)
0146: // -------------------------------------------------------------
0147: float4 ReflectXPS ( REFLECT_VS_OUTPUT In ) : COLOR0
0148: {
0149:     float4 x = 2.0f*tex2D( ParticleSamp, In.x )-1.0f;
0150:     float4 v = 2.0f*tex2D( ParticleSamp, In.v )-1.0f;
0151:     
0152:     if(x.x<-1.0f) x.x = -1.0f; else
0153:     if(+1.0f<x.x) x.x = +1.0f;  
0154:     if(x.y<-1.0f) x.y = -1.0f; else
0155:     if(+1.0f<x.y) x.y = +1.0f;  
0156:     if(x.z<-1.0f) x.z = -1.0f; else
0157:     if(+1.0f<x.z) x.z = +1.0f;  
0158:     
0159:     return 0.5f*x + 0.5f;
0160: }
0161: 
0162: // -------------------------------------------------------------
0163: // technique
0164: // -------------------------------------------------------------
0165: technique TShader
0166: {
0167:     pass P0// パーティクルの描画
0168:     {
0169:         // シェーダ
0170:         VertexShader = <DisplacementVS>;
0171:         PixelShader  = <DisplacementPS>;
0172:         // 定数
0173:         VertexShaderConstantF[0]  = (mWVP);
0174:         // サンプラ
0175:         Sampler[0] = (DecaleSamp);
0176:     }
0177:     pass P1// パーティクルマップの初期化
0178:     {
0179:         // シェーダ
0180:         VertexShader = compile vs_1_1 InitVS();
0181:         PixelShader  = compile ps_1_1 InitPS();
0182:     }
0183:     pass P2// パーティクルの時間発展
0184:     {
0185:         // Shader
0186:         PixelShader  = compile ps_2_0 ParticlePS();
0187:     }
0188:     pass P3// 跳ね返り (速度)
0189:     {
0190:         // Shader
0191:         PixelShader  = compile ps_2_0 ReflectVPS();
0192:     }
0193:     pass P4// 跳ね返り (位置)
0194:     {
0195:         // Shader
0196:         PixelShader  = compile ps_2_0 ReflectXPS();
0197:     }
0198:     
0199: }
0200: