Rise
The Vieneo Province
Loading...
Searching...
No Matches
CloudPerlinNoise.h
Go to the documentation of this file.
1#pragma once
2#include <array>
3#include <cmath>
4#include <cstdint>
5#include <vector>
6
7namespace CloudPerlin
8{
9 // Periodic gradient Perlin noise, baked at eight samples per lattice cell.
10 // The quintic fade has zero first and second derivative at cell boundaries.
11 // This is a shape field, not a moisture or turbulence simulation.
12 inline uint32_t Hash(int x,int y,int z)
13 {
14 uint32_t h=uint32_t(x&7)*73856093u^uint32_t(y&7)*19349663u^uint32_t(z&7)*83492791u^0x7a51c239u;
15 h^=h>>16;h*=0x7feb352du;h^=h>>15;h*=0x846ca68bu;return h^(h>>16);
16 }
17 inline float Gradient(int x,int y,int z,float dx,float dy,float dz)
18 {
19 static constexpr int g[12][3]={{1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0},
20 {1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1},{0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}};
21 const auto& a=g[Hash(x,y,z)%12];return (a[0]*dx+a[1]*dy+a[2]*dz)*.70710678118f;
22 }
23 inline float Fade(float t){return t*t*t*(t*(t*6-15)+10);}
24 inline float Evaluate(float x,float y,float z)
25 {
26 const int ix=int(std::floor(x)),iy=int(std::floor(y)),iz=int(std::floor(z));
27 const float fx=x-ix,fy=y-iy,fz=z-iz,u=Fade(fx),v=Fade(fy),w=Fade(fz);
28 float result=0;
29 for(int dz=0;dz<2;++dz)for(int dy=0;dy<2;++dy)for(int dx=0;dx<2;++dx)
30 result+=Gradient(ix+dx,iy+dy,iz+dz,fx-dx,fy-dy,fz-dz)
31 *(dx ? u : 1-u)*(dy ? v : 1-v)*(dz ? w : 1-w);
32 return result;
33 }
34 inline const std::vector<float>& Values()
35 {
36 static const auto values=[] {
37 std::vector<float> result(64*64*64);
38 for(int z=0;z<64;++z)for(int y=0;y<64;++y)for(int x=0;x<64;++x)
39 result[(z*64+y)*64+x]=Evaluate(x/8.f,y/8.f,z/8.f);
40 return result;
41 }();return values;
42 }
43}
SCRIPT_CONTROL SCRIPT_STATE const void int
float Fade(float t)
float Evaluate(float x, float y, float z)
float Gradient(int x, int y, int z, float dx, float dy, float dz)
const std::vector< float > & Values()
uint32_t Hash(int x, int y, int z)