MaximilianPs

MalfunctioningLight

Aug 3rd, 2024 (edited)
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.66 KB | Source Code | 0 0
  1. using UnityEngine;
  2.  
  3. namespace BrokenWings.Props
  4. {
  5.     public class MalfunctioningLight : MonoBehaviour
  6.     {
  7.         [Tooltip("If the light is on the same gameObject you can leave it alone.")]
  8.         [SerializeField] private Light lightSource;
  9.         [Tooltip("Array of light audio clips, you can leave it empty if you don't want any sound!")]
  10.         [SerializeField] private AudioClip[] clips;
  11.         [Space]
  12.         [Header("Light Intensity:")]
  13.         [SerializeField] private float offIntensity = 0.0f;
  14.         [SerializeField] private float onIntensity = 2.0f;
  15.         [SerializeField] private bool isInverted = false; // New parameter to invert behavior
  16.         [SerializeField] private float sensitivity = 35.3f;
  17.         [SerializeField] private float minRepeatTime = 1.0f;
  18.         [SerializeField] private float maxRepeatTime = 5.0f;
  19.         [Space]
  20.         [Header("Material to turn On/Off the Emission Value")]
  21.         [SerializeField] private Renderer emissionRenderer;
  22.         [SerializeField] private Color emissionColor = Color.cyan;
  23.  
  24.         private AudioSource audioSource;
  25.         private float[] samples = new float[256];
  26.         private float repeatTimer;
  27.         private Material emissionMaterial;
  28.         private Color defaultEmissionColor;
  29.  
  30.         void Start()
  31.         {
  32.             SetupLightSource();
  33.             SetupAudioSource();
  34.             SetupEmissionMaterial();
  35.         }
  36.  
  37.         void Update()
  38.         {
  39.             UpdateAudioPlayback();
  40.             UpdateLightAndEmission();
  41.         }
  42.  
  43.         private void SetupLightSource()
  44.         {
  45.             if (lightSource == null)
  46.             {
  47.                 lightSource = GetComponent<Light>() ?? gameObject.AddComponent<Light>();
  48.                 lightSource.type = LightType.Point;
  49.                 lightSource.range = 5f;
  50.                 lightSource.shadows = LightShadows.Soft;
  51.             }
  52.         }
  53.  
  54.         private void SetupAudioSource()
  55.         {
  56.             if (clips.Length > 0)
  57.             {
  58.                 audioSource = GetComponent<AudioSource>() ?? gameObject.AddComponent<AudioSource>();
  59.                 audioSource.spatialBlend = 1.0f;
  60.                 audioSource.spread = 1.0f;
  61.                 repeatTimer = Random.Range(minRepeatTime, maxRepeatTime);
  62.             }
  63.         }
  64.  
  65.         private void SetupEmissionMaterial()
  66.         {
  67.             if (emissionRenderer != null)
  68.             {
  69.                 emissionMaterial = new Material(emissionRenderer.material);
  70.                 emissionRenderer.material = emissionMaterial;
  71.                 emissionMaterial.EnableKeyword("_EMISSION");
  72.                 defaultEmissionColor = emissionMaterial.GetColor("_EmissionColor");
  73.             }
  74.         }
  75.  
  76.         private void UpdateAudioPlayback()
  77.         {
  78.             if (audioSource != null && clips.Length > 0)
  79.             {
  80.                 repeatTimer -= Time.deltaTime;
  81.                 if (repeatTimer <= 0)
  82.                 {
  83.                     PlayRandomClip();
  84.                     repeatTimer = Random.Range(minRepeatTime, maxRepeatTime);
  85.                 }
  86.             }
  87.         }
  88.  
  89.         private void UpdateLightAndEmission()
  90.         {
  91.             float targetIntensity = isInverted ? offIntensity : onIntensity;
  92.             float currentIntensity = targetIntensity;
  93.  
  94.             if (audioSource != null && audioSource.isPlaying)
  95.             {
  96.                 audioSource.GetOutputData(samples, 0);
  97.                 float rmsValue = CalculateRMS(samples);
  98.                 currentIntensity = isInverted
  99.                     ? Mathf.Lerp(offIntensity, onIntensity, rmsValue * sensitivity)
  100.                     : Mathf.Lerp(onIntensity, offIntensity, rmsValue * sensitivity);
  101.             }
  102.  
  103.             lightSource.intensity = currentIntensity;
  104.  
  105.             if (emissionMaterial != null)
  106.             {
  107.                 Color currentEmissionColor = emissionColor * (currentIntensity / onIntensity);
  108.                 emissionMaterial.SetColor("_EmissionColor", currentEmissionColor);
  109.                 DynamicGI.SetEmissive(emissionRenderer, currentEmissionColor);
  110.             }
  111.         }
  112.  
  113.         private float CalculateRMS(float[] samples)
  114.         {
  115.             float sum = 0f;
  116.             for (int i = 0; i < samples.Length; i++)
  117.             {
  118.                 sum += samples[i] * samples[i];
  119.             }
  120.             return Mathf.Sqrt(sum / samples.Length);
  121.         }
  122.  
  123.         private void PlayRandomClip()
  124.         {
  125.             if (clips.Length > 0)
  126.             {
  127.                 int index = Random.Range(0, clips.Length);
  128.                 audioSource.clip = clips[index];
  129.                 audioSource.Play();
  130.             }
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment