www.pudn.com > notWow.rar > hlsl_nps.fx


// light 
float4 g_LightDir =    {0.0f, 0.0f, -1.0f, 1.0f};        // light Direction  
float4 g_LightColor =  {0.6f, 0.6f, 0.6f, 1.0f};        // Light Diffuse 
 
// material 
float4 g_MaterialAmbient : MATERIALAMBIENT = {0.5f, 0.5f, 0.5f, 1.0f}; 
float4 g_MaterialDiffuse : MATERIALDIFFUSE = {0.8f, 0.8f, 0.8f, 1.0f}; 
float4 g_MaterialEmissive = { 0.0,0.0,0.0,1.0}; 
float4 g_MaterialSpecular = { 0.0,0.0,0.0,1.0}; 
 
// alpha 
float  g_Alpha = 1.0f; 
 
// Positoion and Direction for workd objects and characters 
float3 g_Position  = {0.0f,0.0f,0.0f}; 
float2 g_Direction = {0.0,1.0};        // Direction.x = sin(Dir) y = cos(Dir) 
 
// Matrix Pallette 
static const int MAX_MATRICES = 50; 
float4x3    g_mMatrixPalette[MAX_MATRICES] : WORLDMATRIXARRAY; 
float4x4    g_mViewProj : VIEWPROJECTION; 
texture     g_Texture; 
 
// texture 
sampler TextureSampler =  
sampler_state 
{ 
    Texture = ; 
	MipFilter = LINEAR; 
    MinFilter = LINEAR; 
	MagFilter = LINEAR; 
}; 
  
  
/////////////////////////////////////////////////////// 
// For Vertex Blending Model 
struct VS_INPUT_VB 
{ 
    float4  Pos             : POSITION; 
    float4  BlendWeights    : BLENDWEIGHT; 
    float4  BlendIndices    : BLENDINDICES; 
    float3  Normal          : NORMAL; 
    float2  Tex0            : TEXCOORD0; 
}; 
 
// For Static Model 
struct VS_INPUT_STATIC 
{ 
    float4  Pos             : POSITION; 
    float3  Normal          : NORMAL; 
    float4  Diffuse            : COLOR; 
    float2  Tex0            : TEXCOORD0;     
}; 
 
// For Map terrians 
struct VS_INPUT_TERRIAN 
{ 
    float4  Pos             : POSITION; 
    float3  Normal          : NORMAL; 
    float2  Tex0            : TEXCOORD0;      
}; 
 
struct VS_INPUT_BG 
{ 
    float4    Pos           : POSITION; 
    float4      Diffuse        : COLOR; 
}; 
 
struct VS_INPUT_SKY 
{ 
    float4    Pos             : POSITION; 
    float4    Diffuse         : COLOR; 
    float2    Tex0            : TEXCOORD0; 
}; 
 
struct VS_OUTPUT 
{ 
    float4  Pos     : POSITION; 
    float4  Diffuse : COLOR; 
    float2  Tex0    : TEXCOORD0; 
}; 
 
struct VS_OUTPUT_BG 
{ 
    float4  Pos     : POSITION; 
    float4  Diffuse : COLOR; 
}; 
 
struct VS_OUTPUT_FAR 
{ 
    float4  Pos     : POSITION; 
    float4  Diffuse : COLOR; 
}; 
 
struct PS_OUTPUT 
{ 
    float4 RGBColor : COLOR0;  // Pixel color     
}; 
 
 
float3 Diffuse(float3 Normal) 
{ 
    float CosTheta; 
     
    // N.L Clamped 
    CosTheta = max(0.0f, dot(Normal, g_LightDir.xyz)); 
        
    // propogate scalar result to vector 
    return (CosTheta); 
} 
 
////////////////////////////////////////////////////////// 
// World Object VS 
VS_OUTPUT VS_World(VS_INPUT_STATIC i) 
{ 
    VS_OUTPUT   o; 
    float3      Pos = i.Pos; 
    float3      Normal = i.Normal;     
       
    float x = Pos.x; 
    float z = Pos.z; 
    Pos.x = g_Direction.y * x + g_Direction.x * z; 
    Pos.z = -g_Direction.x * x + g_Direction.y * z; 
    x = Normal.x; 
    z = Normal.z; 
    Normal.x = g_Direction.y * x + g_Direction.x * z; 
    Normal.z = -g_Direction.x * x + g_Direction.y * z;     
      
    Pos += g_Position;   
     
    // transform position from world space into view and then projection space 
    o.Pos = mul(float4(Pos.xyz, 1.0f), g_mViewProj); 
     
    // normalize normals 
    Normal = normalize(Normal); 
 
    // Shade (Ambient + etc.) 
    //o.Diffuse.xyz = MaterialAmbient.xyz + Diffuse(Normal) * MaterialDiffuse.xyz; 
    //o.Diffuse.w = 1.0f; 
    //o.Diffuse.xyz = g_MaterialEmissive.xyz + Diffuse(Normal) * g_MaterialDiffuse.xyz + g_MaterialAmbient; 
    o.Diffuse.xyz = Diffuse(Normal) * g_MaterialDiffuse.xyz + g_MaterialAmbient.xyz; 
    o.Diffuse.w = g_MaterialDiffuse.w * g_Alpha ; 
     
    // copy the input texture coordinate through 
    o.Tex0  = i.Tex0; 
 
    return o; 
} 
 
VS_OUTPUT_FAR VS_World_Far(VS_INPUT_STATIC i) 
{ 
     VS_OUTPUT_FAR   o; 
    float3      Pos = i.Pos;    
       
    float x = Pos.x; 
    float z = Pos.z; 
    Pos.x = g_Direction.y * x + g_Direction.x * z; 
    Pos.z = -g_Direction.x * x + g_Direction.y * z;    
      
    Pos += g_Position;   
     
    // transform position from world space into view and then projection space 
    o.Pos = mul(float4(Pos.xyz, 1.0f), g_mViewProj); 
     
    o.Diffuse.xyz = g_MaterialDiffuse.xyz; 
    o.Diffuse.w = g_MaterialDiffuse.w * g_Alpha ; 
        
    return o; 
} 
 
// Item VS 
VS_OUTPUT VS_Item(VS_INPUT_STATIC i) 
{ 
// For Item,it needn't Position and Direction 
// However,the g_mViewProj include the world transform 
    VS_OUTPUT   o; 
    float3      Pos = i.Pos; 
    float3      Normal = i.Normal;     
    float       LastWeight = 0.0f; 
           
    // transform position from world space into view and then projection space 
    o.Pos = mul(float4(Pos.xyz, 1.0f), g_mViewProj); 
     
    // normalize normals 
    Normal = normalize(Normal); 
 
    // Shade (Ambient + etc.) 
    //o.Diffuse.xyz = MaterialAmbient.xyz + Diffuse(Normal) * MaterialDiffuse.xyz; 
    //o.Diffuse.w = 1.0f; 
    o.Diffuse.xyz = g_MaterialEmissive.xyz + Diffuse(Normal) * g_MaterialDiffuse.xyz + g_MaterialAmbient; 
    o.Diffuse.w = g_MaterialDiffuse.w; 
     
    // copy the input texture coordinate through 
    o.Tex0  = i.Tex0; 
 
    return o; 
} 
 
// Character and Creature VS 
VS_OUTPUT VS_VertexBlend(VS_INPUT_VB i) 
{ 
    VS_OUTPUT   o; 
    float3      Pos = 0.0f; 
    float3      Normal = 0.0f;     
    float       LastWeight = 0.0f; 
      
 
    // cast the vectors to arrays for use in the for loop below 
    int4 BlendIndices = (int4)i.BlendIndices; 
         
    LastWeight += i.BlendWeights.x; 
    Pos += mul(i.Pos,g_mMatrixPalette[BlendIndices.x]) * i.BlendWeights.x;  
    Normal += mul(i.Normal,g_mMatrixPalette[BlendIndices.x]) * i.BlendWeights.x; 
    LastWeight += i.BlendWeights.y; 
    Pos += mul(i.Pos,g_mMatrixPalette[BlendIndices.y]) * i.BlendWeights.y;  
    Normal += mul(i.Normal,g_mMatrixPalette[BlendIndices.y]) * i.BlendWeights.y; 
    LastWeight += i.BlendWeights.z; 
    Pos += mul(i.Pos,g_mMatrixPalette[BlendIndices.z]) * i.BlendWeights.z;  
    Normal += mul(i.Normal,g_mMatrixPalette[BlendIndices.z]) * i.BlendWeights.z;     
    LastWeight = 1.0f - LastWeight; 
    Pos += mul(i.Pos,g_mMatrixPalette[BlendIndices.w]) * LastWeight;  
    Normal += mul(i.Normal,g_mMatrixPalette[BlendIndices.w]) * LastWeight; 
     
    float x = Pos.x; 
    float z = Pos.z; 
    Pos.x = g_Direction.y * x + g_Direction.x * z; 
    Pos.z = -g_Direction.x * x + g_Direction.y * z; 
    x = Normal.x; 
    z = Normal.z; 
    Normal.x = g_Direction.y * x + g_Direction.x * z; 
    Normal.z = -g_Direction.x * x + g_Direction.y * z;     
      
    Pos += g_Position;   
     
    // transform position from world space into view and then projection space 
    o.Pos = mul(float4(Pos.xyz, 1.0f), g_mViewProj); 
     
    // normalize normals 
    Normal = normalize(Normal); 
 
    // Shade (Ambient + etc.) 
    //o.Diffuse.xyz = MaterialAmbient.xyz + Diffuse(Normal) * MaterialDiffuse.xyz; 
    //o.Diffuse.w = 1.0f; 
    o.Diffuse.xyz = g_MaterialEmissive.xyz + Diffuse(Normal) * g_MaterialDiffuse.xyz + g_MaterialAmbient; 
    o.Diffuse.w = g_MaterialDiffuse.w; 
     
    // copy the input texture coordinate through 
    o.Tex0  = i.Tex0; 
 
    return o; 
} 
 
// For Map 
VS_OUTPUT VS_Terrian(VS_INPUT_TERRIAN i) 
{ 
    VS_OUTPUT   o; 
    float3      Pos = i.Pos; 
          
    // transform position from world space into view and then projection space 
    o.Pos = mul(float4(Pos.xyz, 1.0f), g_mViewProj); 
     
    o.Diffuse = g_MaterialDiffuse; 
   
    o.Tex0  = i.Tex0; 
     
    return o;     
} 
 
VS_OUTPUT_FAR VS_Terrian_Far(VS_INPUT_TERRIAN i) 
{ 
    VS_OUTPUT_FAR		o; 
    float3            Pos = i.Pos; 
     
    o.Diffuse = g_MaterialDiffuse; 
          
    // transform position from world space into view and then projection space 
    o.Pos = mul(float4(Pos.xyz, 1.0f), g_mViewProj); 
       
    return o;     
} 
 
VS_OUTPUT_BG VS_BG(VS_INPUT_BG i) 
{ 
    VS_OUTPUT_BG    o; 
    float3        Pos = i.Pos; 
    float x = Pos.x; 
    float z = Pos.z; 
    Pos.x = g_Direction.y * x + g_Direction.x * z; 
    Pos.z = -g_Direction.x * x + g_Direction.y * z; 
      
    Pos += g_Position;   
     
    // transform position from world space into view and then projection space 
    o.Pos = mul(float4(Pos.xyz, 1.0f), g_mViewProj); 
    o.Diffuse = i.Diffuse; 
    return o; 
} 
 
// PS 
PS_OUTPUT PS_Normal( VS_OUTPUT In)  
{  
    PS_OUTPUT Output; 
    Output.RGBColor = tex2D(TextureSampler, In.Tex0) * In.Diffuse; 
    return Output; 
} 
 
PS_OUTPUT PS_Far(VS_OUTPUT In) 
{ 
    PS_OUTPUT Output; 
    Output.RGBColor = float4(0.5,0.5,0.5,1.0) * In.Diffuse; 
    return Output; 
} 
 
PS_OUTPUT PS_World_Far( VS_OUTPUT In)  
{  
    PS_OUTPUT Output; 
    Output.RGBColor = tex2D(TextureSampler, In.Tex0) * In.Diffuse; 
    Output.RGBColor.rgb = float3(0.5,0.5,0.5); 
    return Output; 
} 
 
PS_OUTPUT PS_BG(VS_OUTPUT_BG In) 
{ 
    PS_OUTPUT Output; 
    Output.RGBColor = In.Diffuse; 
    return Output; 
} 
 
////////////////////////////////////// 
// Techniques specs follow 
////////////////////////////////////// 
 
technique Char 
{ 
    pass p0 
    { 
        VertexShader = compile vs_1_1 VS_VertexBlend(); 
    } 
} 
technique World 
{ 
    pass p0 
    { 
        VertexShader = compile vs_1_1 VS_World(); 
    } 
} 
 
technique World_Far 
{ 
    pass p0 
    { 
        VertexShader = compile vs_1_1 VS_World(); 
        PixelShader  = compile ps_1_1 PS_World_Far(); 
    } 
} 
 
technique Item 
{ 
    pass p0 
    { 
        VertexShader = compile vs_1_1 VS_Item(); 
    } 
} 
 
technique Terrian 
{ 
    pass p0 
    { 
        VertexShader = compile vs_1_1 VS_Terrian(); 
    } 
} 
 
technique Terrian_Far 
{ 
    pass p0 
    { 
        VertexShader = compile vs_1_1 VS_Terrian_Far(); 
        PixelShader  = compile ps_1_1 PS_Far(); 
    } 
} 
 
technique BG 
{ 
    pass p0 
    { 
        VertexShader = compile vs_1_1 VS_BG(); 
        PixelShader  = compile ps_1_1 PS_BG(); 
    } 
}