【问题标题】:Texture Lookup in Vertex Shader顶点着色器中的纹理查找
【发布时间】:2016-04-07 19:32:06
【问题描述】:

我正在尝试创建一个加载纹理并使用它来扭曲球体的顶点着色器。然而,即使纹理在实际放置在球体上时是无缝的,但变形在三角形之间存在间隙。如下图所示。

我认为在对纹理进行采样时出了点问题,但我不确定。这是我用于顶点着色器的代码:

cbuffer MatrixBuffer
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};

cbuffer ExplosionBuffer
{
float3 distortion;
float time;
};

Texture2D shaderTexture;
SamplerState SampleType;


//structs

struct VertexInputType
{
float4 position : POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
};

struct PixelInputType
{
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
float4 deltaP : DELTAPOS;
};

//Vertex shader

PixelInputType ExplosionVertexShader(VertexInputType input)
{
PixelInputType output;


//sample the noise texture
uint3 sampleCoord = uint3(512*input.tex.x, 512*input.tex.y, 0);
float4 distex = shaderTexture.Load(sampleCoord);


//calculate the displacement based on noise tex and distortion buffer
float displacement;
displacement = distortion.x * distex.r + distortion.y * distex.g + distortion.z * distex.b;
displacement = displacement * 2;
displacement = distex.r + distex.g + distex.b;
input.position = input.position * (1 + displacement);

//set w to one for proper matrix calculations.
input.position.w = 1.0f;

// Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);

// Store the input color for the pixel shader to use.
output.tex = input.tex;

output.normal = input.normal;

output.deltaP.x = displacement;

return output;
}

【问题讨论】:

  • 您是否为球体使用索引缓冲区?这应该可以避免这些漏洞,因为您没有重复的顶点由于浮点错误而最终位于稍微不同的位置。
  • 这些分裂只发生在模型展开的接缝处。由于这些顶点中的每一个都有不同的 uv 坐标,我认为即使它们具有相同的位置,它们也不能存储为相同的顶点。

标签: directx hlsl


【解决方案1】:

我认为您的问题是,您正在对超出范围的纹素进行采样。

线条

//sample the noise texture
uint3 sampleCoord = uint3(512*input.tex.x, 512*input.tex.y, 0);
float4 distex = shaderTexture.Load(sampleCoord);

是 [0,512] 范围内的采样坐标,超出范围(假设您使用的是 512² 纹理)。 Load 方法需要 [0,texturedimension-1] 中的输入坐标。

因此你的模型中的裂缝可以用documentation的注释来解释:

注意当 Location 中的一个或多个坐标超过纹理的 u、v 或 w mipmap 级别尺寸时,Load 在所有组件中返回零。 Direct3D 保证对越界访问的任何资源返回零。

因此,受影响的顶点被替换为零。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多