Creating varied terrain (SOLVED)

Say I’m trying to create a side-on endless runner type game. Can anyone think of a good way to do the undulating terrain similar to the one in this mockup?

Making the actual height profile is easy enough - just a bunch of sine waves combined. But how to actually draw that in Defold without creating a game object for each vertical line of pixels of the screen.

Suggestions appreciated :slightly_smiling_face:

2 Likes

Without knowing much about your needs, here’s a few ideas:

  • If you have a bunch of sine functions that generate the height data, why not render a fullscreen quad and use those functions in a fragment shader that outputs color if the current pixel UV.y is below the value from your height function
  • Lets say you have a texture with the starting height data. When you want to add a new column, start by copying the texture x columns to the left where x is the number of columns you want to shift in. I would use a rendertarget and a scratch texture with the same sizes.
    • Set the scratch texture as target to write to.
    • Set the texture with the current height data as input to a fragment shader (or material I guess)
    • Render a quad / sprite over the rendertarget and sample pixels with a UV offset that corresponds to the amount of pixels you are shifting in.
    • You should have a shifted copy of the height data now
    • Render a sprite per incoming column that is placed on the column you want new height data. The sprite dimensions should be one pixel width and whatever height you want.
    • Use the texture from the write RT in a shader that renders it to a fullscreen quad.
    • Next frame, switch the read and write textures so you copy from the updated data.
  • Upload your height map pixel data directly to the texture when you need to (with: https://www.defold.com/ref/resource/#resource.set_texture:path-table-buffer) and then render a fullscreen quad with this texture attached

Looks like a fun project! :slight_smile:

6 Likes

Could probably be done with just the quad + setting size in the material + sending time/offset to shader + a few sine waves in the fragment shader.

If same functions were used in the fragment shader and the game script then both can run at same time to display and use the same ground geometry.

5 Likes

Thanks for the ideas! Got a simple prototype up and running this morning.

7 Likes

Cool! Seems to work :slight_smile:

2 Likes