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.