Direct3D 中的 HLSL 多重纹理与雾问题

发布于 2024-10-21 13:43:04 字数 2919 浏览 2 评论 0原文

我正在尝试在演示中的某些地形上实现多重纹理和雾,但显然我在某个地方出错了,因为纹理不可见,唯一渲染的是地形中的雾颜色。

没有任何纹理的迹象。

这是 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();
        
    }
}

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

I am trying to implement a multi-texturing and fog on some terrain in my demo, but apparently i am going wrong somewhere because the texturing is not visible, the only thing that is being rendered is the fog color in the terrain.

No sign of texturing whatsoever.

Here is the fx file:

//------------------------------------
//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();
        
    }
}

as you can see, the fog color is gray, and that is all I am getting. It is very depressing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海之角 2024-10-28 13:43:04

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

罗伯特

My guess would be, that FogSaturate is too high, or in other words, FogRange is too low, so the lerp call always returns FogColor. Try to set FogRange higher and see if it helps.

Robert

旧时模样 2024-10-28 13:43:04

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

尝试通过:

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

它应该有效。

I'm guessing matVP is your (view matrix) * (your projection matrix) however you will need a World, View and Projection matrix to map the location of the terrain correctly. (In other words, right now the location in which you draw your terrain at the moment is completely wrong. That's why you can't see anything).

Try passing:

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

It should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文