How to make a gradient based on a custom shape

I’d like to know if there’s a way to apply a gradient to custom planar meshes like the images below.

customgrads.jpg