Rise
The Vieneo Province
Loading...
Searching...
No Matches
CloudBaseWeather.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <array>
4#include <cmath>
5#include <memory>
6
7// Local lower-cloud controls in planet-fixed render coordinates. These are
8// weather observations, not a new condensation or turbulence physics model.
10{
11 struct Point { float x,y,z; };
12 struct Cell { float base=0,turbulence=0,coverage=0,reserved=0; };
13 inline float Smooth(float x) { x=(std::clamp)(x,0.f,1.f);return x*x*(3-2*x); }
14 inline float AltitudeWeight(float altitude,float top)
15 { return 1-Smooth((altitude-(top-1.5f))); }
16 inline float Dot(Point a,Point b) {return a.x*b.x+a.y*b.y+a.z*b.z;}
17 inline constexpr Point Normals[]={{0,-1,0},{1,0,0},{-1,0,0},{0,1,0},{0,0,-1},{0,0,1}};
18 inline constexpr Point East[]={{1,0,0},{0,1,0},{0,-1,0},{-1,0,0},{1,0,0},{1,0,0}};
19 inline constexpr Point Down[]={{0,0,1},{0,0,1},{0,0,1},{0,0,1},{0,-1,0},{0,1,0}};
20 inline constexpr int CenterU[]={1019,3057,-1019,5095,1019,1019};
21 inline constexpr int CenterV[]={1019,1019,1019,1019,-1019,3057};
22 inline Point Direction(int cube,float u,float v)
23 {
24 const float a=(u-CenterU[cube])/1019,b=(v-CenterV[cube])/1019;
25 return {-Normals[cube].x-East[cube].x*a-Down[cube].x*b,
26 -Normals[cube].y-East[cube].y*a-Down[cube].y*b,
27 -Normals[cube].z-East[cube].z*a-Down[cube].z*b};
28 }
29 struct Grid
30 {
31 int cube=-1,u=0,v=0;
32 float minimumBase=0;
33 std::array<Cell,4096> cells{}; // row=v, column=u, same layout as the GPU
34 // Return premultiplied controls. Missing data blends toward the global
35 // fallback, never toward a fictitious sea-level/zero-turbulence cell.
36 Cell Sample(Point p,bool fadeEdges=true) const
37 {
38 if(cube<0 || cube>5)return {};
39 const float denominator=Dot(p,Normals[cube]);
40 if(!(denominator<-.000001f))return {};
41 const float q=Dot(p,East[cube])/denominator*1019+float(CenterU[cube]-u+31);
42 const float r=Dot(p,Down[cube])/denominator*1019+float(CenterV[cube]-v+31);
43 if(!(q>=0 && q<=63 && r>=0 && r<=63))return {};
44 const int x=int(q),y=int(r);const float fx=q-x,fy=r-y;
45 Cell result;
46 for(int dy=0;dy<2;++dy)for(int dx=0;dx<2;++dx)
47 {
48 const float weight=(dx?fx:1-fx)*(dy?fy:1-fy);
49 if(weight<=0)continue;
50 const auto& cell=cells[(std::min)(63,y+dy)*64+(std::min)(63,x+dx)];
51 const float w=weight*cell.coverage;
52 result.base+=cell.base*w;result.turbulence+=cell.turbulence*w;result.coverage+=w;
53 }
54 const float edge=fadeEdges ? Smooth((std::min)({q,r,63-q,63-r})/2) : 1;
55 result.base*=edge;result.turbulence*=edge;result.coverage*=edge;
56 return result;
57 }
58 void Bounds()
59 {
60 minimumBase=100;
61 for(const auto& cell:cells)if(cell.coverage>0)minimumBase=(std::min)(minimumBase,cell.base);
62 }
63 };
64 using GridPtr=std::shared_ptr<const Grid>;
65 struct Frame {GridPtr grid;float weight=0;};
66 inline bool Active(const Frame& frame) {return frame.grid && frame.weight>0;}
67 struct Controls {float base, turbulence;};
68 inline Controls Sample(const Frame& frame,Point p,float altitude,float top,float base,float turbulence)
69 {
70 if(!Active(frame))return {base,turbulence};
71 const float weight=frame.weight*AltitudeWeight(altitude,top);
72 if(weight<=0)return {base,turbulence};
73 const auto local=frame.grid->Sample(p);
74 return {base*(1-local.coverage*weight)+local.base*weight,
75 turbulence*(1-local.coverage*weight)+local.turbulence*weight};
76 }
77 // Immutable published frames let CPU density and GPU uploads share exactly
78 // the same smoothed controls. Recenter by geography, not by array index.
79 class Store
80 {
82 public:
83 void Reset() {target.reset();current.reset();}
84 GridPtr Current() const {return current;}
85 void Target(GridPtr next)
86 {
87 if(!next){target.reset();return;}
88 if(!current || current->cube!=next->cube || current->u!=next->u || current->v!=next->v)
89 {
90 auto remapped=std::make_shared<Grid>();
91 remapped->cube=next->cube;remapped->u=next->u;remapped->v=next->v;
92 for(int y=0;y<64;++y)for(int x=0;x<64;++x)
93 {
94 const size_t i=y*64+x;
95 const auto old=current ? current->Sample(Direction(next->cube,float(next->u+x-31),float(next->v+y-31)),false) : Cell{};
96 remapped->cells[i]=old.coverage>0 ? Cell{old.base/old.coverage,old.turbulence/old.coverage,old.coverage,0}
97 : Cell{next->cells[i].base,next->cells[i].turbulence,0,0};
98 }
99 remapped->Bounds();current=std::move(remapped);
100 }
101 target=std::move(next);
102 }
103 void Advance(double elapsed)
104 {
105 if(!current || !std::isfinite(elapsed) || elapsed<=0)return;
106 // Existing ceiling response is 0.01/s; global/local activity uses
107 // 120 s. The 2 s coverage fade is an explicit visual handoff only.
108 const float height=float(-std::expm1(-elapsed/100)),activity=float(-std::expm1(-elapsed/120));
109 const float coverage=float(-std::expm1(-elapsed/2));
110 auto next=std::make_shared<Grid>(*current);bool changed=false,occupied=false;
111 for(size_t i=0;i<next->cells.size();++i)
112 {
113 auto& cell=next->cells[i];const auto goal=target?target->cells[i]:Cell{};
114 const auto approach=[&](float& value,float desired,float blend) {
115 const float before=value;value+=blend*(desired-value);
116 if(std::abs(value-desired)<.000001f)value=desired;
117 changed|=value!=before;
118 };
119 if(goal.coverage>0)
120 {
121 if(cell.coverage<=0){cell.base=goal.base;cell.turbulence=goal.turbulence;}
122 approach(cell.base,goal.base,height);approach(cell.turbulence,goal.turbulence,activity);
123 }
124 approach(cell.coverage,goal.coverage,coverage);occupied|=cell.coverage>0;
125 }
126 if(!occupied && !target){current.reset();return;}
127 if(changed){next->Bounds();current=std::move(next);}
128 }
129 };
130}
SCRIPT_CONTROL SCRIPT_STATE const void int
void Target(GridPtr next)
void Advance(double elapsed)
constexpr Point Down[]
bool Active(const Frame &frame)
float Smooth(float x)
Point Direction(int cube, float u, float v)
float Dot(Point a, Point b)
constexpr int CenterV[]
float AltitudeWeight(float altitude, float top)
Controls Sample(const Frame &frame, Point p, float altitude, float top, float base, float turbulence)
std::shared_ptr< const Grid > GridPtr
constexpr Point East[]
constexpr int CenterU[]
constexpr Point Normals[]
std::array< Cell, 4096 > cells
Cell Sample(Point p, bool fadeEdges=true) const