Skip to content

Commit

Permalink
fix artifacts (cloud lines, sunset lines, mie weirdness)
Browse files Browse the repository at this point in the history
  • Loading branch information
wellcaffeinated committed Feb 4, 2024
1 parent 58e2685 commit 1d318de
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/components/Renderer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
bloom = new SelectiveBloomEffect(scene, camera, {
intensity: $bloomIntensity,
luminanceThreshold: 1.2, //0.5,
luminanceThreshold: 1.5, //0.5,
luminanceSmoothing: 0.7,
blendFunction: BlendFunction.ADD,
radius: .9, //.99,
Expand Down
6 changes: 3 additions & 3 deletions src/scenes/Space.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@
<T.Mesh
visible={$skyVisible}
scale.x={AU}
scale.y={AU}
scale.z={AU}
scale.x={$planetRadius + $atmosphereThickness}
scale.y={$planetRadius + $atmosphereThickness}
scale.z={$planetRadius + $atmosphereThickness}
renderOrder={2}
>
<T.IcosahedronGeometry args={[1, 32]} />
Expand Down
58 changes: 30 additions & 28 deletions src/shaders/sky/fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ float miePhase(float mu, float g){
// phases
float mumu = mu * mu;
float gg = g * g;
return THREE_OVER_8_PI * (pow(1.0 - gg, 2.)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5));
// return THREE_OVER_8_PI * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));
// return THREE_OVER_8_PI * (pow(1.0 - gg, 2.)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5));
return THREE_OVER_8_PI * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));
}

vec2 getPhases(float mu, float g){
Expand All @@ -199,8 +199,8 @@ vec2 getPhases(float mu, float g){
float gg = g * g;
float rayleighP = THREE_OVER_16_PI + THREE_OVER_16_PI * mumu;
// alternate mie that may be better
float mieP = THREE_OVER_8_PI * (pow(1.0 - gg, 2.)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5));
// float mieP = THREE_OVER_8_PI * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));
// float mieP = THREE_OVER_8_PI * (pow(1.0 - gg, 2.)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5));
float mieP = THREE_OVER_8_PI * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));
return vec2(rayleighP, mieP);
}

Expand Down Expand Up @@ -422,26 +422,27 @@ vec4 scattering(
pos += dr;
vec2 odStep = opticalDensity(length(pos), scaleHeights, planetRadius) * ds;
primaryDepth += odStep;
vec2 intPlanet2 = raySphereIntersection(pos, sSun, planetRadius);
// if the ray intersects the planet, no light comes this way
if (!intersectsOutside(intPlanet2)) {
float u = umbra(pos, rApparentSun, sunPosition, rApparentMoon, moonPosition);
// u = intersectsOutside(intPlanet2) ? 0.0 : u;

vec2 intAtmosphere2 = raySphereIntersection(pos, sSun, atmosphereRadius);
vec3 exit = pos + intAtmosphere2.y * sSun;

vec2 secondaryDepth = opticalDepths(pos, exit, scaleHeights, planetRadius, steps.y);
// vec2 secondaryDepth = getOpticalDepths(pos, normalize(exit - pos), scaleHeights, planetRadius, atmosphereRadius);
// vec2 secondaryDepth = vec2(0.0);
vec2 depth = primaryDepth + secondaryDepth;
vec4 alpha = vec4(vec3(depth.x), depth.y) * scatteringCoefficients;
vec3 transmittance = exp(- alpha.xyz - alpha.w);
vec3 scatter = u * transmittance;

rayleighT += scatter * odStep.x;
mieT += scatter * odStep.y;
}

// if the ray intersects the planet, no light comes this way...but..
// I tried doing this and it created lines in the atmosphere
// vec2 intPlanet2 = raySphereIntersection(pos, sSun, planetRadius);

float u = umbra(pos, rApparentSun, sunPosition, rApparentMoon, moonPosition);
// u = intersectsOutside(intPlanet2) ? 0.0 : u;

vec2 intAtmosphere2 = raySphereIntersection(pos, sSun, atmosphereRadius);
vec3 exit = pos + intAtmosphere2.y * sSun;

vec2 secondaryDepth = opticalDepths(pos, exit, scaleHeights, planetRadius, steps.y);
// vec2 secondaryDepth = getOpticalDepths(pos, normalize(exit - pos), scaleHeights, planetRadius, atmosphereRadius);
// vec2 secondaryDepth = vec2(0.0);
vec2 depth = primaryDepth + secondaryDepth;
vec4 alpha = vec4(vec3(depth.x), depth.y) * scatteringCoefficients;
vec3 transmittance = exp(- alpha.xyz - alpha.w);
vec3 scatter = u * transmittance;

rayleighT += scatter * odStep.x;
mieT += scatter * odStep.y;
}

float mu = dot(rayDir, sSun);
Expand All @@ -462,19 +463,20 @@ vec4 scattering(
vec2 cloudInt = raySphereIntersection(rayOrigin, rayDir, planetRadius + cloudHeight);
if (intersectsInsideOnly(cloudInt)){
float cloudPhase = miePhase(mu, cloudMie);
vec3 cloudLayer = cloudSize * 900. * (rayDir * (cloudInt.y + 0.2 * atmosphereThickness * noise(vUv))) / atmosphereRadius;
vec3 cloudLayer = cloudSize * 900. * (rayDir * (cloudInt.y + 0.2 * atmosphereThickness)) / atmosphereRadius;
cloudAmount = cloudThickness * smoothstep(0., 1.0, fbm(cloudLayer + windSpeed * time) - cloudThreshold);
// float cloudAmount = 2. * getPhases(rayDir, sSun, 0.5 + 0.4 * fbm(cloudLayer * 20.)).y;
cloud = 1e-6 * I0 * cloudAmount * cloudPhase * rayleighT;
cloudAbsorptionAmount = cloudAbsorption * cloudAmount;
cloud = 1e-6 * I0 * cloudAmount * cloudPhase * rayleighT;
}
}

// final scattering
vec3 scatter = I0 * rayleighT * phases.x * scatteringCoefficients.xyz + I0 * mieT * phases.y * scatteringCoefficients.w;
vec3 scatter = I0 * rayleighT * phases.x * scatteringCoefficients.xyz + I0 * mieT * (phases.y * scatteringCoefficients.w - cloudAbsorption * 1e-6);
scatter = clamp(scatter, 0.0, I0);
// opacity of the atmosphere
float opacity = dot(primaryDepth, vec2(0.2 * length(rayleighCoefficients), mieCoefficient)) + cloudAbsorptionAmount;
return vec4((1. - cloudAbsorptionAmount) * (scatter + sunDiskColor) + cloud, clamp(opacity, 0.0, 1.0));
return vec4((1. - cloudAbsorptionAmount) * (scatter + sunDiskColor + cloud), clamp(opacity, 0.0, 1.0));
}

void main() {
Expand Down
34 changes: 22 additions & 12 deletions src/store/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,24 @@ export const moonPosition = derived(
.applyAxisAngle($moonAxis, $moonRightAscention)
)

const getMie = (n, k) => {
const getScatteringScale = (n, k) => {
return k * 1e26 * 8 * Math.pow(Math.PI, 3) * Math.pow((n * n - 1), 2) / 3 / 2.547e+25
}

const getMie = (n, k) => {
const a = getScatteringScale(n, k)
return 0.01 * a
}

const getRayleigh = (n, k) => {
const rgb = [680, 510, 440]
const corr = 1e10
const a = getMie(n, k)
const a = getScatteringScale(n, k)
const r = new Vector3(
corr * a * Math.pow(1 / rgb[0], 4),
corr * a * Math.pow(1 / rgb[1], 4),
corr * a * Math.pow(1 / rgb[2], 4)
)
console.log(r, a)
return r
}

Expand All @@ -112,22 +116,28 @@ const print = (val, ...args) => {
}

export const sunIntensity = writable(25)
export const overrideRayleigh = writable(false)
// 5.5e-6, 13.0e-6, 22.4e-6
export const rayleighRed = writable(5.5)
export const rayleighGreen = writable(13)
export const rayleighBlue = writable(22.4)
export const rayleighScaleHeight = writable(8e3)
export const mieCoefficient = writable(4.4e-6)
export const mieScaleHeight = writable(1.2e3)
export const mieDirectional = writable(-0.758)
export const airIndexRefraction = writable(1.0003)
export const airSurfacePressure = writable(101.3e3)
export const airSurfaceTemperature = writable(288)
export const airDensityFactor = derived(
[airSurfacePressure, airSurfaceTemperature],
([$P, $T]) => print((288 / 101.3e3) * ($P / $T), $P, $T)
)
export const overrideRayleigh = writable(false)
// 5.5e-6, 13.0e-6, 22.4e-6
export const rayleighRed = writable(5.5)
export const rayleighGreen = writable(13)
export const rayleighBlue = writable(22.4)
export const rayleighScaleHeight = writable(8e3)
export const mieCoefficient = derived(
[airIndexRefraction, airDensityFactor],
([$n, $air]) => {
const m = getMie($n, $air)
return m //new Vector3(m, m, m)
}
)
export const mieScaleHeight = writable(1.2e3)
export const mieDirectional = writable(0.758)
export const rayleighCoefficient = derived(
[airIndexRefraction, airDensityFactor, overrideRayleigh, rayleighRed, rayleighGreen, rayleighBlue],
([$n, $air, $overrideRayleigh, $rayleighRed, $rayleighGreen, $rayleighBlue]) => {
Expand Down

0 comments on commit 1d318de

Please sign in to comment.