using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MultiSourceAudioVisualizerGradient : MonoBehaviour
{
public GameObject[] Visualizer; // Array of visualizer bars
public float smoothingSpeed = 5f; // Controls how fast the visualizer responds
private float[] spectrumData = new float[64]; // Raw spectrum data
private float[] smoothedSpectrumData = new float[64]; // Smoothed spectrum data
public float visualizerScale = 5f; // Controls how much the bars scale
// Material property settings
public string materialColorProperty = "_Color"; // Material color property to modify
public string materialEmissionProperty = "_EmissionColor"; // Emission property to modify
public float emissionIntensity = 2f; // Emission intensity multiplier
// Gradient for smooth color transitions
public Gradient spectrumGradient; // Define colors in the Unity Inspector
void Update()
{
// Reset spectrumData to zero at the start of each frame
for (int i = 0; i < spectrumData.Length; i++)
{
spectrumData[i] = 0f;
}
// Find all AudioSource objects in the scene
AudioSource[] audioSources = FindObjectsOfType<AudioSource>();
// Accumulate spectrum data from all audio sources
foreach (AudioSource audioSource in audioSources)
{
if (audioSource.isPlaying && !audioSource.mute)
{
float[] tempSpectrum = new float[spectrumData.Length];
audioSource.GetSpectrumData(tempSpectrum, 0, FFTWindow.Rectangular);
for (int i = 0; i < spectrumData.Length; i++)
{
spectrumData[i] += tempSpectrum[i];
}
}
}
for (int i = 0; i < Visualizer.Length; i++)
{
if (i >= spectrumData.Length)
{
Debug.LogWarning($"Visualizer index {i} exceeds spectrumData length {spectrumData.Length}. Stopping loop early.");
break;
}
if (Visualizer[i] == null)
{
Debug.LogWarning($"Visualizer element at index {i} is null. Skipping this element.");
continue;
}
// Smooth out the spectrum data
smoothedSpectrumData[i] = Mathf.Lerp(smoothedSpectrumData[i], spectrumData[i], Time.deltaTime * smoothingSpeed);
// Scale the visualizer bars based on the smoothed spectrum data
float newYScale = Mathf.Lerp(0.1f, visualizerScale, smoothedSpectrumData[i] * 100); // Adjust scaling factor as needed
Vector3 newScale = Visualizer[i].transform.localScale;
newScale.y = newYScale;
Visualizer[i].transform.localScale = newScale;
// Update material properties with gradient color
Renderer renderer = Visualizer[i].GetComponent<Renderer>();
if (renderer != null)
{
Material material = renderer.material;
// Calculate the normalized position in the visualizer array
float normalizedPosition = (float)i / (Visualizer.Length - 1); // Maps `i` to a range [0, 1]
// Get the color from the gradient based on normalized position
Color gradientColor = spectrumGradient.Evaluate(normalizedPosition);
// Adjust intensity using smoothed spectrum data
Color finalColor = gradientColor * Mathf.Clamp01(smoothedSpectrumData[i] * 10f);
// Set material color and emission
material.SetColor(materialColorProperty, finalColor);
material.SetColor(materialEmissionProperty, finalColor * emissionIntensity);
// Enable emission (if not already enabled)
material.EnableKeyword("_EMISSION");
}
else
{
Debug.LogWarning($"Renderer component missing on Visualizer element at index {i}. Skipping material updates.");
}
}
}
}