如何在HLSL中设置没有噪声的纹理值的UV协调

时间:2017-05-02 05:40:22

标签: hlsl fragment-shader antialiasing uv-mapping

我有UV渲染通道(RG图像)
enter image description here
我想将uv传递纹理值设置为UV Coordinate而不产生噪音 当我测试它时,结果应该像下面的图像一样像噪声一样 enter image description here

我用其他着色器语言测试过它,比如cgprogramm和GLSL
也统一测试 并尝试使用Zdepth进行mipmap但我无法获得抗锯齿效果 所有结果都是一样的

float4x4 World;
float4x4 View;
float4x4 Projection;
float4x4 WorldInverseTranspose;

float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;

float3 DiffuseLightDirection = float3(1, 0, 0);
float4 DiffuseColor = float4(1, 1, 1, 1);
float DiffuseIntensity = 1.0;

float Shininess = 200;
float4 SpecularColor = float4(1, 1, 1, 1);
float SpecularIntensity = 1;
float3 ViewVector = float3(1, 0, 0);

texture ModelTexture;
texture UvTexture;
sampler2D textureSampler = sampler_state {
    Texture = (ModelTexture);
    MinFilter = Point;
    MagFilter = Point;
    AddressU = Wrap;
    AddressV = Wrap;
};
sampler2D textureSamplerUV = sampler_state {
    Texture = (UvTexture);
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};
struct VertexShaderInput
{
    float4 Position : POSITION0;
    float4 Normal : NORMAL0;
    float2 TextureCoordinate : TEXCOORD0;

};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float2 Color : COLOR0;
    float3 Normal : TEXCOORD0;
    float2 TextureCoordinate : TEXCOORD1;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position = mul(viewPosition, Projection);

    float4 normal = normalize(mul(input.Normal, WorldInverseTranspose));
    float lightIntensity = dot(normal, DiffuseLightDirection);
    output.Color = saturate(DiffuseColor * DiffuseIntensity * lightIntensity);

    output.Normal = normal;

    output.TextureCoordinate = input.TextureCoordinate;
    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    //******* is here 
    float4 textureColorUV = tex2D(textureSamplerUV, input.TextureCoordinate);

    float2 cord = float2(textureColorUV[0],textureColorUV[1]);

    float4 textureColor = tex2D(textureSampler, cord);

    return saturate(textureColor);
}

technique Textured
{
    pass Pass1
    {
        VertexShader = compile vs_1_1 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

2 个答案:

答案 0 :(得分:1)

不是噪音,是点采样。 Min和Mag应该是线性的。并且Mip应该是Point(双线性)或线性(三线性)以使用mipmap

作为旁注,你可以使用uv纹理的点采样来获得边缘上的正确值,你不必在这里使用点采样器,它们可能比现代硬件上的常规滤波慢。您当然可以确保为纹理提取生成的texcoord已经很好地对齐,但如果您可以访问DX11,则可以使用更简单的解决方案。

如果我正在编写着色器,最简单的方法是使用括号运算符在像素着色器中使用SV_Position:

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    //******* 
    float2 uv = textureSamplerUV[input.Position].rg;    

    float4 textureColor = texture.Sample( sampler, uv );    
    return (textureColor); // unless you use a non unorm format, saturate not necessary.
}

答案 1 :(得分:-1)

我发现了噪音的原因 我的uv图像是正常的字节图像R(255)G(255)B(255)
并且纹理W和H分裂为仅255个原始和列并且还不够 - 它是查看噪声的主要原因

我将我的UV图像替换为16Bit图像并解决问题 我的新UV图像将纹理分割为65535 raw和columns enter image description here