I am creating a day-night cycle for my 2D side scrolling game and I have a big problem. The problem can be best understood if you look at the following image:

enter image description here

Problem is very simple: the moon should always be behind terrain (otherwise it would look weird) and terrain should always be behind a light-map (in order for light-map to draw shadows over the terrain).

However, the moon should also appear in front of the light-map, since at night it shouldn't be affected by the light-map darkness over the terrain (light-map should only apply to the terrain).

Essentially, I have the following situation: A should be behind B, B behind C and C behind A (circular ordering). Is there a trick to solve this problem?

  • 1
    \$\begingroup\$ Have you considered sampling the light map inside the terrain shader, effectively combining those two layers into one, which draws nothing where the terrain is absent/transparent? \$\endgroup\$
    – DMGregory
    Jul 21 at 0:09
  • \$\begingroup\$ That won't work because at night everything should actually be darkened (even on air/transparent tiles). \$\endgroup\$
    Jul 21 at 7:41
  • \$\begingroup\$ You can draw your sky on its own layer (behind the moon) and again apply the darkening in the shader as you draw that layer, rather than layering darkness over-top of the full composited scene. \$\endgroup\$
    – DMGregory
    Jul 21 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.