How to use sprite mask with trail renderer?

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.

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.

    private void Start()
    {
        renderTex = new RenderTexture(size.x, size.y, 0);
        renderTex.Create();
        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).
        camera.transform.parent = transform;
        camera.transform.localPosition = new Vector3(0, 0, -10); // Offset the z coord so that the camera can see stuff
        // Set the camera to be 2d
        camera.orthographic = true;
        camera.orthographicSize = transform.localScale.x / 2;
        camera.cullingMask = LayerMask.GetMask("Your Layer Here");
        camera.forceIntoRenderTexture = true; // I don't really know if I need this line, but didn't test it yet.
        camera.targetTexture = renderTex;
        image = GetComponentInChildren<RawImage>();
        image.texture = renderTex;
        image.canvas.worldCamera = camera;
    }

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!

Just to confirm, this has now landed in 2022.1.0a7 :slight_smile:

1 Like

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

7895149--1005427--ss.png

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!

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.

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

There should be a new “Mask Interaction” property in the Trail Renderer Inspector.

8150129--1058684--trail.png

If it’s not there, either you’re on the wrong version, or I told you the wrong version :slight_smile:

1 Like

Thank you!

1 Like

Hi Richard,
Is this then also something that can be added to LineRenderer? That would be wonderful for us and future generations :slight_smile: Thank you.

1 Like

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.

1 Like

Can this backport to 2021.3? Really need this feature when render TrailRenderers in UI.

Sorry, but no, we won’t be backporting this.