Rise
The Vieneo Province
Toggle main menu visibility
Loading...
Searching...
No Matches
LightningChain.h
Go to the documentation of this file.
1
#pragma once
2
#include "
LightningTiming.h
"
3
#include "
LightningStrike.h
"
4
#include "
ProceduralLightning.h
"
5
#include <array>
6
7
// One packet describes one connected discharge. Only shared world endpoints,
8
// seed and chain count determine its shape; the observer affects sound only.
9
namespace
LightningChain
10
{
11
using
ProceduralLightning::Vector3
;
12
struct
Segment
13
{
14
Vector3
start
{},
end
{};
15
uint32_t
seed
=0;
16
float
begin
=0,
finish
=1;
17
bool
groundContact
=
false
;
18
float
Growth
(
float
progress)
const
19
{
return
(std::clamp)((progress-
begin
)/(
finish
-
begin
),0.f,1.f);}
20
Vector3
SoundSource
(Vector3 observer)
const
21
{
22
if
(
groundContact
)
return
end
;
23
const
auto
axis=
end
-
start
,delta=observer-
start
;
24
const
float
t=(std::clamp)(
RiseMath::Vec3Dot
(&delta,&axis)/
RiseMath::Vec3LengthSq
(&axis),0.f,1.f);
25
return
start
+axis*t;
26
}
27
};
28
struct
Path
29
{
30
std::array<Segment,LightningTiming::MaxChainedBolts+1>
segments
{};
31
int
count
=0;
32
};
33
struct
Animation
:
LightningStrike::Animation
34
{
35
Path
path
{};
36
Vector3
groundpos
{},
cloudpos
{};
// Active section's end/start for scene lighting.
37
double
sequenceAge
=0,
boltDuration
=0;
38
int
activeSegment
=0;
39
double
Duration
()
const
{
return
boltDuration
*
path
.count;}
40
double
SegmentStart
(
int
index)
const
{
return
boltDuration
*index;}
41
void
Activate
(
int
index)
42
{
43
const
auto
packetSeed=
seed
;
44
LightningStrike::Animation::Start
(
path
.segments[index].seed,
boltDuration
);
45
seed
=packetSeed;
activeSegment
=index;
46
groundpos
=
path
.segments[index].end;
cloudpos
=
path
.segments[index].start;
47
}
48
void
Start
(uint32_t packetSeed,
const
Path
& geometry)
49
{
50
*
this
={};
51
if
(geometry.
count
<=0 || geometry.
count
>
int
(geometry.
segments
.size()))
return
;
52
path
=geometry;
53
LightningStrike::Animation::Start
(packetSeed);
54
boltDuration
=
LightningStrike::Animation::Duration
();
55
Activate
(0);
56
}
57
void
Update
(
double
elapsed)
58
{
59
if
(!
inuse
|| !std::isfinite(elapsed) || elapsed<=0)
return
;
60
const
double
remaining=
Duration
()-
sequenceAge
;
61
sequenceAge
=elapsed>=remaining ?
Duration
() :
sequenceAge
+elapsed;
62
const
int
index=(std::min)(
int
(
sequenceAge
/
boltDuration
),
path
.count-1);
63
if
(index!=
activeSegment
)
Activate
(index);
64
if
(
sequenceAge
>=
Duration
())
65
{
age
=
boltDuration
;
growth
=1;
flashBrightness
=0;
inuse
=
false
;
return
;}
66
// Evaluate the current section at absolute sequence time, so frame
67
// stalls skip elapsed sections instead of replaying them later.
68
age
=0;
growth
=
flashBrightness
=0;
69
LightningStrike::Animation::Update
(
sequenceAge
-
SegmentStart
(index));
70
inuse
=
true
;
// The chain owns lifetime, including rounded section-end tails.
71
}
72
};
73
inline
Path
Build
(Vector3 ground,Vector3 cloud,uint32_t seed,
int
chains,
float
cloudTopRadius)
74
{
75
using namespace
ProceduralLightning
;
76
Path
result;
77
const
float
baseRadius=
Length
(cloud),groundRadius=
Length
(ground);
78
if
(!
Finite
(ground) || !
Finite
(cloud) || groundRadius<1 || baseRadius<=groundRadius
79
||
Length
(cloud-ground)>40 || !std::isfinite(cloudTopRadius) || cloudTopRadius<baseRadius)
return
result;
80
chains=(std::clamp)(chains,0,
LightningTiming::MaxChainedBolts
);
81
Random
random{seed^0x6c8e9cf5u};
82
const
auto
up=
Unit
(cloud);
83
const
auto
east=
Unit
(
Cross
(std::abs(up.y)<.9f ?
Vector3
(0,1,0) :
Vector3
(1,0,0),up));
84
const
auto
north=
Cross
(up,east);
85
float
heading=random.
Next
()*6.283185307f;
86
std::array<Vector3,LightningTiming::MaxChainedBolts+1> nodes{};
87
nodes[chains]=cloud;
88
// Work backwards from the packet's ground column. No additional terrain
89
// lookups, camera vectors, local weather overrides or gameplay RNG.
90
for
(
int
i=chains-1;i>=0;--i)
91
{
92
heading+=random.
Signed
()*
LightningTiming::CloudHopTurnRadians
;
93
const
float
span=
LightningTiming::CloudHopMinKm
94
+(
LightningTiming::CloudHopMaxKm
-
LightningTiming::CloudHopMinKm
)*random.
Next
();
95
const
auto
direction=east*std::cos(heading)+north*std::sin(heading);
96
const
float
rise=random.
Next
()*(std::min)(
LightningTiming::CloudHopMaxRiseKm
,(cloudTopRadius-baseRadius)*.5f);
97
nodes[i]=
Unit
(nodes[i+1]+direction*span)*(baseRadius+rise);
98
}
99
result.
count
=chains+1;
100
float
total=0;
101
for
(
int
i=0;i<result.
count
;++i)
102
{
103
auto
& segment=result.
segments
[i];
104
segment.start=nodes[i];segment.end=i<chains ? nodes[i+1] : ground;
105
segment.seed=i==chains ? seed : seed+0x9e3779b9u*uint32_t(i+1);
106
segment.groundContact=i==chains;
107
segment.begin=total;total+=
Length
(segment.end-segment.start);segment.finish=total;
108
}
109
for
(
int
i=0;i<result.
count
;++i)
110
{result.
segments
[i].begin/=total;result.
segments
[i].finish/=total;}
111
return
result;
112
}
113
}
LightningStrike.h
LightningTiming.h
ProceduralLightning.h
LightningChain
Definition
LightningChain.h:10
LightningChain::Build
Path Build(Vector3 ground, Vector3 cloud, uint32_t seed, int chains, float cloudTopRadius)
Definition
LightningChain.h:73
LightningTiming::CloudHopMaxRiseKm
constexpr float CloudHopMaxRiseKm
Definition
LightningTiming.h:14
LightningTiming::MaxChainedBolts
constexpr int MaxChainedBolts
Definition
LightningTiming.h:12
LightningTiming::CloudHopMinKm
constexpr float CloudHopMinKm
Definition
LightningTiming.h:13
LightningTiming::CloudHopMaxKm
constexpr float CloudHopMaxKm
Definition
LightningTiming.h:13
LightningTiming::CloudHopTurnRadians
constexpr float CloudHopTurnRadians
Definition
LightningTiming.h:14
ProceduralLightning
Definition
ProceduralLightning.h:11
ProceduralLightning::Finite
bool Finite(Vector3 v)
Definition
ProceduralLightning.h:25
ProceduralLightning::Unit
Vector3 Unit(Vector3 v)
Definition
ProceduralLightning.h:24
ProceduralLightning::Length
float Length(Vector3 v)
Definition
ProceduralLightning.h:23
ProceduralLightning::Cross
Vector3 Cross(Vector3 a, Vector3 b)
Definition
ProceduralLightning.h:21
RiseMath::Vec3LengthSq
float Vec3LengthSq(const Vector3 *a)
Definition
ClientMath.h:94
RiseMath::Vec3Dot
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Definition
ClientMath.h:93
LightningChain::Animation
Definition
LightningChain.h:34
LightningChain::Animation::activeSegment
int activeSegment
Definition
LightningChain.h:38
LightningChain::Animation::Duration
double Duration() const
Definition
LightningChain.h:39
LightningChain::Animation::SegmentStart
double SegmentStart(int index) const
Definition
LightningChain.h:40
LightningChain::Animation::sequenceAge
double sequenceAge
Definition
LightningChain.h:37
LightningChain::Animation::groundpos
Vector3 groundpos
Definition
LightningChain.h:36
LightningChain::Animation::cloudpos
Vector3 cloudpos
Definition
LightningChain.h:36
LightningChain::Animation::Update
void Update(double elapsed)
Definition
LightningChain.h:57
LightningChain::Animation::Activate
void Activate(int index)
Definition
LightningChain.h:41
LightningChain::Animation::Start
void Start(uint32_t packetSeed, const Path &geometry)
Definition
LightningChain.h:48
LightningChain::Animation::boltDuration
double boltDuration
Definition
LightningChain.h:37
LightningChain::Animation::path
Path path
Definition
LightningChain.h:35
LightningChain::Path
Definition
LightningChain.h:29
LightningChain::Path::count
int count
Definition
LightningChain.h:31
LightningChain::Path::segments
std::array< Segment, LightningTiming::MaxChainedBolts+1 > segments
Definition
LightningChain.h:30
LightningChain::Segment
Definition
LightningChain.h:13
LightningChain::Segment::begin
float begin
Definition
LightningChain.h:16
LightningChain::Segment::seed
uint32_t seed
Definition
LightningChain.h:15
LightningChain::Segment::finish
float finish
Definition
LightningChain.h:16
LightningChain::Segment::SoundSource
Vector3 SoundSource(Vector3 observer) const
Definition
LightningChain.h:20
LightningChain::Segment::start
Vector3 start
Definition
LightningChain.h:14
LightningChain::Segment::end
Vector3 end
Definition
LightningChain.h:14
LightningChain::Segment::groundContact
bool groundContact
Definition
LightningChain.h:17
LightningChain::Segment::Growth
float Growth(float progress) const
Definition
LightningChain.h:18
LightningStrike::Animation
Definition
LightningStrike.h:26
LightningStrike::Animation::flashBrightness
float flashBrightness
Definition
LightningStrike.h:31
LightningStrike::Animation::Start
void Start(uint32_t strikeSeed, double durationSeconds=0)
Definition
LightningStrike.h:37
LightningStrike::Animation::Duration
double Duration() const
Definition
LightningStrike.h:35
LightningStrike::Animation::age
double age
Definition
LightningStrike.h:29
LightningStrike::Animation::inuse
bool inuse
Definition
LightningStrike.h:27
LightningStrike::Animation::growth
float growth
Definition
LightningStrike.h:31
LightningStrike::Animation::seed
uint32_t seed
Definition
LightningStrike.h:28
LightningStrike::Animation::Update
void Update(double elapsed)
Definition
LightningStrike.h:87
ProceduralLightning::Random
Definition
ProceduralLightning.h:16
ProceduralLightning::Random::Signed
float Signed()
Definition
ProceduralLightning.h:19
ProceduralLightning::Random::Next
float Next()
Definition
ProceduralLightning.h:18
RiseMath::Vector3
Definition
ClientMath.h:38
Viewscreen
LightningChain.h
Generated on
for Rise by
1.18.0