hlsl之multi texture.

D3DXCOLOR colorMtrlDiffuse(0.4f, 0.4f, 0.4f, 1.0f);
    D3DXCOLOR colorMtrlAmbient(0.4f, 0.4f, 0.4f, 1.0f);
    D3DXCOLOR colorMtrlSpecular(0.5f, 0.5f, 0.5f, 1.0f);

    D3DXCOLOR colorLightAmbient(1.0f, 0.5f, 0.1f, 1.0f);
    D3DXCOLOR colorLightDiffuse(1.0f, 1.0f, 1.0f, 1.0f);
    D3DXCOLOR colorLightSpecular(1.0f, 1.0f, 1.0f, 1.0f);

    D3DXVECTOR3 lightDir(0.0f, 0.0f, 1.0f);
    D3DXVECTOR4 eyePos(0.0f, 8.0f, -20.0f, 1.0f);


    V_RETURN( g_pEffect9->SetValue( "g_MaterialAmbientColor", &colorMtrlAmbient, sizeof( D3DXCOLOR ) ) );
    V_RETURN( g_pEffect9->SetValue( "g_MaterialDiffuseColor", &colorMtrlDiffuse, sizeof( D3DXCOLOR ) ) );
    V_RETURN( g_pEffect9->SetValue( "g_MaterialSpecularColor", &colorMtrlSpecular, sizeof( D3DXCOLOR ) ) );

    V_RETURN( g_pEffect9->SetValue( "g_LightAmbient", &colorLightAmbient, sizeof( D3DXCOLOR ) ) );
    V_RETURN( g_pEffect9->SetValue( "g_LightDiffuse", &colorLightDiffuse, sizeof( D3DXCOLOR ) ) );
    V_RETURN( g_pEffect9->SetValue( "g_LightSpecular", &colorLightSpecular, sizeof( D3DXCOLOR ) ) );

    V_RETURN( g_pEffect9->SetValue( "g_LightDir", &lightDir, sizeof( D3DXVECTOR3 ) ) );

    V_RETURN( g_pEffect9->SetValue( "g_eyePos", &eyePos, sizeof( D3DXVECTOR4 ) ) );

 

//--------------------------------------------------------------------------------------
// File: SimpleSample.fx
//
// The effect file for the SimpleSample sample.  
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------


//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float4 g_MaterialAmbientColor;      // Material's ambient color
float4 g_MaterialDiffuseColor;      // Material's diffuse color
float4 g_MaterialSpecularColor;
float4 g_LightAmbient;              // Light's diffuse color
float3 g_LightDir;                  // Light's direction in world space
float4 g_LightDiffuse;              // Light's diffuse color
float4 g_LightSpecular;
texture g_Texture1;              // Color texture for mesh
texture g_Texture2;

float4     g_eyePos;
float    g_fTime;                   // App's time in seconds
float4x4 g_mWorld;                  // World matrix for object
float4x4 g_mWorldViewProjection;    // World * View * Projection matrix



//--------------------------------------------------------------------------------------
// Texture samplers
//--------------------------------------------------------------------------------------
sampler TextureSampler1 = 
sampler_state
{
    Texture = <g_Texture1>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

sampler TextureSampler2 = 
sampler_state
{
    Texture = <g_Texture2>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};


//--------------------------------------------------------------------------------------
// Vertex shader output structure
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Position   : POSITION;   // vertex position 
    float2 TextureUV  : TEXCOORD0;  // vertex texture coords
    float3 L          : TEXCOORD1;
    float3 V          : TEXCOORD2;
    float3 N          : TEXCOORD3;
};


//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0 )
{
    VS_OUTPUT Output;
    float3 vNormalWorldSpace;
    
    // Transform the position from object space to homogeneous projection space
    Output.Position = mul(vPos, g_mWorldViewProjection);
    
    // Transform the normal from object space to world space    
    vNormalWorldSpace = normalize(mul(vNormal, g_mWorld)); // normal (world space)

    float4 PosWorld = mul(vPos, g_mWorld);
    Output.L = g_LightDir;
    Output.V = g_eyePos - PosWorld;
    Output.N = vNormalWorldSpace;
    
    // Just copy the texture coordinate through
    Output.TextureUV = vTexCoord0; 
    
    return Output;    
}


//--------------------------------------------------------------------------------------
// Pixel shader output structure
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};


//--------------------------------------------------------------------------------------
// This shader outputs the pixel's color by modulating the texture's
// color with diffuse material color
//--------------------------------------------------------------------------------------
PS_OUTPUT RenderScenePS( VS_OUTPUT In ) 
{ 
    PS_OUTPUT Output;
    
    float3 Normal = normalize(In.N);
    float3 LightDir = normalize(In.L);
    float3 ViewDir = normalize(In.V);
    
    float Diff = saturate(dot(Normal, LightDir));
    
    float3 Reflect = normalize(2 * Diff * Normal - LightDir);
    
    float4 specular = g_MaterialSpecularColor * g_LightSpecular * pow(saturate(dot(Reflect, ViewDir)), 15);
    float4 ambient = g_MaterialAmbientColor * g_LightAmbient;
    float4 diffuse = g_MaterialDiffuseColor * g_LightDiffuse * Diff;

    // Lookup mesh texture and modulate it with diffuse
    float4 tex1 = tex2D(TextureSampler1, In.TextureUV);
    float4 tex2 = tex2D(TextureSampler2, In.TextureUV);
    
    float4 tex = lerp(tex1, tex2, tex2.w);
    
    Output.RGBColor = tex * (ambient + diffuse + specular);

    return Output;
}


//--------------------------------------------------------------------------------------
// Renders scene 
//--------------------------------------------------------------------------------------
technique RenderScene
{
    pass P0
    {          
        VertexShader = compile vs_2_0 RenderSceneVS();
        PixelShader  = compile ps_2_0 RenderScenePS(); 
    }
}

posted on 2012-07-04 22:09  zengqh  阅读(387)  评论(0编辑  收藏  举报