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)
30 for(
double candidate:{.5,.125,.03125})
32 const double reach=candidate+.01;
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);
38 const double delta=p.
radius*reach/minimumRadius+.002;
39 const double footprint=angularPixel*reach/ .15*1.4;
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)
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);
51 const auto clustered=
NoiseRange(lo,hi,footprint*.06);
52 const double cluster=
Smooth((.65*clustered.high+.35*broad.high-.25)/.5);
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);
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));
65 const double organicTop=p.
top+12*std::sqrt(weatherUpper);
66 topUpper=(std::max)(topUpper,organicTop);
68 if(altitude-reach>topUpper+.01)
return float(candidate);
78 for(
unsigned a=0;a<3;++a)
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);
84 const auto topBillow=
NoiseRange(lo,hi,footprint*.21);
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);
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));
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);
107 const double parentUpper=capUpper(.07,.55),shoulderUpper=capUpper(.19,.65);
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));
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);
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)
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;
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)
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];}
156 for(
int dz=0;dz<2;++dz)
for(
int dy=0;dy<2;++dy)
for(
int dx=0;dx<2;++dx)
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]);
162 result.
low=(std::min)(result.
low,value);result.
high=(std::max)(result.
high,value);
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)};
170 double length,
double reach,
double footprint,
double planetRadius)
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));
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;
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)
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];
200 for(
unsigned y=0;y<counts[1];++y)
for(
unsigned x=0;x<counts[0];++x)
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)
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);
211 maximum=(std::max)(maximum,value);
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);