顶点着色器中的纹理查找

时间:2016-04-06 13:42:06

标签: directx hlsl

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

Image

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

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;
}

1 个答案:

答案 0 :(得分:0)

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

    #include <stdio.h>
    #include <string.h>


    int main(int argc, char* argv[])
    {


        FILE* file = fopen("questions-words.txt", "r"); /* should check the result */
        if (file==NULL){
            return-1;
        }
        char line[256];
        char first[20],second[20],third[20],fourth[20],temp[20];


        while (! feof(file)) {
            fscanf(file,"%s \t", first);
            if (!strcmp(first,":")){
                fscanf(file,"%s \t",temp);
            continue;
                }

                fscanf(file,"%s \t", second);
                fscanf(file,"%s \t", third);
                fscanf(file,"%s \t", fourth);
                printf("%s %s %s %s \n", first, second, third, fourth);



        }
                fclose(file);

                return 0;
    }

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

因此,可以使用documentation

中的注释来解释模型中的裂缝
  

注意当位置中的一个或多个坐标超出纹理的u,v或w mipmap级别尺寸时,Load会在所有组件中返回零。 Direct3D保证为超出边界的任何资源返回零。

因此受影响的顶点以零移位。