Rise
The Vieneo Province
Loading...
Searching...
No Matches
CelestialEclipse.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cmath>
4
6{
7 using std::abs;
8 using std::acos;
9 using std::asin;
10 using std::atan2;
11 using std::clamp;
12 using std::max;
13 using std::sqrt;
14 #include "CelestialEclipse.fxh"
15
16 struct Point { float x=0, y=0, z=0; };
17 struct Sphere
18 {
20 float radius=0;
22 };
23 // Existing solar-core scale used by the sun visibility query, relative to
24 // the billboard's half-width. Keep its glow out of the occulting disk.
25 constexpr float SolarCoreScale = .164f;
26 inline float SolarAngularRadius(float billboardScale, float billboardDistance)
27 {
28 if (!(billboardScale>0) || !(billboardDistance>0)
29 || !std::isfinite(billboardScale) || !std::isfinite(billboardDistance)) return 0;
30 return std::atan(SolarCoreScale*billboardScale/billboardDistance);
31 }
32 inline Sphere Configure(bool simulator, Point center, float radius, float solarRadius)
33 {
34 if (simulator || !(radius>0) || !(solarRadius>0) || !std::isfinite(radius)
35 || !std::isfinite(solarRadius) || !std::isfinite(center.x)
36 || !std::isfinite(center.y) || !std::isfinite(center.z)) return {};
37 return {center,radius,solarRadius};
38 }
39 inline float Visibility(const Sphere& body, Point point, Point towardSun)
40 {
41 if (body.radius<=0) return 1;
42 const float length2=towardSun.x*towardSun.x+towardSun.y*towardSun.y+towardSun.z*towardSun.z;
43 if (!(length2>0) || !std::isfinite(length2) || !std::isfinite(point.x)
44 || !std::isfinite(point.y) || !std::isfinite(point.z)) return 1;
45 const float inverse=1/std::sqrt(length2);
46 return EclipseVisibility(body.center.x-point.x,body.center.y-point.y,body.center.z-point.z,
47 towardSun.x*inverse,towardSun.y*inverse,towardSun.z*inverse,body.radius,body.sunAngularRadius);
48 }
49 // Conservative bound for a sphere centered on the render-world origin.
50 // Return -1 when its samples need individual eclipse evaluation. Most
51 // frames cannot eclipse any clouds, so their shaders take a uniform branch.
52 inline float UniformVisibility(const Sphere& body, float bound, Point towardSun)
53 {
54 if (body.radius<=0) return 1;
55 const float length=std::sqrt(towardSun.x*towardSun.x+towardSun.y*towardSun.y+towardSun.z*towardSun.z);
56 if (!(length>0) || !std::isfinite(length)) return 1;
57 const auto p=body.center;
58 const float distance=std::sqrt(p.x*p.x+p.y*p.y+p.z*p.z);
59 if (distance+bound<=body.radius) return 0;
60 if (distance<=body.radius+bound) return -1;
61 const Point s{towardSun.x/length,towardSun.y/length,towardSun.z/length};
62 const float along=p.x*s.x+p.y*s.y+p.z*s.z;
63 if (along+bound<=0) return 1;
64 if (along<=bound) return -1;
65 const float x=p.y*s.z-p.z*s.y,y=p.z*s.x-p.x*s.z,z=p.x*s.y-p.y*s.x;
66 const float across=std::sqrt(x*x+y*y+z*z);
67 const float solarExtent=(distance+bound)*body.sunAngularRadius;
68 if (across-bound>=body.radius+solarExtent) return 1;
69 if (across+bound+solarExtent<=body.radius) return 0;
70 return -1;
71 }
72}
constexpr float SolarCoreScale
float UniformVisibility(const Sphere &body, float bound, Point towardSun)
float SolarAngularRadius(float billboardScale, float billboardDistance)
float EclipseVisibility(float x, float y, float z, float sx, float sy, float sz, float bodyRadius, float sunRadius)
Sphere Configure(bool simulator, Point center, float radius, float solarRadius)
float Visibility(const Sphere &body, Point point, Point towardSun)