Rise
The Vieneo Province
Loading...
Searching...
No Matches
CloudOrganicShape.inl
Go to the documentation of this file.
1
2// Shared CPU shape, included after the legacy medium by CloudVolume.h.
3// Parameters::organic selects this candidate; defaults remain legacy until validation.
5{
6 using namespace CloudVolume;
8 inline float Dot(Point a,Point b) {return a.x*b.x+a.y*b.y+a.z*b.z;}
9 inline Point Cross(Point a,Point b) {return {a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x};}
10 // The renderer uploads both field channels as R16_FLOAT. Retain those
11 // stored values on the CPU too; tiny drift errors amplify at thin edges.
12 inline float PackedFieldValue(float value)
13 {
14 if(value==0)return 0;
15 int exponent=0;std::frexp(value,&exponent);
16 const int shift=(std::max)(-24,exponent-11);
17 const float scaled=std::ldexp(value,-shift),whole=std::floor(scaled);
18 const float fraction=scaled-whole;
19 const bool up=fraction>.5f || (fraction==.5f && std::fmod(whole,2.f)!=0);
20 return std::ldexp(whole+(up ? 1.f : 0.f),shift);
21 }
22 inline float FieldNoise(Point p,uint32_t seed=DefaultSeed)
23 {
24 static const auto packed=[] {
25 std::array<float,256> values{};
26 for(unsigned i=0;i<values.size();++i)values[i]=PackedFieldValue(float(i)/255.f);
27 return values;
28 }();
29 const int x=int(std::floor(p.x)),y=int(std::floor(p.y)),z=int(std::floor(p.z));
30 const float u=Smooth(p.x-x),v=Smooth(p.y-y),w=Smooth(p.z-z);
31 float slices[2];
32 for(int k=0;k<2;++k)
33 slices[k]=Lerp(Lerp(packed[Voxel(x,y,z+k,seed)],packed[Voxel(x+1,y,z+k,seed)],u),
34 Lerp(packed[Voxel(x,y+1,z+k,seed)],packed[Voxel(x+1,y+1,z+k,seed)],u),v);
35 return Lerp(slices[0],slices[1],w);
36 }
38 inline Region Regional(Point p,const Parameters& params,uint32_t seed=DefaultSeed)
39 {
40 const Point radial=Scale(p,1.f/(std::max)(1.f,Length(p)));
41 const Point surface=Add(Scale(radial,params.radius),params.noiseOffset);
42 const float weather=CloudWeather::Sample(params.weather,p.x,p.y,p.z);
43 // Fixed square-root visual response, matching the native shader.
44 // The raw weather remains unchanged for atmospheric turbulence.
45 const float growth=std::sqrt(Saturate(weather));
46 const float low=FieldNoise(Add(Scale(surface,.01f),{19,7,31}),seed);
47 const Point warp=Scale({1.5f,-1.1f,.9f},low-.5f);
48 const float broad=FieldNoise(Add(Scale(surface,.04f),warp),seed);
49 const float localCluster=Smooth((.65f*broad+.35f*low-.35f)/.3f);
50 const float cluster=Saturate(localCluster+5.f*(weather-.45f));
51 const float structure=growth*(.35f+.65f*cluster);
52 const float top=params.top+MaxTopRiseKm*growth;
53 const Point drift{FieldNoise(Add(Scale(surface,.03f),{7,11,19}),seed)-.5f,
54 FieldNoise(Add(Scale(surface,.03f),{29,3,13}),seed)-.5f,low-.5f};
55 return {radial,surface,drift,weather,low,cluster,structure,top};
56 }
57 inline Point Flow(const Region& region,Point drift,float height)
58 {
59 // A continuous tangent field, with smooth stagnation points instead of
60 // a latitude-dependent basis switch. This is not a pressure-gradient
61 // calculation. The server's 35-degree 10-to-20 km turn is the guide;
62 // the integrated bend uses half the turn. Lean is an artistic choice.
63 const float heading=6.2831853f*region.low;
64 const Point guide{std::cos(heading),std::sin(heading),.5f*(drift.x+drift.y)};
65 Point tangent=Add(guide,Scale(region.radial,-Dot(guide,region.radial)));
66 tangent=Scale(tangent,1.f/std::sqrt(Dot(tangent,tangent)+.04f));
67 const float turn=.6108652f*Saturate(height/10.f)*.5f;
68 return Add(Scale(tangent,std::cos(turn)),Scale(Cross(region.radial,tangent),std::sin(turn)));
69 }
70 inline float Billow(Point p)
71 {
72 static const auto packed=[] {
73 auto values=CloudBillows::Values();
74 for(auto& value:values)value=PackedFieldValue(value);
75 return values;
76 }();
77 p=Scale(p,8);
78 const int x=int(std::floor(p.x)),y=int(std::floor(p.y)),z=int(std::floor(p.z));
79 const float u=p.x-x,v=p.y-y,w=p.z-z;float result=0;
80 for(int dz=0;dz<2;++dz)for(int dy=0;dy<2;++dy)for(int dx=0;dx<2;++dx)
81 result+=packed[(((z+dz)&63)*64+((y+dy)&63))*64+((x+dx)&63)]
82 *(dx ? u : 1-u)*(dy ? v : 1-v)*(dz ? w : 1-w);
83 return result;
84 }
85 inline float Perlin(Point p)
86 {
87 static const auto packed=[] {
88 auto values=CloudPerlin::Values();
89 for(auto& value:values)value=PackedFieldValue(value);
90 return values;
91 }();
92 p=Scale(p,8);
93 const int x=int(std::floor(p.x)),y=int(std::floor(p.y)),z=int(std::floor(p.z));
94 const float u=p.x-x,v=p.y-y,w=p.z-z;float result=0;
95 for(int dz=0;dz<2;++dz)for(int dy=0;dy<2;++dy)for(int dx=0;dx<2;++dx)
96 result+=packed[(((z+dz)&63)*64+((y+dy)&63))*64+((x+dx)&63)]
97 *(dx ? u : 1-u)*(dy ? v : 1-v)*(dz ? w : 1-w);
98 return result;
99 }
100 inline float Weight(const Parameters& params)
101 {
102 // Administrative thin/high layers retain their established core.
103 // Normal ceilings at least 2.5 km below the deck use the new body.
104 return 1.f-Smooth((params.base-(params.top-2.5f)));
105 }
107 inline float LegacyDepth(Point p,const Parameters& params,uint32_t seed=DefaultSeed)
108 {
109 const auto column=LegacyBoundaries(p,params,seed);
110 const Point advected=Add(p,params.noiseOffset);
111 const float raw=TopErosion(advected,Noise(Scale(advected,2.17f),seed),Noise(Scale(advected,6.13f),seed),column.structure);
112 const float available=(std::max)(.01f,column.top-(std::max)(params.top+.65f,column.base+1.3f));
113 return (std::max)(params.top+.65f,column.top-available*raw/(available+raw))-(Length(p)-params.radius);
114 }
115 inline Sample Evaluate(Point p,const Parameters& params,uint32_t seed=DefaultSeed,const Region* prepared=nullptr)
116 {
117 const float altitude=Length(p)-params.radius;
118 const auto region=prepared ? *prepared : Regional(p,params,seed);
119 const float weight=Weight(params);
120 const float blend=Smooth((altitude-params.top+.5f)/.5f)*weight;
121 float upper=0;
122 if(altitude<=region.top && blend>0)
123 {
124 const float height=(std::max)(0.f,altitude-params.top+.65f);
125 const float h=height/(std::max)(.65f,(region.top-params.top)*(.04f+.96f*region.cluster*region.cluster)+.65f);
126 const Point drift=region.drift;
127 const Point flow=Flow(region,drift,height);
128 const Point domain=Add(Add(region.surface,Scale(drift,18)),
129 Add(Scale(region.radial,height*.85f),Scale(flow,-height*.35f)));
130 // Two smoothly overlapping parent scales and smaller 3D billows.
131 // All frequencies retain the 6400-km wind-wrap period.
132 const Point parentDomain=Add(Add(region.surface,Scale(drift,18)),
133 Add(Scale(region.radial,height*.5f),Scale(flow,-height*.35f)));
134 const float parentA=Billow(Scale(parentDomain,.105f));
135 const float parentB=Billow(Scale(Add(domain,{3,7,1}),.155f))*.86f+.09f;
136 const float overlap=Saturate(.5f+.5f*(parentB-parentA)/.14f);
137 const float parent=Lerp(parentB,parentA,overlap)-.14f*overlap*(1-overlap);
138 float shoulders=Billow(Add(Scale(domain,.43f),{5,1,3}));
139 float smallField=Perlin(Add(Scale(domain,1.23f),{1,7,5}));
140 // Fixed altitude response keeps increasing weather monotonic:
141 // changing convection must not rotate or erode an existing wisp.
142 const float wispy=(std::max)(.65f*(1-Smooth((height-.6f)/2.2f)),Smooth((height/MaxTopRiseKm-.15f)/.5f))*Smooth((region.low-.35f)/.35f);
143 if(wispy>.01f)
144 {
145 const float a=Perlin(Add(Scale(Add(domain,Scale(flow,-.9f)),1.23f),{1,7,5}));
146 const float b=Perlin(Add(Scale(Add(domain,Scale(flow,.9f)),1.23f),{1,7,5}));
147 smallField=Lerp(smallField,(a+2*smallField+b)*.25f,wispy);
148 const float shoulderA=Billow(Add(Scale(Add(domain,Scale(flow,-1.4f)),.43f),{5,1,3}));
149 const float shoulderB=Billow(Add(Scale(Add(domain,Scale(flow,1.4f)),.43f),{5,1,3}));
150 shoulders=Lerp(shoulders,(shoulderA+2*shoulders+shoulderB)*.25f,wispy);
151 }
152 const float grain=Perlin(Add(Scale(domain,2.91f),{7,3,11}));
153 const float field=.78f-.50f*h*h*h-parent-.38f*shoulders+.12f*smallField+.035f*grain;
154 const float sheetGrowth=Saturate(std::sqrt(Saturate(region.weather))*4.f);
155 const float shape=Smooth(field/(.10f+.12f*wispy))*sheetGrowth;
156 float floorDensity=0;
157 if(altitude<params.top+1.35f)
158 {
159 // The map supplies contour alignment, not a measured wind.
160 const Point axis=std::abs(region.radial.z)<.9f ? Point{0,0,1} : Point{0,1,0};
161 Point east=Cross(region.radial,axis);east=Scale(east,1.f/Length(east));
162 const Point north=Cross(region.radial,east);
163 const auto weatherAt=[&](Point offset) {
164 const auto q=Add(p,offset);return CloudWeather::Sample(params.weather,q.x,q.y,q.z);
165 };
166 const float de=weatherAt(Scale(east,24))-weatherAt(Scale(east,-24));
167 const float dn=weatherAt(Scale(north,24))-weatherAt(Scale(north,-24));
168 const Point gradient=Add(Scale(east,de),Scale(north,dn));
169 const float magnitude=Length(gradient),contourWeight=Saturate(magnitude*32.f);
170 const Point contour=Scale(Cross(region.radial,gradient),1.f/std::sqrt(Dot(gradient,gradient)+.0001f));
171 const Point sheetFlow=Add(Scale(flow,1-contourWeight),Scale(contour,contourWeight));
172 const Point sheetDomain=Add(region.surface,Scale(drift,18));
173 const auto sheet=[&](float offset) {
174 return FieldNoise(Add(Scale(Add(sheetDomain,Scale(sheetFlow,offset)),.35f),{5,3,17}),seed);
175 };
176 const float a=sheet(0),ribbon=(2*a+sheet(-3)+sheet(3))*.25f;
177 // Match the continuous, world-anchored sheet relief and calm
178 // ceiling treatment in CloudOrganic11.hlsli. No height terraces.
179 const float pattern=Smooth((region.low-.25f)/.5f);
180 const float broad=Lerp(a,ribbon,pattern);
181 const float medium=Perlin(Add(Scale(sheetDomain,.70f),{11,5,23}));
182 const float fine=Perlin(Add(Scale(sheetDomain,2.10f),{3,17,7}));
183 const float relief=.57f+.50f*(broad-.5f)+.20f*medium+.08f*fine+.04f*(region.low-.5f);
184 const float floorTop=params.top-.35f+relief*(.28f+.72f*sheetGrowth);
185 floorDensity=Smooth((floorTop-altitude)/.35f);
186 }
187 upper=(std::max)(floorDensity,shape)*Smooth((region.top-altitude)/.35f)
188 *(.65f+.35f*region.weather)*(.5f+.5f*region.weather);
189 }
190 // Preserve the medium below 9.5 km; blend continuously to the 10-km deck.
191 const auto lower=blend<1 ? LegacySampleMedium(p,params,seed) : Medium{};
192 float topDepth=region.top-altitude;
193 if(weight<1)
194 topDepth=Lerp(LegacyDepth(p,params,seed),topDepth,weight);
195 return {Lerp(lower.density,upper,blend),topDepth,region.weather,
196 Lerp(lower.extinction,upper*32.f,blend)};
197 }
198 // Effective visible top for height-based consumers; the optical field
199 // remains Evaluate(). A 3D column may contain clear gaps and overhangs.
200 // This bounded 10 m search is an approximation, not a clearance proof.
201 inline Column SurfaceColumn(Point p,const Parameters& params,uint32_t seed=DefaultSeed)
202 {
203 const auto region=Regional(p,params,seed);
204 if(Weight(params)<=0)return LegacyBoundaries(p,params,seed);
205 const auto controls=BaseControls(p,params,region.weather);
206 const float base=controls.base+(Noise(Scale(region.surface,.71f),seed)-.5f)
207 *(.12f+.24f*Smooth((controls.turbulence-.2f)/.65f));
208 const float envelope=Weight(params)<1 ? (std::max)(region.top,LegacyBoundaries(p,params,seed).top) : region.top;
209 float high=envelope,low=params.top;
210 const auto occupied=[&](float altitude) {
211 return Evaluate(Scale(region.radial,params.radius+altitude),params,seed,&region).density>.001f;
212 };
213 for(int step=0;step<1201;++step)
214 {
215 const float altitude=(std::max)(params.top,envelope-.01f*step);
216 if(occupied(altitude)){low=altitude;break;}
217 high=altitude;
218 // A zero-growth column ends at the deck. Do not sample that same
219 // empty cap another 1200 times when the height envelope is 10 km.
220 if(altitude<=params.top)break;
221 }
222 for(int step=0;step<8;++step)
223 {
224 const float middle=(low+high)*.5f;
225 if(occupied(middle))low=middle;else high=middle;
226 }
227 return {base,high,region.weather,region.structure,controls.turbulence};
228 }
229}
SCRIPT_CONTROL SCRIPT_STATE const void int
const std::vector< float > & Values()
Point Cross(Point a, Point b)
Column SurfaceColumn(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
float PackedFieldValue(float value)
float Dot(Point a, Point b)
Point Flow(const Region &region, Point drift, float height)
float FieldNoise(Point p, uint32_t seed=DefaultSeed)
Region Regional(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
Sample Evaluate(Point p, const Parameters &params, uint32_t seed=DefaultSeed, const Region *prepared=nullptr)
float Weight(const Parameters &params)
float LegacyDepth(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
const std::vector< float > & Values()
float Smooth(float x)
Definition CloudVolume.h:51
Column LegacyBoundaries(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
uint8_t Voxel(int x, int y, int z, uint32_t seed)
constexpr float MaxTopRiseKm
Definition CloudVolume.h:23
float Lerp(float a, float b, float t)
Definition CloudVolume.h:50
Point Scale(Point p, float s)
Definition CloudVolume.h:93
Point Add(Point a, Point b)
Definition CloudVolume.h:95
CloudBaseWeather::Controls BaseControls(Point p, const Parameters &params, float weather)
float Noise(Point p, uint32_t seed=DefaultSeed, int mask=63)
constexpr uint32_t DefaultSeed
Definition CloudVolume.h:19
float Saturate(float x)
Definition CloudVolume.h:49
Medium LegacySampleMedium(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
float Length(Point p)
Definition CloudVolume.h:94
float TopErosion(Point advected, float detail, float fine, float structure)
float Sample(const MapPtr &map, float u, float v)
CloudWeather::Frame weather
Definition CloudVolume.h:35