195 lines
8.2 KiB
C#
195 lines
8.2 KiB
C#
using System.Collections;
|
||
using UnityEngine;
|
||
using AtmosphericHeightFog; // Upewnij siê, ¿e przestrzeñ nazw odpowiada Twojemu projektowi
|
||
|
||
[RequireComponent(typeof(Collider))]
|
||
public class FogIntensityZone : MonoBehaviour
|
||
{
|
||
[Header("Tryb dzia³ania")]
|
||
[Tooltip("Jeœli zaznaczone – zmieniana bêdzie tylko intensywnoœæ foga. W przeciwnym razie zmieniane s¹ równie¿ kolory, ustawienia odleg³oœci i intensywnoœæ œwiat³a kierunkowego.")]
|
||
public bool onlyChangeIntensity = true;
|
||
|
||
[Header("Ustawienia dla trybu 'Tylko intensywnoϾ'")]
|
||
[Tooltip("Docelowa intensywnoϾ foga, gdy zmieniamy tylko intensywnoϾ.")]
|
||
public float targetIntensity = 0.5f;
|
||
|
||
[Header("Ustawienia dla trybu 'Pe³na zmiana'")]
|
||
[Tooltip("Docelowa intensywnoœæ foga przy pe³nej zmianie ustawieñ.")]
|
||
public float manualTargetIntensity = 0.5f;
|
||
|
||
[Tooltip("Docelowy kolor startowy foga przy pe³nej zmianie.")]
|
||
[ColorUsage(true, true)]
|
||
public Color manualTargetFogColorStart = new Color(0.5f, 0.75f, 1f, 1f);
|
||
|
||
[Tooltip("Docelowy kolor koñcowy foga przy pe³nej zmianie.")]
|
||
[ColorUsage(true, true)]
|
||
public Color manualTargetFogColorEnd = new Color(0.75f, 1f, 1.25f, 1f);
|
||
|
||
[Tooltip("Docelowa wartoœæ blendowania kolorów (0 - 1).")]
|
||
[Range(0f, 1f)]
|
||
public float manualTargetFogColorDuo = 0f;
|
||
|
||
[Tooltip("Docelowa wartoœæ odleg³oœci startowej foga.")]
|
||
public float manualTargetFogDistanceStart = -50f;
|
||
|
||
[Tooltip("Docelowa wartoœæ odleg³oœci koñcowej foga.")]
|
||
public float manualTargetFogDistanceEnd = 150f;
|
||
|
||
[Tooltip("Docelowa wartoœæ spadku odleg³oœci foga.")]
|
||
public float manualTargetFogDistanceFalloff = 2f;
|
||
|
||
[Tooltip("Docelowa intensywnoœæ œwiat³a kierunkowego przy pe³nej zmianie.")]
|
||
public float manualTargetDirectionalIntensity = 0.5f;
|
||
|
||
[Header("Pozosta³e ustawienia")]
|
||
[Tooltip("Czas (w sekundach) przejœcia miêdzy wartoœciami.")]
|
||
public float transitionDuration = 1f;
|
||
|
||
[Tooltip("Tag obiektu gracza.")]
|
||
public string playerTag = "Player";
|
||
|
||
// Referencja do globalnego skryptu foga
|
||
private HeightFogGlobal fogGlobal;
|
||
|
||
// Domyœlne wartoœci pobrane przy starcie
|
||
private float defaultIntensity;
|
||
private Color defaultFogColorStart;
|
||
private Color defaultFogColorEnd;
|
||
private float defaultFogColorDuo;
|
||
private float defaultFogDistanceStart;
|
||
private float defaultFogDistanceEnd;
|
||
private float defaultFogDistanceFalloff;
|
||
private float defaultDirectionalIntensity;
|
||
|
||
// Przechowywana aktywna korutyna, by móc j¹ przerwaæ
|
||
private Coroutine transitionCoroutine;
|
||
|
||
void Start()
|
||
{
|
||
fogGlobal = FindObjectOfType<HeightFogGlobal>();
|
||
if (fogGlobal == null)
|
||
{
|
||
Debug.LogError("Nie znaleziono HeightFogGlobal w scenie!");
|
||
return;
|
||
}
|
||
|
||
// Zapamiêtujemy domyœlne wartoœci pobrane z globalnego skryptu foga
|
||
defaultIntensity = fogGlobal.fogIntensity;
|
||
defaultFogColorStart = fogGlobal.fogColorStart;
|
||
defaultFogColorEnd = fogGlobal.fogColorEnd;
|
||
defaultFogColorDuo = fogGlobal.fogColorDuo;
|
||
defaultFogDistanceStart = fogGlobal.fogDistanceStart;
|
||
defaultFogDistanceEnd = fogGlobal.fogDistanceEnd;
|
||
defaultFogDistanceFalloff = fogGlobal.fogDistanceFalloff;
|
||
defaultDirectionalIntensity = fogGlobal.directionalIntensity;
|
||
}
|
||
|
||
private void OnTriggerEnter(Collider other)
|
||
{
|
||
if (!other.CompareTag(playerTag))
|
||
return;
|
||
|
||
if (transitionCoroutine != null)
|
||
StopCoroutine(transitionCoroutine);
|
||
|
||
if (onlyChangeIntensity)
|
||
{
|
||
// Tylko intensywnoœæ – pozosta³e w³aœciwoœci pozostaj¹ na domyœlnych wartoœciach.
|
||
transitionCoroutine = StartCoroutine(TransitionProperties(
|
||
fogGlobal.fogIntensity, targetIntensity,
|
||
defaultFogColorStart, defaultFogColorStart,
|
||
defaultFogColorEnd, defaultFogColorEnd,
|
||
defaultFogColorDuo, defaultFogColorDuo,
|
||
defaultFogDistanceStart, defaultFogDistanceStart,
|
||
defaultFogDistanceEnd, defaultFogDistanceEnd,
|
||
defaultFogDistanceFalloff, defaultFogDistanceFalloff,
|
||
defaultDirectionalIntensity, defaultDirectionalIntensity,
|
||
transitionDuration));
|
||
}
|
||
else
|
||
{
|
||
// Pe³na zmiana – przejœcie do ustawieñ podanych rêcznie.
|
||
transitionCoroutine = StartCoroutine(TransitionProperties(
|
||
fogGlobal.fogIntensity, manualTargetIntensity,
|
||
fogGlobal.fogColorStart, manualTargetFogColorStart,
|
||
fogGlobal.fogColorEnd, manualTargetFogColorEnd,
|
||
fogGlobal.fogColorDuo, manualTargetFogColorDuo,
|
||
fogGlobal.fogDistanceStart, manualTargetFogDistanceStart,
|
||
fogGlobal.fogDistanceEnd, manualTargetFogDistanceEnd,
|
||
fogGlobal.fogDistanceFalloff, manualTargetFogDistanceFalloff,
|
||
fogGlobal.directionalIntensity, manualTargetDirectionalIntensity,
|
||
transitionDuration));
|
||
}
|
||
}
|
||
|
||
private void OnTriggerExit(Collider other)
|
||
{
|
||
if (!other.CompareTag(playerTag))
|
||
return;
|
||
|
||
if (transitionCoroutine != null)
|
||
StopCoroutine(transitionCoroutine);
|
||
|
||
// Przy wyjœciu wracamy do domyœlnych ustawieñ.
|
||
transitionCoroutine = StartCoroutine(TransitionProperties(
|
||
fogGlobal.fogIntensity, defaultIntensity,
|
||
fogGlobal.fogColorStart, defaultFogColorStart,
|
||
fogGlobal.fogColorEnd, defaultFogColorEnd,
|
||
fogGlobal.fogColorDuo, defaultFogColorDuo,
|
||
fogGlobal.fogDistanceStart, defaultFogDistanceStart,
|
||
fogGlobal.fogDistanceEnd, defaultFogDistanceEnd,
|
||
fogGlobal.fogDistanceFalloff, defaultFogDistanceFalloff,
|
||
fogGlobal.directionalIntensity, defaultDirectionalIntensity,
|
||
transitionDuration));
|
||
}
|
||
|
||
IEnumerator TransitionProperties(
|
||
float startIntensity, float targetIntensity,
|
||
Color startFogColorStart, Color targetFogColorStart,
|
||
Color startFogColorEnd, Color targetFogColorEnd,
|
||
float startFogColorDuo, float targetFogColorDuo,
|
||
float startFogDistanceStart, float targetFogDistanceStart,
|
||
float startFogDistanceEnd, float targetFogDistanceEnd,
|
||
float startFogDistanceFalloff, float targetFogDistanceFalloff,
|
||
float startDirectionalIntensity, float targetDirectionalIntensity,
|
||
float duration)
|
||
{
|
||
float elapsed = 0f;
|
||
while (elapsed < duration)
|
||
{
|
||
elapsed += Time.deltaTime;
|
||
float t = elapsed / duration;
|
||
|
||
// Interpolacja intensywnoœci foga
|
||
fogGlobal.fogIntensity = Mathf.Lerp(startIntensity, targetIntensity, t);
|
||
|
||
// Jeœli tryb pe³ny, interpolujemy tak¿e kolory, ustawienia odleg³oœci i intensywnoœæ œwiat³a kierunkowego
|
||
if (!onlyChangeIntensity)
|
||
{
|
||
fogGlobal.fogColorStart = Color.Lerp(startFogColorStart, targetFogColorStart, t);
|
||
fogGlobal.fogColorEnd = Color.Lerp(startFogColorEnd, targetFogColorEnd, t);
|
||
fogGlobal.fogColorDuo = Mathf.Lerp(startFogColorDuo, targetFogColorDuo, t);
|
||
fogGlobal.fogDistanceStart = Mathf.Lerp(startFogDistanceStart, targetFogDistanceStart, t);
|
||
fogGlobal.fogDistanceEnd = Mathf.Lerp(startFogDistanceEnd, targetFogDistanceEnd, t);
|
||
fogGlobal.fogDistanceFalloff = Mathf.Lerp(startFogDistanceFalloff, targetFogDistanceFalloff, t);
|
||
fogGlobal.directionalIntensity = Mathf.Lerp(startDirectionalIntensity, targetDirectionalIntensity, t);
|
||
}
|
||
yield return null;
|
||
}
|
||
|
||
// Ustawienie ostatecznych wartoœci
|
||
fogGlobal.fogIntensity = targetIntensity;
|
||
if (!onlyChangeIntensity)
|
||
{
|
||
fogGlobal.fogColorStart = targetFogColorStart;
|
||
fogGlobal.fogColorEnd = targetFogColorEnd;
|
||
fogGlobal.fogColorDuo = targetFogColorDuo;
|
||
fogGlobal.fogDistanceStart = targetFogDistanceStart;
|
||
fogGlobal.fogDistanceEnd = targetFogDistanceEnd;
|
||
fogGlobal.fogDistanceFalloff = targetFogDistanceFalloff;
|
||
fogGlobal.directionalIntensity = targetDirectionalIntensity;
|
||
}
|
||
transitionCoroutine = null;
|
||
}
|
||
}
|