and most of it has little to do with indirect fire.
First, ship cannons fire at far, far longer ranges than antitank guns.
Yes. We call this 'indirect fire'. Two battleships could duke it out with an island between them and never even touch the island, barring some freak circumstance where shells collide in mid-air. There's this little thing called "the horizon" that is between a ships guns and what a ship can actually fire at. It's not direct fire, that's for sure. Even if in theory the range can close enough for direct fire to become a thing (Guadalcanal anyone?).
And you're right, it's not the *only* reason, but it puts pretty severe limits on the velocity of striking rounds. Larger, slower rounds aren't great at penetration unless they have a HEAT warhead (admittedly, the idea of a 16in HEAT round scares me). Throw in the fact that they're really just HE filled murder balls, and you get even less penetration. It's a bludgeon that explodes. Compare to HEAT rounds, Explosively Formed Penetrators, and APFSDS rounds. They each do the same thing, just somewhat differently. Hit a small area with a shit ton of force. What you need to kill/mission kill a tank is a great deal less than what you need to do so to a ship, hence a MUCH greater primacy on filling that shell with explosives, instead of just punching a hole through a ship.
Second, most naval guns in antitank calibers were not given armor-piercing ammunition.
Because there is no point to it. First, the extreme ranges, as we've both pointed out, would make it pointless. And second, as I've pointed out multiple times, because AP rounds are stupid for anti-ship duties.
Third, and this is unrelated to the above point, but I take serious exception to saying ship cannons have "incredibly shit penetration".
They
do, compared to tanks, because they have different roles. It's an unfair comparison though, because it's comparing mostly HE rounds with a hardened cap to mostly AP rounds with a bit of explosive filler. They're simply designed for different purposes.
The American 5"/51, for instance, firing AP ammo could penetrate 4" of armor - at 3200 yards, longer than the effective range of most antitank guns.
That's...not particularly impressive for that kind of ridiculously large cannon. For easy reference to tanks, a 5" cannon is a 127mm cannon. A 127mm HEAT round would go through even more. For example, a 122mm HEAT round that the Soviets used would do 200mm of armor (Or a little bit under 8 inches) at...well, any range you could hit a target at. Admittedly, the platform had low odds of hitting outside 2000m. The Soviet L/46 100mm gun could punch an AP round through 96mm, or just under 4 inches of armor. At 2500m. That 5"/51 is out performing tank cannons on range, and that's about it. Even then, there's a huge problem of finding 3200m of ground with nothing to obstruct direct fire. Your target really fucked up if they let themselves be caught in the open for that.
Minor addendum, these numbers are for armor with a 30 degree slope, so you can add....15%ish to those penetration numbers. And keep in mind, these are much more limited platforms. Ships are simply bad at AP compared to tanks, because penetration is less important. It was the design trade off. The hilariously ridiculously over-penetration power of an 88KwK on a Sherman didn't make the round less effective after all.
Which makes not using this readily available weapon for a heavy assault gun or a heavy tank project a bit of a puzzle to me
Let me solve that puzzle. A 37mm AT gun was sufficient for most enemy tanks until the Cats showed up. 57mm AT guns could actually handle those for the most part. Despite running jokes about the Sherman, the cannon on the Sherman can, and did, penetrate the Tiger's armor from the front. We simply had no need for a super heavy tank or assault gun. Slapping a 127mm cannon on a pair of treads would be more about public masturbation than fulfilling an actual military need.