I think there's a software route you can go, but I went the hardware route. Essentially requires opening up the iMac, gutting out all the internals, and replacing it with a display driver board (from AliExpress, eBay, etc).
The display driver board has various inputs like HDMI, Thunderbolt, USB-C etc so it ends up acting like any other external monitor. It has a remote you control the brightness/contrast etc settings.
I've got my Mac and PS5 hooked up to it. Love it!