SKYPRO_Lighting.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Mathematics;
  5. [ExecuteInEditMode]
  6. public class SKYPRO_Lighting : MonoBehaviour
  7. {
  8. Light _light;
  9. [SerializeField] private float sunIntensity = 3;
  10. //[SerializeField] private Gradient dayToEveningGradient;
  11. [SerializeField] private Color dayColour;
  12. [SerializeField] private Color eveningColour;
  13. void Update()
  14. {
  15. if(_light == null)
  16. {
  17. _light = GetComponent<Light>();
  18. }
  19. float dotProduct = Vector3.Dot(-transform.forward, Vector3.up);
  20. float clampedDot = Mathf.Clamp((dotProduct + 0.9f), 0, 1);
  21. float topDot = (1 - Mathf.Clamp01(dotProduct)) * Mathf.Clamp01(Mathf.Sign(dotProduct));
  22. float bottomDot = (1 - Mathf.Clamp01(-dotProduct)) * Mathf.Clamp01(Mathf.Sign(-dotProduct));
  23. topDot = math.smoothstep(0.25f, 0.4f, topDot);
  24. bottomDot = Mathf.Pow(bottomDot, 5);
  25. _light.intensity = Mathf.Lerp(0.1f, sunIntensity, Mathf.Pow(clampedDot, 5));
  26. _light.color = Color.Lerp(dayColour, eveningColour, topDot + bottomDot);
  27. if(transform.localEulerAngles.x == -90)
  28. {
  29. transform.localEulerAngles = new Vector3(-89.9f, transform.eulerAngles.y, transform.eulerAngles.z);
  30. }
  31. }
  32. }