Search Unity

[SOLVED] Have TrailRenderer draw in front of itself

Discussion in 'General Discussion' started by _watcher_, Apr 4, 2020.

  1. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    261
    My problem is best illustrated with a picture:


    Any way to make TrailRenderer draw the 'younger texture' in front of the 'older texture'?
    I've tried all the Inspector settings.. no luck.
     
    Last edited: Apr 4, 2020
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    Most likely you'd need to replace trail renderer with line renderer, and either reverse the direction of it (meaning oldest vertices would be at the start of the trail), or utilize z depth to handle sorting.
     
    _watcher_ likes this.
  3. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    261
    Thank you for this suggestion, its looks usable from what i can tell (just have to manually draw and clear the line over time, i assume).
    I do not have any experience with LineRenderer or TrailRenderer, but it seems TrailRenderer uses LineRenderer and just adds some functionality on top (like drawing/clearing over time)? But then TrailRenderer extends Renderer (API), so im unsure what else TrailRenderer does.

    The reason im asking is - i want to have a type of 'snake' that follows mouse (if you can tell from the picture). It seems i could get such functionality from LineRenderer, with few more lines of code (it does draw 'on top of itself' by default, which i need - and i can manipulate the indexes however i want - which is good). Anything else you can think of that i should watch out for, when creating the snake, that LineRenderer (as opposed to TrailRenderer) doesnt handle?

    Any other suggestions regarding TrailRenderer's point positions manipulation would be welcome. It seems they are not exposed, which is a shame. It seems id just have to invert them (but doing so every frame might be annoying) - even better if i could just manipulate how they are added, but i haven't found a way through the API
     
  4. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    Yes, that seems to be the case.

    It tracks object movement and adds points to the trail accordingly.

    Well, if your snake writes into zbuffer,you might get z-fighting. Z-fighting can be solved by using orthographic projection and adding depth to the snake. Meaning making sure the older points are further away from the camera. Trail renderer also might be able to produce smoother corners by defauhlt.

    Traril renderer has "GetPositions" and "SetPositions" methods which allow you to directly alter vertex positions.
    http://resetoter.cn/UnityDoc/ScriptReference/TrailRenderer.html

    You can also use othographic projection, and slowly move drawing position towards the camera even with Trail renderer. This will produce correct appearance, but you need to make sure you won't run out of availbale space.
     
    _watcher_ likes this.
  5. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    261
    I swear i checked that before, and i could only find GetPositions, so i thought 'though luck, no write', and still cant see SetPositions in either TrailRenderer or its base classes, but in code i do get the tooltip that it exists? Where did you find it? Either way, thanks man, this should do it!
     
    neginfinity likes this.
  6. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    261
    A good way to reply to myself! I was using scripting runtime for 2017.2 API! Lets hope it works if i simply invert the array (ill have to have a refference array probably, and monitor what was added and removed in the TrailRenderer, as everything should be inverted except the new points that are added each frame).
     
    Last edited: Apr 4, 2020
  7. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    Ouch. My bad. There's no SetPositions, I misread it.

    Inverting array should work if you switch to line renderer, but if you don't want to do it, use orthographic projection with tral position slowly moving towards the camera....

    UNLESS you're also using a shader that does not write into a zbuffer.
     
  8. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    261
    No you were right! there is such function (it was most prob. added after 2017.2, i can see it in 2019.3)
    https://docs.unity3d.com/ScriptReference/TrailRenderer.SetPositions.html
    But, anyways - i tested it, and the outputs look super buggy.
    So just to TL;DR my results:

    SetPositions returns an int:
    int How many positions were actually stored in the output array.
    Since this is internal array [], it doesnt resize, so the rest of the items are Vector3.zero-s, which is fine, but the bug i found is, that when vertex is removed during a frame (in testing in Update), and another one (with different position) is added, the number of positions is increased by 1 (which obviously is wrong). So it seems i might run into trouble when policing these (since i need to nitpick and only move the newly added positions, ignore the removed ones - btw any idea if this should be done in Update, or LateUpdate or whatnot).

    To your other point, i am using orthographic camera since start - so that's fine, and i actually tested moving the trail with camera on z-axis, so that each frame the next vertices are written 'in front of the other vertices on z-axis', which should work fine without manipulating the positions themselves through GetPositions/SetPositions - and i did try many different shaders, but none of them worked. Im unsure which shaders write into z-buffer, can you recommend any off the top of your head? - im using sprite shader from universal RP, but can go back to test with any other shader. My HLSL skills are low, but maybe i can in the meantime google and find out how to recognize the specific line that writes to z-buffer and check some matching shaders.. thanks!
     
    Last edited: Apr 4, 2020
  9. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    Oh, NOW I see. The power of google led me to random chinese server with mirror copy of unity docs instead of unity script reference (-_-)

    It is "ZWrite Off" within Pass block.
    https://docs.unity3d.com/Manual/SL-CullAndDepth.html

    If your rainbow isn't supposed to be transparent, you could likely use unlit shader without transparency support. If it is supposed to be transparent, and transparency isn't additive/subtractive/multiplicative, but "glass door transparency" (requires sorting), things will get complicated.
     
    _watcher_ likes this.
  10. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    261
    I don't need transparency and Unlit/Texture works!!!
    Thanks so much! <3
     
    neginfinity likes this.
  11. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    Have fun (^_^)
     
  12. _watcher_

    _watcher_

    Joined:
    Nov 7, 2014
    Posts:
    261