How to use sprite mask with trail renderer?

I have two objects. One with a sprite renderer and material, one with a trail renderer. I want the trail to only be visible when it is inside the sprite renderer bounds. Something like sprite mask, but from what I heard, sprite mask doesn’t work with trail renderer.

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.

Some example code:

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;
    }