Rise
The Vieneo Province
Loading...
Searching...
No Matches
CloudClearance11.h
Go to the documentation of this file.
1#pragma once
3#include <DirectXPackedVector.h>
4
5namespace Rendering
6{
7 // Conservative bounds for the existing cloud density, not a replacement
8 // density model. Only used to reject a whole short, empty composite ray.
10 {
11 public:
12 void SetNoise(std::vector<uint16_t> values) {noise=std::move(values);}
13 void Reset() {noise.clear();}
15 const CloudWeather::Frame& weather,double angularPixel) const
16 {
17 // The existing proof assumes one base height. Geographic slopes
18 // retain full integration until a matching spatial bound is proven.
20 const double length=std::sqrt(double(eye.x)*eye.x+double(eye.y)*eye.y+double(eye.z)*eye.z);
21 if(noise.size()!=64*64*64 || !std::isfinite(length) || length<1 || length>20000
22 || !std::isfinite(angularPixel) || angularPixel<=0 || !std::isfinite(p.radius)
23 || p.radius<1 || p.radius>10000 || !(p.base>=0 && p.base<=p.top && p.top<100)
24 || !(std::abs(p.noiseOffset.x)<=6400 && std::abs(p.noiseOffset.y)<=6400 && std::abs(p.noiseOffset.z)<=6400)
25 || !(weather.blend>=0 && weather.blend<=1))return 0;
26 const double surface[]={eye.x*p.radius/length+p.noiseOffset.x,
27 eye.y*p.radius/length+p.noiseOffset.y,eye.z*p.radius/length+p.noiseOffset.z};
28 // Fixed bounded CPU work, independent of resolution and MSAA. A
29 // failed proof always retains the original per-sample ray march.
30 for(double candidate:{.5,.125,.03125})
31 {
32 const double reach=candidate+.01; // 10 m guard for shader float coordinates/ray normalization
33 const double minimumRadius=length-reach;
34 if(minimumRadius<=1)continue;
35 const double altitude=length-p.radius;
36 if(altitude+reach<double(p.base)-.18-.01)return float(candidate);
37 // The Jacobian norm of R*normalize(p) is R/length(p).
38 const double delta=p.radius*reach/minimumRadius+.002;
39 const double footprint=angularPixel*reach/ .15*1.4;
40 double lo[3],hi[3];
41 const double offset[]={19,7,31};
42 for(unsigned a=0;a<3;++a){lo[a]=(surface[a]-delta)*.01+offset[a];hi[a]=(surface[a]+delta)*.01+offset[a];}
43 const auto broad=NoiseRange(lo,hi,footprint*.01);
44 const double warp[]={1.5,-1.1,.9};
45 for(unsigned a=0;a<3;++a)
46 {
47 const double wa=(broad.low-.5)*warp[a],wb=(broad.high-.5)*warp[a];
48 lo[a]=(surface[a]-delta)*.04+(std::min)(wa,wb);
49 hi[a]=(surface[a]+delta)*.04+(std::max)(wa,wb);
50 }
51 const auto clustered=NoiseRange(lo,hi,footprint*.06);
52 const double cluster=Smooth((.65*clustered.high+.35*broad.high-.25)/.5);
53 const double weatherUpper=(std::min)(1.,WeatherUpper(weather.previous,eye,length,reach,footprint,p.radius)
54 *(1-weather.blend)+WeatherUpper(weather.current,eye,length,reach,footprint,p.radius)*weather.blend+.00001);
55 const double growth=Smooth((weatherUpper-.2)/.65),topGrowth=Smooth((weatherUpper-.15)/.85);
56 const double structure=topGrowth*(.35+.65*cluster);
57 const double baseUpper=p.base+.5*(.12+.24*growth);
58 // billow/topBillow <= 1; erosion cannot increase the top.
59 double topUpper=(std::max)(baseUpper+2.5,p.top+.7+cluster*.12+.08
60 +.4+.4*structure+topGrowth*(cluster*8.2+.5+1.5*cluster));
61 if(p.organic)
62 {
63 // Match the organic envelope's cluster and monotonic weather response.
64 // Keep BOTH bounds for the lower/thin-layer blend.
65 const double organicTop=p.top+12*std::sqrt(weatherUpper);
66 topUpper=(std::max)(topUpper,organicTop);
67 }
68 if(altitude-reach>topUpper+.01)return float(candidate);
69
70 // The organic field contains overhangs and sheared 3D lobes.
71 // Its regional envelope is conservative; the legacy local
72 // height-field cap bound below does not apply to that body.
73 if(p.organic)continue;
74
75 // Near a top, the global billow maximum is much taller than
76 // the local surface. Bound the same shader shape fields
77 // over this ball before falling back to per-sample marching.
78 for(unsigned a=0;a<3;++a)
79 {
80 const double wa=(broad.low-.5)*warp[a],wb=(broad.high-.5)*warp[a];
81 lo[a]=(surface[a]-delta)*.17+(std::min)(wa,wb);
82 hi[a]=(surface[a]+delta)*.17+(std::max)(wa,wb);
83 }
84 const auto topBillow=NoiseRange(lo,hi,footprint*.21);
85 // Storm caps only lower their existing allowance. Bound
86 // them locally too, so clear air above a carved recess still
87 // benefits from the original short-ray rejection.
88 // A trilinear distance field is <=1-Lipschitz on each axis:
89 // all neighboring distance texels differ by <=1/8 cell.
90 // Thus its gradient length is <=sqrt(3). The projected
91 // world-space ball has radius <=delta (the Jacobian bound
92 // above), tighter than enclosing that ball in a cube.
93 // Mip 1 can only lower the nonnegative sample toward zero;
94 // bound its blend and include half/filter/coordinate error.
95 const auto distanceMinimum=[&](double frequency,double x,double y,double z) {
96 const double cellFootprint=footprint*frequency;
97 const double lod=.5*std::log2(1+cellFootprint*cellFootprint*64);
98 return (std::max)(0.,double(CloudBillows::Sample(
99 float(surface[0]*frequency+x),float(surface[1]*frequency+y),float(surface[2]*frequency+z)))
100 -std::sqrt(3.)*delta*frequency-.01)*(1-(std::min)(1.,lod+.008));
101 };
102 const auto capUpper=[&](double frequency,double radius) {
103 const double d=distanceMinimum(frequency,0,0,0);
104 const double cap=(std::max)(0.,1-d*d/(radius*radius));
105 return std::sqrt(cap)*Smooth(cap/.5);
106 };
107 const double parentUpper=capUpper(.07,.55),shoulderUpper=capUpper(.19,.65);
108 // Crown carving is nonnegative; dropping it bounds the union.
109 const double towerUpper=(std::max)(parentUpper,shoulderUpper*(.35+.45*parentUpper));
110 const auto shallowCap=[](double distance,double radius) {
111 const double cap=(std::max)(0.,1-distance*distance/(radius*radius));
112 return cap*cap;
113 };
114 const double shallow=shallowCap(distanceMinimum(.19,0,0,0),.9);
115 const double crownDistance=distanceMinimum(.43,3,5,1);
116 const double crownCap=(std::max)(0.,1-crownDistance*crownDistance/(.55*.55));
117 const double crownUpper=crownCap*crownCap;
118 const double shallowUpper=.35*shallow+.65*crownUpper*(.25+.75*shallow);
119 const double localTopUpper=(std::max)(baseUpper+2.5,p.top+.7+cluster*.12+topBillow.high*.08
120 +shallowUpper*(.4+.4*structure)
121 +topGrowth*(cluster*8.2*towerUpper+topBillow.high*(.5+1.5*cluster)));
122 if(altitude-reach>localTopUpper+.01)return float(candidate);
123 }
124 return 0;
125 }
126 private:
127 struct Range {double low,high;};
128 std::vector<uint16_t> noise;
129 static double Smooth(double x) {x=(std::max)(0.,(std::min)(1.,x));return x*x*(3-2*x);}
130 static double SmoothCoordinate(double x) {const double whole=std::floor(x);return whole+Smooth(x-whole);}
131 Range NoiseRange(const double* lower,const double* upper,double footprint) const
132 {
133 // At larger footprints the shader changes its coordinate smoothing;
134 // use the full [0,1] range instead of assuming a base-level sample.
135 if(footprint>=.5)return {0,1};
136 std::array<std::array<double,8>,3> points{};unsigned counts[3]{};
137 for(unsigned a=0;a<3;++a)
138 {
139 // D3D11 requires >=8 fractional bits for coordinates and LOD.
140 // Expand beyond a full subtexel plus arithmetic rounding.
141 const double low=SmoothCoordinate(lower[a]-.002)-.008;
142 const double high=SmoothCoordinate(upper[a]+.002)+.008;
143 if(high-low>4)return {0,1};
144 points[a][counts[a]++]=low;
145 for(double n=std::floor(low)+1;n<high;++n)points[a][counts[a]++]=n;
146 points[a][counts[a]++]=high;
147 }
148 Range result{1,0};
149 // Trilinear interpolation is multilinear within each texel cell;
150 // its extrema on a box occur at corners (including cell boundaries).
151 for(unsigned z=0;z<counts[2];++z)for(unsigned y=0;y<counts[1];++y)for(unsigned x=0;x<counts[0];++x)
152 {
153 const double p[]={points[0][x],points[1][y],points[2][z]};int cell[3];double f[3];
154 for(unsigned a=0;a<3;++a){cell[a]=int(std::floor(p[a]));f[a]=p[a]-cell[a];}
155 double value=0;
156 for(int dz=0;dz<2;++dz)for(int dy=0;dy<2;++dy)for(int dx=0;dx<2;++dx)
157 {
158 const auto voxel=noise[(((cell[2]+dz)&63)*64+((cell[1]+dy)&63))*64+((cell[0]+dx)&63)];
159 value+=DirectX::PackedVector::XMConvertHalfToFloat(voxel)
160 *(dx ? f[0] : 1-f[0])*(dy ? f[1] : 1-f[1])*(dz ? f[2] : 1-f[2]);
161 }
162 result.low=(std::min)(result.low,value);result.high=(std::max)(result.high,value);
163 }
164 // Higher mip texels are in [0,1]. Include their maximum possible
165 // contribution, including fixed-point LOD rounding, and FP error.
166 const double leak=.5*std::log2(1+footprint*footprint)+.008;
167 return {(std::max)(0.,result.low-leak),(std::min)(1.,result.high+leak)};
168 }
170 double length,double reach,double footprint,double planetRadius)
171 {
172 if(!map)return 0;
173 if(map->mips.empty())return 1;
174 const auto& m=map->mips.front();
175 if(!m.width || !m.height || m.pixels.size()!=size_t(m.width)*m.height)return 1;
176 const double equator=std::hypot(double(eye.x),double(eye.y));
177 // Avoid the ill-conditioned longitude/asin coordinates at the poles.
178 if(equator-reach<length*.05)return 1;
179 constexpr double pi=3.14159265358979323846;
180 const double u=.5+std::atan2(-double(eye.x),double(eye.y))/(2*pi);
181 const double v=.5-std::asin(double(eye.z)/length)/pi;
182 const double du=reach/(2*pi*(equator-reach))+.0001;
183 const double dv=reach/(pi*(length-reach))+.0001;
184 // Bound the bilinear value at the local coordinates, not the
185 // largest nearby texel. A high-convection neighbor several cells
186 // away must not force an unrelated clear camera into the fallback.
187 // Extrema occur at rectangle/cell corners. Keep the existing UV
188 // safety margin and add more than a full fixed-point subtexel.
189 std::array<std::array<double,20>,2> points{};unsigned counts[2]{};
190 const double lower[]={(u-du)*m.width-.5-.008,(v-dv)*m.height-.5-.008};
191 const double upper[]={(u+du)*m.width-.5+.008,(v+dv)*m.height-.5+.008};
192 for(unsigned a=0;a<2;++a)
193 {
194 if(upper[a]-lower[a]>16)return 1;
195 points[a][counts[a]++]=lower[a];
196 for(double n=std::floor(lower[a])+1;n<upper[a];++n)points[a][counts[a]++]=n;
197 points[a][counts[a]++]=upper[a];
198 }
199 double maximum=0;
200 for(unsigned y=0;y<counts[1];++y)for(unsigned x=0;x<counts[0];++x)
201 {
202 const double px=points[0][x],py=points[1][y];
203 const int ix=int(std::floor(px)),iy=int(std::floor(py));
204 const double fx=px-ix,fy=py-iy;double value=0;
205 for(int dy=0;dy<2;++dy)for(int dx=0;dx<2;++dx)
206 {
207 const auto sx=((ix+dx)%int(m.width)+int(m.width))%int(m.width);
208 const auto sy=(std::max)(0,(std::min)(int(m.height)-1,iy+dy));
209 value+=m.pixels[size_t(sy)*m.width+sx]/255.*(dx ? fx : 1-fx)*(dy ? fy : 1-fy);
210 }
211 maximum=(std::max)(maximum,value);
212 }
213 const double cosine=(std::max)(.02,(equator-reach)/(length+reach));
214 const double lod=.5*std::log2(1+std::pow(footprint*m.width/(2*pi*planetRadius*cosine),2));
215 return (std::min)(1.,maximum+lod+.008);
216 }
217 };
218}
SCRIPT_CONTROL SCRIPT_STATE const void int
float Radius(CloudVolume::Point eye, const CloudVolume::Parameters &p, const CloudWeather::Frame &weather, double angularPixel) const
static double Smooth(double x)
std::vector< uint16_t > noise
void SetNoise(std::vector< uint16_t > values)
static double WeatherUpper(const CloudWeather::MapPtr &map, CloudVolume::Point eye, double length, double reach, double footprint, double planetRadius)
Range NoiseRange(const double *lower, const double *upper, double footprint) const
static double SmoothCoordinate(double x)
bool Active(const Frame &frame)
float Sample(float x, float y, float z)
std::shared_ptr< const Map > MapPtr
CloudBaseWeather::Frame localBase
Definition CloudVolume.h:36