Search Unity

Question How to use sprite mask with trail renderer?

Discussion in 'General Graphics' started by Long2904, Jul 23, 2021.

  1. Long2904

    Long2904

    Joined:
    May 13, 2019
    Posts:
    81
    Is there any way to use sprite mask on trail renderer? I only want my trail to visible when it is on some specific sprite renderer, sprite mask fits this description, too bad it isn't worked with trail renderer :(. I'm making a 2d game and currently using URP in Unity 2020.
     
  2. Long2904

    Long2904

    Joined:
    May 13, 2019
    Posts:
    81
    In the end, I use a second camera with a render texture to achieve this. First, create a camera and a raw image with the same size as my material, then output the camera view to the raw image through a render texture (I do this through code at runtime). Also, remember to set the culling mask of the camera to the trail renderer's layer.

    Code (CSharp):
    1.  
    2.     private void Start()
    3.     {
    4.         renderTex = new RenderTexture(size.x, size.y, 0);
    5.         renderTex.Create();
    6.         camera = new GameObject("Window Camera", typeof(Camera)).GetComponent<Camera>(); // I create a new game object with a camera rather than add it to the current object because I need to set the camera's pos to (0, 0, -10).
    7.         camera.transform.parent = transform;
    8.         camera.transform.localPosition = new Vector3(0, 0, -10); // Offset the z coord so that the camera can see stuff
    9.         // Set the camera to be 2d
    10.         camera.orthographic = true;
    11.         camera.orthographicSize = transform.localScale.x / 2;
    12.         camera.cullingMask = LayerMask.GetMask("Your Layer Here");
    13.         camera.forceIntoRenderTexture = true; // I don't really know if I need this line, but didn't test it yet.
    14.         camera.targetTexture = renderTex;
    15.         image = GetComponentInChildren<RawImage>();
    16.         image.texture = renderTex;
    17.         image.canvas.worldCamera = camera;
    18.     }
     
  3. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Hey, glad you figured out a workaround here.

    Your question has inspired me to add proper support for this, like we have for particle systems.
    Hopefully landing in 2022.1!
     
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Just to confirm, this has now landed in 2022.1.0a7 :)
     
    unity_VYAo9PCTYYamFA likes this.
  5. ungbotond

    ungbotond

    Joined:
    Jan 29, 2018
    Posts:
    2
    Hi,

    I installed Unity version 2022.1.0b7 which includes this function but it's not working the same way as on particles. For simplicity I created an object with a particle system and a trail renderer both set to 'Visible outside mask' but it seems the trail renderer ignores the mask. Is there anything else I should set for it to work?

    Thanks in advance

    ss.png
     
  6. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Hard to say with just the picture. The shader/material used is important. It needs to be compatible, such as Sprites/Default.

    If that doesn’t explain it, a bug report would be appreciated!
     
  7. pslz

    pslz

    Joined:
    Sep 23, 2015
    Posts:
    21
    Hi. There is a way to make the trail render as a mask of an image? I want an "eraser" effect but mixed with the trail effect, so the "erased" part will be reconstructed after a while. Thank you.
     
  8. LazerRock

    LazerRock

    Joined:
    Jan 28, 2020
    Posts:
    1
    Where is this setting located? I can't seem to find it on my Trail Renderer component... Neither can I get the code example to work. Help would be greatly appreciated
     
  9. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    There should be a new "Mask Interaction" property in the Trail Renderer Inspector.

    trail.png

    If it's not there, either you're on the wrong version, or I told you the wrong version :)
     
    Shapely likes this.
  10. Siliko

    Siliko

    Joined:
    Sep 7, 2020
    Posts:
    8
    Thank you!
     
    richardkettlewell likes this.
  11. ClockworkBeetle

    ClockworkBeetle

    Joined:
    Apr 5, 2021
    Posts:
    3
    Hi Richard,
    Is this then also something that can be added to LineRenderer? That would be wonderful for us and future generations :) Thank you.
     
    richardkettlewell likes this.
  12. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    It's already there. When I added it to the TrailRenderer, i added it to the LineRenderer at the same time.

    Available in 2022.2 and newer.
     
    dongch007 likes this.
  13. dongch007

    dongch007

    Joined:
    Jan 8, 2014
    Posts:
    41
    Can this backport to 2021.3? Really need this feature when render TrailRenderers in UI.
     
  14. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Sorry, but no, we won't be backporting this.