pnoise2 is a misnomer, it supports 2d and 3d.

This commit is contained in:
yggdrasil75
2025-11-22 12:11:33 -05:00
parent 4508c8d7f6
commit c4b6cd6c54
4 changed files with 154 additions and 55 deletions

View File

@@ -93,7 +93,10 @@
"stack": "cpp", "stack": "cpp",
"future": "cpp", "future": "cpp",
"coroutine": "cpp", "coroutine": "cpp",
"resumable": "cpp" "resumable": "cpp",
"set": "cpp",
"shared_mutex": "cpp",
"cfenv": "cpp"
}, },
"files.exclude": { "files.exclude": {
"**/*.rpyc": true, "**/*.rpyc": true,

View File

@@ -49,6 +49,7 @@ struct AnimationConfig {
int totalFrames = 480; int totalFrames = 480;
float fps = 30.0f; float fps = 30.0f;
int numSeeds = 8; int numSeeds = 8;
int noisemod = 42;
}; };
Grid2 setup(AnimationConfig config) { Grid2 setup(AnimationConfig config) {
@@ -232,7 +233,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) {
if (gradnoise == 0) { if (gradnoise == 0) {
grid = setup(config); grid = setup(config);
} else if (gradnoise == 1) { } else if (gradnoise == 1) {
grid = grid.noiseGenGrid(0,0,config.height, config.width); grid = grid.noiseGenGrid(0,0,config.height, config.width, 0.01, 1.0, true, config.noisemod);
} }
grid.setDefault(Vec4(0,0,0,0)); grid.setDefault(Vec4(0,0,0,0));
{ {
@@ -241,7 +242,9 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) {
state.hasNewFrame = true; state.hasNewFrame = true;
state.currentFrame = 0; state.currentFrame = 0;
} }
std::cout << "generated grid" << std::endl;
Preview(grid); Preview(grid);
std::cout << "generated preview" << std::endl;
std::vector<std::tuple<size_t, Vec2, Vec4>> seeds = pickSeeds(grid, config); std::vector<std::tuple<size_t, Vec2, Vec4>> seeds = pickSeeds(grid, config);
std::vector<frame> frames; std::vector<frame> frames;
@@ -262,7 +265,7 @@ void mainLogic(const AnimationConfig& config, Shared& state, int gradnoise) {
// Print compression info for this frame // Print compression info for this frame
if (i % 10 == 0 ) { if (i % 10 == 0 ) {
frame bgrframe; frame bgrframe;
//std::cout << "Processing frame " << i + 1 << "/" << config.totalFrames << std::endl; std::cout << "Processing frame " << i + 1 << "/" << config.totalFrames << std::endl;
bgrframe = grid.getGridAsFrame(frame::colormap::BGR); bgrframe = grid.getGridAsFrame(frame::colormap::BGR);
frames.push_back(bgrframe); frames.push_back(bgrframe);
//bgrframe.decompress(); //bgrframe.decompress();
@@ -375,6 +378,7 @@ int main() {
static int i2 = 1024; static int i2 = 1024;
static int i3 = 480; static int i3 = 480;
static int i4 = 8; static int i4 = 8;
static int noisemod = 42;
static float fs = 1.0; static float fs = 1.0;
std::future<void> mainlogicthread; std::future<void> mainlogicthread;
@@ -398,7 +402,8 @@ int main() {
ImGui::SliderInt("width", &i1, 256, 4096); ImGui::SliderInt("width", &i1, 256, 4096);
ImGui::SliderInt("height", &i2, 256, 4096); ImGui::SliderInt("height", &i2, 256, 4096);
ImGui::SliderInt("frame count", &i3, 10, 5000); ImGui::SliderInt("frame count", &i3, 10, 5000);
ImGui::SliderInt("numSeeds", &i4, 0, 10); ImGui::SliderInt("number of Seeds", &i4, 0, 10);
ImGui::SliderInt("Noise Mod", &noisemod, 0, 1000);
ImGui::SliderFloat("Scale Preview", &fs, 0.0, 2.0); ImGui::SliderFloat("Scale Preview", &fs, 0.0, 2.0);
ImGui::RadioButton("Gradient", &gradnoise, 0); ImGui::RadioButton("Gradient", &gradnoise, 0);
ImGui::RadioButton("Perlin Noise", &gradnoise, 1); ImGui::RadioButton("Perlin Noise", &gradnoise, 1);
@@ -408,7 +413,7 @@ int main() {
} }
if (ImGui::Button("Generate Animation")) { if (ImGui::Button("Generate Animation")) {
config = AnimationConfig(i1, i2, i3, f, i4); config = AnimationConfig(i1, i2, i3, f, i4, noisemod);
mainlogicthread = std::async(std::launch::async, mainLogic, config, std::ref(state), gradnoise); mainlogicthread = std::async(std::launch::async, mainLogic, config, std::ref(state), gradnoise);
} }

View File

@@ -3,6 +3,7 @@
#include <unordered_map> #include <unordered_map>
#include "../vectorlogic/vec2.hpp" #include "../vectorlogic/vec2.hpp"
#include "../vectorlogic/vec3.hpp"
#include "../vectorlogic/vec4.hpp" #include "../vectorlogic/vec4.hpp"
#include "../timing_decorator.hpp" #include "../timing_decorator.hpp"
#include "../output/frame.hpp" #include "../output/frame.hpp"
@@ -10,6 +11,8 @@
#include <vector> #include <vector>
#include <unordered_set> #include <unordered_set>
const float EPSILON = 0.0000000000000000000000001;
class reverselookupassistantclasscausecppisdumb { class reverselookupassistantclasscausecppisdumb {
private: private:
std::unordered_map<size_t, Vec2> Positions; std::unordered_map<size_t, Vec2> Positions;
@@ -223,8 +226,9 @@ public:
return it; return it;
} }
Grid2 noiseGenGrid(size_t minx,size_t miny, size_t maxx, size_t maxy Grid2 noiseGenGrid(size_t minx,size_t miny, size_t maxx, size_t maxy, float minChance = 0.1f
, float minChance = 0.1f, float maxChance = 1.0f, bool color = true) { , float maxChance = 1.0f, bool color = true, int noisemod = 42) {
TIME_FUNCTION;
std::cout << "generating a noise grid with the following: (" << minx << ", " << miny std::cout << "generating a noise grid with the following: (" << minx << ", " << miny
<< ") by (" << maxx << ", " << maxy << ") " << "chance: " << minChance << ") by (" << maxx << ", " << maxy << ") " << "chance: " << minChance
<< " max: " << maxChance << " gen colors: " << color << std::endl; << " max: " << maxChance << " gen colors: " << color << std::endl;
@@ -233,21 +237,24 @@ public:
std::vector<float> sizes; std::vector<float> sizes;
for (int x = minx; x < maxx; x++) { for (int x = minx; x < maxx; x++) {
for (int y = miny; y < maxy; y++) { for (int y = miny; y < maxy; y++) {
Vec2 pos = Vec2(x,y); float nx = (x+noisemod)/(maxx+EPSILON)/0.1;
float alpha = noisegen.permute(Vec2(x,y)); float ny = (y+noisemod)/(maxy+EPSILON)/0.1;
Vec2 pos = Vec2(nx,ny);
float alpha = noisegen.permute(pos);
if (alpha > minChance && alpha < maxChance) { if (alpha > minChance && alpha < maxChance) {
std::cout << "generating at: " << pos.x << ", " << pos.y << ")" << std::endl;
if (color) { if (color) {
float red = noisegen.permute(pos); // float red = noisegen.noise(x,y,1000);
float green = noisegen.permute(pos); // float green = noisegen.noise(x,y,2000);
float blue = noisegen.permute(pos); // float blue = noisegen.noise(x,y,3000);
Vec4 newc = Vec4(red,green,blue,alpha); float red = noisegen.permute(Vec2(nx*0.3,ny*0.3));
float green = noisegen.permute(Vec2(nx*0.6,ny*.06));
float blue = noisegen.permute(Vec2(nx*0.9,ny*0.9));
Vec4 newc = Vec4(red,green,blue,1.0);
colors.push_back(newc); colors.push_back(newc);
poses.push_back(pos); poses.push_back(Vec2(x,y));
sizes.push_back(1.0f); sizes.push_back(1.0f);
} } else {
else { Vec4 newc = Vec4(alpha,alpha,alpha,1.0);
Vec4 newc = Vec4(alpha,alpha,alpha,alpha);
colors.push_back(newc); colors.push_back(newc);
poses.push_back(pos); poses.push_back(pos);
sizes.push_back(1.0f); sizes.push_back(1.0f);
@@ -255,6 +262,7 @@ public:
} }
} }
} }
std::cout << "noise generated" << std::endl;
bulkAddObjects(poses,colors,sizes); bulkAddObjects(poses,colors,sizes);
return *this; return *this;
} }
@@ -558,19 +566,22 @@ public:
} }
// Batch insertion // Batch insertion
//#pragma omp parallel for std::vector<size_t> newids;
for (size_t i = 0; i < poses.size(); ++i) { for (size_t i = 0; i < poses.size(); ++i) {
size_t id = Positions.set(poses[i]); size_t id = Positions.set(poses[i]);
Colors[id] = colors[i]; Colors[id] = colors[i];
Sizes[id] = sizes[i]; Sizes[id] = sizes[i];
spatialGrid.insert(id,poses[i]); spatialGrid.insert(id,poses[i]);
newids.push_back(id);
} }
shrinkIfNeeded(); shrinkIfNeeded();
std::cout << "shrunk. " << std::endl;
updateNeighborMap(); updateNeighborMap();
std::cout << "neighbormap updated. " << std::endl;
usable = true; usable = true;
return getAllIDs(); return newids;
} }
//get all ids //get all ids
@@ -955,7 +966,7 @@ public:
neighborMap.clear(); neighborMap.clear();
// For each object, find nearby neighbors // For each object, find nearby neighbors
//#pragma omp parallel for #pragma omp parallel for
for (const auto& [id1, pos1] : Positions) { for (const auto& [id1, pos1] : Positions) {
std::vector<size_t> neighbors; std::vector<size_t> neighbors;
float radiusSq = neighborRadius * neighborRadius; float radiusSq = neighborRadius * neighborRadius;

View File

@@ -7,18 +7,53 @@
#include <functional> #include <functional>
#include <random> #include <random>
#include "../vectorlogic/vec2.hpp" #include "../vectorlogic/vec2.hpp"
#include "../vectorlogic/vec3.hpp"
#include "../timing_decorator.hpp"
class PNoise2 { class PNoise2 {
private: private:
std::vector<float> permutation; std::vector<int> permutation;
std::default_random_engine rng; std::default_random_engine rng;
float TR,TL,BR,BL; float lerp(float t, float a1, float a2) {
return a1 + t * (a2 - a1);
}
static double fade(double t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
Vec2 GetConstantVector(int v) {
int h = v & 3;
if (h == 0) return Vec2(1,1);
else if (h == 1) return Vec2(-1,1);
else if (h == 2) return Vec2(-1,-1);
else return Vec2(1,-1);
}
Vec3 GetConstantVector3(int v) {
int h = v & 7;
if (h == 0) return Vec3(1,1,1);
else if (h == 1) return Vec3(-1,1, 1);
else if (h == 2) return Vec3(-1,-1, 1);
else if (h == 3) return Vec3(-1,-1, 1);
else if (h == 4) return Vec3(-1,-1,-1);
else if (h == 5) return Vec3(-1,-1, -1);
else if (h == 6) return Vec3(-1,-1, -1);
else return Vec3(1,-1, -1);
}
static double grad(int hash, double x, double y, double z = 0.0) {
int h = hash & 15;
double u = h < 8 ? x : y;
double v = h < 4 ? y : (h == 12 || h == 14 ? x : z);
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
}
public: public:
PNoise2() : rng(std::random_device{}()) { PNoise2() : rng(std::random_device{}()) {
//permutation.reserve(256); std::vector<int> permutationt;
std::vector<float> permutationt; permutationt.reserve(256);
for (int i = 0; i < 255; i++){ for (int i = 0; i < 256; i++){
//permutation[i] = i;
permutationt.push_back(i); permutationt.push_back(i);
} }
std::ranges::shuffle(permutationt, rng); std::ranges::shuffle(permutationt, rng);
@@ -27,6 +62,7 @@ public:
} }
float permute(Vec2 point) { float permute(Vec2 point) {
TIME_FUNCTION;
float x = point.x; float x = point.x;
float y = point.y; float y = point.y;
int X = (int)floor(x); int X = (int)floor(x);
@@ -36,42 +72,86 @@ public:
float xf = point.x - X; float xf = point.x - X;
float yf = point.y - Y; float yf = point.y - Y;
Vec2 TR = Vec2(xf-1, yf-1);
Vec2 TL = Vec2(xf-0, yf-1);
Vec2 BR = Vec2(xf-1, yf-0);
Vec2 BL = Vec2(xf-0, yf-0); Vec2 BL = Vec2(xf-0, yf-0);
Vec2 BR = Vec2(xf-1, yf-0);
Vec2 TL = Vec2(xf-0, yf-1);
Vec2 TR = Vec2(xf-1, yf-1);
int vTR = permutation[permutation[xmod+1]+ymod+1];
int vTL = permutation[permutation[xmod+0]+ymod+1];
int vBR = permutation[permutation[xmod+1]+ymod+0];
int vBL = permutation[permutation[xmod+0]+ymod+0]; int vBL = permutation[permutation[xmod+0]+ymod+0];
int vBR = permutation[permutation[xmod+1]+ymod+0];
int vTL = permutation[permutation[xmod+0]+ymod+1];
int vTR = permutation[permutation[xmod+1]+ymod+1];
float dTR = TR.dot(GetConstantVector(vTR));
float dTL = TL.dot(GetConstantVector(vTL));
float dBR = BR.dot(GetConstantVector(vBR));
float dBL = BL.dot(GetConstantVector(vBL)); float dBL = BL.dot(GetConstantVector(vBL));
float dBR = BR.dot(GetConstantVector(vBR));
float dTL = TL.dot(GetConstantVector(vTL));
float dTR = TR.dot(GetConstantVector(vTR));
float u = Fade(xf); float u = fade(xf);
float v = Fade(yf); float v = fade(yf);
return lerp(u,lerp(v,dBL,dTL),lerp(v,dBR,dTR));
float x1 = lerp(u, grad(vBL, xf, yf), grad(vBR, xf - 1, yf));
float x2 = lerp(u, grad(vTL, xf, yf - 1), grad(vTR, xf - 1, yf - 1));
float retval = lerp(v, x1, x2);
//std::cout << "returning: " << retval << std::endl;
return retval;
} }
float lerp(float t, float a1, float a2) { float permute(Vec3 point) {
return a1 + t * (a2 - a1); TIME_FUNCTION;
} int X = (int)floor(point.x) & 255;
int Y = (int)floor(point.y) & 255;
int Z = (int)floor(point.z) & 255;
float xf = point.x - X;
float yf = point.y - Y;
float zf = point.z - Z;
float Fade(float t) { Vec3 FBL = Vec3(xf-0, yf-0, zf-0);
return (((6 * t - 15)* t + 10) * t * t * t); Vec3 FBR = Vec3(xf-1, yf-0, zf-0);
} Vec3 FTL = Vec3(xf-0, yf-1, zf-0);
Vec3 FTR = Vec3(xf-1, yf-1, zf-0);
Vec2 GetConstantVector(float v) { Vec3 RBL = Vec3(xf-0, yf-0, zf-1);
int h = (int)v & 3; Vec3 RBR = Vec3(xf-1, yf-0, zf-1);
if (h == 0) return Vec2(1,1); Vec3 RTL = Vec3(xf-0, yf-1, zf-1);
else if (h == 1) return Vec2(-1,1); Vec3 RTR = Vec3(xf-1, yf-1, zf-1);
else if (h == 2) return Vec2(-1,-1);
else return Vec2(1,-1); int vFBL = permutation[permutation[permutation[Z+0]+X+0]+Y+0];
int vFBR = permutation[permutation[permutation[Z+0]+X+1]+Y+0];
int vFTL = permutation[permutation[permutation[Z+0]+X+0]+Y+1];
int vFTR = permutation[permutation[permutation[Z+0]+X+1]+Y+1];
int vRBL = permutation[permutation[permutation[Z+1]+X+0]+Y+0];
int vRBR = permutation[permutation[permutation[Z+1]+X+1]+Y+0];
int vRTL = permutation[permutation[permutation[Z+1]+X+0]+Y+1];
int vRTR = permutation[permutation[permutation[Z+1]+X+1]+Y+1];
float dFBL = FBL.dot(GetConstantVector3(vFBL));
float dFBR = FBR.dot(GetConstantVector3(vFBR));
float dFTL = FTL.dot(GetConstantVector3(vFTL));
float dFTR = FTR.dot(GetConstantVector3(vFTR));
float dRBL = RBL.dot(GetConstantVector3(vRBL));
float dRBR = RBR.dot(GetConstantVector3(vRBR));
float dRTL = RTL.dot(GetConstantVector3(vRTL));
float dRTR = RTR.dot(GetConstantVector3(vRTR));
float u = fade(xf);
float v = fade(yf);
float w = fade(zf);
float x1 = lerp(u, grad(vFBL, xf, yf + 0, zf + 0), grad(vFBR, xf - 1, yf + 0, zf + 0));
float x2 = lerp(u, grad(vFTL, xf, yf - 1, zf + 0), grad(vFTR, xf - 1, yf - 1, zf + 0));
float y1 = lerp(v, x1, x2);
float x3 = lerp(u, grad(vRBL, xf, yf - 1, zf + 1), grad(vRBR, xf - 1, yf - 1, zf + 1));
float x4 = lerp(u, grad(vRTL, xf, yf - 1, zf + 1), grad(vRTR, xf - 1, yf - 1, zf + 1));
float y2 = lerp(v, x3, x4);
float retval = lerp(w, y1, y2);
std::cout << "returning: " << retval << std::endl;
return retval;
} }
}; };