0001: // -------------------------------------------------------------
0002: // Ashikhminモデル
0003: // 
0004: // Copyright (c) 2003 IMAGIRE Takashi. All rights reserved.
0005: // -------------------------------------------------------------
0006: 
0007: // -------------------------------------------------------------
0008: // グローバル変数
0009: // -------------------------------------------------------------
0010: 
0011: float4x4 mWVP;      // ローカルから射影空間への座標変換
0012: float4   vCol;      // メッシュの色
0013: float4   vLightPos; // ライトの位置
0014: float4   vCamPos;   // カメラの位置
0015: float    A;
0016: float    B;
0017: 
0018: 
0019: // -------------------------------------------------------------
0020: // テクスチャ
0021: // -------------------------------------------------------------
0022: texture tDiffuse;
0023: sampler DiffuseSamp = sampler_state
0024: {
0025:     Texture = <tDiffuse>;
0026:     MinFilter = LINEAR;
0027:     MagFilter = LINEAR;
0028:     MipFilter = NONE;
0029: 
0030:     AddressU = Clamp;
0031:     AddressV = Clamp;
0032: };
0033: 
0034: 
0035: texture tGp;
0036: sampler GpSamp = sampler_state
0037: {
0038:     Texture = <tGp>;
0039:     MinFilter = LINEAR;
0040:     MagFilter = LINEAR;
0041:     MipFilter = NONE;
0042: 
0043:     AddressU = Clamp;
0044:     AddressV = Clamp;
0045: };
0046: 
0047: 
0048: texture tGs;
0049: sampler GsSamp = sampler_state
0050: {
0051:     Texture = <tGs>;
0052:     MinFilter = LINEAR;
0053:     MagFilter = LINEAR;
0054:     MipFilter = NONE;
0055: 
0056:     AddressU = Clamp;
0057:     AddressV = Clamp;
0058: };
0059: 
0060: 
0061: // -------------------------------------------------------------
0062: // 頂点シェーダからピクセルシェーダに渡すデータ
0063: // -------------------------------------------------------------
0064: struct VS_OUTPUT
0065: {
0066:     float4 Pos          : POSITION;
0067:     float4 Color        : COLOR0;
0068:     float3 Normal       : TEXCOORD0;
0069:     float3 Light        : TEXCOORD1;
0070:     float3 Eye          : TEXCOORD2;
0071:     float3 U            : TEXCOORD3;
0072:     float3 V            : TEXCOORD4;
0073: };
0074: 
0075: // -------------------------------------------------------------
0076: // 頂点シェーダ
0077: // -------------------------------------------------------------
0078: VS_OUTPUT VS(
0079:       float4 Pos      : POSITION,        // モデルの頂点
0080:       float3 Normal   : NORMAL,          // モデルの法線
0081:       float3 BiNormal : BINORMAL,        // モデルの従法線
0082:       float3 Tangent  : TANGENT          // モデルの接ベクトル
0083: ){
0084:     VS_OUTPUT Out = (VS_OUTPUT)0;        // 出力データ
0085:     float4  uv;
0086:     
0087:     // 座標変換
0088:     Out.Pos = mul(Pos, mWVP);
0089:     
0090:     // 色
0091:     Out.Color = vCol;
0092:     // 計算用のベクトル
0093:     Out.Normal = Normal;
0094:     Out.Light  = vLightPos.xyz - Pos.xyz;
0095:     Out.Eye    = vCamPos.xyz - Pos.xyz;
0096:     
0097:     Out.U    = BiNormal;
0098:     Out.V    = Tangent;
0099:     
0100:     return Out;
0101: }
0102: // -------------------------------------------------------------
0103: // ピクセルシェーダ
0104: // -------------------------------------------------------------
0105: float4 PS(VS_OUTPUT In) : COLOR
0106: {
0107:     float nu = 1000;
0108:     float nv = 10;
0109:     float PI = 3.14159;
0110:     float4 Out = 0;
0111:     float4 Rd = In.Color;
0112:     float  Rs = 0.5f;
0113:     float fp, fu, fv;
0114:     float fd, fs;
0115:     
0116:     float3 n = normalize(In.Normal);
0117:     float3 l = normalize(In.Light);
0118:     float3 e = normalize(In.Eye);
0119:     float3 h = normalize(l+e);
0120:     
0121:     float2 td = {dot(l,n), dot(e,n)};
0122:     fd = (28/(23*PI))*tex2D( DiffuseSamp, td ).x;
0123:     
0124:     float2 tp = {dot(h,l), max(dot(e,n),dot(l,n))};
0125:     fp = tex2D( GpSamp, tp ).x;
0126: 
0127:     float2 tu = {dot(n,h), sqrt(nu)*dot(h,normalize(In.U))/5.0f+0.5f};
0128:     fu = tex2D( GsSamp, tu ).x;
0129:     float2 tv = {dot(n,h), sqrt(nv)*dot(h,normalize(In.V))/5.0f+0.5f};
0130:     fv = tex2D( GsSamp, tv ).x;
0131: 
0132:     fs = (sqrt((nu+1)*(nv+1))/(8*PI))*fu*fv*fp;
0133:     
0134:     Out = (Rd*(1-Rs)*fd + Rs*fs)*dot(l,n);
0135:     
0136:     return Out;
0137: }
0138: // -------------------------------------------------------------
0139: // テクニック
0140: // -------------------------------------------------------------
0141: technique TShader
0142: {
0143:     pass P0
0144:     {
0145:         // シェーダ
0146:         VertexShader = compile vs_2_0 VS();
0147:         PixelShader  = compile ps_2_0 PS();
0148:     }
0149: }
0150: