265 lines
10 KiB
C#
265 lines
10 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class NeptuneThermosphereEffects : MonoBehaviour
|
|
{
|
|
[Header("Íàëàøòóâàííÿ")]
|
|
public Material auroraMaterial;
|
|
public float planetRadius = 58f;
|
|
|
|
[Range(0f, 1f)]
|
|
public float thermosphereLevel = 1f;
|
|
public float previousLevel = 1f;
|
|
|
|
private ParticleSystem _auroraNorth;
|
|
private ParticleSystem _auroraSouth;
|
|
private ParticleSystem _ionizationSystem;
|
|
private Light _auroraGlowNorth;
|
|
private Light _auroraGlowSouth;
|
|
|
|
void Awake() { enabled = false; }
|
|
|
|
public void InitState()
|
|
{
|
|
if (_auroraNorth != null) Destroy(_auroraNorth.gameObject);
|
|
if (_auroraSouth != null) Destroy(_auroraSouth.gameObject);
|
|
if (_ionizationSystem != null) Destroy(_ionizationSystem.gameObject);
|
|
if (_auroraGlowNorth != null) Destroy(_auroraGlowNorth.gameObject);
|
|
if (_auroraGlowSouth != null) Destroy(_auroraGlowSouth.gameObject);
|
|
|
|
_auroraNorth = null;
|
|
_auroraSouth = null;
|
|
_ionizationSystem = null;
|
|
_auroraGlowNorth = null;
|
|
_auroraGlowSouth = null;
|
|
|
|
thermosphereLevel = 1f;
|
|
previousLevel = 1f;
|
|
|
|
CreateAurora();
|
|
CreateIonization();
|
|
}
|
|
|
|
void CreateAurora()
|
|
{
|
|
float r = planetRadius * transform.lossyScale.x;
|
|
|
|
_auroraNorth = CreateAuroraSystem("NeptuneAuroraNorth", r, Vector3.up);
|
|
_auroraSouth = CreateAuroraSystem("NeptuneAuroraSouth", r, Vector3.down);
|
|
|
|
GameObject glowN = new GameObject("NeptuneAuroraGlowNorth");
|
|
glowN.transform.position = transform.position + Vector3.up * r * 1.1f;
|
|
_auroraGlowNorth = glowN.AddComponent<Light>();
|
|
_auroraGlowNorth.type = LightType.Point;
|
|
_auroraGlowNorth.color = new Color(0.2f, 0.5f, 1f);
|
|
_auroraGlowNorth.intensity = 80f;
|
|
_auroraGlowNorth.range = r * 5f;
|
|
|
|
GameObject glowS = new GameObject("NeptuneAuroraGlowSouth");
|
|
glowS.transform.position = transform.position + Vector3.down * r * 1.1f;
|
|
_auroraGlowSouth = glowS.AddComponent<Light>();
|
|
_auroraGlowSouth.type = LightType.Point;
|
|
_auroraGlowSouth.color = new Color(0.5f, 0.2f, 1f);
|
|
_auroraGlowSouth.intensity = 80f;
|
|
_auroraGlowSouth.range = r * 5f;
|
|
}
|
|
|
|
ParticleSystem CreateAuroraSystem(string name, float r, Vector3 poleDir)
|
|
{
|
|
GameObject obj = new GameObject(name);
|
|
obj.transform.parent = null;
|
|
obj.transform.position = transform.position + poleDir * r * 0.85f;
|
|
obj.transform.up = poleDir;
|
|
|
|
ParticleSystem ps = obj.AddComponent<ParticleSystem>();
|
|
var main = ps.main;
|
|
main.loop = true;
|
|
main.playOnAwake = true;
|
|
main.maxParticles = 8000;
|
|
main.startLifetime = new ParticleSystem.MinMaxCurve(2f, 5f);
|
|
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.1f, 0.4f, 1f, 1f),
|
|
new Color(0.5f, 0.2f, 1f, 0.8f)
|
|
);
|
|
main.simulationSpace = ParticleSystemSimulationSpace.World;
|
|
main.gravityModifier = 0f;
|
|
|
|
var emission = ps.emission;
|
|
emission.rateOverTime = 400f;
|
|
|
|
var shape = ps.shape;
|
|
shape.enabled = true;
|
|
shape.shapeType = ParticleSystemShapeType.Donut;
|
|
shape.radius = r * 0.35f;
|
|
shape.donutRadius = r * 0.06f;
|
|
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(r * 0.012f);
|
|
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.2f),
|
|
new Keyframe(0.15f, 1f),
|
|
new Keyframe(0.7f, 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.1f, 0.4f, 1f), 0f),
|
|
new GradientColorKey(new Color(0.3f, 0.7f, 1f), 0.3f),
|
|
new GradientColorKey(new Color(0.6f, 0.2f, 1f), 0.7f),
|
|
new GradientColorKey(new Color(0.2f, 0.5f, 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.25f;
|
|
noise.scrollSpeed = 0.4f;
|
|
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()
|
|
{
|
|
float r = planetRadius * transform.lossyScale.x;
|
|
|
|
GameObject obj = new GameObject("NeptuneIonization");
|
|
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 = 6000;
|
|
main.startLifetime = new ParticleSystem.MinMaxCurve(4f, 8f);
|
|
main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f);
|
|
main.startSize = new ParticleSystem.MinMaxCurve(r * 0.005f, r * 0.012f);
|
|
main.startColor = new ParticleSystem.MinMaxGradient(
|
|
new Color(0.2f, 0.5f, 1f, 0.8f),
|
|
new Color(0.5f, 0.2f, 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 * 1.2f;
|
|
shape.radiusThickness = 0.03f;
|
|
|
|
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.2f, 0.5f, 1f), 0f),
|
|
new GradientColorKey(new Color(0.5f, 0.2f, 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 renderer = _ionizationSystem.GetComponent<ParticleSystemRenderer>();
|
|
renderer.renderMode = ParticleSystemRenderMode.Billboard;
|
|
renderer.sortingFudge = 3f;
|
|
if (auroraMaterial != null) renderer.material = auroraMaterial;
|
|
|
|
_ionizationSystem.Play();
|
|
}
|
|
|
|
void UpdateAurora()
|
|
{
|
|
float t = Mathf.InverseLerp(1f, 0f, thermosphereLevel);
|
|
if (_auroraNorth != null) { var e = _auroraNorth.emission; e.rateOverTime = thermosphereLevel <= 0f ? 0f : Mathf.Lerp(400f, 0f, t); }
|
|
if (_auroraSouth != null) { var e = _auroraSouth.emission; e.rateOverTime = thermosphereLevel <= 0f ? 0f : Mathf.Lerp(400f, 0f, t); }
|
|
if (_auroraGlowNorth != null) _auroraGlowNorth.intensity = thermosphereLevel <= 0f ? 0f : Mathf.Lerp(80f, 0f, t);
|
|
if (_auroraGlowSouth != null) _auroraGlowSouth.intensity = thermosphereLevel <= 0f ? 0f : Mathf.Lerp(80f, 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, 3000f, t);
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!Application.isPlaying) return;
|
|
if (thermosphereLevel >= 1f && previousLevel < 1f) InitState();
|
|
previousLevel = thermosphereLevel;
|
|
UpdateAurora();
|
|
UpdateIonization();
|
|
}
|
|
|
|
public void CleanUp()
|
|
{
|
|
if (_auroraNorth != null) Destroy(_auroraNorth.gameObject);
|
|
if (_auroraSouth != null) Destroy(_auroraSouth.gameObject);
|
|
if (_ionizationSystem != null) Destroy(_ionizationSystem.gameObject);
|
|
if (_auroraGlowNorth != null) Destroy(_auroraGlowNorth.gameObject);
|
|
if (_auroraGlowSouth != null) Destroy(_auroraGlowSouth.gameObject);
|
|
_auroraNorth = null;
|
|
_auroraSouth = null;
|
|
_ionizationSystem = null;
|
|
_auroraGlowNorth = null;
|
|
_auroraGlowSouth = null;
|
|
enabled = false;
|
|
}
|
|
}
|