我需要在DirectX 11中绘制线条,以显示GDI中的虚线笔画之类的颜色。我知道镶嵌会在每条线之间放置更多的顶点。有人告诉我如何在DirectX 11中获得虚线图形绘制线吗?
答案 0 :(得分:1)
您可以在屏幕空间的像素着色器中平铺一个小的纹理。像素着色器的外观如下:
Texture2D<float4> patternTexture : register(t0);
static const uint2 patternSize = uint2( 8, 8 );
float4 main( float4 screenSpace : SV_Position ) : SV_Target
{
// Convert position to pixels
const uint2 px = (uint2)screenSpace.xy;
// Tile the pattern texture.
// patternSize is constexpr;
// if it's power of 2, the `%` will compile into bitwise and, much faster.
const uint2 readPosition = px % patternSize;
// Read from the pattern texture
return patternTexture.Load( uint3( readposition, 0 ) );
}
或者您可以在运行时生成图案,而无需读取纹理。这是一个跳过所有其他像素的像素着色器:
float4 main( float4 color: COLOR0, float4 screenSpace : SV_Position ) : SV_Target
{
// Discard every second pixel
const uint2 px = ((uint2)screenSpace.xy) & uint2( 1, 1 );
if( 0 != ( px.x ^ px.y ) )
return color;
discard;
return float4( 0 );
}