ebruneton / precomputed_atmospheric_scattering

This project provides a new implementation of our EGSR 2008 paper "Precomputed Atmospheric Scattering".
BSD 3-Clause "New" or "Revised" License
922 stars 121 forks source link

Catastrophic cancellation errors in the intersection tests #21

Closed toastedcrumpets closed 5 years ago

toastedcrumpets commented 6 years ago

Hi, I was playing around with this code in a VR rendering and I noticed that I get artifacts if I set the scale (kLengthUnitInMeters) to be 1m (to give realistic scales for earth). From experience, these artifacts seem to be from catastrophic cancellation in your shader routines when you calculate the intersection distance using the quadratic formula.

I'm currently pulling together a patch set to fix this, but I'm posting this issue first to check if you have any comment about this: i.e, are you already aware of this and is there something more that needs work that I've overlooked?

toastedcrumpets commented 6 years ago

OK, after a play around I'm a bit stumped. I can see you already worked around cancellation when calculating the discriminant, but I seem to have some numerical error implementing the second quadratic formula. I'll play around some more, but if you can shed any insight it would be welcome

ebruneton commented 6 years ago

You can leave kLengthUnitInMeters to 1000.0 to avoid artifacts, and use a different length unit for your scene. In this case, you just need to convert from your scene unit to kilometers when calling the atmosphere functions (GetSkyRadiance, GetSkyRadianceToPoint and GetSunAndSkyIrradiance - for the 'camera', 'p' / 'point' and 'shadow_length' parameters).

toastedcrumpets commented 6 years ago

Thank you for the idea! I'll give it a test later this week when I'm back working on VR and report.