HLSL在Direct3D中具有雾问题的多纹理

时间:2011-03-11 09:08:17

标签: c++ hlsl texture-mapping shader

嘿伙计们 我试图在我的演示中的某些地形上实现多纹理和雾化,但显然我在某处出错,因为纹理不可见,唯一被渲染的是地形中的雾色。 没有任何纹理的迹象

这是fx文件:

//------------------------------------
//Stuff for the Terrain
uniform extern float4x4 MatVP;
uniform extern float3  SunPos;
uniform extern texture Tex0;
uniform extern texture Tex1;
uniform extern texture Tex2;
uniform extern texture TexGS;
uniform extern float3  EyePos;


//------------------------------------

sampler sTex0 = sampler_state
{
    Texture = <Tex0>;
    MagFilter = LINEAR;
    MinFilter = ANISOTROPIC;
    MaxAnisotropy = 8;
    MipFilter = LINEAR;
    AddressU = WRAP;
    AddressV = WRAP;
};

sampler sTex1 = sampler_state
{
    Texture = <Tex1>;
    MagFilter = LINEAR;
    MinFilter = ANISOTROPIC;
    MaxAnisotropy = 8;
    MipFilter = LINEAR;
    AddressU = WRAP;
    AddressV = WRAP;
};
sampler sTex2 = sampler_state
{
    Texture = <Tex2>;
    MagFilter = LINEAR;
    MinFilter = ANISOTROPIC;
    MaxAnisotropy = 8;
    MipFilter = LINEAR;
    AddressU = WRAP;
    AddressV = WRAP;
};
sampler sTexGS = sampler_state
{
    Texture = <TexGS>;
    MinFilter = LINEAR;
    Magfilter = LINEAR;
    Mipfilter = POINT;
    AddressU = WRAP;
    AddressV = WRAP;
};

const float3 FogColor = {0.5f, 0.5f, 0.5f};
const float  FogStart = 10.0f;
const float  FogRange = 200.0f;

struct vOut {
    float4 PosH            : POSITION0;
    float2 cTexTiled    : TEXCOORD0;
    float2 cTexNonTiled : TEXCOORD1;
    float shade            : TEXCOORD2;
    float FogSaturate    : TEXCOORD3;
};

//------------------------------------
vOut VS_Def(float3 PosW : POSITION0
 , float3 NormW : NORMAL0
 , float2 cTex : TEXCOORD0)
{
    vOut V = (vOut)0;

    V.PosH = mul(float4(PosW, 1.0f), MatVP);

    float3 SunVec = normalize(SunPos - EyePos);
    V.shade = saturate(max(0.0f, dot(NormW, SunVec)) + 0.25f);

    float Dist = distance(PosW, EyePos);
    V.FogSaturate = saturate((Dist - FogStart)/FogRange);

    V.cTexTiled = cTex * 16.0f;
    V.cTexNonTiled = cTex;
    return V;
}

float4 PS_Def(float2 cTexTiled : TEXCOORD0
, float2 cTexNonTiled : TEXCOORD1
, float shade : TEXCOORD2
, float FogSaturate : TEXCOORD3): COLOR
{
    float3 Tex0 = tex2D(sTex0, cTexTiled).rgb;
    float3 Tex1 = tex2D(sTex1, cTexTiled).rgb;
    float3 Tex2 = tex2D(sTex2, cTexTiled).rgb;
    float3 TexGS= tex2D(sTexGS,cTexNonTiled).rgb;

    float inv = 1 / (TexGS.r + TexGS.g + TexGS.b);
    Tex0 *= TexGS.r * inv;
    Tex1 *= TexGS.g * inv;
    Tex2 *= TexGS.b * inv;
    float3 TexColor = (Tex0 + Tex1 + Tex2) * shade;
    float3 FinalColor = lerp(TexColor, FogColor, FogSaturate);

    return float4(FinalColor, 1.0f);
}


//-----------------------------------
technique DefTech
{
    pass p0 
    {        
        VertexShader = compile vs_2_0 VS_Def();
        PixelShader = compile ps_2_0 PS_Def();

    }
}

如你所见,雾的颜色是灰色的,这就是我所得到的。这非常令人沮丧!

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

我的猜测是,FogSaturate太高了,换句话说,FogRange太低了,所以lerp调用总是返回FogColor。尝试将FogRange设置得更高,看看它是否有帮助。

罗伯特

答案 1 :(得分:1)

我猜测matVP是你的(视图矩阵)*(你的投影矩阵)但是你需要一个World,View和Projection矩阵来正确地映射地形的位置。 (换句话说,现在你绘制地形的位置是完全错误的。这就是你看不到任何东西的原因。)

尝试传递:

pEffect->SetMatrix( "MatVP", &(matW*matV*matP));

它应该有用。