Rise
The Vieneo Province
Toggle main menu visibility
Loading...
Searching...
No Matches
SpatialFogWeather.h
Go to the documentation of this file.
1
#pragma once
2
#include "
CloudBaseWeather.h
"
3
#include <cstdint>
4
5
// Client prediction of the spatial fog's visibility and precipitation tint.
6
// Terrain, cloud ceilings, accumulated snow/water and the wire store never move.
7
namespace
SpatialFogWeather
8
{
9
struct
Point
{
double
x
=0,
y
=0,
z
=0; };
10
inline
Point
Add
(
Point
a,
Point
b) {
return
{a.
x
+b.
x
,a.
y
+b.
y
,a.
z
+b.
z
};}
11
inline
Point
Scale
(
Point
a,
double
b) {
return
{a.
x
*b,a.
y
*b,a.
z
*b};}
12
inline
double
Dot
(
Point
a,
Point
b) {
return
a.
x
*b.
x
+a.
y
*b.
y
+a.
z
*b.
z
;}
13
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
};}
14
inline
bool
Finite
(
Point
p) {
return
std::isfinite(p.
x
)&&std::isfinite(p.
y
)&&std::isfinite(p.
z
);}
15
inline
Point
Direction
(
int
cube,
double
u,
double
v)
16
{
17
const
auto
n=
CloudBaseWeather::Normals
[cube],e=
CloudBaseWeather::East
[cube],d=
CloudBaseWeather::Down
[cube];
18
const
double
a=(u-
CloudBaseWeather::CenterU
[cube])/1019,b=(v-
CloudBaseWeather::CenterV
[cube])/1019;
19
return
{-n.x-e.x*a-d.x*b,-n.y-e.y*a-d.y*b,-n.z-e.z*a-d.z*b};
20
}
21
struct
Cell
{
float
visibility
=42.16f,
precipitation
=2;};
22
inline
Cell
Blend
(
Cell
a,
Cell
b,
double
weight)
23
{
return
{float(a.
visibility
+(b.
visibility
-a.
visibility
)*weight),float(a.
precipitation
+(b.
precipitation
-a.
precipitation
)*weight)};}
24
struct
Grid
25
{
26
int
cube
=0,
u
=0,
v
=0;
27
uint64_t
revision
=0;
28
std::array<Cell,4096>
cells
{};
29
Cell
Sample
(
Point
p)
const
30
{
31
if
(
cube<0 || cube>
5 || !
Finite
(p))
return
{};
32
const
auto
n=
CloudBaseWeather::Normals
[
cube
],e=
CloudBaseWeather::East
[
cube
],d=
CloudBaseWeather::Down
[
cube
];
33
const
double
denominator=
Dot
(p,{n.x,n.y,n.z});
34
if
(!(denominator<0))
return
{};
35
const
double
q=
Dot
(p,{e.x,e.y,e.z})/denominator*1019+
CloudBaseWeather::CenterU
[
cube
]-
u
+31;
36
const
double
r=
Dot
(p,{d.x,d.y,d.z})/denominator*1019+
CloudBaseWeather::CenterV
[
cube
]-
v
+31;
37
// Blend to clear fallback over the outermost control interval;
38
// never clamp an old weather front indefinitely at the grid edge.
39
if
(q<=-1 || q>=64 || r<=-1 || r>=64)
return
{};
40
const
int
x=
int
(std::floor(q)),y=
int
(std::floor(r));
41
const
auto
at=[&](
int
a,
int
b){
return
a>=0&&a<64&&b>=0&&b<64 ?
cells
[b*64+a] :
Cell
{};};
42
return
Blend
(
Blend
(at(x,y),at(x+1,y),q-x),
Blend
(at(x,y+1),at(x+1,y+1),q-x),r-y);
43
}
44
};
45
using
GridPtr
=std::shared_ptr<const Grid>;
46
struct
Rotation
47
{
48
Point
v
{};
double
w
=1;
49
Point
Apply
(
Point
p)
const
50
{
const
auto
t=
Scale
(
Cross
(
v
,p),2);
return
Add
(p,
Add
(
Scale
(t,
w
),
Cross
(
v
,t)));}
51
void
Append
(
const
Rotation
& b)
52
{
53
const
auto
next=
Add
(
Add
(
Scale
(b.
v
,
w
),
Scale
(
v
,b.
w
)),
Cross
(
v
,b.
v
));
54
const
double
scalar=
w
*b.
w
-
Dot
(
v
,b.
v
),inverse=1/std::sqrt(
Dot
(next,next)+scalar*scalar);
55
v
=
Scale
(next,inverse);
w
=scalar*inverse;
56
}
57
};
58
struct
Layer
59
{
60
GridPtr
grid
;
61
Rotation
toSource
;
62
Cell
Sample
(
Point
p)
const
{
return
grid
?
grid
->Sample(
toSource
.
Apply
(p)) :
Cell
{};}
63
};
64
class
Store
65
{
66
Layer
previous
,
target
;
67
double
transition
=0;
68
uint64_t
generation
=0;
69
public
:
70
// Presentation choice, not a fog formation/evaporation timescale.
71
static
constexpr
double
ReconcileSeconds
=10;
72
bool
Ready
()
const
{
return
bool(
target
.grid);}
73
uint64_t
SourceRevision
()
const
{
return
target
.grid ?
target
.grid->revision : 0;}
74
uint64_t
Generation
()
const
{
return
generation
;}
75
void
Reset
() {
previous
={};
target
={};
transition
=0;++
generation
;}
76
Cell
Sample
(
Point
renderPosition)
const
77
{
78
const
auto
latest=
target
.Sample(renderPosition);
79
if
(!
previous
.
grid
)
return
latest;
80
const
double
t=(std::clamp)(
transition
/
ReconcileSeconds
,0.,1.);
81
return
Blend
(
previous
.
Sample
(renderPosition),latest,t*t*(3-2*t));
82
}
83
void
Accept
(
GridPtr
next,
bool
full)
84
{
85
if
(!next){
Reset
();
return
;}
86
if
(
target
.grid && !full)
87
{
88
// A strip uses the original full snapshot's server epoch.
89
// Preserve displacement and an in-progress reconciliation.
90
target
.grid=std::move(next);++
generation
;
return
;
91
}
92
if
(
previous
.
grid
)
93
{
94
// A new full during a transition starts from what is visible,
95
// not from either endpoint. Reproject only on packet arrival,
96
// never every frame (which would diffuse fronts over time).
97
auto
visible=std::make_shared<Grid>();
98
visible->cube=next->cube;visible->u=next->u;visible->v=next->v;
99
for
(
int
y=0;y<64;++y)
for
(
int
x=0;x<64;++x)
100
visible->cells[y*64+x]=
Sample
(
Direction
(next->cube,next->u+x-31,next->v+y-31));
101
previous
={std::move(visible),{}};
102
}
103
else
previous
=
target
;
104
target
={std::move(next),{}};
transition
=0;++
generation
;
105
}
106
void
Advance
(
double
elapsed,
Point
physicalPosition,
Point
physicalWindKms)
107
{
108
if
(!
Ready
() || !std::isfinite(elapsed) || elapsed<=0)
return
;
109
bool
changed=bool(
previous
.
grid
);
110
if
(
previous
.
grid
)
111
{
112
transition
=(std::min)(
ReconcileSeconds
,
transition
+elapsed);
113
if
(
transition
>=
ReconcileSeconds
)
previous
={};
114
}
115
const
double
radiusSquared=
Dot
(physicalPosition,physicalPosition);
116
if
(
Finite
(physicalPosition) &&
Finite
(physicalWindKms) && radiusSquared>1)
117
{
118
// Angular transport keeps weather on the sphere, across cube
119
// faces and at the poles. Wind is already km/s at the player
120
// altitude: no surface-wind, density or cloud-speed multiplier.
121
const
auto
omega=
Scale
(
Cross
(physicalPosition,physicalWindKms),1/radiusSquared);
122
const
double
rate=std::sqrt(
Dot
(omega,omega));
123
if
(rate>0 && std::isfinite(rate*elapsed))
124
{
125
const
double
half=-rate*elapsed*.5;
126
const
Rotation
backtrace{
Scale
(omega,std::sin(half)/rate),std::cos(half)};
127
target
.toSource.Append(backtrace);
128
if
(
previous
.
grid
)
previous
.
toSource
.
Append
(backtrace);
129
changed=
true
;
130
}
131
}
132
if
(changed)++
generation
;
133
}
134
};
135
}
CloudBaseWeather.h
int
SCRIPT_CONTROL SCRIPT_STATE const void int
Definition
Controls.cpp:5032
SpatialFogWeather::Store
Definition
SpatialFogWeather.h:65
SpatialFogWeather::Store::Ready
bool Ready() const
Definition
SpatialFogWeather.h:72
SpatialFogWeather::Store::generation
uint64_t generation
Definition
SpatialFogWeather.h:68
SpatialFogWeather::Store::transition
double transition
Definition
SpatialFogWeather.h:67
SpatialFogWeather::Store::previous
Layer previous
Definition
SpatialFogWeather.h:66
SpatialFogWeather::Store::Reset
void Reset()
Definition
SpatialFogWeather.h:75
SpatialFogWeather::Store::Advance
void Advance(double elapsed, Point physicalPosition, Point physicalWindKms)
Definition
SpatialFogWeather.h:106
SpatialFogWeather::Store::Accept
void Accept(GridPtr next, bool full)
Definition
SpatialFogWeather.h:83
SpatialFogWeather::Store::target
Layer target
Definition
SpatialFogWeather.h:66
SpatialFogWeather::Store::Generation
uint64_t Generation() const
Definition
SpatialFogWeather.h:74
SpatialFogWeather::Store::SourceRevision
uint64_t SourceRevision() const
Definition
SpatialFogWeather.h:73
SpatialFogWeather::Store::Sample
Cell Sample(Point renderPosition) const
Definition
SpatialFogWeather.h:76
SpatialFogWeather::Store::ReconcileSeconds
static constexpr double ReconcileSeconds
Definition
SpatialFogWeather.h:71
CloudBaseWeather::Down
constexpr Point Down[]
Definition
CloudBaseWeather.h:19
CloudBaseWeather::CenterV
constexpr int CenterV[]
Definition
CloudBaseWeather.h:21
CloudBaseWeather::East
constexpr Point East[]
Definition
CloudBaseWeather.h:18
CloudBaseWeather::CenterU
constexpr int CenterU[]
Definition
CloudBaseWeather.h:20
CloudBaseWeather::Normals
constexpr Point Normals[]
Definition
CloudBaseWeather.h:17
SpatialFogWeather
Definition
SpatialFogWeather.h:8
SpatialFogWeather::Dot
double Dot(Point a, Point b)
Definition
SpatialFogWeather.h:12
SpatialFogWeather::Direction
Point Direction(int cube, double u, double v)
Definition
SpatialFogWeather.h:15
SpatialFogWeather::Finite
bool Finite(Point p)
Definition
SpatialFogWeather.h:14
SpatialFogWeather::Blend
Cell Blend(Cell a, Cell b, double weight)
Definition
SpatialFogWeather.h:22
SpatialFogWeather::Add
Point Add(Point a, Point b)
Definition
SpatialFogWeather.h:10
SpatialFogWeather::GridPtr
std::shared_ptr< const Grid > GridPtr
Definition
SpatialFogWeather.h:45
SpatialFogWeather::Scale
Point Scale(Point a, double b)
Definition
SpatialFogWeather.h:11
SpatialFogWeather::Cross
Point Cross(Point a, Point b)
Definition
SpatialFogWeather.h:13
SpatialFogWeather::Cell
Definition
SpatialFogWeather.h:21
SpatialFogWeather::Cell::precipitation
float precipitation
Definition
SpatialFogWeather.h:21
SpatialFogWeather::Cell::visibility
float visibility
Definition
SpatialFogWeather.h:21
SpatialFogWeather::Grid
Definition
SpatialFogWeather.h:25
SpatialFogWeather::Grid::cells
std::array< Cell, 4096 > cells
Definition
SpatialFogWeather.h:28
SpatialFogWeather::Grid::cube
int cube
Definition
SpatialFogWeather.h:26
SpatialFogWeather::Grid::revision
uint64_t revision
Definition
SpatialFogWeather.h:27
SpatialFogWeather::Grid::v
int v
Definition
SpatialFogWeather.h:26
SpatialFogWeather::Grid::u
int u
Definition
SpatialFogWeather.h:26
SpatialFogWeather::Grid::Sample
Cell Sample(Point p) const
Definition
SpatialFogWeather.h:29
SpatialFogWeather::Layer
Definition
SpatialFogWeather.h:59
SpatialFogWeather::Layer::toSource
Rotation toSource
Definition
SpatialFogWeather.h:61
SpatialFogWeather::Layer::Sample
Cell Sample(Point p) const
Definition
SpatialFogWeather.h:62
SpatialFogWeather::Layer::grid
GridPtr grid
Definition
SpatialFogWeather.h:60
SpatialFogWeather::Point
Definition
SpatialFogWeather.h:9
SpatialFogWeather::Point::x
double x
Definition
SpatialFogWeather.h:9
SpatialFogWeather::Point::y
double y
Definition
SpatialFogWeather.h:9
SpatialFogWeather::Point::z
double z
Definition
SpatialFogWeather.h:9
SpatialFogWeather::Rotation
Definition
SpatialFogWeather.h:47
SpatialFogWeather::Rotation::Apply
Point Apply(Point p) const
Definition
SpatialFogWeather.h:49
SpatialFogWeather::Rotation::v
Point v
Definition
SpatialFogWeather.h:48
SpatialFogWeather::Rotation::Append
void Append(const Rotation &b)
Definition
SpatialFogWeather.h:51
SpatialFogWeather::Rotation::w
double w
Definition
SpatialFogWeather.h:48
Viewscreen
SpatialFogWeather.h
Generated on
for Rise by
1.18.0