Ultra Wormhole Mechanics
You have an energy gauge that starts at 70, out of a maximum of 100. This gauge decays by 1 point every 12 frames (based on a framerate of 30fps, so 0.4 seconds). Collecting an energy refill adds 10 points to the gauge, and hitting an electric orb (referred to internally as an "obstacle") subtracts 20. If you would collect energy to go over the maximum, you get to increase the maximum by 20, but only five times: once you get the maximum gauge up to 200, it's fixed there for the remainder of the ride. Attempting to gain more than that just leaves you capped out at 200. A rule of thumb is that if you count off 4 seconds between one energy refill and the next, you're breaking even; in order to gain energy you have to run into them at a rate faster than once every 4 seconds (and avoid obstacles), which becomes less likely as you go deeper and the refills become less frequent (more on that later).
If your energy meter is empty, your forward speed is only 42% of what it would be at full, and maneuvering yourself in any direction can only be done at one-seventh the speed of a fully energized Solgaleo or Lunala (which is obviously weaker than the gravitational pull exerted by portals). Note that "full" is relative to your current maximum, so 200/200 doesn't make you move any faster than 100/100 (nor does 0/200 move any slower than 0/100), it just means the decay rate takes longer to completely drain the gauge.
The units used in the wormhole, while flavorfully described in-game as "light-years", are internally referred in developer comments as meters (in fact, the unit scale is really in terms of centimeters then scaled down by a factor of 100 later) which is obviously more plausible with respect to the scale everything is drawn at.
The process of placing stuff in the tunnel, meanwhile, is achieved by breaking it up into 15-meter-deep segments. The first segment comes at 50m (it's not possible for anything to be placed sooner than that point), the second segment starts at 65m, the third at 80, and so on. Initially, all the segments up to a depth of 290m are loaded (the only things that can be placed during these segments are energy refills). The next loading zone happens when the player reaches a distance of 155, causing the segment at 305 to load, and from then on every 15m depth causes the segment 150m ahead of the player to load so that there's reasonable lead-in time to react to the stuff that gets generated.
Each segment can generate up to four objects to be placed, though the object won't always be placed
within the segment to which it's attributed. There's a "fudge region" where it's randomly placed in any one of the next
four segments, by adding a depth that can range from 0-60m.
Distance | Odds of Energy | Odds of Obstacle | Odds of Portal |
50-290 | 80% | 0% | 0% |
305-590 | linear 80%→40% | 20% | 0% |
605-995 | linear 40%→30% | linear 25%→30% | 33.3% |
1010-1490 | 30% | 35% | 33.3% |
1505-1985 | 25% | 40% | 33.3% |
2000-2090 | 20% | 200% | 33.3% |
2105-2495 | 20% | 45% | 33.3% |
2510-2585 | 20% | 200% | 33.3% |
2600-2990 | 15% | 50% | 33.3% |
3005-3095 | 12.5% | 200% | 33.3% |
3110-3485 | 12.5% | 55% | 33.3% |
3500-3590 | 10% | 200% | 33.3% |
3605-3995 | 10% | 60% | 33.3% |
4010-4085 | 7.5% | 200% | 33.3% |
4100-4490 | 7.5% | 65% | 33.3% |
4505-4595 | 5% | 200% | 33.3% |
4610-4985 | 5% | 70% | 33.3% |
5000-5090 | 5% | 200% | 33.3% |
5105+ | 0% | 75% | 33.3% |
Where the odds are marked as a linear gradient, the endpoints it's actually interpolating from are 300-600 and 600-1000. So the 305 segment has 79.33% chance to place an energy refill, 320 has 77.33%, 680 has 26% chance to place an obstacle, and so on.
The segments shortly after multiples of 500 between 2000 and 5000 are marked as 200% odds of obstacle; what this really means is a "guarantee" that two separate obstacles will be placed in each segment allotment (so two that can end up in 2000-2059, then separately another two that can end up in 2015-2074...and on to the 2090-2149 range). These "storms" of 12-14 obstacles in rapid succession are thus an entirely deliberate consequence of the way they've set these odds up, and can be used as a reliable barometer of how far along you are as you're flying through the tunnel and watching your surroundings. Other consequences of this system are that
no portal of any kind can appear earlier than a distance of 605, and that
energy refills are completely nonexistent once you reach 5150--after that point, all you can do is trudge on with your remaining, monotonically decreasing energy until it runs out, and hope that you find a desired portal in the mean time.
When the game is placing energy refills or obstacles, it uses a rectangular coordinate system: picture a square with an inscribed circle. Pick a random point within the square, then if that point is also within the circle, that's a good point and will be placed at whatever depth was randomly chosen. If not, try again with another random point. If the game generates 10 random points and all of them fall outside the circle (this has a minuscule chance of happening, worse than missing Lovely Kiss 11 times in a row), then it gives up and places it at the dead center of the tunnel's cross section, wherever that may be as it winds through space.
On the other hand, portals are generated using polar coordinates: imagine the tunnel as a roll of cookie dough, and you slice it up into cookies to represent the segments, then you take a cookie and divide it into four concentric rings of equal width, like an archery target. Then a random point that's guaranteed to be in the outermost ring is chosen as the center-point of the portal. EXCEPTION: If you're still trying to get to Ultra Megalopolis for the first time, white portals will always be placed right at the center of the bulls-eye.
In any event, there are checks to make sure that a newly spawned object isn't "too close" to another currently loaded object. If an object is chosen for a spawn location that's within a specified minimum radius, the object simply won't spawn at all (so the "guaranteed" storms of obstacles won't always generate the full 12-14 as scheduled, some of them might get caught up in the understandably crowded tunnel). There are further checks with regard to two
portals being too close: if the cross-sectional projection of a newly scheduled portal's center is within 0.5% of the width of the circle compared to that of any other currently loaded portal, or if the scheduled depth is within 40m of any other portal regardless of how spaced out their projections are on the cross-section, that's considered "too close" and the portal won't spawn, significantly reducing their effective spawn rate compared to the nominal average rate of one per three segments.
If a portal does spawn, it has to receive a color and rarity. 4% of all portals are white (leading to Ultra Beast worlds), and the rest are divided evenly as 24% to each color. Here again there's a difference when you're playing in story mode and just looking for Ultra Megalopolis: the first time through you get those normal odds, but if you end up hitting a different color instead, you'll have to go back for a second trip (after optionally getting a chance to KO or catch the Pokemon you found there, of course) and on that trip the white portal odds increase to 16%, leaving 21% for each of the other colors. Continued misses will increase the odds further to 32% on the third trip, 44% on the fourth, and finally 100% on the fifth to guarantee that your ride has no choice but to come out in the right destination.
As opposed to color, the rarities of each portal were already datamined a long time back. As a refresher (yes, the odds here reach their final step at 4500, not 5000):
Distance | 0-Ring Odds | 1-Ring Odds | 2-Ring Odds | Bloom Odds |
605-995 | 100% | 0% | 0% | 0% |
1010-1490 | 78% | 20% | 2% | 0% |
1505-1985 | 66% | 30% | 4% | 0% |
2000-2495 | 52% | 40% | 8% | 0% |
2510-2990 | 33% | 50% | 16% | 1% |
3005-3485 | 32% | 50% | 16% | 2% |
3500-3995 | 31% | 50% | 16% | 3% |
4010-4490 | 30% | 50% | 16% | 4% |
4505+ | 29% | 50% | 16% | 5% |
It's worth noting that the odds of portal rarities are set based on the beginning of the segment it was spawned from, even though the variable depth may push it back to a later point. So when segment 2495 gets loaded, it can summon a portal past the 2500 mark, but if it does so, there's no chance for that portal to show up with the "bloom" overlay in addition to the two rings.
On the contrary, what you'll find in Ultra Space isn't determined until you get sucked in, and is based on the actual depth the portal was placed at. For white portals, this roll determines which world you'll be spat out into, while for other colors it controls which Pokemon is set to appear and, if it's not legendary, whether it will be forced shiny or locked out of that. So in the previous example, a spawn from the 2495 segment that gets placed after the 2500 mark, you
would get the uptick in odds (for legend/shiny) that goes with crossing 2500. Those tables have also been datamined before, but here they are again for posterity:
0-Ring Odds
| UB | Portals | | | | Non-UB | Portals |
Distance | Nihilego | Buzzwole/Pheromosa | Xurkitree | Kartana/Celesteela | Guzzlord | Legendary | Shiny (if nonlegendary) |
605-999 | 60% | 35% | 2% | 2% | 1% | 3% | 1% |
1000-1499 | 54% | 39% | 4% | 2% | 1% | 3% | 1% |
1500-1999 | 48% | 43% | 6% | 2% | 1% | 3% | 1% |
2000-2499 | 42% | 47% | 8% | 2% | 1% | 3% | 1% |
2500-2999 | 36% | 51% | 10% | 2% | 1% | 3% | 1% |
3000-3499 | 30% | 55% | 12% | 2% | 1% | 3% | 1% |
3500-3999 | 24% | 59% | 14% | 2% | 1% | 3% | 1% |
4000-4499 | 18% | 63% | 16% | 2% | 1% | 3% | 1% |
4500-4999 | 12% | 67% | 18% | 2% | 1% | 3% | 1% |
5000+ | 6% | 71% | 20% | 2% | 1% | 3% | 1% |
1-Ring Odds
| UB | Portals | | | | Non-UB | Portals |
Distance | Nihilego | Buzzwole/Pheromosa | Xurkitree | Kartana/Celesteela | Guzzlord | Legendary | Shiny (if nonlegendary) |
1000-1499 | 2% | 54% | 39% | 4% | 1% | 4% | 2% |
1500-1999 | 2% | 48% | 43% | 6% | 1% | 5% | 3% |
2000-2499 | 2% | 42% | 47% | 8% | 1% | 6% | 4% |
2500-2999 | 2% | 36% | 51% | 10% | 1% | 7% | 5% |
3000-3499 | 2% | 30% | 55% | 12% | 1% | 8% | 6% |
3500-3999 | 2% | 24% | 59% | 14% | 1% | 9% | 7% |
4000-4499 | 2% | 18% | 63% | 16% | 1% | 10% | 8% |
4500-4999 | 2% | 12% | 67% | 18% | 1% | 11% | 9% |
5000+ | 2% | 6% | 71% | 20% | 1% | 12% | 10% |
2-Ring Odds
| UB | Portals | | | | Non-UB | Portals |
Distance | Nihilego | Buzzwole/Pheromosa | Xurkitree | Kartana/Celesteela | Guzzlord | Legendary | Shiny (if nonlegendary) |
1000-1499 | 0% | 2% | 54% | 39% | 5% | 10% | 3% |
1500-1999 | 0% | 2% | 48% | 43% | 7% | 13% | 5% |
2000-2499 | 0% | 2% | 42% | 47% | 9% | 16% | 7% |
2500-2999 | 0% | 2% | 36% | 51% | 11% | 19% | 9% |
3000-3499 | 0% | 2% | 30% | 55% | 13% | 22% | 11% |
3500-3999 | 0% | 2% | 24% | 59% | 15% | 25% | 13% |
4000-4499 | 0% | 2% | 18% | 63% | 17% | 28% | 15% |
4500-4999 | 0% | 2% | 12% | 67% | 19% | 31% | 17% |
5000+ | 0% | 2% | 6% | 71% | 21% | 34% | 19% |
Bloom Odds
| UB | Portals | | | | Non-UB | Portals |
Distance | Nihilego | Buzzwole/Pheromosa | Xurkitree | Kartana/Celesteela | Guzzlord | Legendary | Shiny (if nonlegendary) |
2500-2999 | 0% | 0% | 0% | 40% | 60% | 100% | 16% |
3000-3499 | 0% | 0% | 0% | 34% | 66% | 100% | 20% |
3500-3999 | 0% | 0% | 0% | 28% | 72% | 100% | 24% |
4000-4499 | 0% | 0% | 0% | 22% | 78% | 100% | 28% |
4500-4999 | 0% | 0% | 0% | 16% | 84% | 100% | 32% |
5000+ | 0% | 0% | 0% | 10% | 90% | 100% | 36% |