Dashed Line in Metal

How can I create a dashed line using a Metal shader? I'm thinking something similar to the CoreGraphics "lineDash" property.

Replies

I think the easiest way would be to use a 1DTexture with an alternating white and transparent pattern. You could create a sampler with the sAddressMode of the MTLSamplerStateDescriptor set to MTLSamplerAddressModeRepeat mode to (or set the address mode of a sampler in your fragment shader). You would need to calculate texture coordinates in the vertex shader based upon the distance between the two vertices of the line so that the pattern isn't "stretched" for longer lines.
  • I'm also interested in knowing how to do this, and I have a feeling a lot of people are as well. Could you perhaps write som sample code to help us out? thx

Add a Comment

I'm also interested in knowing how to do this, and I have a feeling a lot of people are as well. Could you perhaps write som sample code to help us out? thx

You don’t need a 1D texture. You use logic in the fragment shader to determine whether you’re in a dash or a gap based on the distance along the line.

Example in webgl: https://www.shadertoy.com/view/ldGfWt

  • This creates some pretty horrible artefacts if the lines are rotating or vertical…!

Add a Comment