Advertisement
noradninja

Weather_Manager

Apr 10th, 2023
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6.  
  7. public class Weather_Manager : MonoBehaviour {
  8.  
  9. // Properties
  10. public AudioSource audioSource;
  11. public AudioSource audioSourceB;
  12. public GameObject windSource;
  13. public float step = 0.33f;
  14. public int sampleDataLength = 512;
  15. public float scaleFactor = 1;
  16. public Material skyBox;
  17. public Material glowMat;
  18. public Material crepuscularMat;
  19. public Material[] treeMats;
  20. public float clipLoudness;
  21. public float clip2Loudness;
  22. public float oldClipLoudness;
  23. public float clipLoudnessB;
  24. private float[] clipSampleData;
  25. private float[] clip2SampleData;
  26. private float currentUpdateTime;
  27. public float enviroReflectivity;
  28. private static readonly int Contrast = Shader.PropertyToID("_Contrast");
  29. private static readonly int Exposure = Shader.PropertyToID("_Exposure");
  30.  
  31.  
  32.     public void Awake () {
  33.             audioSourceB = windSource.GetComponent<AudioSource>();
  34.             clipSampleData = new float[sampleDataLength];
  35.             clip2SampleData = new float[sampleDataLength];
  36.             crepuscularMat.GetFloat(Contrast);
  37.     }
  38.    
  39.     public void Update () {
  40.         if(!PauseManager.isPaused && !Inventory_Screen_Manager.inventoryOn){
  41.             currentUpdateTime += Time.deltaTime;
  42.            
  43.             if (currentUpdateTime >= step){
  44.                 currentUpdateTime = 0f;
  45.             }
  46.             else {
  47.                 currentUpdateTime += 0.1f;
  48.             }
  49.  
  50.             audioSource.clip.GetData(clipSampleData, audioSource.timeSamples);
  51.             audioSourceB.clip.GetData(clip2SampleData, audioSourceB.timeSamples);
  52.             oldClipLoudness = clipLoudness;
  53.             clipLoudness = 0f;
  54.             clip2Loudness = 0f;
  55.            
  56.             foreach (var sample in clipSampleData){
  57.                 if (Math.Abs(sample) > 0.1f){
  58.                     clipLoudness += Math.Abs(sample);
  59.                 }
  60.             }
  61.             foreach (var sample2 in clip2SampleData){
  62.                 if (Math.Abs(sample2) > 0.1f){
  63.                     clip2Loudness += Math.Abs(sample2);
  64.                 }
  65.             }
  66.             clipLoudness /= sampleDataLength;
  67.             clip2Loudness /= sampleDataLength;
  68.             clipLoudness *= scaleFactor;
  69.             clip2Loudness *= 10f;
  70.             clipLoudness += 0.55f;
  71.  
  72.             //spin mats, set the wave influence to fake wind changes
  73.             foreach (var material in treeMats)
  74.             {
  75.                 material.SetFloat("_influence", clip2Loudness);
  76.             }
  77.             enviroReflectivity = clipLoudness;
  78.             RenderSettings.reflectionIntensity =
  79.                 ExtensionMethods.Math.Remap(enviroReflectivity, 0.55f,1.1f,0.3f, 0.65f);
  80.             skyBox.SetFloat(Exposure,
  81.                         ExtensionMethods.Math.Remap(enviroReflectivity, 0.55f,0.8f,
  82.                                                     0.55f, 0.85f));
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement