XaiJu
pavelsevecek
pavelsevecek

patreon


Depths and Shadows

Recent updates have made the real-time renderer much more realistic, thanks to improvements like the surface smoothing, specular textures, and gas particles. However, there are still issues that sometimes cause strange artifacts. The two biggest offenders are shadows and depth buffer precision. I would like to improve both in the next version.

Logarithmic depth buffer  

The real-time renderer is a standard OpenGL rasterizer, rendering particles as quads and handling their mutual occlusion using a depth buffer. By default, the depth buffer is linear, which is not well suited for the perspective projection - the resolution drops with distance as 1/z, which means particles further away from the camera are poorly resolved, causing visual artifacts known as z-fighting. You might have noticed that if you zoomed in very close to a small object.

While the resolution is always going to be limited, the rendering can be significantly improved by using a transform that moves the particles in the z-direction (in camera coordinates), so that the resolution is independent of the distance from the camera. This leads to the idea of the logarithmic depth buffer.

Each rendered fragment (or pixel) gets assigned a new depth according to the formula:

logDepth = log2(1 + linearDepth / nearPlane) / log2(1 + farPlane / nearPlane)

where nearPlane and farPlane are projection parameters of the camera.

This needs to be done consistently for all shaders (solid and gas particles, rigid objects, trajectories, ...), in order for depth testing to work properly.

The logarithmic depth buffer should (hopefully) solve all depth-related artifacts and allow to properly render things like a spaceship orbiting a star.

Insufficient precision of the linear depth buffer leads to all sorts of shading artifacts.

Logarithmic depth buffer gives a much better result. 

Shadow mapping

The current shadow system was written a long time ago, back when there were only spherical objects in the simulation. It gives reasonable results for planets and stars with little or no deformation, but becomes impractical for irregular shapes, rings, etc. It works as follows:

1. Find all large bodies in the simulation (by default bodies with >5% of the total particle count).

2. For each body, compute its occlusion sphere, given by the center of mass and effective radius of the body.

3. When computing the pixel color, trace a ray from the current world-space position towards the light source, and find the intersection of the ray with all occlusion spheres. 

If there is at least one intersection, the pixel is shadowed.

The problem with this solution is that it's too restricting, it cannot cast realistic shadows from asteroids and rings, and creates distracting artifacts when two objects collide. It also does not scale, the number of occluders has to be small for performance reasons. It's not possible to cast shadows from thousands of small objects.

Therefore, I replaced the spherical occluders with a new solution based on shadow mapping. This is a standard method used frequently in games and other 3D graphical applications, so I won't go into much detail here. It can be summarized as:

1. Render a depth texture of all particles and rigid objects in the simulation from the point of view of the light source, i.e. using orthographic projection.

2. When computing the pixel color, project the pixel world-space position using the same orthographic projection and calculate its depth.

3. If the depth of the pixel matches the depth stored in the depth texture, this pixel can be 'seen' from the light and is therefore illuminated, otherwise it's shadowed.

This method immediately gives much more realistic results, see the examples below.

Current version, objects always cast spherical shadows.

 

Improved solution using shadow maps 

 

The old system works well for spherical Earth, but the ring does not cast any shadows.

 

Shadow mapping allows the ring to cast a shadow onto Earth.

 

That's it for now. Stay tuned for more updates!

Depths and Shadows Depths and Shadows

More Creators