Flickering that could be z-fighting (SOLVED)

I’m tearing my hair out here… I’ve got this issue with a background image flickering. It looks kind of like z-fighting, but I can’t seem to pin it down.

This is what it looks like:

I’ll try to summarise the setup as briefly as possible.

  • I’m using a slightly modified version of Orthographic. No huge changes, certainly nothing that I would expect could cause this.

  • I have three components to the background: a nebula (this seems to flicker) and two sets of stars.

  • I achieve the parallax effect by calculating the change in camera position each frame, and adding a portion of that position change to the background image. I make sure to set the z position after doing this.

  • The background images have a custom material. The material is straightforward, ensuring the background images are rendered below everything else.

Hot-reloading a change back to the default sprite material still leaves the flickering happening.

  • At the end of the frame, I print the z positions of the first starfield, the second starfield, the nebula, and the camera:
DEBUG:SCRIPT: 0.5	0	-0.5	0

I can’t see any z issues here.

  • The flickering doesn’t always happen - it seems to be related to the position we are at. I have to fly around to “find” the flickering.

I’m totally stuck on this and can’t figure out what’s causing the issue. Any pointers or ideas? What would you try next?

I might be way off here, but could it be caused by frustum culling?

1 Like

I think @Klear might be correct. quick test you can look in your render script and find render.draw(self.tile_bg, {frustum = frustum}) remove the table - render.draw(self.tile_bg) and see if it helps.

Thank you both, @Klear and & @MasterMind - but you are too clever for my foolishness! :slight_smile:

It was a very good suggestion and I got excited - because I do indeed do some frustum culling and it is applied to the custom material as well. However, disabling it made no difference.

Turns out it was something silly. I didn’t have sufficient copies of the background in the game object, so when I was in the area where a loop is triggered then the game object would flicker back and forth, giving the impression of z-fighting.

It could be that my logic for the infinite background is incorrect. I feel like having 9 copies (3x3) of the image (the ones selected in the screenshot above) should be sufficient for an infinite background, but seems like 25 copies are needed (5x5) in my setup for whatever reason.

2 Likes

You should have a look at this: Example for parallax background with shader

I used that as a base when implementing scrolling background.

2 Likes