Skip to content

Commit

Permalink
use czm_tanh()
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrgags committed Jan 18, 2024
1 parent 7adf61d commit 6cefbe1
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions packages/engine/Source/Shaders/AtmosphereCommon.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ const float ATMOSPHERE_THICKNESS = 111e3; // The thickness of the atmosphere in
const int PRIMARY_STEPS_MAX = 16; // Maximum number of times the ray from the camera to the world position (primary ray) is sampled.
const int LIGHT_STEPS_MAX = 4; // Maximum number of times the light is sampled from the light source's intersection with the atmosphere to a sample position on the primary ray.

/**
* Rational approximation to tanh(x)
*/
float approximateTanh(float x) {
float x2 = x * x;
return max(-1.0, min(+1.0, x * (27.0 + x2) / (27.0 + 9.0 * x2)));
}

/**
* This function computes the colors contributed by Rayliegh and Mie scattering on a given ray, as well as
* the transmittance value for the ray.
Expand Down Expand Up @@ -65,7 +57,7 @@ void computeScattering(
float x = 1e-7 * primaryRayAtmosphereIntersect.stop / length(primaryRayLength);
// Value close to 0.0: close to the horizon
// Value close to 1.0: above in the sky
float w_stop_gt_lprl = 0.5 * (1.0 + approximateTanh(x));
float w_stop_gt_lprl = 0.5 * (1.0 + czm_approximateTanh(x));

// The ray should start from the first intersection with the outer atmopshere, or from the camera position, if it is inside the atmosphere.
float start_0 = primaryRayAtmosphereIntersect.start;
Expand All @@ -77,7 +69,7 @@ void computeScattering(
// (1) from outer space we have to use more ray steps to get a realistic rendering
// (2) within atmosphere we need fewer steps for faster rendering
float x_o_a = start_0 - ATMOSPHERE_THICKNESS; // ATMOSPHERE_THICKNESS used as an ad-hoc constant, no precise meaning here, only the order of magnitude matters
float w_inside_atmosphere = 1.0 - 0.5 * (1.0 + approximateTanh(x_o_a));
float w_inside_atmosphere = 1.0 - 0.5 * (1.0 + czm_approximateTanh(x_o_a));
int PRIMARY_STEPS = PRIMARY_STEPS_MAX - int(w_inside_atmosphere * 12.0); // Number of times the ray from the camera to the world position (primary ray) is sampled.
int LIGHT_STEPS = LIGHT_STEPS_MAX - int(w_inside_atmosphere * 2.0); // Number of times the light is sampled from the light source's intersection with the atmosphere to a sample position on the primary ray.

Expand Down

0 comments on commit 6cefbe1

Please sign in to comment.