Files
ScienceLab.AtmosphericPressure/Assets/Scripts/UranusStratosphereEffects.cs
2026-05-29 18:21:53 +03:00

211 lines
7.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UranusStratosphereEffects : MonoBehaviour
{
[Header("Íàëàøòóâàííÿ")]
public Material fogMaterial;
public float planetRadius = 58f;
[Range(0f, 1f)]
public float stratosphereLevel = 1f;
public float previousLevel = 1f;
private ParticleSystem _chemicalFogSystem;
private ParticleSystem _glowSystem;
void Awake() { enabled = false; }
public void InitState()
{
if (_chemicalFogSystem != null) Destroy(_chemicalFogSystem.gameObject);
if (_glowSystem != null) Destroy(_glowSystem.gameObject);
_chemicalFogSystem = null;
_glowSystem = null;
stratosphereLevel = 1f;
previousLevel = 1f;
CreateChemicalFog();
CreateGlow();
}
void CreateChemicalFog()
{
float r = planetRadius * transform.lossyScale.x;
GameObject obj = new GameObject("UranusChemicalFog");
obj.transform.parent = null;
obj.transform.position = transform.position;
_chemicalFogSystem = obj.AddComponent<ParticleSystem>();
var main = _chemicalFogSystem.main;
main.loop = true;
main.playOnAwake = true;
main.maxParticles = 8000;
main.startLifetime = new ParticleSystem.MinMaxCurve(8f, 16f);
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.9f, 0.8f, 0.3f, 0.15f),
new Color(0.8f, 0.7f, 0.2f, 0.08f)
);
main.simulationSpace = ParticleSystemSimulationSpace.World;
main.gravityModifier = 0f;
var emission = _chemicalFogSystem.emission;
emission.rateOverTime = 150f;
var shape = _chemicalFogSystem.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Sphere;
shape.radius = r * 1.12f;
shape.radiusThickness = 0.05f;
var vel = _chemicalFogSystem.velocityOverLifetime;
vel.enabled = true;
vel.space = ParticleSystemSimulationSpace.World;
vel.orbitalX = new ParticleSystem.MinMaxCurve(0f);
vel.orbitalY = new ParticleSystem.MinMaxCurve(0.3f);
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 = _chemicalFogSystem.colorOverLifetime;
colorOverLifetime.enabled = true;
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] {
new GradientColorKey(new Color(0.9f, 0.8f, 0.3f), 0f),
new GradientColorKey(new Color(0.7f, 0.6f, 0.2f), 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 = _chemicalFogSystem.noise;
noise.enabled = true;
noise.strength = 0.8f;
noise.frequency = 0.1f;
noise.scrollSpeed = 0.05f;
var renderer = _chemicalFogSystem.GetComponent<ParticleSystemRenderer>();
renderer.renderMode = ParticleSystemRenderMode.Billboard;
renderer.sortingFudge = 2f;
if (fogMaterial != null) renderer.material = fogMaterial;
_chemicalFogSystem.Play();
}
void CreateGlow()
{
float r = planetRadius * transform.lossyScale.x;
GameObject obj = new GameObject("UranusStratoGlow");
obj.transform.parent = null;
obj.transform.position = transform.position;
_glowSystem = obj.AddComponent<ParticleSystem>();
var main = _glowSystem.main;
main.loop = true;
main.playOnAwake = true;
main.maxParticles = 4000;
main.startLifetime = new ParticleSystem.MinMaxCurve(5f, 10f);
main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f);
main.startSize = new ParticleSystem.MinMaxCurve(r * 0.01f, r * 0.025f);
main.startColor = new ParticleSystem.MinMaxGradient(
new Color(1f, 0.9f, 0.4f, 0.4f),
new Color(0.9f, 0.7f, 0.2f, 0.2f)
);
main.simulationSpace = ParticleSystemSimulationSpace.World;
main.gravityModifier = 0f;
var emission = _glowSystem.emission;
emission.rateOverTime = 80f;
var shape = _glowSystem.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Sphere;
shape.radius = r * 1.1f;
shape.radiusThickness = 0.03f;
var vel = _glowSystem.velocityOverLifetime;
vel.enabled = true;
vel.space = ParticleSystemSimulationSpace.World;
vel.orbitalX = new ParticleSystem.MinMaxCurve(0f);
vel.orbitalY = new ParticleSystem.MinMaxCurve(0.5f);
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 = _glowSystem.colorOverLifetime;
colorOverLifetime.enabled = true;
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] {
new GradientColorKey(new Color(1f, 0.9f, 0.4f), 0f),
new GradientColorKey(new Color(0.8f, 0.6f, 0.1f), 1f)
},
new GradientAlphaKey[] {
new GradientAlphaKey(0f, 0f),
new GradientAlphaKey(0.4f, 0.15f),
new GradientAlphaKey(0.2f, 0.8f),
new GradientAlphaKey(0f, 1f)
}
);
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);
var renderer = _glowSystem.GetComponent<ParticleSystemRenderer>();
renderer.renderMode = ParticleSystemRenderMode.Billboard;
renderer.sortingFudge = 1f;
if (fogMaterial != null) renderer.material = fogMaterial;
_glowSystem.Play();
}
void UpdateFog()
{
if (_chemicalFogSystem == null) return;
var e = _chemicalFogSystem.emission;
if (stratosphereLevel <= 0f) { e.rateOverTime = 0f; return; }
float t = Mathf.InverseLerp(1f, 0f, stratosphereLevel);
e.rateOverTime = Mathf.Lerp(150f, 500f, t);
}
void UpdateGlow()
{
if (_glowSystem == null) return;
var e = _glowSystem.emission;
if (stratosphereLevel <= 0f) { e.rateOverTime = 0f; return; }
float t = Mathf.InverseLerp(1f, 0f, stratosphereLevel);
e.rateOverTime = Mathf.Lerp(80f, 0f, t);
}
void Update()
{
if (!Application.isPlaying) return;
if (stratosphereLevel >= 1f && previousLevel < 1f) InitState();
previousLevel = stratosphereLevel;
UpdateFog();
UpdateGlow();
}
public void CleanUp()
{
if (_chemicalFogSystem != null) Destroy(_chemicalFogSystem.gameObject);
if (_glowSystem != null) Destroy(_glowSystem.gameObject);
_chemicalFogSystem = null;
_glowSystem = null;
enabled = false;
}
}