44 const bool next=matchingRendererReady && !simulator;
45 const bool changed=params.
organic!=next;
49 inline float Saturate(
float x) {
return (std::max)(0.f, (std::min)(1.f, x)); }
50 inline float Lerp(
float a,
float b,
float t) {
return a + (b-a)*t; }
63 return 1.f-
Smooth((altitudeAboveTop+.5f)/1.5f);
68 return std::sqrt(1.f-
Saturate(altitude/(6.f*scaleHeight)));
72 float visibilityKm,
float localCloudOpacity)
80 const float medium=
Lerp(air,1.f,
Saturate(localCloudOpacity));
81 const float height=(std::max)(0.f,altitude-deckTop);
82 return medium*std::exp(-height/scaleHeight)*(1.f-
Smooth((altitude-4.f*scaleHeight)/(2.f*scaleHeight)));
90 return {
Lerp(skyLight.
x*.5f,cloudLight.
x,cloud),
91 Lerp(skyLight.
y*.5f,cloudLight.
y,cloud),
Lerp(skyLight.
z*.5f,cloudLight.
z,cloud)};
98 if (!std::isfinite(elapsed) || elapsed<=0)
return;
102 const auto advance=[&](
float offset,
float speed) {
103 return std::isfinite(speed) ? float(std::remainder(
double(offset)+speed*2.1*elapsed,6400.0)) : offset;
109 float base,
float activity,
Point observedWind,
double elapsed,
bool reset =
false)
117 params.
base=(std::min)(8.5f,base);
120 else if (std::isfinite(elapsed) && elapsed>0)
122 deckWind=observedWind;
124 Advect(params,deckWind,elapsed);
126 inline float ScreenOpacity(
bool volumeEnabled,
float cloudOpacity,
float overrideOpacity)
130 return volumeEnabled && overrideOpacity<0 ? 0.f : cloudOpacity;
136 float visibleSun,
float transmission)
140 return volumeEnabled || altitudeAboveTop>0 ?
Saturate(visibleSun)*
Saturate(transmission) : 0.f;
142 inline float AtmosphericVisibility(
float surfaceVisibility,
float altitude,
float surfaceAltitude,
float clearAltitude)
145 const float surface=(std::max)(0.f,(std::min)(clearAltitude-.01f,surfaceAltitude));
164 const auto rgb=lighting.
pixels[index];
165 const float r=rgb.x,g=rgb.y,b=rgb.z;
173 static constexpr Point normals[]={{0,-1,0},{1,0,0},{-1,0,0},{0,1,0},{0,0,-1},{0,0,1}};
174 static constexpr Point east[]={{1,0,0},{0,1,0},{0,-1,0},{-1,0,0},{1,0,0},{1,0,0}};
175 static constexpr Point down[]={{0,0,1},{0,0,1},{0,0,1},{0,0,1},{0,-1,0},{0,1,0}};
176 static constexpr int centerU[]={1019,3057,-1019,5095,1019,1019};
177 static constexpr int centerV[]={1019,1019,1019,1019,-1019,3057};
178 if (cube<0 || cube>5)
return {};
179 return {normals[cube],
Scale(east[cube],1019.f/64),
Scale(down[cube],1019.f/64),
180 (centerU[cube]-u+31.5f)/64,(centerV[cube]-v+31.5f)/64};
183 inline uint8_t
Voxel(
int x,
int y,
int z, uint32_t seed)
185 uint32_t h = seed ^ (uint32_t(x)&63u)*0x8da6b343u
186 ^ (uint32_t(y)&63u)*0xd8163841u ^ (uint32_t(z)&63u)*0xcb1ab31fu;
187 h ^= h >> 16; h *= 0x7feb352du; h ^= h >> 15;
188 h *= 0x846ca68bu; h ^= h >> 16;
189 return uint8_t(h >> 24);
194 const int x =
int(std::floor(p.
x)), y =
int(std::floor(p.
y)), z =
int(std::floor(p.
z));
197 for (
int k=0;k<2;++k)
198 slices[k] =
Lerp(
Lerp(
float(
Voxel(x&mask,y&mask,(z+k)&mask,seed)),
float(
Voxel((x+1)&mask,y&mask,(z+k)&mask,seed)),u),
199 Lerp(
float(
Voxel(x&mask,(y+1)&mask,(z+k)&mask,seed)),
float(
Voxel((x+1)&mask,(y+1)&mask,(z+k)&mask,seed)),u),v);
200 return Lerp(slices[0],slices[1],w)/255.f;
204 inline float TopLobe(
float distance,
float radius)
206 const float cell=distance/radius,cap=
Saturate(1.f-cell*cell);
207 return std::sqrt(cap)*
Smooth(cap/.5f);
211 const float cell=distance/radius,cap=
Saturate(1.f-cell*cell);
224 const float len =
Length(p);
229 const float coarse=
Noise(
Add(
Scale(surface,.01f),{19,7,31}),seed);
230 const Point warp=
Scale({1.5f,-1.1f,.9f},coarse-.5f);
231 const float broad=.65f*
Noise(
Add(
Scale(surface,.04f),warp),seed)+.35f*coarse;
232 const float topBillow=
Noise(
Add(
Scale(surface,.17f),warp),seed);
233 const float billow =
Noise(
Scale(surface,.71f),seed);
237 const float cluster=
Smooth((broad-.25f)/.5f);
238 const float structure=topGrowth*(.35f+.65f*cluster);
245 const float shoulder=
TopLobe(shoulderDistance,.65f);
246 const float crown=
TopLobe(crownDistance,.55f);
247 const float joined=(std::max)(parent,shoulder*(.35f+.45f*parent));
248 const float parent2=parent*parent,parent4=parent2*parent2;
249 const float tower=joined-.3f*(joined-parent4*parent4)*(1.f-crown);
250 const float shallow=
ShallowLobe(shoulderDistance,.9f);
251 const float topLobe=.35f*shallow+.65f*
ShallowLobe(crownDistance,.55f)*(.25f+.75f*shallow);
254 const float globalBase=params.
base + (billow-.5f)*(.12f+.24f*growth);
256 const float base=controls.base+(billow-.5f)*(.12f+.24f*
Smooth((controls.turbulence-.2f)/.65f));
260 (std::max)(globalBase+2.5f, params.
top+.7f+cluster*.12f+topBillow*.08f
261 +topLobe*(.4f+.4f*structure)
262 +topGrowth*(cluster*8.2f*tower+topBillow*(.5f+1.5f*cluster))),
263 weather,structure,controls.turbulence};
272 return (broad*1.4f+fineBillow*.35f+(1.f-detail)*.04f+(1.f-fine)*.01f)*(.18f+.40f*structure);
275 inline float Pocket(
float detail,
float fine)
277 return Smooth((.35f*detail+.65f*fine-.32f)/.36f);
285 const float body =
Noise(
Scale(advected,.71f),seed);
286 const float detail =
Noise(
Scale(advected,2.17f),seed);
287 const float fine =
Noise(
Scale(advected,6.13f),seed);
288 const float topRawErosion =
TopErosion(advected,detail,fine,column.structure);
290 *(.12f+.24f*
Smooth((column.weather-.2f)/.65f));
291 const float available=(std::max)(.01f,column.top-(std::max)(params.
top+.65f,globalBase+1.3f));
292 const float erosion=available*topRawErosion/(available+topRawErosion);
294 if (altitude<column.base+.8f)
298 const float rawErosion=(1.f-detail)*.16f+(1.f-fine)*.12f
299 +(1.f-
Noise(
Scale(advected,19.17f),seed))*.03f
300 +(1.f-
Noise(
Scale(advected,53.71f),seed))*.01f;
301 const float growth=
Smooth((column.baseWeather-.2f)/.65f);
304 const float baseErosion=.16f+(rawErosion-.16f)*(.3f+.4f*growth);
305 bottom=
Smooth((altitude-column.base-baseErosion)/.32f);
307 const float top =
Smooth(((std::max)(params.
top+.65f,column.top-erosion)-altitude)/.65f);
310 const float contrast=
Lerp(1.f,.35f+.65f*column.structure,
Smooth((altitude-column.base-.8f)/1.2f));
311 const float bodyDensity=
Lerp(.75f,
Saturate(.3f+body*.65f+detail*.25f),contrast);
312 const float d=bottom*top*bodyDensity*(.65f+column.weather*.35f)*(.5f+.5f*column.weather);
318 const float core=
Smooth((altitude-column.base-.3f)/.5f)*
Smooth((column.top-altitude-.3f)/.7f);
319 return {d,(std::min)(100.f,d*24.f*(1.f+9.f*pocket*pocket*pocket*core)),pocket};
334 if(sample.density<=0)
return {};
339 return {sample.density,sample.extinction,pocket};
359 const float radius=
Length(p),altitude=radius-params.
radius;
361 const float cosine=(std::max)(.15f,(p.
x*towardSun.
x+p.
y*towardSun.
y+p.
z*towardSun.
z)/radius);
362 const float start=(std::max)(0.f,(column.base-altitude)/cosine);
363 const float span=(std::max)(.5f,(column.top-(std::max)(column.base,altitude))/cosine);
364 const float step=span/8;
365 float opticalDepth=0;
368 opticalDepth+=
Density(
Add(p,
Scale(towardSun,start+(i+.5f)*step)),params,seed)*step*.7f;
370 return std::exp(-opticalDepth);
376 return 7.5f/(.5f*std::exp(-nominalTop/7.5f));
382 return 1.f-
Smooth((sunCosine+.104528f)/.156864f);
397 const float attenuation=.06f+.94f*
Saturate(transmission);
398 return Lerp(attenuation,std::pow(attenuation,1.f/2.2f),
Smooth((sunCosine-.05f)/.20f));
405 float altitude,
float base,
float top)
407 const float height=
Saturate((altitude-base)/(std::max)(1.f,top-base));
409 *(.15f+.85f*std::exp(-.125f*(altitude-9)*(altitude-9)))*
Lerp(1.f,.04f,
Smooth(height));
410 const auto channel=[&](
float light,
float glow) {
413 return (std::max)(
Saturate(light),glow*emission);
415 return {channel(scattered.
x,airglow.
x),channel(scattered.
y,airglow.
y),channel(scattered.
z,airglow.
z)};
423 const float distance=
Length(direction);
424 if (distance<=.00001f)
return 1;
425 direction=
Scale(direction,1/distance);
426 const double eyeLength=
Length(from);
427 const double b=double(from.
x)*direction.
x+double(from.
y)*direction.
y+double(from.
z)*direction.
z;
428 const auto sphere=[&](
double radius,
double& nearHit,
double& farHit) {
429 const double discriminant=b*b-(eyeLength-radius)*(eyeLength+radius);
430 if (discriminant<0)
return false;
431 const double root=std::sqrt(discriminant);
432 nearHit=-b-root; farHit=-b+root;
return true;
434 double nearHit,farHit;
436 float start=float((std::max)(0.0,nearHit)),end=float((std::min)(
double(distance),farHit));
438 if (sphere(inner,nearHit,farHit))
440 if (eyeLength<inner) start=(std::max)(start,
float(farHit));
441 else if (nearHit>0) end=(std::min)(end,
float(nearHit));
443 if (end<=start)
return 1;
444 float opticalDepth=0,step=.025f;
448 for (
int i=0;i<512 && start<end;++i)
450 const float ds=(std::min)((std::max)(step,(end-start)/(512-i)),end-start);
452 if (opticalDepth>12)
return 0;
453 start+=ds; step=(std::min)(.5f,step*1.12f);
455 return std::exp(-opticalDepth);
SCRIPT_CONTROL SCRIPT_STATE const void int
bool Active(const Frame &frame)
Controls Sample(const Frame &frame, Point p, float altitude, float top, float base, float turbulence)
float Sample(float x, float y, float z)
Column SurfaceColumn(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
Sample Evaluate(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed, const Region *prepared=nullptr)
float Weight(const Parameters ¶ms)
float DiffuseSunDisplayAttenuation(float transmission, float sunCosine)
Column LegacyBoundaries(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
uint8_t Voxel(int x, int y, int z, uint32_t seed)
GroundProjection ProjectGround(int cube, int u, int v)
constexpr float MaxTopRiseKm
float Lerp(float a, float b, float t)
Point Scale(Point p, float s)
float AtmosphericHazeDistanceKm(float nominalTop)
float BackgroundZenithWeight(float altitude, const Parameters ¶ms)
float AirglowVisibility(float sunCosine)
Point Add(Point a, Point b)
float AtmosphereWeight(float altitude, float scaleHeight)
void UpdateLocalWeather(Parameters ¶ms, Point &deckWind, bool belowDeck, float base, float activity, Point observedWind, double elapsed, bool reset=false)
void Advect(Parameters ¶ms, Point physicalWindKms, double elapsed)
CloudBaseWeather::Controls BaseControls(Point p, const Parameters ¶ms, float weather)
Point VehicleAmbient(Point cloudLight, Point skyLight, float altitudeAboveTop)
float Noise(Point p, uint32_t seed=DefaultSeed, int mask=63)
constexpr unsigned MaxHeight
constexpr float ShiplightScatterDistanceKm
constexpr uint32_t DefaultSeed
float AmbientWeight(float altitudeAboveTop)
constexpr float ClearVisibilityKm
bool SelectOrganicMode(Parameters ¶ms, bool matchingRendererReady, bool simulator)
float Transmittance(Point from, Point to, const Parameters ¶ms, uint32_t seed=DefaultSeed)
GroundLightTexel GroundLightAt(const GroundLighting &lighting, size_t index)
float DiffuseSunTransmission(Point p, Point towardSun, const Parameters ¶ms, uint32_t seed=DefaultSeed)
float ShiplightScatterWeight(float altitude, float deckTop, float scaleHeight, float visibilityKm, float localCloudOpacity)
Column Boundaries(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
Medium SampleMedium(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
float ShallowLobe(float distance, float radius)
float Pocket(float detail, float fine)
float MinimumBase(const Parameters ¶ms)
float AtmosphericVisibility(float surfaceVisibility, float altitude, float surfaceAltitude, float clearAltitude)
constexpr unsigned MaxWidth
Medium LegacySampleMedium(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
float Density(Point p, const Parameters ¶ms, uint32_t seed=DefaultSeed)
float SunGlareStrength(bool volumeEnabled, float altitudeAboveTop, float visibleSun, float transmission)
float RainDensity(const Medium &medium)
float ScreenOpacity(bool volumeEnabled, float cloudOpacity, float overrideOpacity)
float BaseLightingWeight(Point eye, const Parameters ¶ms, uint32_t seed=DefaultSeed)
float TopErosion(Point advected, float detail, float fine, float structure)
float TopLobe(float distance, float radius)
Point ScatteredSunlight(Point incident, float sunCosine, float transmission)
Point WithAirglow(Point scattered, Point airglow, float sunCosine, float altitude, float base, float top)
float Sample(const MapPtr &map, float u, float v)
std::array< Point, 64 *64 > pixels
std::array< float, 64 *64 > sourceAltitudeKm
CelestialEclipse::Sphere eclipse
CloudWeather::Frame weather
CloudBaseWeather::Frame localBase