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

370 lines
13 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JupiterThermosphereEffects : MonoBehaviour
{
[Header("Íàëàøòóâàííÿ")]
public Material auroraMaterial;
public Material solarWindMaterial;
public float planetRadius = 72f;
[Range(0f, 1f)]
public float thermosphereLevel = 1f;
public float previousLevel = 1f;
private ParticleSystem auroraSystemNorth;
private ParticleSystem auroraSystemSouth;
private ParticleSystem ionizationSystem;
private ParticleSystem solarWindSystem;
private Light auroraGlowNorth;
private Light auroraGlowSouth;
void Awake()
{
enabled = false;
}
public void InitState()
{
if (auroraSystemNorth != null) Destroy(auroraSystemNorth.gameObject);
if (auroraSystemSouth != null) Destroy(auroraSystemSouth.gameObject);
if (ionizationSystem != null) Destroy(ionizationSystem.gameObject);
if (solarWindSystem != null) Destroy(solarWindSystem.gameObject);
if (auroraGlowNorth != null) Destroy(auroraGlowNorth.gameObject);
if (auroraGlowSouth != null) Destroy(auroraGlowSouth.gameObject);
auroraSystemNorth = null;
auroraSystemSouth = null;
ionizationSystem = null;
solarWindSystem = null;
auroraGlowNorth = null;
auroraGlowSouth = null;
thermosphereLevel = 1f;
previousLevel = 1f;
CreateAurora();
CreateIonization();
CreateSolarWind();
}
void CreateAurora()
{
auroraSystemNorth = CreateAuroraSystem("JupiterAuroraNorth", Vector3.up);
auroraSystemSouth = CreateAuroraSystem("JupiterAuroraSouth", Vector3.down);
GameObject glowNorthObj = new GameObject("AuroraGlowNorth");
glowNorthObj.transform.parent = null;
glowNorthObj.transform.position = transform.position + Vector3.up * planetRadius * 1.1f;
auroraGlowNorth = glowNorthObj.AddComponent<Light>();
auroraGlowNorth.type = LightType.Point;
auroraGlowNorth.color = new Color(0.3f, 0.5f, 1f);
auroraGlowNorth.intensity = 15f;
auroraGlowNorth.range = planetRadius * 6f;
GameObject glowSouthObj = new GameObject("AuroraGlowSouth");
glowSouthObj.transform.parent = null;
glowSouthObj.transform.position = transform.position + Vector3.down * planetRadius * 1.1f;
auroraGlowSouth = glowSouthObj.AddComponent<Light>();
auroraGlowSouth.type = LightType.Point;
auroraGlowSouth.color = new Color(0.3f, 0.5f, 1f);
auroraGlowSouth.intensity = 15f;
auroraGlowSouth.range = planetRadius * 6f;
}
ParticleSystem CreateAuroraSystem(string name, Vector3 poleDir)
{
GameObject obj = new GameObject(name);
obj.transform.parent = null;
obj.transform.position = transform.position + poleDir * (planetRadius * 0.9f);
obj.transform.up = poleDir;
ParticleSystem ps = obj.AddComponent<ParticleSystem>();
var main = ps.main;
main.loop = true;
main.playOnAwake = true;
main.maxParticles = 3000;
main.startLifetime = new ParticleSystem.MinMaxCurve(2f, 4f);
main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f);
main.startSize = new ParticleSystem.MinMaxCurve(2f, 6f);
main.startColor = new ParticleSystem.MinMaxGradient(
new Color(0.4f, 0.1f, 1f, 1f),
new Color(0.1f, 0.8f, 0.5f, 0.8f)
);
main.simulationSpace = ParticleSystemSimulationSpace.World;
main.gravityModifier = 0f;
var emission = ps.emission;
emission.rateOverTime = 200f;
var shape = ps.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Donut;
shape.radius = planetRadius * 0.4f;
shape.donutRadius = planetRadius * 0.05f;
shape.radiusThickness = 1f;
shape.arc = 360f;
var vel = ps.velocityOverLifetime;
vel.enabled = true;
vel.space = ParticleSystemSimulationSpace.Local;
vel.x = new ParticleSystem.MinMaxCurve(0f);
vel.y = new ParticleSystem.MinMaxCurve(5f);
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 sizeOverLifetime = ps.sizeOverLifetime;
sizeOverLifetime.enabled = true;
AnimationCurve sizeCurve = new AnimationCurve(
new Keyframe(0f, 0.3f),
new Keyframe(0.2f, 1f),
new Keyframe(0.8f, 0.8f),
new Keyframe(1f, 0f)
);
sizeOverLifetime.size = new ParticleSystem.MinMaxCurve(1f, sizeCurve);
var colorOverLifetime = ps.colorOverLifetime;
colorOverLifetime.enabled = true;
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] {
new GradientColorKey(new Color(0.4f, 0.1f, 1f), 0f),
new GradientColorKey(new Color(0.2f, 0.6f, 1f), 0.3f),
new GradientColorKey(new Color(0.1f, 0.9f, 0.5f), 0.6f),
new GradientColorKey(new Color(0.3f, 0.2f, 1f), 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 = ps.noise;
noise.enabled = true;
noise.strength = 2f;
noise.frequency = 0.3f;
noise.scrollSpeed = 0.5f;
noise.octaveCount = 2;
var renderer = ps.GetComponent<ParticleSystemRenderer>();
renderer.renderMode = ParticleSystemRenderMode.Billboard;
renderer.sortingFudge = 5f;
if (auroraMaterial != null)
renderer.material = auroraMaterial;
ps.Play();
return ps;
}
void CreateIonization()
{
GameObject obj = new GameObject("JupiterIonization");
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 = 5000;
main.startLifetime = new ParticleSystem.MinMaxCurve(4f, 8f);
main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f);
main.startSize = new ParticleSystem.MinMaxCurve(1f, 3f);
main.startColor = new ParticleSystem.MinMaxGradient(
new Color(0.3f, 0.4f, 1f, 0.6f),
new Color(0.2f, 0.3f, 0.8f, 0.4f)
);
main.simulationSpace = ParticleSystemSimulationSpace.World;
main.gravityModifier = 0f;
var emission = ionizationSystem.emission;
emission.rateOverTime = 300f;
var shape = ionizationSystem.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Sphere;
shape.radius = planetRadius * 1.15f;
shape.radiusThickness = 0.05f;
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.4f, 1f), 0f),
new GradientColorKey(new Color(0.2f, 0.3f, 0.8f), 0.5f),
new GradientColorKey(new Color(0.1f, 0.2f, 0.6f), 1f)
},
new GradientAlphaKey[] {
new GradientAlphaKey(0f, 0f),
new GradientAlphaKey(0.6f, 0.15f),
new GradientAlphaKey(0.4f, 0.8f),
new GradientAlphaKey(0f, 1f)
}
);
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);
var renderer = ionizationSystem.GetComponent<ParticleSystemRenderer>();
renderer.renderMode = ParticleSystemRenderMode.Billboard;
renderer.sortingFudge = 3f;
if (auroraMaterial != null)
renderer.material = auroraMaterial;
ionizationSystem.Play();
}
void CreateSolarWind()
{
GameObject obj = new GameObject("JupiterSolarWind");
obj.transform.parent = null;
obj.transform.position = transform.position;
solarWindSystem = obj.AddComponent<ParticleSystem>();
var main = solarWindSystem.main;
main.loop = true;
main.playOnAwake = true;
main.maxParticles = 5000;
main.startLifetime = new ParticleSystem.MinMaxCurve(3f, 6f);
main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f);
main.startSize = new ParticleSystem.MinMaxCurve(0.5f, 1.5f);
main.startColor = new ParticleSystem.MinMaxGradient(
new Color(1f, 0.9f, 0.5f, 0.7f),
new Color(1f, 0.7f, 0.3f, 0.5f)
);
main.simulationSpace = ParticleSystemSimulationSpace.World;
main.gravityModifier = 0f;
var emission = solarWindSystem.emission;
emission.rateOverTime = 400f;
var shape = solarWindSystem.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Sphere;
shape.radius = planetRadius * 2.9f;
shape.radiusThickness = 0.05f;
var vel = solarWindSystem.velocityOverLifetime;
vel.enabled = true;
vel.space = ParticleSystemSimulationSpace.World;
vel.orbitalX = new ParticleSystem.MinMaxCurve(0f);
vel.orbitalY = new ParticleSystem.MinMaxCurve(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 = solarWindSystem.colorOverLifetime;
colorOverLifetime.enabled = true;
Gradient gradient = new Gradient();
gradient.SetKeys(
new GradientColorKey[] {
new GradientColorKey(new Color(1f, 0.9f, 0.5f), 0f),
new GradientColorKey(new Color(1f, 0.7f, 0.3f), 0.5f),
new GradientColorKey(new Color(0.8f, 0.5f, 0.2f), 1f)
},
new GradientAlphaKey[] {
new GradientAlphaKey(0f, 0f),
new GradientAlphaKey(0.7f, 0.1f),
new GradientAlphaKey(0.5f, 0.8f),
new GradientAlphaKey(0f, 1f)
}
);
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);
var renderer = solarWindSystem.GetComponent<ParticleSystemRenderer>();
renderer.renderMode = ParticleSystemRenderMode.Billboard;
renderer.sortingFudge = 2f;
if (solarWindMaterial != null)
renderer.material = solarWindMaterial;
solarWindSystem.Play();
}
void UpdateAurora()
{
float t = Mathf.InverseLerp(1f, 0f, thermosphereLevel);
if (auroraSystemNorth != null)
{
var e = auroraSystemNorth.emission;
e.rateOverTime = Mathf.Lerp(100f, 0f, t);
}
if (auroraSystemSouth != null)
{
var e = auroraSystemSouth.emission;
e.rateOverTime = Mathf.Lerp(100f, 0f, t);
}
if (auroraGlowNorth != null)
auroraGlowNorth.intensity = Mathf.Lerp(3f, 0f, t);
if (auroraGlowSouth != null)
auroraGlowSouth.intensity = Mathf.Lerp(3f, 0f, t);
}
void UpdateIonization()
{
if (ionizationSystem == null) return;
var e = ionizationSystem.emission;
float t = Mathf.InverseLerp(1f, 0f, thermosphereLevel);
e.rateOverTime = Mathf.Lerp(150f, 400f, t);
}
void UpdateSolarWind()
{
if (solarWindSystem == null) return;
var e = solarWindSystem.emission;
float t = Mathf.InverseLerp(1f, 0f, thermosphereLevel);
e.rateOverTime = Mathf.Lerp(200f, 600f, t);
}
void Update()
{
if (!Application.isPlaying) return;
if (thermosphereLevel >= 1f && previousLevel < 1f)
InitState();
previousLevel = thermosphereLevel;
UpdateAurora();
UpdateIonization();
UpdateSolarWind();
}
public void CleanUp()
{
if (auroraSystemNorth != null) Destroy(auroraSystemNorth.gameObject);
if (auroraSystemSouth != null) Destroy(auroraSystemSouth.gameObject);
if (ionizationSystem != null) Destroy(ionizationSystem.gameObject);
if (solarWindSystem != null) Destroy(solarWindSystem.gameObject);
if (auroraGlowNorth != null) Destroy(auroraGlowNorth.gameObject);
if (auroraGlowSouth != null) Destroy(auroraGlowSouth.gameObject);
auroraSystemNorth = null;
auroraSystemSouth = null;
ionizationSystem = null;
solarWindSystem = null;
auroraGlowNorth = null;
auroraGlowSouth = null;
enabled = false;
}
}