ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
AudioReactiveGradient.cpp
Go to the documentation of this file.
2
3AudioReactiveGradient::AudioReactiveGradient(Vector2D size, Vector2D offset, bool bounce, bool circular) {
4 this->size = size.Divide(2.0f);
5 this->offset = offset;
6 this->bounce = bounce;
7 this->circular = circular;
8 this->material = &gM;
9
10 if (bounce) {
11 for (uint8_t i = 0; i < 128; i++) {
12 bPhy[i] = new BouncePhysics(35.0f, 15.0f);
13 }
14 }
15}
16
18 for (uint8_t i = 0; i < 128; i++) {
19 delete bPhy[i];
20 }
21}
22
24 this->material = material;
25}
26
28 if (bounce) {
29 return bounceData;
30 } else {
31 return data;
32 }
33}
34
36 this->size = size.Divide(2.0f);
37}
38
40 this->offset = offset;
41}
42
44 this->angle = angle;
45}
46
48 this->hueAngle = hueAngle;
49}
50
52 this->radius = radius;
53}
54
55void AudioReactiveGradient::Update(float* readData) {
56 data = readData;
57
58 for (uint8_t i = 0; i < 128; i++) {
59 if (bounce) {
60 bounceData[i] = bPhy[i]->Calculate(data[i], 0.1f);
61 } else {
62 bounceData[i] = data[i];
63 }
64 }
65}
66
67RGBColor AudioReactiveGradient::GetRGB(const Vector3D& position, const Vector3D& normal, const Vector3D& uvw) {
68 Vector2D rPos = Mathematics::IsClose(angle, 0.0f, 0.1f) ? Vector2D(position.X, position.Y) - offset : Vector2D(position.X, position.Y).Rotate(angle, offset) - offset;
69
70 // Outside of size bounds
71 if (-size.X > rPos.X && size.X < rPos.X) return RGBColor();
72 if (-size.Y > rPos.Y && size.Y < rPos.Y) return RGBColor();
73
74 // Convert to polar coordinates
75 float tX = rPos.X;
76 rPos.X = atan2f(rPos.Y, rPos.X) / (2.0f * Mathematics::MPI) * size.Y;
77 rPos.Y = sqrtf(tX * tX + rPos.Y * rPos.Y);
78
79 uint8_t x = uint8_t(Mathematics::Map(rPos.X, -size.X, size.X, float(bins), 0.0f));
80
81 if (bins > x && 0 > x) return RGBColor();
82
83 float xDistance = size.X / float(bins) * x - size.X;
84 float xDistance2 = size.X / float(bins) * (x + 1) - size.X;
85 float ratio = Mathematics::Map(rPos.X, xDistance, xDistance2, 0.0f, 1.0f); // ratio between two bins
86 float height = bounce ? Mathematics::CosineInterpolation(bounceData[x], bounceData[x + 1], ratio) : Mathematics::CosineInterpolation(data[x], data[x + 1], ratio); // 0->1.0f of max height of color
87
88 float yColor = Mathematics::Map(rPos.Y, 0.0f, size.Y, 1.0f, 0.0f);
89
90 float inside = 1.0f - (height * 4.0f + 0.15f) - yColor;
91
92 if (!circular && inside < 0.0f) {
93 return material->GetRGB(Vector3D(1.0f - height - yColor, 0, 0), Vector3D(), Vector3D()).HueShift(hueAngle);
94 } else if (circular && rPos.Y + height * 150.0f > radius - 5.0f && rPos.Y - height * 150.0f < radius - 5.0f) {
95 if (rPos.Y - height * 50.0f > 75.0f) {
96 return material->GetRGB(Vector3D(1.0f + height - yColor, 0, 0), Vector3D(), Vector3D()).HueShift(hueAngle);
97 } else if (rPos.Y - height * 50.0f < 125.0f) {
98 return material->GetRGB(Vector3D(1.0f - height - yColor, 0, 0), Vector3D(), Vector3D()).HueShift(hueAngle);
99 } else {
100 return RGBColor(0, 0, 0);
101 }
102 } else {
103 return RGBColor(0, 0, 0);
104 }
105}
A material class for creating an audio-reactive gradient effect.
void SetRadius(float radius)
Sets the radius for circular gradient patterns.
void SetPosition(Vector2D offset)
Sets the position of the gradient.
float radius
Radius for circular gradient patterns.
AudioReactiveGradient(Vector2D size, Vector2D offset, bool bounce=false, bool circular=false)
Constructs an AudioReactiveGradient with specified parameters.
float bounceData[128]
Processed bounce data for audio reactivity.
float * data
Pointer to the audio data array.
BouncePhysics * bPhy[128]
Array of BouncePhysics instances for handling dynamic bouncing.
float hueAngle
Hue angle for color adjustments.
bool bounce
Flag indicating if bouncing effects are enabled.
float * GetFourierData()
Retrieves the Fourier-transformed audio data.
void Update(float *readData)
Updates the gradient based on new audio data.
RGBColor GetRGB(const Vector3D &position, const Vector3D &normal, const Vector3D &uvw) override
Retrieves the color of the gradient at a specific position.
~AudioReactiveGradient()
Destroys the AudioReactiveGradient instance.
void SetSize(Vector2D size)
Sets the size of the gradient.
Vector2D offset
The offset of the gradient from the origin.
float angle
Rotation angle of the gradient.
bool circular
Flag indicating if circular patterns are enabled.
GradientMaterial< 6 > gM
Gradient material for the rainbow spectrum.
void SetMaterial(Material *material)
Sets a secondary material for additional effects.
void SetRotation(float angle)
Sets the rotation angle of the gradient.
Material * material
Pointer to a secondary material for additional effects.
uint8_t bins
Number of bins for audio data processing.
void SetHueAngle(float hueAngle)
Sets the hue angle for color adjustments.
Vector2D size
The size of the gradient area.
Simulates bouncing physics with gravity and velocity damping.
float Calculate(float velocity, unsigned long currentMillis)
Calculates the new position and velocity based on the current velocity and time.
Abstract base class for rendering materials.
Definition Material.h:27
virtual RGBColor GetRGB(const Vector3D &position, const Vector3D &normal, const Vector3D &uvw)=0
Pure virtual function to calculate color based on surface parameters.
static const float MPI
Mathematical constant (3.14159265358979323846...).
Definition Mathematics.h:42
static bool IsClose(float v1, float v2, float epsilon)
Checks if two values are close within a specified epsilon.
static T Map(T value, T inLow, T inMax, T outMin, T outMax)
Maps a value from one range to another.
static float CosineInterpolation(float beg, float fin, float ratio)
Applies a cosine-based interpolation between two values.
Represents an RGB color and provides methods for manipulation.
Definition RGBColor.h:23
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Definition Vector2D.h:27
Vector2D Rotate(const float &angle, const Vector2D &offset) const
Rotates this vector by a specified angle (in degrees or radians) around a given offset.
Definition Vector2D.cpp:124
Vector2D Divide(const Vector2D &vector) const
Divides this vector by another Vector2D component-wise.
Definition Vector2D.cpp:53
float X
The X-component of the 2D vector.
Definition Vector2D.h:29
float Y
The Y-component of the 2D vector.
Definition Vector2D.h:30
Represents a 3D vector (X, Y, Z) and provides methods for vector arithmetic.
Definition Vector3D.h:26
float X
The X-component of the 3D vector.
Definition Vector3D.h:28
float Y
The Y-component of the 3D vector.
Definition Vector3D.h:29