如何在 DirectX 11 中使用曲面细分绘制虚线图案 3D 线

How to draw a dotted pattern 3D line with tessellation in DirectX 11?

本文关键字:虚线 绘制 细分 3D 曲面 DirectX      更新时间:2023-10-16

我需要在directx 11中绘制线条,这将在GDI中显示像虚线笔画一样的颜色。我知道曲面细分会在每条线之间放置更多的顶点。有没有人告诉我如何在 directx 11 中获得虚线图案绘制线?

您可以在屏幕空间和像素着色器中平铺小纹理。像素着色器的外观如下:

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