Part 8: Creating an Infinite Ocean


Where the sky meets the sea

Up until now the ship in Pirate Sea Jam has been sailing about on a small patch of ocean in the middle of nowhere. The time has come to create an infinite ocean, or at least something that looks more vast than a duck pond, for our brave pirate ship.

Infinite Ocean

The naive way to create an infinite ocean is to increase the overall size of the ocean tile and to subdivide the surface into more and more triangles to maintain a high level of detail. Unfortunately, as straight forward as it might be, that approach is quite wasteful when it comes to computer resources. 

The corner positions for all the triangle that together make up the ocean surface are, among a whole lot of other things, ideally recalculated 60 times per second. With an increasing number of triangles, personal computers and not least mobile devices would quite soon be brought to their knees and the frame rate would plummet. 

To mitigate this performance issue, a grand sea can be divided into multiple sectors that are drawn with different level of detail depending on the distance to the camera . The sector in the centre would have the highest number of triangles per surface area and the outer sectors, farthest away from the camera, would have the lowest level of detail.


The entire ocean is divided into three groups of sectors with different level of detail

The secondary tiles have half the level of detail compared to the primary tile. The tertiary tiles are three times as big as the primary and secondary tiles, but consist of only two large triangles and have the lowest level of detail.

Stiching

Due to the different level of detail gaps are formed with noticeable seams between the different sectors of tiles as the waves are animated.

Gaps

The gap between sector of tiles marked with red

The trick to remove the seam between the primary and secondary sectors is to make sure that every second outer triangle corner of the centre tile is positioned in the middle of the two adjacent outer triangle corners.  By doing so the outer triangles of the primary tile line up nicely with the triangles of the secondary tiles.

No seams

The gap between sector of tiles have been removed

A different approach is used to remove the seam between the secondary and the tertiary sector of tiles. The large outermost tertiary tiles are totally level and not affected by the wave calculations at all. To make the triangles in the secondary sector blend in with the tertiary tiles, the effect of the wave calculation is reduced the farther away from the centre a triangle is positioned. Visually the waves fade out and become stationary and level at the outer edge of the secondary tile to match the flat large tertiary tiles.

Second seam

The waves fade out to blend in seamlessly with the stationary outer tile

The final ingredient in making the ocean infinite, is to make sure the ocean tile configuration travels together with the player ship making sure that the ship is constantly in the centre of primary ocean tile so that the most detailed waves stay in focus the whole time.

Atmospheric fog

Even after optimising by having multiple ocean tiles and diminishing level of detail, the frame rate drops when tiles are large with a high number of triangles. An atmospheric fog has been added to Pirate Sea Jam to hide the flatness of the outer ocean tiles and reduce the need for a huge sea that stretches out into the horizon.

Fog

An atmospheric fog reduce the amount of sea that needs to be drawn

Further optimisations

To further reduce the work load, ocean tiles that are not visible in the camera view port should not have their triangle positions calculated each frame.

Despite all efforts the frame rate has dropped significantly on my old laptop after adding the infinite ocean to Pirate Sea Jam.  A small frame rate counter has been added to the upper left corner of the user interface, which on my computer reports frame rates way below the target 60 frames per seconds. High-end hardware probably don't  show as bad numbers.  (If you don't mind, leave a brief comment about your hardware and reported frame rate.)

But as the saying goes: "The developer with the slowest computer writes the fastest applications." Pirate Sea Jam is really getting slow and painful to play on my computer. Something needs to be done! 

Next up

All wave calculations are currently handled by the Central Processing Unit (CPU). A big speed up would be to offload the CPU and do the wave calculations on the Graphical Processing Unit (GPU), which can handle several order of magnitude more calculations in parallel compared to the CPU. 

Next up is optimisation and putting the GPU to work, I just hope I'm not biting off more than I can chew, as I have limited experience in developing programs that run on the GPU. 

Ship ahoy!

Get Pirate Sea Jam

Leave a comment

Log in with itch.io to leave a comment.