How to merge/unions polygons in a tilemap

Godot Version

v4.2.2.stable.official [15073afe3]


Hello soo during all my studying of godot as i attempted to make projects ive stumbled upon this problem that ive spent countless weeks trying to find a solution for

What i need:
i need to merge all polygons in tilemap walls, both collision and light occluder ones, that way i can increase performance by merging collision, and fix the issue of light occlusion not working as intended on counter-clockwise mode

What i want:
i would like a code (would help other people and i would be able to study it as i dont like copy and pasting and would actually like to learn) or atleast a way to implement this that i could actually learn from,

my biggest issue was due to godot 4 changes i was unable to understand the new tilemap variables and functions, making it very hard for me to find a way to get all collision/light occluders polygons in all wall tiles, then convert them to a single polygon and then use it to generate collision and light occlusion

What i tried:
ive tried to find solutions for this but what i managed to come with was manually setup polygons for both collision and light occlusion

ive also had this idea of generating texture, collision and light occlusion by converting a single polygon via script which would work great for my goal except that would not be the same as using a tilemap

sorry for the excessive yapping and bad english, im from brazil