2
\$\begingroup\$

I have a LineRenderer, which has 1 dot as texture, which is repeated over the whole line. Now I want to know, if there is some way to give this dots a margin, so that I can make the gap between them bigger.

enter image description here

For the moment there is no gap, I could make a new Texture and add the margin there, but I hope for some more dynamic way to do this.

Edit: Inspector View : enter image description here

\$\endgroup\$
  • \$\begingroup\$ could you paste a screenshot of your inspector for this line renderer? I just get one stretched texture on a line. How do you do it? \$\endgroup\$ – Nika Kasradze Mar 29 '16 at 11:34
  • \$\begingroup\$ Done, but I guess that won't help much, but here is a thread which should contain all you need : gamedev.stackexchange.com/questions/118814/… \$\endgroup\$ – TobiasW Mar 29 '16 at 11:57
5
\$\begingroup\$

If all you need are dots, I wouldn't even use a texture for this. We can shape a repeated circular zone to our liking within the shader:

LineRenderer with custom dotted line shader

By playing with the Repeat Count and Spacing material parameters, you can change the number & spacing of the dots. Offset will shift the dot pattern along the line.

Here's the shader I wrote for this:

Shader "Unlit/DottedLineShader"
{
    Properties
    {
        _RepeatCount("Repeat Count", float) = 5
        _Spacing("Spacing", float) = 0.5
        _Offset("Offset", float) = 0
    }
        SubShader
    {
        Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
        LOD 100

        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            float _RepeatCount;
            float _Spacing;
            float _Offset;

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                fixed4 color : COLOR0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;              
                float4 vertex : SV_POSITION;
                fixed4 color : COLOR0;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.uv = v.uv;
                o.uv.x = (o.uv.x + _Offset) * _RepeatCount * (1.0f + _Spacing);
                o.color = v.color;

                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                i.uv.x = fmod(i.uv.x, 1.0f + _Spacing);
                float r = length(i.uv - float2(1.0f + _Spacing, 1.0f) * 0.5f) * 2.0f;

                fixed4 color = i.color;
                color.a *= saturate((0.99f - r) * 100.0f);

                return color;
            }
            ENDCG
        }
    }
}

One little issue: in my test scene this gets rendered behind the skybox. I'm used to "Queue"="Transparent" fixing that, so I may have something else a little off in here, but I don't have time to debug right now. If it poses a problem for you, you can make the dots alpha tested instead of blended as a quick & dirty fix.

\$\endgroup\$
  • \$\begingroup\$ Okay this works pretty good, but is there a possibility to set the scale? Because depending on the length of the line, the dots are getting oval, I could fix this issue for the texture with this: line.material.SetTextureScale(....). But this is not working anymore. \$\endgroup\$ – TobiasW Mar 29 '16 at 12:41
  • \$\begingroup\$ Instead you'd use line.material.SetFloat("_RepeatCount", foo); where foo is a float describing your desired number of dots. You can also cache the property ID from Shader.PropertyToID and use the integer version of Material.SetFloat if you're using it a lot. \$\endgroup\$ – DMGregory Mar 29 '16 at 12:50
  • \$\begingroup\$ Okay, so I can still not increase the gap between the dots right? I mean I can lower the amount of the dots but then I will get ovals. And is there an easy way to make the offset work here? Sorry I'm completely new to shaders :/ \$\endgroup\$ – TobiasW Mar 29 '16 at 12:56
  • \$\begingroup\$ Yeah obviously, but I would like to keep the dot size and increase the gap at the same time, would that be possible? And the offset part was nice to "animate" the dots, since I was able to constantly increase the offset so the dots were flowing along the line. \$\endgroup\$ – TobiasW Mar 29 '16 at 13:24
  • \$\begingroup\$ @Thunder updated with requested parameters \$\endgroup\$ – DMGregory Mar 29 '16 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.