在域着色器中计算UV坐标

问题描述 投票:0回答:1

我试图在frank luna的游戏编程简介中实现地形教程。我成功地使用效果文件实现了它。

当我尝试分离顶点,外壳,域和像素着色器时,我在地形纹理中得到了一个非常奇怪的行为。经过调试后,我发现问题在于计算域着色器中的UV纹理坐标。

这是我如何计算UV坐标。

[domain("quad")]    
DomainOut main(PatchTess patchTess,
float2 uv : SV_DomainLocation,
const OutputPatch<HullOut, 4> quad)
{
DomainOut dout;

// Bilinear interpolation.
dout.PosW = lerp(
    lerp(quad[0].PosW, quad[1].PosW, uv.x),
    lerp(quad[2].PosW, quad[3].PosW, uv.x),
    uv.y);

dout.Tex = lerp(
    lerp(quad[0].Tex, quad[1].Tex, uv.x),
    lerp(quad[2].Tex, quad[3].Tex, uv.x),
    uv.y);

// Tile layer textures over terrain.
dout.TiledTex = dout.Tex * 50.0f;

dout.TiledTex = dout.Tex*50.0f;

// Displacement mapping
dout.PosW.y = gHeightMap.SampleLevel(samHeightmap, dout.Tex, 0).r;

// NOTE: We tried computing the normal in the shader using finite difference, 
// but the vertices move continuously with fractional_even which creates
// noticable light shimmering artifacts as the normal changes.  Therefore,
// we moved the calculation to the pixel shader.  

// Project to homogeneous clip space.
dout.PosH = mul(float4(dout.PosW, 1.0f), gViewProj);

return dout;
}

我正在为域着色器使用四边形。

在使用图形分析器进行调试之后,我在域着色器中得到了数据与我实现的域着色器的效果文件不同,尽管两个文件中都使用了相同的代码。

可能是什么问题?

我有一个与您共享的更新,进入域着色器的数据流与分离文件中的效果文件不同。它不是计算的等式。

是什么让数据流不同,有没有办法改变补丁从Hull着色器进入域着色器的顺序。

这是像素着色器代码:

    Texture2DArray gLayerMapArray : register(t3);
    Texture2D gBlendMap : register(t1);

    SamplerState samLinear
    {
        Filter = MIN_MAG_MIP_LINEAR;

    AddressU = WRAP;
    AddressV = WRAP;
    AddressW = WRAP;
};

struct DomainOut
{
    float4 PosH     : SV_POSITION;
    float3 PosW     : POSITION;
    float2 Tex      : TEXCOORD0;
    float2 TiledTex : TEXCOORD1;
};


float4 main(DomainOut pin) : SV_Target
{
    //
    // Texturing
    //
    float4 c0 = gLayerMapArray.Sample(samLinear, float3(pin.TiledTex, 0.0f));
    float4 c1 = gLayerMapArray.Sample(samLinear, float3(pin.TiledTex, 1.0f));
    float4 c2 = gLayerMapArray.Sample(samLinear, float3(pin.TiledTex, 2.0f));
    float4 c3 = gLayerMapArray.Sample(samLinear, float3(pin.TiledTex, 3.0f));

    // Sample the blend map.
    float4 t = gBlendMap.Sample(samLinear, pin.Tex);

    // Blend the layers on top of each other.
    float4 texColor = c0;
    texColor = lerp(texColor, c1, t.r);
    texColor = lerp(texColor, c2, t.g);
    texColor = lerp(texColor, c3, t.b);
    return texColor;
}
graphics directx hlsl
1个回答
0
投票

最后,解决方案是我应该从c ++代码设置采样器,即使你在着色器中有一个采样器。我不知道为什么但这解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.