Polygon Planet: Post 8 - Crescent moon
In a previous video I mentioned that I wasn't quite happy with the moon in the game.
The reason for this was due to something that I noticed when I was out driving to the shops on a fairly crisp cold morning and the sky was a beautiful clear blue with just a few white clouds. The moon was clearly visible that morning and about half of it was visible as white as the fluffy clouds but the unlit part was not visible at all. It was as if the unlit part was transparent. All that could be seen was the blue atmosphere where the grey or black of the unlit surface should have been.
Up to this point the moon in my game was always fully visible both the lit and the unlit areas but always at all times a complete shaded sphere.
Custom moon shader...
So i set about attempting to change this by making my own shader graph for the moon that would set the unlit region transparent.
My logic was initially intended to be - obtain the moon gameobject surface light intensity and where it is below a certain threshold value switch the alpha of the master node fully off.
However, in the shader graph there was no node that could provide the light intensity of the gameobject surface. I tried the Scene color node instead and got the beginnings of something promising only to realise that this was measuring the color of the background skybox in the scene and not the moon gameobject which explained why it wasn't working as I had expected.
I the end I changed the logic to be to calculate the light intensity myself using the dot product of the light direction and the normal of the moon at the pixel under consideration. This worked like a charm and is shown in the video below.
The final effect using the custom shader.
My custom moon shader
All fogged up... (camera stacking to the rescue)
One thing that had me confused for quite a while was that the moon's colour was actually being totally overpowered by the environment fog. The fog in my environment is set to increase in density in proportion to the distance from the camera exponentially squared. Whilst this gives a great visual effect for the landscape the problem was that anything along way away becomes mid grey - the colour of my dense fog. Thus the moon, being an exceedingly far object was always appearing as a mid-grey disc with no variation in shading or texture showing.
After googling for solutions it turned out that camera stacking can help with this kind of thing. This was a new concept for me but essentially you can designate secondary cameras to act as overlays on the base (main) camera. Each camera can be selective about what it renders. With this approach I edited the main camera so that it excluded the moon from its rendering. The new overlay camera "MoonCam" was added to render only the moon object but not clear the depth buffer so that it correctly occluded the moon behind the terrain when below the horizon. The new MoonCam was not set to render the layers that environment fog are on and hence did not suffer from being fogged up!!