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

249 lines
8.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UranusThermosphereEffects : MonoBehaviour
{
[Header("Íàëàøòóâàííÿ")]
public Material auroraMaterial;
public float planetRadius = 58f;
[Range(0f, 1f)]
public float thermosphereLevel = 1f;
public float previousLevel = 1f;
private ParticleSystem _auroraSystem;
private ParticleSystem _ionizationSystem;
private Light _auroraGlow;
void Awake() { enabled = false; }
public void InitState()
{
if (_auroraSystem != null) Destroy(_auroraSystem.gameObject);
if (_ionizationSystem != null) Destroy(_ionizationSystem.gameObject);
if (_auroraGlow != null) Destroy(_auroraGlow.gameObject);
_auroraSystem = null;
_ionizationSystem = null;
_auroraGlow = null;
thermosphereLevel = 1f;
previousLevel = 1f;
CreateAurora();
CreateIonization();
}
void CreateAurora()
{
float r = planetRadius * transform.lossyScale.x;
GameObject obj = new GameObject("UranusAurora");
obj.transform.parent = null;
obj.transform.position = transform.position;
_auroraSystem = obj.AddComponent<ParticleSystem>();
var main = _auroraSystem.main;
main.loop = true;
main.playOnAwake = true;
main.maxParticles = 15000;
main.startLifetime = new ParticleSystem.MinMaxCurve(3f, 7f);
main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f);
main.startSize = new ParticleSystem.MinMaxCurve(r * 0.002f, r * 0.006f);
main.startColor = new ParticleSystem.MinMaxGradient(
new Color(0.4f, 0.3f, 1f, 1f),
new Color(0.2f, 0.6f, 0.9f, 0.8f)
);
main.simulationSpace = ParticleSystemSimulationSpace.World;
main.gravityModifier = 0f;
var emission = _auroraSystem.emission;
emission.rateOverTime = 400f;
var shape = _auroraSystem.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Sphere;
shape.radius = r * 0.95f;
shape.radiusThickness = 1f;
var vel = _auroraSystem.velocityOverLifetime;
vel.enabled = true;
vel.space = ParticleSystemSimulationSpace.World;
vel.x = new ParticleSystem.MinMaxCurve(0f);
vel.y = new ParticleSystem.MinMaxCurve(0f);
vel.z = new ParticleSystem.MinMaxCurve(0f);
vel.orbitalX = new ParticleSystem.MinMaxCurve(0f);
vel.orbitalY = new ParticleSystem.MinMaxCurve(1.5f);
vel.orbitalZ = new ParticleSystem.MinMaxCurve(0f);
var sizeOverLifetime = _auroraSystem.sizeOverLifetime;
sizeOverLifetime.enabled = true;
AnimationCurve sizeCurve = new AnimationCurve(
new Keyframe(0f, 0.1f),
new Keyframe(0.2f, 1f),
new Keyframe(0.8f, 0.8f),
new Keyframe(1f, 0f)
);
sizeOverLifetime.size = new ParticleSystem.MinMaxCurve(1f, sizeCurve);
var colorOverLifetime = _auroraSystem.colorOverLifetime;
colorOverLifetime.enabled = true;
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] {
new GradientColorKey(new Color(0.4f, 0.3f, 1f), 0f),
new GradientColorKey(new Color(0.2f, 0.7f, 1f), 0.4f),
new GradientColorKey(new Color(0.5f, 0.2f, 0.9f), 1f)
},
new GradientAlphaKey[] {
new GradientAlphaKey(0f, 0f),
new GradientAlphaKey(1f, 0.1f),
new GradientAlphaKey(0.8f, 0.7f),
new GradientAlphaKey(0f, 1f)
}
);
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);
var noise = _auroraSystem.noise;
noise.enabled = true;
noise.strength = 1.5f;
noise.frequency = 0.15f;
noise.scrollSpeed = 0.2f;
noise.octaveCount = 2;
var renderer = _auroraSystem.GetComponent<ParticleSystemRenderer>();
renderer.renderMode = ParticleSystemRenderMode.Billboard;
renderer.sortingFudge = 5f;
if (auroraMaterial != null) renderer.material = auroraMaterial;
_auroraSystem.Play();
GameObject glowObj = new GameObject("UranusAuroraGlow");
glowObj.transform.parent = null;
glowObj.transform.position = transform.position;
_auroraGlow = glowObj.AddComponent<Light>();
_auroraGlow.type = LightType.Point;
_auroraGlow.color = new Color(0.4f, 0.3f, 1f);
_auroraGlow.intensity = 60f;
_auroraGlow.range = r * 1.2f;
}
void CreateIonization()
{
float r = planetRadius * transform.lossyScale.x;
GameObject obj = new GameObject("UranusIonization");
obj.transform.parent = null;
obj.transform.position = transform.position;
_ionizationSystem = obj.AddComponent<ParticleSystem>();
var main = _ionizationSystem.main;
main.loop = true;
main.playOnAwake = true;
main.maxParticles = 10000;
main.startLifetime = new ParticleSystem.MinMaxCurve(4f, 8f);
main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f);
main.startSize = new ParticleSystem.MinMaxCurve(r * 0.006f, r * 0.015f);
main.startColor = new ParticleSystem.MinMaxGradient(
new Color(0.3f, 0.5f, 1f, 0.8f),
new Color(0.5f, 0.3f, 0.9f, 0.5f)
);
main.simulationSpace = ParticleSystemSimulationSpace.World;
main.gravityModifier = 0f;
var emission = _ionizationSystem.emission;
emission.rateOverTime = 600f;
var shape = _ionizationSystem.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Sphere;
shape.radius = r * 0.9f;
shape.radiusThickness = 1f;
var vel = _ionizationSystem.velocityOverLifetime;
vel.enabled = true;
vel.space = ParticleSystemSimulationSpace.World;
vel.orbitalX = new ParticleSystem.MinMaxCurve(0f);
vel.orbitalY = new ParticleSystem.MinMaxCurve(2f);
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 = _ionizationSystem.colorOverLifetime;
colorOverLifetime.enabled = true;
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] {
new GradientColorKey(new Color(0.3f, 0.5f, 1f), 0f),
new GradientColorKey(new Color(0.5f, 0.3f, 0.9f), 1f)
},
new GradientAlphaKey[] {
new GradientAlphaKey(0f, 0f),
new GradientAlphaKey(0.8f, 0.1f),
new GradientAlphaKey(0.5f, 0.7f),
new GradientAlphaKey(0f, 1f)
}
);
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);
var noise = _ionizationSystem.noise;
noise.enabled = true;
noise.strength = 1f;
noise.frequency = 0.1f;
noise.scrollSpeed = 0.15f;
var renderer = _ionizationSystem.GetComponent<ParticleSystemRenderer>();
renderer.renderMode = ParticleSystemRenderMode.Billboard;
renderer.sortingFudge = 3f;
if (auroraMaterial != null) renderer.material = auroraMaterial;
_ionizationSystem.Play();
}
void UpdateAurora()
{
if (_auroraSystem == null) return;
var e = _auroraSystem.emission;
if (thermosphereLevel <= 0f)
{
e.rateOverTime = 0f;
if (_auroraGlow != null) _auroraGlow.intensity = 0f;
return;
}
float t = Mathf.InverseLerp(1f, 0f, thermosphereLevel);
e.rateOverTime = Mathf.Lerp(400f, 0f, t);
if (_auroraGlow != null) _auroraGlow.intensity = Mathf.Lerp(60f, 0f, t);
}
void UpdateIonization()
{
if (_ionizationSystem == null) return;
var e = _ionizationSystem.emission;
if (thermosphereLevel <= 0f) { e.rateOverTime = 0f; return; }
float t = Mathf.InverseLerp(1f, 0f, thermosphereLevel);
e.rateOverTime = Mathf.Lerp(600f, 2500f, t);
}
void Update()
{
if (!Application.isPlaying) return;
if (thermosphereLevel >= 1f && previousLevel < 1f) InitState();
previousLevel = thermosphereLevel;
UpdateAurora();
UpdateIonization();
}
public void CleanUp()
{
if (_auroraSystem != null) Destroy(_auroraSystem.gameObject);
if (_ionizationSystem != null) Destroy(_ionizationSystem.gameObject);
if (_auroraGlow != null) Destroy(_auroraGlow.gameObject);
_auroraSystem = null;
_ionizationSystem = null;
_auroraGlow = null;
enabled = false;
}
}