Rise
The Vieneo Province
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.
12{
13struct Point { float x=0,y=0; };
14inline Point operator+(Point a,Point b){return {a.x+b.x,a.y+b.y};}
15inline Point operator-(Point a,Point b){return {a.x-b.x,a.y-b.y};}
16inline Point operator*(Point a,float s){return {a.x*s,a.y*s};}
17inline float Dot(Point a,Point b){return a.x*b.x+a.y*b.y;}
18inline float Cross(Point a,Point b){return a.x*b.y-a.y*b.x;}
19struct Segment {Point a,b;};
20struct Piece {std::vector<Point> points;std::vector<bool> interior;};
21inline 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}
27inline 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}
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}
82constexpr int Resolution=512;
83using Texel=std::array<float,4>;
84inline 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}
char area[3]
Definition globals.cpp:87
float Dot(Point a, Point b)
float Area(const Piece &p)
std::vector< Texel > Field(const std::vector< Segment > &cuts, float width, float height, int condition)
Point operator-(Point a, Point b)
Point operator*(Point a, float s)
float SegmentDistanceSquared(Point p, Segment line)
float Cross(Point a, Point b)
Point operator+(Point a, Point b)
std::array< float, 4 > Texel
std::vector< Segment > Network(float width, float height, uint32_t seed)