For wild Pokémon before LGPE, it’s generated when you start the encounter, on the frame the battle music starts. Roamers are an exception, and are generated when you first activate them to start roaming. In LGPE and SwSh, it’s generated when the Pokémon spawns in the overworld.
Gift Pokémon and eggs, for the most part, are generated on the frame the short congratulation music starts along with the “you received [Pokémon/egg]” message. There are a few exceptions to this, but they’re few and far between (one example would be the starters in HGSS, which generates all three when you open the choosing screen).