Guest User

Untitled

a guest
Jan 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(Camera), typeof(AudioSource))]
  4. public class AudioVisualizer : MonoBehaviour
  5. {
  6. /* ... */
  7.  
  8. void Update()
  9. {
  10. /* ... */
  11.  
  12. // Find minimum and maximum value.
  13. // Use a second array of floats to smooth the animation.
  14. int sampleLen = samples.Length;
  15. float minSample = 0.0f;
  16. float maxSample = 0.0f;
  17. for(int i = 0; i < sampleLen; ++i)
  18. {
  19. // SmoothStep is a variant easing function; could also use Lerp.
  20. smoothed[i] = Mathf.SmoothStep(smoothed[i],
  21. samples[i],
  22. smoothingFactor);
  23. minSample = Mathf.Min(minSample, smoothed[i]);
  24. maxSample = Mathf.Max(maxSample, smoothed[i]);
  25. }
  26.  
  27. int barLen = bars.Length;
  28. float toPercent = 1.0f / (barLen - 1);
  29. for(int i = 0; i < barLen; ++i)
  30. {
  31. // Convert progress through bars array to samples array index.
  32. float iPercent = i * toPercent;
  33. int sampleIndex = (int)(iPercent * (sampleLen - 1));
  34.  
  35. // Get sample, convert to bar height.
  36. float sample = smoothed[sampleIndex];
  37. float barHeight = P5.Map(sample,
  38. minSample, maxSample,
  39. barMinHeight, barMaxHeight);
  40. Vector3 scale = originalBarScale;
  41. scale.y = barHeight;
  42.  
  43. // Get transform, set properties.
  44. GameObject bar = bars[i];
  45. Transform tr = bar.GetComponent<Transform>();
  46. tr.localScale = scale;
  47. }
  48. }
  49. }
Add Comment
Please, Sign In to add comment