0

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?

  • Don't use more verticles; you can do it with a pixel shader. – Stefan Jul 16 '19 at 12:29
  • @Stefan yes I am not using more vertices and I am wondering if tessellation can bring the result that I am expecting - a collection of connected lines each will show dotted stripes of alternating color like GDI pattern pen – Tajuddin Khandaker Jul 16 '19 at 12:36
  • YEs, and I am saying; if your goal is `I need to draw lines in directx 11 which will show colors like dotted pen drawing in GDI`; then you should not use tesselation. If you want to use tessealtion; it will be doable, but quite hard I guess. – Stefan Jul 16 '19 at 12:51
  • Ok. how can I draw a line in dotted pattern in pixel shader so easily? Can I achieve that with simple algorithm. I need suggestion. – Tajuddin Khandaker Jul 16 '19 at 13:04
  • I think it's best to open a new question, explaining what it exactly is what you want. Do you want a single 2D line? or a line on a surface? or some axes? – Stefan Jul 16 '19 at 13:07
  • I want to draw 3D lines where each line will be drawn in dotted color pattern. As you were saying I can achieve the result without tessellation then suggest me please how can I do it using pixel shader. If it's possible easily with pixel shader then I will change my question's title – Tajuddin Khandaker Jul 16 '19 at 13:17

1 Answers1

1

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 );
}
Soonts
  • 20,079
  • 9
  • 57
  • 130