Tajuddin Khandaker
Tajuddin Khandaker

Reputation: 722

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

I need to draw lines in directx 11 which will show colors like dotted pen drawing in GDI. I know tessellation will put more vertices in between each line. Does anybody make it clear to me how can I get a dotted pattern drawing line in directx 11?

Upvotes: 0

Views: 532

Answers (1)

Soonts
Soonts

Reputation: 21956

You can tile a small texture in screen space, in pixel shader. Here's how pixel shader may look like:

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

Or you can generate pattern in runtime, without reading textures. Here's a pixel shader which skips every other pixel:

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

Upvotes: 1

Related Questions