【问题标题】:how do i set UV coordination from Texture value without noise in HLSL如何在 HLSL 中从纹理值设置 UV 协调而没有噪声
【发布时间】:2017-09-29 13:27:11
【问题描述】:

我有 UV 渲染通道(RG 图像)

我想将 uv pass 纹理值设置为没有噪音的 UV 坐标 当我测试它时,结果应该像下图那样像素化噪声

我已经在 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();
    }
}

【问题讨论】:

    标签: hlsl fragment-shader antialiasing uv-mapping


    【解决方案1】:

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

    作为旁注,您可以对 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.
    }
    

    【讨论】:

    • thanx galop 但是当我想尝试时,我的 Position :( 和 .rg 有错误
    • @ehsanwwe 这是因为您使用的是非常旧的着色器模型,1.1 和 2.0。除非您尝试支持古代硬件并且您或不在 DX11 中,否则您不能这样做。如果您只是修复采样器状态,您的原始着色器代码就可以工作。
    • 感谢帮助,但我尝试了,我在 Unity 着色器中使用 DX11 - 但结果是相同的 - 我用我的对象 UV 替换 input.position (平面对象 0,0 1,0 ,11, 1,0 ) :( 我认为 mip 过滤在 pixle uv 中不起作用,只适用于矢量 uv - 我的评论是真的?
    【解决方案2】:

    我发现噪音的原因是什么
    我的 uv 图像是普通字节图像 R(255)G(255)B(255)
    并且纹理 W 和 H 拆分为仅 255 个原始和列,这还不够 - 这是看到噪音的主要原因

    我将我的 UV 图像替换为 16 位图像并解决问题
    我的新 UV 图像将纹理拆分为 65535 个原始和列

    【讨论】:

      猜你喜欢
      • 2013-05-08
      • 2014-05-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 2013-11-23
      • 1970-01-01
      • 2014-12-15
      • 1970-01-01
      相关资源
      最近更新 更多