Rise
The Vieneo Province
Loading...
Searching...
No Matches
ProceduralLightning.h
Go to the documentation of this file.
1#pragma once
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6#include <vector>
7
8// Geometry is generated once from the network seed. Camera motion and the
9// existing flash envelope change only ribbon facing and radiance, not the path.
11{
14 struct Mesh { std::vector<Vertex> vertices;std::vector<uint16_t> indices;unsigned branches=0; };
15 struct Random
16 {
17 uint32_t state;
18 float Next() {state=state*1664525u+1013904223u;return float(state>>8)*(1.f/16777216.f);}
19 float Signed() {return Next()*2-1;}
20 };
22 {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};}
23 inline float Length(Vector3 v) {return std::sqrt(v.x*v.x+v.y*v.y+v.z*v.z);}
24 inline Vector3 Unit(Vector3 v) {const float l=Length(v);return l>1e-8f ? v/l : Vector3(0,1,0);}
25 inline bool Finite(Vector3 v) {return std::isfinite(v.x)&&std::isfinite(v.y)&&std::isfinite(v.z);}
26 inline Mesh Generate(Vector3 cloudOffset,uint32_t seed)
27 {
28 Mesh mesh;
29 const float height=Length(cloudOffset);
30 if(!Finite(cloudOffset) || height<.001f || height>40)return mesh;
31 Random random{seed^0x9e3779b9u};
32 const auto up=Unit(cloudOffset);
33 const auto right=Unit(Cross(up,std::fabs(up.y)<.9f ? Vector3(0,1,0) : Vector3(1,0,0)));
34 const auto forward=Cross(up,right);
35 const auto path=[&](Vector3 start,Vector3 end,unsigned segments,float deviation) {
36 std::vector<Vector3> points(segments+1);points.front()=start;points.back()=end;
37 for(unsigned stride=segments;stride>1;stride/=2)for(unsigned i=0;i<segments;i+=stride) {
38 const float scale=deviation*std::pow(float(stride)/segments,.85f);
39 points[i+stride/2]=(points[i]+points[i+stride])*.5f+
40 right*(random.Signed()*scale)+forward*(random.Signed()*scale);
41 }
42 return points;
43 };
44 const auto append=[&](const std::vector<Vector3>& points,float width,float energy,bool branch) {
45 const auto first=static_cast<uint16_t>(mesh.vertices.size());
46 for(unsigned i=0;i<points.size();++i) {
47 const auto tangent=Unit(points[(std::min)(i+1,unsigned(points.size()-1))]-points[i?i-1:0]);
48 const float t=float(i)/float(points.size()-1);
49 const float taper=branch ? (std::max)(.08f,1-t) : .85f+.15f*(1-t);
50 const float fade=branch ? (std::min)(1.f,(1-t)*6) : 1.f;
51 for(float side:{-1.f,1.f})mesh.vertices.push_back({points[i],tangent,side,width*taper,energy*fade});
52 if(i) {
53 const auto a=uint16_t(first+(i-1)*2),b=uint16_t(a+1),c=uint16_t(a+2),d=uint16_t(a+3);
54 mesh.indices.insert(mesh.indices.end(),{a,b,c,c,b,d});
55 }
56 }
57 };
58 const auto main=path(cloudOffset,{0,0,0},128,height*.095f);
59 append(main,.018f,1,false);
60 const unsigned branches=7+unsigned(random.Next()*5);
61 for(unsigned i=0;i<branches;++i) {
62 const unsigned attach=12+unsigned(random.Next()*86);
63 const float remaining=height*(1-float(attach)/128);
64 const float drop=remaining*(.2f+random.Next()*.5f);
65 const float angle=random.Next()*6.283185307f;
66 const auto outward=right*std::cos(angle)+forward*std::sin(angle);
67 const auto end=main[attach]-up*drop+outward*(drop*(.45f+random.Next()*.7f));
68 const auto branch=path(main[attach],end,32,drop*.12f);
69 append(branch,.009f,.35f+random.Next()*.25f,true);++mesh.branches;
70 if(i%3==0) {
71 const auto forkEnd=branch[12]-up*(drop*.25f)+forward*(random.Signed()*drop*.4f);
72 append(path(branch[12],forkEnd,16,drop*.04f),.005f,.2f,true);++mesh.branches;
73 }
74 }
75 return mesh;
76 }
77}
Vector3 Unit(Vector3 v)
Mesh Generate(Vector3 cloudOffset, uint32_t seed)
float Length(Vector3 v)
Vector3 Cross(Vector3 a, Vector3 b)
std::vector< Vertex > vertices
std::vector< uint16_t > indices