Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to show only a part from the line renderer?

Discussion in 'General Graphics' started by Bongmo, Dec 1, 2022.

  1. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    Hi,

    I've created a simple Line Renderer.


    What I want is to show only a part of the Line Renderer. The range value is from 0 to 1. The values will be changed in Update().


    Any ideas?

    The solution must work on iOS and Android.

    I've tried to solve it myself, but I didn't get it to work.

    Thanks for any help. :)
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    Can you write a shader that performs clip() on the texture V value?
     
  3. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    I'm not an material shader expert. :(
    But I will search for what you've said.

    So, without a shader this is not possible?
     
    Last edited: Dec 1, 2022
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    In theory you could remove/maniplulate the points to make it look like only part of your full desired line was visible. But I wouldn't recommend that approach. Clipping in a shader will be much simpler.

    Set the material to alpha test, declare an exposed float2 in your shader for the range (might need to be float4 even though you only need 2 values), then do the clip in the pixel shader:

    clip(in.uv.v < visibleRange.x || in.uv.v > visibleRange.y);

    Then in your script you can do:

    material.SetVector("visibleRange", new Vector4(minRange, maxRange, 0.0f, 0.0f));
     
    Bongmo likes this.
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    Bongmo and richardkettlewell like this.
  6. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    @mgear

    Thank you for your answer.

    The first solution:
    In original the Line Renderer has no gradient. It has only a texture. The gradient was only an example. Sorry. My fault. I didn't wrote that.

    The second solution:
    This looks promising. Do you know fast where to add the cut from beginning and at the end and without animation?
     
    Last edited: Dec 2, 2022
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
  8. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    I get these errors:
    Shader error in 'UnityLibrary/Patterns/ScrollingFill': 'clip': no matching 0 parameter intrinsic function; Possible intrinsic functions are: clip(float|half) at line 74 (on d3d11)

    Shader error in 'UnityLibrary/Patterns/ScrollingFill': syntax error: unexpected token 'in' at line 74 (on d3d11)


    Sorry. I told, that I'm not an advanced Shader programmer. Really sorry.

    ***

    This is the code. Commented the speed. Added new lines:

    // animated scrolling texture with fill amount
    // https://unitycoder.com/blog/2020/03/13/shader-scrolling-texture-with-fill-amount/

    Shader "UnityLibrary/Patterns/ScrollingFill"
    {
    Properties
    {
    _MainTex ("Texture", 2D) = "white" {}
    _Fill("Fill", Range(0.0,1.0)) = 0.5
    //_Speed("Speed", float) = 5 // Commented

    // +++ NEW LINE CODE +++
    _MyVector ("Some Vector", Vector) = (0,0,0,0)


    }

    SubShader
    {
    Tags { "RenderType"="Opaque" }
    LOD 100

    Pass
    {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag

    #include "UnityCG.cginc"

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

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

    sampler2D _MainTex;
    float4 _MainTex_ST;
    float _Fill;
    // float _Speed; // Commented

    // New variable
    Vector _MyVector;


    v2f vert (appdata v)
    {
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    return o;
    }

    fixed4 frag(v2f i) : SV_Target
    {
    // get scroll value
    //float2 scroll = float2(0, frac(_Time.x*_Speed)); // Commented

    // sample texture
    //fixed4 col = tex2D(_MainTex, i.uv -scroll); // Commented
    fixed4 col = tex2D(_MainTex, i.uv); // Rewritten without scroll

    // discard if uv.y is below below cut value
    //clip(step(i.uv.y, _Fill * _MainTex_ST.y)-0.1); // Commented


    // +++ NEW LINE CODE +++
    clip(i.uv.x < _MyVector.x || in.uv.v > _MyVector.y); // <--- ERROR LINE


    return col;

    // make un-animated part black
    //return col*step(i.uv.y, _Cut * _MainTex_ST.y);
    }
    ENDCG
    }
    }
    }
     
    Last edited: Dec 2, 2022
  9. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    Last edited: Dec 3, 2022
    Bongmo likes this.
  10. Bongmo

    Bongmo

    Joined:
    Jan 18, 2014
    Posts:
    155
    @mgear

    You're awesome. Thank you very much. :)