I've been thinking about this problem, and here's what I've managed to do with it.It works fine with cubes, I haven't tested it on more complex shapes.
Oh my gosh, thank you for posting this! You are a champion, I appreciate you remembering this!