Search Unity

Tiled line renderer material - i don't want it to stretch

Discussion in 'General Graphics' started by Oshigawa, Feb 5, 2017.

  1. Oshigawa

    Oshigawa

    Joined:
    Jan 26, 2016
    Posts:
    362
    The question is simple enough.

    I'm making a 2D laser for a sidescrolling shooter, and it goes like this:

    I'm using a Line Renderer which uses its own position (on the player) as a start position. Its end position is childed with an empty end object which is off screen, so it moves together with the start object and they always form a straight line. 2D Ray is cast to detect hits, and the end position has its position set every frame on the detected hit position so the line rendering stops there giving the illusion of the laser hiting an object. When raycast is no longer detecting a hit, object defining the end of the line returns back to predefined X position, so it's all nice and dandy.

    However, Line Renderer is using Particles/Additive material with 1/1 tiling and its offset being modified every frame to make an illusion of movement. When i'm moving closer to the object hit, the material on the line is shrinking, and when i'm moving away it gets stretched.

    How can i always have the fixed size of the material applied to Line Renderer regardless off the line length? I've been googling for two days, similar topic have been around, but i haven't found any appropriate answers. I suppose i could change material tiling on runtime, but i don't know what parameters i should use to scale correctly. Besides, i suppose there must be a simpler way.
     
    Last edited: Feb 6, 2017
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    It's been an annoyance with the line and trail renderer for a while now. The solution is to set the material's texture scale dynamically via script. The other solution is to not use Unity's line renderer at all and write your own (or use one from the asset store or modify a trail renderer from the community wiki, nearly all of which add this feature).
     
    MetroSlim likes this.
  3. Oshigawa

    Oshigawa

    Joined:
    Jan 26, 2016
    Posts:
    362
    Thanks for the answer, looks like i'll have to use a third party solution, i'm not very good at scripting of that level. It's a shame that there isn't a simple tick to prevent the texture from scaling, which seems like a logical thing to be supported.
     
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    In Unity 5.5, we added a Texture Mode option to lines and trails, to solve this problem.

    I believe we have an additional feature request to reverse the U direction, to make the texture appear totally static in the world. However, even without this extra feature, maybe the new Texture Mode solves your problem.. give it a go, if you're using 5.5!
     
  5. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Also, maybe this will help, in you are not using 5.5:
     
  6. Oshigawa

    Oshigawa

    Joined:
    Jan 26, 2016
    Posts:
    362
    @richardkettlewell

    Hello and thank for the answer! As for the video, i've seen it, but it's too low res to see the code, and i'm afraid i'm not quite apt at scripting to make it out by listening only (though i'm aware what should i do in theory).

    It's not critical right now and 5.5 is quite new (i'm still on 5.3.5) so i'm afraid i'll break some assets if i upgrade right now, but it's good that the function is implemented and i'll give it a go when the time comes to polish things up.
     
  7. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Ok, good luck! :)

    And sorry about the link.. I didn't check the resolution, and foolishly assumed that it would have been uploaded at a legible resolution! :D
     
  8. Oshigawa

    Oshigawa

    Joined:
    Jan 26, 2016
    Posts:
    362
    No problem, appreciate the help :)
     
    richardkettlewell likes this.
  9. Oshigawa

    Oshigawa

    Joined:
    Jan 26, 2016
    Posts:
    362
    @richardkettlewell

    Just a small follow-up, i recently updated to 5.5.0f3 and the texture mode option on line renderers is very nice, does what i needed :)
     
    richardkettlewell likes this.
  10. kalibcrone

    kalibcrone

    Joined:
    Sep 4, 2015
    Posts:
    10
    The feature to reverse U direction to have the texture appear static would be great! For now the tiled texture mode will do, thanks! In the meantime, I'll have to check if it is possible to make my own script to make the texture seem static, though I'm not sure I have the skills necessary.

    Cheers!
     
  11. id347627

    id347627

    Joined:
    Apr 22, 2019
    Posts:
    1
  12. marco200002010

    marco200002010

    Joined:
    Mar 29, 2020
    Posts:
    1
    Thanks, you are great!
     
    richardkettlewell likes this.
  13. farooqiasfandyar

    farooqiasfandyar

    Joined:
    Apr 19, 2021
    Posts:
    32
    Hi there!
    For anyone wondering a simple non-scripting solution to that problem simply create a shader graph and add a tiling property as a vector2 and manually tweak it with your desired texture. Apply the shader to a material and the material to the line renderer. Set the line renderer Texture Mode to Tile and hit save.

    Hope it helps!
     
    richardkettlewell likes this.
  14. gul-jenya

    gul-jenya

    Joined:
    Nov 30, 2013
    Posts:
    1
    Solution is simple - enable tiling for Line Renderer, choose `wrap mode - repeat` in texture import params and set `Corner Vertices` (Line renderer param in ' positions' group) greater than zero

    Screenshot 2023-04-01 at 04.47.55.png
     
    unity_AkGames likes this.
  15. unity_AkGames

    unity_AkGames

    Joined:
    Dec 23, 2020
    Posts:
    12
    Corner Vertices = 1 did it for me. Thanks