Rise
The Vieneo Province
Loading...
Searching...
No Matches
CloudBillowNoise.h
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <cmath>
4#include <cstdint>
5#include <algorithm>
6
7namespace CloudBillows
8{
9 // Periodic scattered, variable-width lobes with softly merged joins.
10 // Generated once; CPU flight samples and GPU mip zero share the field.
11 // This is geometric cloud sculpting, not atmospheric microphysics.
12 inline uint32_t Hash(int x,int y,int z,uint32_t salt)
13 {
14 uint32_t h=uint32_t(x&7)*73856093u^uint32_t(y&7)*19349663u^uint32_t(z&7)*83492791u^salt;
15 h^=h>>16;h*=0x7feb352du;h^=h>>15;h*=0x846ca68bu;return h^(h>>16);
16 }
17 inline const std::vector<float>& Values()
18 {
19 static const auto values=[] {
20 std::vector<float> first(64*64*64,100.f),second(first);
21 // Independent positions over the entire tile, not one seed per
22 // lattice cell. Unequal support widths let adjacent lobes overlap.
23 // Width >= 1 preserves the <=1 per-axis distance slope used by
24 // conservative clear-air bounds, including the periodic seam.
25 for(int feature=0;feature<256;++feature)
26 {
27 const auto random=[&](uint32_t salt) {
28 return float(Hash(feature,feature>>3,feature>>6,salt)>>8)/16777216.f;
29 };
30 const float width=1.f+1.15f*random(0x31b8f259u);
31 const float center[3]={8*random(0x52495345u),8*random(0x16372abdu),8*random(0x612dbcafu)};
32 float axis[3][64];
33 for(int a=0;a<3;++a)for(int i=0;i<64;++i)
34 {
35 const float distance=std::abs(i/8.f-center[a]);
36 const float wrapped=(std::min)(distance,8.f-distance)/width;
37 axis[a][i]=wrapped*wrapped;
38 }
39 // Search every feature exactly; precomputed axis distances
40 // avoid repeated roots and wrapping in the inner bake loop.
41 for(int z=0;z<64;++z)for(int y=0;y<64;++y)for(int x=0;x<64;++x)
42 {
43 const int index=(z*64+y)*64+x;
44 const float square=axis[0][x]+axis[1][y]+axis[2][z];
45 if(square<first[index]){second[index]=first[index];first[index]=square;}
46 else second[index]=(std::min)(second[index],square);
47 }
48 }
49 std::vector<float> result(first.size());
50 for(size_t i=0;i<result.size();++i)
51 {
52 const float a=std::sqrt(first[i]),b=std::sqrt(second[i]);
53 // Smooth minimum of the two closest scaled distances. Its
54 // derivatives are convex weights, retaining the slope bound.
55 const float blend=(std::max)(0.f,1.f-(b-a)/.18f);
56 result[i]=(std::max)(0.f,a-.045f*blend*blend);
57 }
58 return result;
59 }();
60 return values;
61 }
62 inline float Sample(float x,float y,float z)
63 {
64 x*=8;y*=8;z*=8;
65 const int ix=int(std::floor(x)),iy=int(std::floor(y)),iz=int(std::floor(z));
66 const float fx=x-ix,fy=y-iy,fz=z-iz;
67 const auto& values=Values();float result=0;
68 for(int dz=0;dz<2;++dz)for(int dy=0;dy<2;++dy)for(int dx=0;dx<2;++dx)
69 result+=values[(((iz+dz)&63)*64+((iy+dy)&63))*64+((ix+dx)&63)]
70 *(dx ? fx : 1-fx)*(dy ? fy : 1-fy)*(dz ? fz : 1-fz);
71 return result;
72 }
73}
SCRIPT_CONTROL SCRIPT_STATE const void int
uint32_t Hash(int x, int y, int z, uint32_t salt)
const std::vector< float > & Values()
float Sample(float x, float y, float z)