Rise
The Vieneo Province
Toggle main menu visibility
Loading...
Searching...
No Matches
WindshieldFractures.h
Go to the documentation of this file.
1
#pragma once
2
#include <algorithm>
3
#include <array>
4
#include <cmath>
5
#include <cstdint>
6
#include <vector>
7
8
// Deterministic cracks in metres on the same joined pane chart as rain.
9
// Every cut starts on an older fracture; a newly revealed tip cannot float
10
// independently. Splitting convex pieces keeps the complete network planar.
11
namespace
WindshieldFractures
12
{
13
struct
Point
{
float
x
=0,
y
=0; };
14
inline
Point
operator+
(
Point
a,
Point
b){
return
{a.
x
+b.
x
,a.
y
+b.
y
};}
15
inline
Point
operator-
(
Point
a,
Point
b){
return
{a.
x
-b.
x
,a.
y
-b.
y
};}
16
inline
Point
operator*
(
Point
a,
float
s){
return
{a.
x
*s,a.
y
*s};}
17
inline
float
Dot
(
Point
a,
Point
b){
return
a.
x
*b.
x
+a.
y
*b.
y
;}
18
inline
float
Cross
(
Point
a,
Point
b){
return
a.
x
*b.
y
-a.
y
*b.
x
;}
19
struct
Segment
{
Point
a
,
b
;};
20
struct
Piece
{std::vector<Point>
points
;std::vector<bool>
interior
;};
21
inline
float
Area
(
const
Piece
& p)
22
{
23
float
area
=0;
24
for
(
size_t
i=0;i<p.
points
.size();++i)
area
+=
Cross
(p.
points
[i],p.
points
[(i+1)%p.
points
.size()]);
25
return
std::abs(
area
)*.5f;
26
}
27
inline
std::vector<Segment>
Network
(
float
width,
float
height,uint32_t seed)
28
{
29
const
auto
random=[&]() {seed=seed*1664525u+1013904223u;
return
float(seed>>8)/16777216.f;};
30
std::vector<Piece> pieces{{{{0,0},{width,0},{width,height},{0,height}},{
false
,
false
,
false
,
false
}}};
31
std::vector<Segment> cuts;
32
// Artistic fracture density, independent of atmospheric mass density.
33
const
size_t
count=size_t((std::clamp)(width*height*190.f,48.f,384.f));
34
while
(cuts.size()<count)
35
{
36
auto
found=std::max_element(pieces.begin(),pieces.end(),[](
const
Piece
& a,
const
Piece
& b){return Area(a)<Area(b);});
37
Piece
p=*found;
const
size_t
n=p.
points
.size();
38
Point
center{};
for
(
auto
point:p.
points
)center=center+point;center=center*(1.f/n);
39
size_t
start=0;
float
best=-1;
40
for
(
size_t
i=0;i<n;++i)
41
{
42
if
(!cuts.empty() && !p.
interior
[i])
continue
;
43
const
auto
d=p.
points
[(i+1)%n]-p.
points
[i];
44
const
float
score=
Dot
(d,d)*(.6f+.8f*random());
45
if
(score>best){best=score;start=i;}
46
}
47
if
(best<=0)
break
;
48
const
auto
a=p.
points
[start]+(p.
points
[(start+1)%n]-p.
points
[start])*(.28f+.44f*random());
49
const
auto
direction=center-a;
50
float
nearest=1.e20f;
size_t
end=start;
Point
b{};
51
for
(
size_t
i=0;i<n;++i)
if
(i!=start)
52
{
53
const
auto
edge=p.
points
[(i+1)%n]-p.
points
[i],delta=p.
points
[i]-a;
54
const
float
divisor=
Cross
(direction,edge);
55
if
(std::abs(divisor)<1.e-12f)
continue
;
56
const
float
t=
Cross
(delta,edge)/divisor,u=
Cross
(delta,direction)/divisor;
57
if
(t>0 && u>=0 && u<=1 && t<nearest){nearest=t;end=i;b=a+direction*t;}
58
}
59
if
(end==start)
break
;
60
// Each child ends with the new fracture edge.
61
const
auto
child=[&](
size_t
first,
size_t
last,
Point
from,
Point
to) {
62
Piece
q;q.
points
.push_back(from);
63
for
(
size_t
i=first;;i=(i+1)%n)
64
{
65
q.
interior
.push_back(p.
interior
[i]);
66
if
(i==last){q.
points
.push_back(to);
break
;}
67
q.
points
.push_back(p.
points
[(i+1)%n]);
68
}
69
q.
interior
.push_back(
true
);
return
q;
70
};
71
*found=child(start,end,a,b);pieces.push_back(child(end,start,b,a));
72
cuts.push_back({a,b});
73
}
74
return
cuts;
75
}
76
inline
float
SegmentDistanceSquared
(
Point
p,
Segment
line)
77
{
78
const
auto
d=line.
b
-line.
a
;
79
const
float
t=(std::clamp)(
Dot
(p-line.
a
,d)/(std::max)(
Dot
(d,d),1.e-15f),0.f,1.f);
80
const
auto
delta=p-(line.
a
+d*t);
return
Dot
(delta,delta);
81
}
82
constexpr
int
Resolution
=512;
83
using
Texel
=std::array<float,4>;
84
inline
std::vector<Texel>
Field
(
const
std::vector<Segment>& cuts,
float
width,
float
height,
int
condition)
85
{
86
std::vector<Texel> field(
Resolution
*
Resolution
,{-10000,-10000,-10000,-10000});
87
if
(condition>95 || cuts.empty())
return
field;
88
std::vector<float> nearest(field.size(),1.e20f);
89
// Constant number of added fractures per condition point. The partial tip
90
// grows out of its older parent, never fades in as an isolated whole edge.
91
const
float
revealed=1+(cuts.size()-1)*(95-(std::clamp)(condition,0,95))/95.f;
92
for
(
size_t
i=0;i<cuts.size() && i<revealed;++i)
93
{
94
Segment
line=cuts[i];line.
b
=line.
a
+(line.
b
-line.
a
)*(std::min)(1.f,revealed-float(i));
95
// Bake nearest segment ENDPOINTS, not a painted line or sampled alpha.
96
// The shader computes sub-texel distance and the optical edge normal.
97
const
int
x0=(std::max)(0,
int
(std::floor((std::min)(line.
a
.
x
,line.
b
.
x
)/width*
Resolution
))-4);
98
const
int
x1=(std::min)(
Resolution
-1,
int
(std::ceil((std::max)(line.
a
.
x
,line.
b
.
x
)/width*
Resolution
))+4);
99
const
int
y0=(std::max)(0,
int
(std::floor((std::min)(line.
a
.
y
,line.
b
.
y
)/height*
Resolution
))-4);
100
const
int
y1=(std::min)(
Resolution
-1,
int
(std::ceil((std::max)(line.
a
.
y
,line.
b
.
y
)/height*
Resolution
))+4);
101
for
(
int
y=y0;y<=y1;++y)
for
(
int
x=x0;x<=x1;++x)
102
{
103
const
Point
p{(x+.5f)*width/
Resolution
,(y+.5f)*height/
Resolution
};
104
const
float
distance=
SegmentDistanceSquared
(p,line);
const
int
at=y*
Resolution
+x;
105
if
(distance<nearest[at]){nearest[at]=distance;field[at]={line.
a
.
x
,line.
a
.
y
,line.
b
.
x
,line.
b
.
y
};}
106
}
107
}
108
return
field;
109
}
110
}
area
char area[3]
Definition
globals.cpp:87
WindshieldFractures
Definition
WindshieldFractures.h:12
WindshieldFractures::Dot
float Dot(Point a, Point b)
Definition
WindshieldFractures.h:17
WindshieldFractures::Resolution
constexpr int Resolution
Definition
WindshieldFractures.h:82
WindshieldFractures::Area
float Area(const Piece &p)
Definition
WindshieldFractures.h:21
WindshieldFractures::Field
std::vector< Texel > Field(const std::vector< Segment > &cuts, float width, float height, int condition)
Definition
WindshieldFractures.h:84
WindshieldFractures::operator-
Point operator-(Point a, Point b)
Definition
WindshieldFractures.h:15
WindshieldFractures::operator*
Point operator*(Point a, float s)
Definition
WindshieldFractures.h:16
WindshieldFractures::SegmentDistanceSquared
float SegmentDistanceSquared(Point p, Segment line)
Definition
WindshieldFractures.h:76
WindshieldFractures::Cross
float Cross(Point a, Point b)
Definition
WindshieldFractures.h:18
WindshieldFractures::operator+
Point operator+(Point a, Point b)
Definition
WindshieldFractures.h:14
WindshieldFractures::Texel
std::array< float, 4 > Texel
Definition
WindshieldFractures.h:83
WindshieldFractures::Network
std::vector< Segment > Network(float width, float height, uint32_t seed)
Definition
WindshieldFractures.h:27
WindshieldFractures::Piece
Definition
WindshieldFractures.h:20
WindshieldFractures::Piece::points
std::vector< Point > points
Definition
WindshieldFractures.h:20
WindshieldFractures::Piece::interior
std::vector< bool > interior
Definition
WindshieldFractures.h:20
WindshieldFractures::Point
Definition
WindshieldFractures.h:13
WindshieldFractures::Point::y
float y
Definition
WindshieldFractures.h:13
WindshieldFractures::Point::x
float x
Definition
WindshieldFractures.h:13
WindshieldFractures::Segment
Definition
WindshieldFractures.h:19
WindshieldFractures::Segment::b
Point b
Definition
WindshieldFractures.h:19
WindshieldFractures::Segment::a
Point a
Definition
WindshieldFractures.h:19
Rendering
WindshieldFractures.h
Generated on
for Rise by
1.18.0