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

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

View File

@@ -7,18 +7,53 @@
#include <functional>
#include <random>
#include "../vectorlogic/vec2.hpp"
#include "../vectorlogic/vec3.hpp"
#include "../timing_decorator.hpp"
class PNoise2 {
private:
std::vector<float> permutation;
std::vector<int> permutation;
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:
PNoise2() : rng(std::random_device{}()){
//permutation.reserve(256);
std::vector<float> permutationt;
for (int i = 0; i < 255; i++){
//permutation[i] = i;
PNoise2() : rng(std::random_device{}()) {
std::vector<int> permutationt;
permutationt.reserve(256);
for (int i = 0; i < 256; i++){
permutationt.push_back(i);
}
std::ranges::shuffle(permutationt, rng);
@@ -27,6 +62,7 @@ public:
}
float permute(Vec2 point) {
TIME_FUNCTION;
float x = point.x;
float y = point.y;
int X = (int)floor(x);
@@ -36,42 +72,86 @@ public:
float xf = point.x - X;
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 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 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 dBR = BR.dot(GetConstantVector(vBR));
float dTL = TL.dot(GetConstantVector(vTL));
float dTR = TR.dot(GetConstantVector(vTR));
float u = Fade(xf);
float v = Fade(yf);
float u = fade(xf);
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 permute(Vec3 point) {
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;
Vec3 FBL = Vec3(xf-0, yf-0, zf-0);
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);
Vec3 RBL = Vec3(xf-0, yf-0, zf-1);
Vec3 RBR = Vec3(xf-1, yf-0, zf-1);
Vec3 RTL = Vec3(xf-0, yf-1, zf-1);
Vec3 RTR = Vec3(xf-1, yf-1, zf-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 lerp(float t, float a1, float a2) {
return a1 + t * (a2 - a1);
}
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 Fade(float t) {
return (((6 * t - 15)* t + 10) * t * t * t);
}
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);
Vec2 GetConstantVector(float v) {
int h = (int)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);
float retval = lerp(w, y1, y2);
std::cout << "returning: " << retval << std::endl;
return retval;
}
};