【发布时间】: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 坐标,我认为即使它们具有相同的位置,它们也不能存储为相同的顶点。