249 lines
9.0 KiB
C#
249 lines
9.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class UranusTroposphereEffects : MonoBehaviour
|
|
{
|
|
[Header("Íàëàøòóâàííÿ")]
|
|
public Material cloudMaterial;
|
|
public Material windMaterial;
|
|
public float planetRadius = 58f;
|
|
|
|
[Range(0f, 1f)]
|
|
public float troposphereLevel = 1f;
|
|
public float previousLevel = 1f;
|
|
|
|
private List<ParticleSystem> _bandClusters = new List<ParticleSystem>();
|
|
private List<ParticleSystem> _cloudClusters = new List<ParticleSystem>();
|
|
|
|
void Awake() { enabled = false; }
|
|
|
|
public void InitState()
|
|
{
|
|
foreach (var ps in _bandClusters)
|
|
if (ps != null) Destroy(ps.gameObject);
|
|
_bandClusters.Clear();
|
|
|
|
foreach (var ps in _cloudClusters)
|
|
if (ps != null) Destroy(ps.gameObject);
|
|
_cloudClusters.Clear();
|
|
|
|
troposphereLevel = 1f;
|
|
previousLevel = 1f;
|
|
|
|
CreateBands();
|
|
CreateClouds();
|
|
}
|
|
|
|
void CreateBands()
|
|
{
|
|
float r = planetRadius * transform.lossyScale.x;
|
|
float[] latitudes = { -40f, -15f, 15f, 40f };
|
|
float[] speeds = { 1.5f, -2f, 2f, -1.5f };
|
|
Color[] colors =
|
|
{
|
|
new Color(0.3f, 0.8f, 0.85f),
|
|
new Color(0.4f, 0.9f, 0.95f),
|
|
new Color(0.35f, 0.85f, 0.9f),
|
|
new Color(0.25f, 0.75f, 0.82f)
|
|
};
|
|
|
|
for (int i = 0; i < latitudes.Length; i++)
|
|
{
|
|
GameObject obj = new GameObject("UranusBand_" + i);
|
|
obj.transform.parent = null;
|
|
obj.transform.position = transform.position;
|
|
|
|
ParticleSystem ps = obj.AddComponent<ParticleSystem>();
|
|
var main = ps.main;
|
|
main.loop = true;
|
|
main.playOnAwake = true;
|
|
main.maxParticles = 8000;
|
|
main.startLifetime = new ParticleSystem.MinMaxCurve(8f, 15f);
|
|
main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f);
|
|
main.startSize = new ParticleSystem.MinMaxCurve(r * 0.015f, r * 0.04f);
|
|
main.startColor = new ParticleSystem.MinMaxGradient(
|
|
new Color(colors[i].r, colors[i].g, colors[i].b, 0.5f),
|
|
new Color(colors[i].r * 0.8f, colors[i].g * 0.8f, colors[i].b * 0.8f, 0.3f)
|
|
);
|
|
main.simulationSpace = ParticleSystemSimulationSpace.World;
|
|
main.gravityModifier = 0f;
|
|
|
|
var emission = ps.emission;
|
|
emission.rateOverTime = 0f;
|
|
|
|
var shape = ps.shape;
|
|
shape.enabled = true;
|
|
shape.shapeType = ParticleSystemShapeType.Sphere;
|
|
shape.radius = r;
|
|
shape.radiusThickness = 0.02f;
|
|
shape.arc = 360f;
|
|
shape.rotation = new Vector3(latitudes[i], 0f, 0f);
|
|
|
|
var vel = ps.velocityOverLifetime;
|
|
vel.enabled = true;
|
|
vel.space = ParticleSystemSimulationSpace.World;
|
|
vel.orbitalX = new ParticleSystem.MinMaxCurve(0f);
|
|
vel.orbitalY = new ParticleSystem.MinMaxCurve(speeds[i]);
|
|
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 = ps.colorOverLifetime;
|
|
colorOverLifetime.enabled = true;
|
|
Gradient gradient = new Gradient();
|
|
gradient.SetKeys(
|
|
new GradientColorKey[] {
|
|
new GradientColorKey(colors[i], 0f),
|
|
new GradientColorKey(colors[i] * 0.8f, 0.5f),
|
|
new GradientColorKey(colors[i] * 0.6f, 1f)
|
|
},
|
|
new GradientAlphaKey[] {
|
|
new GradientAlphaKey(0f, 0f),
|
|
new GradientAlphaKey(0.5f, 0.15f),
|
|
new GradientAlphaKey(0.3f, 0.8f),
|
|
new GradientAlphaKey(0f, 1f)
|
|
}
|
|
);
|
|
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);
|
|
|
|
var noise = ps.noise;
|
|
noise.enabled = true;
|
|
noise.strength = 0.5f;
|
|
noise.frequency = 0.1f;
|
|
noise.scrollSpeed = 0.05f;
|
|
|
|
var renderer = ps.GetComponent<ParticleSystemRenderer>();
|
|
renderer.renderMode = ParticleSystemRenderMode.Billboard;
|
|
renderer.sortingFudge = 3f;
|
|
if (windMaterial != null) renderer.material = windMaterial;
|
|
|
|
ps.Play();
|
|
_bandClusters.Add(ps);
|
|
}
|
|
}
|
|
|
|
void CreateClouds()
|
|
{
|
|
float r = planetRadius * transform.lossyScale.x;
|
|
float[] latitudes = { -20f, 0f, 20f };
|
|
|
|
for (int i = 0; i < latitudes.Length; i++)
|
|
{
|
|
GameObject obj = new GameObject("UranusCloud_" + i);
|
|
obj.transform.parent = null;
|
|
obj.transform.position = transform.position;
|
|
|
|
ParticleSystem ps = obj.AddComponent<ParticleSystem>();
|
|
var main = ps.main;
|
|
main.loop = true;
|
|
main.playOnAwake = true;
|
|
main.maxParticles = 3000;
|
|
main.startLifetime = new ParticleSystem.MinMaxCurve(5f, 10f);
|
|
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.9f, 0.95f, 1f, 0.6f),
|
|
new Color(0.8f, 0.88f, 0.95f, 0.4f)
|
|
);
|
|
main.simulationSpace = ParticleSystemSimulationSpace.World;
|
|
main.gravityModifier = 0f;
|
|
|
|
var emission = ps.emission;
|
|
emission.rateOverTime = 30f;
|
|
|
|
var shape = ps.shape;
|
|
shape.enabled = true;
|
|
shape.shapeType = ParticleSystemShapeType.Sphere;
|
|
shape.radius = r * 1.02f;
|
|
shape.radiusThickness = 0.01f;
|
|
shape.arc = 360f;
|
|
shape.rotation = new Vector3(latitudes[i], 0f, 0f);
|
|
|
|
var vel = ps.velocityOverLifetime;
|
|
vel.enabled = true;
|
|
vel.space = ParticleSystemSimulationSpace.World;
|
|
vel.orbitalX = new ParticleSystem.MinMaxCurve(0f);
|
|
vel.orbitalY = new ParticleSystem.MinMaxCurve(i % 2 == 0 ? 3f : -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 = ps.colorOverLifetime;
|
|
colorOverLifetime.enabled = true;
|
|
Gradient gradient = new Gradient();
|
|
gradient.SetKeys(
|
|
new GradientColorKey[] {
|
|
new GradientColorKey(new Color(0.9f, 0.95f, 1f), 0f),
|
|
new GradientColorKey(new Color(0.7f, 0.8f, 0.9f), 1f)
|
|
},
|
|
new GradientAlphaKey[] {
|
|
new GradientAlphaKey(0f, 0f),
|
|
new GradientAlphaKey(0.6f, 0.2f),
|
|
new GradientAlphaKey(0.4f, 0.8f),
|
|
new GradientAlphaKey(0f, 1f)
|
|
}
|
|
);
|
|
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);
|
|
|
|
var renderer = ps.GetComponent<ParticleSystemRenderer>();
|
|
renderer.renderMode = ParticleSystemRenderMode.Billboard;
|
|
renderer.sortingFudge = 4f;
|
|
if (cloudMaterial != null) renderer.material = cloudMaterial;
|
|
|
|
ps.Play();
|
|
_cloudClusters.Add(ps);
|
|
}
|
|
}
|
|
|
|
void UpdateBands()
|
|
{
|
|
foreach (var ps in _bandClusters)
|
|
{
|
|
if (ps == null) continue;
|
|
var e = ps.emission;
|
|
if (troposphereLevel <= 0f) { e.rateOverTime = 0f; continue; }
|
|
if (troposphereLevel >= 0.7f) e.rateOverTime = 0f;
|
|
else if (troposphereLevel >= 0.5f) e.rateOverTime = Mathf.Lerp(0f, 200f, Mathf.InverseLerp(0.7f, 0.5f, troposphereLevel));
|
|
else if (troposphereLevel >= 0.3f) e.rateOverTime = Mathf.Lerp(200f, 500f, Mathf.InverseLerp(0.5f, 0.3f, troposphereLevel));
|
|
else e.rateOverTime = Mathf.Lerp(500f, 1000f, Mathf.InverseLerp(0.3f, 0f, troposphereLevel));
|
|
}
|
|
}
|
|
|
|
void UpdateClouds()
|
|
{
|
|
foreach (var ps in _cloudClusters)
|
|
{
|
|
if (ps == null) continue;
|
|
var e = ps.emission;
|
|
if (troposphereLevel <= 0f) { e.rateOverTime = 0f; continue; }
|
|
float t = Mathf.InverseLerp(1f, 0f, troposphereLevel);
|
|
e.rateOverTime = Mathf.Lerp(30f, 0f, t);
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!Application.isPlaying) return;
|
|
if (troposphereLevel >= 1f && previousLevel < 1f) InitState();
|
|
previousLevel = troposphereLevel;
|
|
UpdateBands();
|
|
UpdateClouds();
|
|
}
|
|
|
|
public void CleanUp()
|
|
{
|
|
foreach (var ps in _bandClusters)
|
|
if (ps != null) Destroy(ps.gameObject);
|
|
_bandClusters.Clear();
|
|
|
|
foreach (var ps in _cloudClusters)
|
|
if (ps != null) Destroy(ps.gameObject);
|
|
_cloudClusters.Clear();
|
|
|
|
enabled = false;
|
|
}
|
|
}
|