How to crop or mask a sprite to a specific shape?

I’m trying to do a procedural terrain generation. I’ve managed to make the function to generate the points, and I’ve been testing/debugging by creating the terrain using a polygon collider on an object.

Now that it’s done, I want to use this shape to crop a large sprite using the shape so that I obtain the shaped terrain. If I have a square terrain sprite, is there any way to crop the top or mask it to the shape of the polygon collider (or the shape of the function I’m using).

Well, you haven’t specifically specified by looking at that screenshot I assume you are talking about a 2D terrain.

If that’s the case I believe the best way would be to manually create a 2D polygonal mesh that represents your terrain (very much like the image you have there). Then you can assign a texture to it and assign of UV coordinates.

With X/Y based naive UVs you should get your texture naturally cropped by the mesh edges.