using System.Collections; using System.Collections.Generic; using UnityEngine; public class NeptuneStratosphereEffects : MonoBehaviour { [Header("Налаштування")] public Material fogMaterial; public float planetRadius = 58f; [Range(0f, 1f)] public float stratosphereLevel = 1f; public float previousLevel = 1f; private ParticleSystem _fogSystem; private ParticleSystem _flashSystem; private Coroutine _flashCoroutine; void Awake() { enabled = false; } public void InitState() { if (_fogSystem != null) Destroy(_fogSystem.gameObject); if (_flashSystem != null) Destroy(_flashSystem.gameObject); if (_flashCoroutine != null) StopCoroutine(_flashCoroutine); _fogSystem = null; _flashSystem = null; stratosphereLevel = 1f; previousLevel = 1f; CreateFog(); CreateFlash(); _flashCoroutine = StartCoroutine(SpawnFlashes()); } void CreateFog() { float r = planetRadius * transform.lossyScale.x; GameObject obj = new GameObject("NeptuneStratoFog"); obj.transform.parent = null; obj.transform.position = transform.position; _fogSystem = obj.AddComponent(); var main = _fogSystem.main; main.loop = true; main.playOnAwake = true; main.maxParticles = 6000; main.startLifetime = new ParticleSystem.MinMaxCurve(8f, 15f); main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f); main.startSize = new ParticleSystem.MinMaxCurve(r * 0.03f, r * 0.08f); main.startColor = new ParticleSystem.MinMaxGradient( new Color(0.4f, 0.2f, 0.8f, 0.15f), new Color(0.3f, 0.1f, 0.6f, 0.08f) ); main.simulationSpace = ParticleSystemSimulationSpace.World; main.gravityModifier = 0f; var emission = _fogSystem.emission; emission.rateOverTime = 120f; var shape = _fogSystem.shape; shape.enabled = true; shape.shapeType = ParticleSystemShapeType.Sphere; shape.radius = r * 1.1f; shape.radiusThickness = 0.05f; var vel = _fogSystem.velocityOverLifetime; vel.enabled = true; vel.space = ParticleSystemSimulationSpace.World; vel.orbitalX = new ParticleSystem.MinMaxCurve(0f); vel.orbitalY = new ParticleSystem.MinMaxCurve(0.8f); vel.orbitalZ = new ParticleSystem.MinMaxCurve(0f); vel.x = new ParticleSystem.MinMaxCurve(0f); vel.y = new ParticleSystem.MinMaxCurve(0f); vel.z = new ParticleSystem.MinMaxCurve(0f); var colorOverLifetime = _fogSystem.colorOverLifetime; colorOverLifetime.enabled = true; Gradient gradient = new Gradient(); gradient.SetKeys( new GradientColorKey[] { new GradientColorKey(new Color(0.4f, 0.2f, 0.8f), 0f), new GradientColorKey(new Color(0.3f, 0.1f, 0.6f), 1f) }, new GradientAlphaKey[] { new GradientAlphaKey(0f, 0f), new GradientAlphaKey(0.15f, 0.2f), new GradientAlphaKey(0.08f, 0.8f), new GradientAlphaKey(0f, 1f) } ); colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient); var noise = _fogSystem.noise; noise.enabled = true; noise.strength = 1f; noise.frequency = 0.12f; noise.scrollSpeed = 0.08f; var renderer = _fogSystem.GetComponent(); renderer.renderMode = ParticleSystemRenderMode.Billboard; renderer.sortingFudge = 2f; if (fogMaterial != null) renderer.material = fogMaterial; _fogSystem.Play(); } void CreateFlash() { float r = planetRadius * transform.lossyScale.x; GameObject obj = new GameObject("NeptuneFlash"); obj.transform.parent = null; obj.transform.position = transform.position; _flashSystem = obj.AddComponent(); var main = _flashSystem.main; main.loop = false; main.playOnAwake = false; main.maxParticles = 50; main.startLifetime = new ParticleSystem.MinMaxCurve(0.1f, 0.3f); main.startSpeed = new ParticleSystem.MinMaxCurve(r * 0.005f, r * 0.015f); main.startSize = new ParticleSystem.MinMaxCurve(r * 0.01f, r * 0.03f); main.startColor = new ParticleSystem.MinMaxGradient( new Color(0.6f, 0.4f, 1f, 1f), new Color(0.8f, 0.6f, 1f, 0.8f) ); main.simulationSpace = ParticleSystemSimulationSpace.World; var shape = _flashSystem.shape; shape.enabled = true; shape.shapeType = ParticleSystemShapeType.Sphere; shape.radius = r * 0.05f; var renderer = _flashSystem.GetComponent(); renderer.renderMode = ParticleSystemRenderMode.Billboard; renderer.sortingFudge = 6f; if (fogMaterial != null) renderer.material = fogMaterial; } IEnumerator SpawnFlashes() { while (enabled) { if (stratosphereLevel < 1f) { float interval = Mathf.Lerp(0.3f, 3f, stratosphereLevel); yield return new WaitForSeconds(interval); if (_flashSystem != null) { float r = planetRadius * transform.lossyScale.x; _flashSystem.transform.position = transform.position + Random.onUnitSphere * r * 1.08f; _flashSystem.Play(); GameObject lightObj = new GameObject("NeptuneFlashLight"); lightObj.transform.position = _flashSystem.transform.position; Light l = lightObj.AddComponent(); l.type = LightType.Point; l.color = new Color(0.6f, 0.4f, 1f); l.intensity = Mathf.Lerp(20f, 120f, 1f - stratosphereLevel); l.range = r * 2f; Destroy(lightObj, 0.15f); } } else { yield return new WaitForSeconds(0.5f); } } } void UpdateFog() { if (_fogSystem == null) return; var e = _fogSystem.emission; if (stratosphereLevel <= 0f) { e.rateOverTime = 0f; return; } float t = Mathf.InverseLerp(1f, 0f, stratosphereLevel); e.rateOverTime = Mathf.Lerp(120f, 600f, t); } void Update() { if (!Application.isPlaying) return; if (stratosphereLevel >= 1f && previousLevel < 1f) InitState(); previousLevel = stratosphereLevel; UpdateFog(); } public void CleanUp() { if (_fogSystem != null) Destroy(_fogSystem.gameObject); if (_flashSystem != null) Destroy(_flashSystem.gameObject); if (_flashCoroutine != null) StopCoroutine(_flashCoroutine); _fogSystem = null; _flashSystem = null; enabled = false; } }