# SceneKit flickering with Ambient and Omnidirectional lights

I have a project where I have a sphere with a png texture that represents a planet (or a moon in the videos I'm attaching).

Wrapped around that sphere I have a hexasphere (implemented using the results of my question from last year: How to orient a hexagonal tile on a geodesic sphere (Goldberg polyhedron)?).

Almost everything works just fine however I've noticed that in some situations, the hexasphere flickers at some points.

From my experiments, it seems that the problem is the lighting I am using.

The hierarchy of nodes I have is as follows:

bodyContainer    OMNI
planet         OMNI (sphere, radius:0.987 with image texture as it's only (diffuse) material)


There are two light sources, both placed at the centre of the "sun" in my larger model. OMNI is an Omnidirectional light source with default settings. AMBIENT is an Ambient light source.

The reason that "body" uses OMNI is that I want the planet or moon to have a light and dark side which changes as the body orbits the sun.

The hexasphere that is co-located with the body by being a sibling of the body in the same container. It needs to use the AMBIENT light source so that it is equally visible on the dark side of the planet or moon.

What I'm noticing, it that when I move the camera further away from the planet, the parts of the hexasphere closest to the camera seem to flicker, and I seem to be able to see through to some of the hexasphere's cells on the opposite of the sphere.

Here are some videos to show what I'm seeing.

This first shows the scene in it's full 'glory' with all nodes in play as described above:

https://youtu.be/o3hdpuLoSbQ

This next one shows the same hierarchy, however I've changed the planet body to use the AMBIENT light instead. Note that there is no flicker, but also, I've lost the dark/light side of the planet.

https://youtu.be/O0Ca-MrYgJc

In this 3rd video, I've replaced the body texture with a simple translucent blue color, and reverted to using the OMNI light. It looks great, but the flickering is back.

https://youtu.be/ftGWnaqoN5I

Finally, I've changed back to the AMBIENT light again, but kept the blue body texture. Note that the flickering is gone again.

https://youtu.be/c6i5vjOEauw

I'm quite positive that this is something I can fix, but I don't understand yet how. Is this simply a weakness in the lighting mechanisms in SceneKit?

Is there something special I should be doing to allow the outer hexasphere to render cleanly (i.e. without the flickering), with the ambient light, whilst having the inner sphere that is using an omni light?

UPDATE

I've found one solution that appears to work, however I'm not sure if this will result in a lower quality visual overall. If I set the material property litPerPixel to NO, then the problem I've outlined goes away. I'm just not sure that the presumed "lower quality" rendering might cause other problems elsewhere.

• Do you know what your near and far clipping planes are set to on the camera? It looks like it could possibly be related to loss of precision in the depth buffer, although it doesn’t present itself in quite the way I would expect. – Ed Marty Sep 29 '18 at 14:15
• For zNear, I use the default of 1.0. zFar by necessity (owing to this being a solar system I'm trying to represent) is quite large: 649,701.38992. I'm basically trying to ensure that no matter where in the solar system I place the camera, I can see Pluto, and the skybox beyond it. – PKCLsoft Sep 29 '18 at 22:39