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

248 lines
8.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SaturnThermosphereEffects : MonoBehaviour
{
[Header("Íàëàøòóâàííÿ")]
public Material ringRainMaterial;
public Material solarWindMaterial;
public float planetRadius = 58f;
[Range(0f, 1f)]
public float thermosphereLevel = 1f;
public float previousLevel = 1f;
private ParticleSystem _ionizationSystem;
private ParticleSystem _ringRainSystem;
void Awake()
{
enabled = false;
}
float WorldRadius()
{
return planetRadius * transform.lossyScale.x;
}
public void InitState()
{
if (_ionizationSystem != null) Destroy(_ionizationSystem.gameObject);
if (_ringRainSystem != null) Destroy(_ringRainSystem.gameObject);
_ionizationSystem = null;
_ringRainSystem = null;
thermosphereLevel = 1f;
previousLevel = 1f;
CreateIonization();
CreateRingRain();
}
void CreateIonization()
{
float r = WorldRadius();
GameObject obj = new GameObject("SaturnIonization");
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 = 30000;
main.startLifetime = new ParticleSystem.MinMaxCurve(4f, 9f);
main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f);
main.startSize = new ParticleSystem.MinMaxCurve(r * 0.02f, r * 0.06f);
main.startColor = new ParticleSystem.MinMaxGradient(
new Color(0.5f, 0.8f, 1f, 1f),
new Color(0.8f, 0.5f, 1f, 0.8f)
);
main.simulationSpace = ParticleSystemSimulationSpace.World;
main.gravityModifier = 0f;
var emission = _ionizationSystem.emission;
emission.rateOverTime = 1500f;
var shape = _ionizationSystem.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Sphere;
shape.radius = r * 1.18f;
shape.radiusThickness = 0.04f;
var vel = _ionizationSystem.velocityOverLifetime;
vel.enabled = true;
vel.space = ParticleSystemSimulationSpace.World;
vel.orbitalX = new ParticleSystem.MinMaxCurve(0f);
vel.orbitalY = new ParticleSystem.MinMaxCurve(2.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 = _ionizationSystem.colorOverLifetime;
colorOverLifetime.enabled = true;
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] {
new GradientColorKey(new Color(0.5f, 0.8f, 1f), 0f),
new GradientColorKey(new Color(0.7f, 0.5f, 1f), 0.5f),
new GradientColorKey(new Color(1f, 0.3f, 0.8f), 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 = _ionizationSystem.noise;
noise.enabled = true;
noise.strength = 2f;
noise.frequency = 0.15f;
noise.scrollSpeed = 0.3f;
var renderer = _ionizationSystem.GetComponent<ParticleSystemRenderer>();
renderer.renderMode = ParticleSystemRenderMode.Billboard;
renderer.sortingFudge = 3f;
if (solarWindMaterial != null)
renderer.material = solarWindMaterial;
_ionizationSystem.Play();
}
void CreateRingRain()
{
float r = WorldRadius();
GameObject obj = new GameObject("SaturnRingRain");
obj.transform.parent = null;
obj.transform.position = transform.position;
obj.transform.rotation = Quaternion.Euler(90f, 0f, 0f);
_ringRainSystem = obj.AddComponent<ParticleSystem>();
var main = _ringRainSystem.main;
main.loop = true;
main.playOnAwake = true;
main.maxParticles = 30000;
main.startLifetime = new ParticleSystem.MinMaxCurve(4f, 8f);
main.startSpeed = new ParticleSystem.MinMaxCurve(r * 0.01f, r * 0.02f);
main.startSize = new ParticleSystem.MinMaxCurve(r * 0.004f, r * 0.012f);
main.startColor = new ParticleSystem.MinMaxGradient(
new Color(0.6f, 0.9f, 1f, 1f),
new Color(0.4f, 0.7f, 1f, 0.8f)
);
main.simulationSpace = ParticleSystemSimulationSpace.World;
main.gravityModifier = 0f;
var emission = _ringRainSystem.emission;
emission.rateOverTime = 2000f;
var shape = _ringRainSystem.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Donut;
shape.radius = r * 2f;
shape.donutRadius = r * 0.5f;
shape.radiusThickness = 1f;
shape.arc = 360f;
var vel = _ringRainSystem.velocityOverLifetime;
vel.enabled = true;
vel.space = ParticleSystemSimulationSpace.World;
vel.x = new ParticleSystem.MinMaxCurve(0f);
vel.y = new ParticleSystem.MinMaxCurve(-(r * 0.015f));
vel.z = new ParticleSystem.MinMaxCurve(0f);
vel.orbitalX = new ParticleSystem.MinMaxCurve(0f);
vel.orbitalY = new ParticleSystem.MinMaxCurve(0f);
vel.orbitalZ = new ParticleSystem.MinMaxCurve(0f);
var colorOverLifetime = _ringRainSystem.colorOverLifetime;
colorOverLifetime.enabled = true;
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] {
new GradientColorKey(new Color(0.8f, 0.95f, 1f), 0f),
new GradientColorKey(new Color(0.5f, 0.8f, 1f), 0.4f),
new GradientColorKey(new Color(0.3f, 0.6f, 0.9f), 1f)
},
new GradientAlphaKey[] {
new GradientAlphaKey(1f, 0f),
new GradientAlphaKey(0.8f, 0.5f),
new GradientAlphaKey(0f, 1f)
}
);
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);
var renderer = _ringRainSystem.GetComponent<ParticleSystemRenderer>();
renderer.renderMode = ParticleSystemRenderMode.Stretch;
renderer.velocityScale = 0.15f;
renderer.lengthScale = 4f;
renderer.sortingFudge = 4f;
if (ringRainMaterial != null)
renderer.material = ringRainMaterial;
else if (solarWindMaterial != null)
renderer.material = solarWindMaterial;
_ringRainSystem.Play();
}
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(1500f, 4000f, t);
}
void UpdateRingRain()
{
if (_ringRainSystem == null) return;
var e = _ringRainSystem.emission;
if (thermosphereLevel <= 0f)
{
e.rateOverTime = 0f;
return;
}
float t = Mathf.InverseLerp(1f, 0f, thermosphereLevel);
e.rateOverTime = Mathf.Lerp(2000f, 0f, t);
}
void Update()
{
if (!Application.isPlaying) return;
if (thermosphereLevel >= 1f && previousLevel < 1f)
InitState();
previousLevel = thermosphereLevel;
UpdateIonization();
UpdateRingRain();
}
public void CleanUp()
{
if (_ionizationSystem != null) Destroy(_ionizationSystem.gameObject);
if (_ringRainSystem != null) Destroy(_ringRainSystem.gameObject);
_ionizationSystem = null;
_ringRainSystem = null;
enabled = false;
}
}