noise features, fixed some grid stuff.

This commit is contained in:
Yggdrasil75
2026-01-16 13:37:13 -05:00
parent 4dfe6c9a5e
commit acba629774
4 changed files with 226 additions and 8 deletions

View File

@@ -659,6 +659,89 @@ public:
bool isMeshDirty() const {
return meshDirty;
}
std::vector<frame> genSlices(frame::colormap colorFormat = frame::colormap::RGB) const {
TIME_FUNCTION;
int colors;
std::vector<frame> outframes;
switch (colorFormat) {
case frame::colormap::RGBA:
case frame::colormap::BGRA: {
colors = 4;
break;
}
case frame::colormap::B: {
colors = 1;
break;
}
case frame::colormap::RGB:
case frame::colormap::BGR:
default: {
colors = 3;
break;
}
}
size_t cbsize = gridSize.x * gridSize.y * colors;
for (int layer = 0; layer < getDepth(); layer++) {
int layerMult = layer * gridSize.x * gridSize.y;
frame layerFrame(gridSize.x, gridSize.y, colorFormat);
std::vector<uint8_t> colorBuffer(cbsize);
for (int y = 0; y < gridSize.y; y++) {
int yMult = layerMult + (y * gridSize.x);
for (int x = 0; x < gridSize.x; x++) {
int vidx = yMult+x;
int pidx = (y * gridSize.x + x) * colors;
Voxel cv = voxels[vidx];
Vec3ui8 cvColor;
float cvAlpha;
if (cv.active) {
cvColor = cv.color;
cvAlpha = cv.alpha;
} else {
cvColor = Vec3ui8(255,255,255);
cvAlpha = 255;
}
switch (colorFormat) {
case frame::colormap::RGBA: {
colorBuffer[pidx + 1] = cvColor.x;
colorBuffer[pidx + 2] = cvColor.y;
colorBuffer[pidx + 3] = cvColor.z;
colorBuffer[pidx + 0] = cvAlpha;
break;
}
case frame::colormap::BGRA: {
colorBuffer[pidx + 3] = cvColor.x;
colorBuffer[pidx + 2] = cvColor.y;
colorBuffer[pidx + 1] = cvColor.z;
colorBuffer[pidx + 0] = cvAlpha;
break;
}
case frame::colormap::RGB: {
colorBuffer[pidx + 1] = cvColor.x;
colorBuffer[pidx + 2] = cvColor.y;
colorBuffer[pidx + 3] = cvColor.z;
break;
}
case frame::colormap::BGR: {
colorBuffer[pidx + 3] = cvColor.x;
colorBuffer[pidx + 2] = cvColor.y;
colorBuffer[pidx + 1] = cvColor.z;
break;
}
case frame::colormap::B: {
colorBuffer[pidx] = (cvColor.x * 0.299) + (cvColor.y * 0.587) + (cvColor.z * 0.114);
break;
}
}
}
}
layerFrame.setData(colorBuffer);
//layerFrame.compressFrameLZ78();
outframes.emplace_back(layerFrame);
}
return outframes;
}
};
//#include "g3_serialization.hpp" needed to be usable

View File

@@ -62,6 +62,30 @@ private:
permutation.insert(permutation.end(), permutationt.begin(), permutationt.end());
permutation.insert(permutation.end(), permutationt.begin(), permutationt.end());
}
float normalizedNoise(const Vec2<float>& point) {
return (permute(point) + 1.0f) * 0.5f;
}
float mapRange(float value, float inMin, float inMax, float outMin, float outMax) {
return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
float blendNoises(float noise1, float noise2, float blendFactor) {
return lerp(noise1, noise2, blendFactor);
}
float addNoises(float noise1, float noise2) {
return std::clamp(noise1 + noise2, -1.0f, 1.0f);
}
float multiplyNoises(float noise1, float noise2) {
return noise1 * noise2;
}
float hash(int x, int y) {
return (permutation[(x + permutation[y & 255]) & 255] / 255.0f) * 2.0f - 1.0f;
}
public:
PNoise2() : rng(std::random_device{}()) {
initializePermutation();
@@ -74,8 +98,8 @@ public:
template<typename T>
float permute(Vec2<T> point) {
TIME_FUNCTION;
float x = point.x;
float y = point.y;
float x = static_cast<float>(point.x);
float y = static_cast<float>(point.y);
int X = (int)floor(x);
int xmod = X & 255;
int Y = (int)floor(point.y);
@@ -166,6 +190,34 @@ public:
return retval;
}
float valueNoise(const Vec2<float>& point) {
int xi = (int)std::floor(point.x);
int yi = (int)std::floor(point.y);
float tx = point.x - xi;
float ty = point.y - yi;
int rx0 = xi & 255;
int rx1 = (xi + 1) & 255;
int ry0 = yi & 255;
int ry1 = (yi + 1) & 255;
// Random values at corners
float c00 = hash(rx0, ry0);
float c10 = hash(rx1, ry0);
float c01 = hash(rx0, ry1);
float c11 = hash(rx1, ry1);
// Interpolation
float sx = fade(tx);
float sy = fade(ty);
float nx0 = lerp(c00, c10, sx);
float nx1 = lerp(c01, c11, sx);
return lerp(nx0, nx1, sy);
}
Vec4ui8 permuteColor(const Vec3<float>& point) {
TIME_FUNCTION;
float noiseR = permute(point);
@@ -188,6 +240,65 @@ public:
return Vec4ui8(r, g, b, a);
}
template<typename T>
float fractalNoise(const Vec2<T>& point, int octaves, float persistence, float lacunarity) {
float total = 0.0f;
float frequency = 1.f;
float amplitude = 1.f;
float maxV = 0.f;
for (int i = 0; i < octaves; i++) {
total += permute(point*frequency) * amplitude;
maxV += amplitude;
amplitude *= persistence;
frequency *= lacunarity;
}
return total / maxV;
}
template<typename T>
float turbulence(const Vec2<T>& point, int octaves) {
float value = 0.0f;
Vec2<float> tempPoint = point;
for (int i = 0; i < octaves; i++) {
value += std::abs(permute(tempPoint));
tempPoint *= 2.f;
}
return value;
}
float ridgedNoise(const Vec2<float>& point, int octaves, float offset = 1.0f) {
float result = 0.f;
float weight = 1.f;
Vec2<float> p = point;
for (int i = 0; i < octaves; i++) {
float signal = 1.f - std::abs(permute(p));
signal *= signal;
signal *= weight;
weight = signal * offset;
result += signal;
p *= 2.f;
}
return result;
}
float billowNoise(const Vec2<float>& point, int octaves) {
float value = 0.0f;
float amplitude = 1.0f;
float frequency = 1.0f;
for (int i = 0; i < octaves; i++) {
value += std::abs(permute(point * frequency)) * amplitude;
amplitude *= 0.5f;
frequency *= 2.0f;
}
return value;
}
};
#endif