ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
SimplexNoise.h
Go to the documentation of this file.
1/**
2 * @file SimplexNoise.h
3 * @brief A material class that generates and applies Simplex Noise.
4 *
5 * This class utilizes Simplex Noise algorithms for 2D and 3D noise generation,
6 * mapping the noise values to colors through a gradient material.
7 *
8 * @date 22/12/2024
9 * @author Coela Can't
10 */
11
12#pragma once
13
14#include <cstdlib>
15#include "../Material.h"
16#include "../../../Utils/Math/Mathematics.h"
17#include "../../../Utils/Math/Vector3D.h"
18#include "../Static/GradientMaterial.h"
19
20/**
21 * @class SimplexNoise
22 * @brief A material class for rendering Simplex Noise.
23 *
24 * This template-based class generates noise values using the Simplex Noise algorithm
25 * and maps the values to colors using a `GradientMaterial`.
26 *
27 * @tparam colors The number of colors in the gradient material.
28 */
29template<size_t colors>
30class SimplexNoise : public Material {
31private:
32 GradientMaterial<colors>* gradientMaterial; ///< GradientMaterial used to map noise values to colors.
33
34 Vector3D noiseScale = Vector3D(1, 1, 1); ///< Scaling factor for the noise generation.
35 const float F2 = 0.5f * (sqrtf(3.0f) - 1.0f); ///< Simplex Noise constants for 2D.
36 const float G2 = (3.0f - sqrtf(3.0f)) / 6.0f; ///< Simplex Noise constants for 2D.
37 const float F3 = 1.0f / 3.0f; ///< Simplex Noise constants for 3D.
38 const float G3 = 1.0f / 6.0f; ///< Simplex Noise constants for 3D.
39 float zPosition = 0.0f; ///< Z-position for 3D noise.
40
41 // Gradient vectors for noise generation.
42 Vector3D grad3[12] = {Vector3D(1, 1, 0), Vector3D(-1, 1, 0), Vector3D(1, -1, 0), Vector3D(-1, -1, 0),
43 Vector3D(1, 0, 1), Vector3D(-1, 0, 1), Vector3D(1, 0, -1), Vector3D(-1, 0, -1),
44 Vector3D(0, 1, 1), Vector3D(0, -1, 1), Vector3D(0, 1, -1), Vector3D(0, -1, -1) };
45
46
47 uint8_t p_supply[256] = {151,160,137,91,90,15, //this contains all the numbers between 0 and 255, these are put in a random order depending upon the seed
48 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
49 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
50 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
51 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
52 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
53 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
54 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
55 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
56 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
57 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
58 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
59 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
60
61 uint8_t p[256] = {151,160,137,91,90,15, //this contains all the numbers between 0 and 255, these are put in a random order depending upon the seed
62 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
63 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
64 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
65 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
66 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
67 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
68 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
69 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
70 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
71 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
72 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
73 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180};
74
75 // Permutation arrays for noise generation.
76 uint8_t perm[512]; ///< Permutation table for noise generation.
77 uint8_t permMod12[512]; ///< Permutation table modulo 12 for gradient selection.
78
79 /**
80 * @brief Initializes the permutation tables based on the seed.
81 *
82 * @param seed The seed for randomizing the permutation tables.
83 */
84 void InitializePermutation(int seed);
85
86public:
87 /**
88 * @brief Constructs a SimplexNoise instance.
89 *
90 * @param seed The seed for noise generation.
91 * @param gradientMaterial Pointer to a `GradientMaterial` for mapping noise to colors.
92 */
94
95 /**
96 * @brief Generates 2D Simplex Noise.
97 *
98 * @param xin X-coordinate.
99 * @param yin Y-coordinate.
100 * @return The noise value at the given coordinates.
101 */
102 float Noise(float xin, float yin);
103
104 /**
105 * @brief Generates 3D Simplex Noise.
106 *
107 * @param xin X-coordinate.
108 * @param yin Y-coordinate.
109 * @param zin Z-coordinate.
110 * @return The noise value at the given coordinates.
111 */
112 float Noise(float xin, float yin, float zin);
113
114 /**
115 * @brief Sets the scale for noise generation.
116 *
117 * @param noiseScale A Vector3D representing the scaling factors for X, Y, and Z.
118 */
120
121 /**
122 * @brief Sets the Z-position for 3D noise generation.
123 *
124 * @param zPosition The Z-coordinate for noise generation.
125 */
127
128 /**
129 * @brief Retrieves the color for a given position based on the noise value.
130 *
131 * @param position 3D position in the scene.
132 * @param normal Normal vector at the position (not used for this material).
133 * @param uvw Texture coordinates at the position (not used for this material).
134 * @return The RGB color corresponding to the noise value at the position.
135 */
136 RGBColor GetRGB(const Vector3D& position, const Vector3D& normal, const Vector3D& uvw) override;
137};
138
139#include "SimplexNoise.tpp"
Creates a customizable gradient material for rendering.
Abstract base class for rendering materials.
Definition Material.h:27
Represents an RGB color and provides methods for manipulation.
Definition RGBColor.h:23
A material class for rendering Simplex Noise.
uint8_t p[256]
Vector3D grad3[12]
const float G2
Simplex Noise constants for 2D.
GradientMaterial< colors > * gradientMaterial
GradientMaterial used to map noise values to colors.
void SetScale(Vector3D noiseScale)
Sets the scale for noise generation.
void SetZPosition(float zPosition)
Sets the Z-position for 3D noise generation.
SimplexNoise(int seed, GradientMaterial< colors > *gradientMaterial)
Constructs a SimplexNoise instance.
void InitializePermutation(int seed)
Initializes the permutation tables based on the seed.
float Noise(float xin, float yin, float zin)
Generates 3D Simplex Noise.
const float G3
Simplex Noise constants for 3D.
Vector3D noiseScale
Scaling factor for the noise generation.
uint8_t p_supply[256]
float Noise(float xin, float yin)
Generates 2D Simplex Noise.
uint8_t permMod12[512]
Permutation table modulo 12 for gradient selection.
RGBColor GetRGB(const Vector3D &position, const Vector3D &normal, const Vector3D &uvw) override
Retrieves the color for a given position based on the noise value.
uint8_t perm[512]
Permutation table for noise generation.
float zPosition
Z-position for 3D noise.
const float F2
Simplex Noise constants for 2D.
const float F3
Simplex Noise constants for 3D.
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26