251 lines
8.4 KiB
C#
251 lines
8.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class JupiterStratosphereEffects : MonoBehaviour
|
|
{
|
|
[Header("Íàëàøòóâàííÿ")]
|
|
public Material cloudMaterial;
|
|
public Material lightningMaterial;
|
|
public float planetRadius = 72f;
|
|
public ParticleSystem jupiterGlow;
|
|
|
|
[Range(0f, 1f)]
|
|
public float stratosphereLevel = 1f;
|
|
public float previousLevel = 1f;
|
|
|
|
private List<ParticleSystem> cloudClusters = new List<ParticleSystem>();
|
|
private List<GameObject> lightningObjects = new List<GameObject>();
|
|
private Coroutine lightningCoroutine;
|
|
|
|
void Awake()
|
|
{
|
|
enabled = false;
|
|
}
|
|
|
|
public void InitState()
|
|
{
|
|
foreach (var ps in cloudClusters)
|
|
if (ps != null) Destroy(ps.gameObject);
|
|
cloudClusters.Clear();
|
|
|
|
foreach (var obj in lightningObjects)
|
|
if (obj != null) Destroy(obj);
|
|
lightningObjects.Clear();
|
|
|
|
if (lightningCoroutine != null)
|
|
StopCoroutine(lightningCoroutine);
|
|
|
|
stratosphereLevel = 1f;
|
|
previousLevel = 1f;
|
|
|
|
CreateClouds();
|
|
lightningCoroutine = StartCoroutine(SpawnLightning());
|
|
|
|
if (jupiterGlow != null)
|
|
{
|
|
var e = jupiterGlow.emission;
|
|
e.rateOverTime = 100f;
|
|
if (!jupiterGlow.isPlaying) jupiterGlow.Play();
|
|
}
|
|
}
|
|
|
|
void CreateClouds()
|
|
{
|
|
float[] latitudes = { -40f, -15f, 15f, 40f };
|
|
Color[] colors =
|
|
{
|
|
new Color(0.75f, 0.5f, 0.25f),
|
|
new Color(0.85f, 0.65f, 0.35f),
|
|
new Color(0.8f, 0.6f, 0.3f),
|
|
new Color(0.7f, 0.45f, 0.2f)
|
|
};
|
|
|
|
for (int i = 0; i < latitudes.Length; i++)
|
|
{
|
|
GameObject obj = new GameObject("JupiterCloud_" + 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 = 1500;
|
|
main.startLifetime = new ParticleSystem.MinMaxCurve(6f, 12f);
|
|
main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f);
|
|
main.startSize = new ParticleSystem.MinMaxCurve(1.5f, 4f);
|
|
main.startColor = new ParticleSystem.MinMaxGradient(
|
|
new Color(colors[i].r, colors[i].g, colors[i].b, 0.35f),
|
|
new Color(colors[i].r * 0.8f, colors[i].g * 0.8f, colors[i].b * 0.8f, 0.2f)
|
|
);
|
|
main.simulationSpace = ParticleSystemSimulationSpace.World;
|
|
main.gravityModifier = 0f;
|
|
|
|
var emission = ps.emission;
|
|
emission.rateOverTime = 80f;
|
|
|
|
var shape = ps.shape;
|
|
shape.enabled = true;
|
|
shape.shapeType = ParticleSystemShapeType.Sphere;
|
|
shape.radius = planetRadius * 1.1f;
|
|
shape.radiusThickness = 0.05f;
|
|
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 ? 2f : -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 = 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.35f, 0.15f),
|
|
new GradientAlphaKey(0.25f, 0.8f),
|
|
new GradientAlphaKey(0f, 1f)
|
|
}
|
|
);
|
|
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);
|
|
|
|
var noise = ps.noise;
|
|
noise.enabled = true;
|
|
noise.strength = 0.4f;
|
|
noise.frequency = 0.2f;
|
|
noise.scrollSpeed = 0.1f;
|
|
|
|
var renderer = ps.GetComponent<ParticleSystemRenderer>();
|
|
renderer.renderMode = ParticleSystemRenderMode.Billboard;
|
|
renderer.sortingFudge = 4f;
|
|
if (cloudMaterial != null)
|
|
renderer.material = cloudMaterial;
|
|
|
|
ps.Play();
|
|
cloudClusters.Add(ps);
|
|
}
|
|
}
|
|
|
|
IEnumerator SpawnLightning()
|
|
{
|
|
while (enabled)
|
|
{
|
|
if (stratosphereLevel < 1f)
|
|
{
|
|
float interval = Mathf.Lerp(0.3f, 2f, stratosphereLevel);
|
|
yield return new WaitForSeconds(interval);
|
|
|
|
Vector3 dir = Random.onUnitSphere;
|
|
Vector3 pos = transform.position + dir * planetRadius * 1.05f;
|
|
|
|
GameObject lightObj = new GameObject("JupiterLightning");
|
|
lightObj.transform.position = pos;
|
|
Light light = lightObj.AddComponent<Light>();
|
|
light.type = LightType.Point;
|
|
light.color = new Color(0.8f, 0.9f, 1f);
|
|
light.intensity = Mathf.Lerp(50f, 200f, 1f - stratosphereLevel);
|
|
light.range = planetRadius * 3f;
|
|
lightningObjects.Add(lightObj);
|
|
|
|
yield return new WaitForSeconds(0.1f);
|
|
if (lightObj != null) Destroy(lightObj);
|
|
lightningObjects.Remove(lightObj);
|
|
}
|
|
else
|
|
{
|
|
yield return new WaitForSeconds(0.5f);
|
|
}
|
|
}
|
|
}
|
|
|
|
void UpdateClouds()
|
|
{
|
|
foreach (var ps in cloudClusters)
|
|
{
|
|
if (ps == null) continue;
|
|
var emission = ps.emission;
|
|
|
|
if (stratosphereLevel >= 1f)
|
|
emission.rateOverTime = 80f;
|
|
else if (stratosphereLevel >= 0.7f)
|
|
emission.rateOverTime = Mathf.Lerp(80f, 50f, Mathf.InverseLerp(1f, 0.7f, stratosphereLevel));
|
|
else if (stratosphereLevel >= 0.5f)
|
|
emission.rateOverTime = Mathf.Lerp(50f, 20f, Mathf.InverseLerp(0.7f, 0.5f, stratosphereLevel));
|
|
else if (stratosphereLevel >= 0.3f)
|
|
emission.rateOverTime = Mathf.Lerp(20f, 5f, Mathf.InverseLerp(0.5f, 0.3f, stratosphereLevel));
|
|
else
|
|
emission.rateOverTime = 0f;
|
|
}
|
|
}
|
|
|
|
void UpdateGlow()
|
|
{
|
|
if (jupiterGlow == null) return;
|
|
var glowEmission = jupiterGlow.emission;
|
|
|
|
if (stratosphereLevel >= 1f)
|
|
glowEmission.rateOverTime = 100f;
|
|
else if (stratosphereLevel >= 0.7f)
|
|
glowEmission.rateOverTime = Mathf.Lerp(100f, 60f, Mathf.InverseLerp(1f, 0.7f, stratosphereLevel));
|
|
else if (stratosphereLevel >= 0.5f)
|
|
glowEmission.rateOverTime = Mathf.Lerp(60f, 30f, Mathf.InverseLerp(0.7f, 0.5f, stratosphereLevel));
|
|
else if (stratosphereLevel >= 0.3f)
|
|
glowEmission.rateOverTime = Mathf.Lerp(30f, 10f, Mathf.InverseLerp(0.5f, 0.3f, stratosphereLevel));
|
|
else if (stratosphereLevel > 0f)
|
|
glowEmission.rateOverTime = Mathf.Lerp(10f, 0f, Mathf.InverseLerp(0.3f, 0f, stratosphereLevel));
|
|
else
|
|
{
|
|
glowEmission.rateOverTime = 0f;
|
|
jupiterGlow.Stop();
|
|
}
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!Application.isPlaying) return;
|
|
|
|
if (stratosphereLevel >= 1f && previousLevel < 1f)
|
|
InitState();
|
|
|
|
previousLevel = stratosphereLevel;
|
|
UpdateClouds();
|
|
UpdateGlow();
|
|
}
|
|
|
|
public void CleanUp()
|
|
{
|
|
foreach (var ps in cloudClusters)
|
|
if (ps != null) Destroy(ps.gameObject);
|
|
cloudClusters.Clear();
|
|
|
|
foreach (var obj in lightningObjects)
|
|
if (obj != null) Destroy(obj);
|
|
lightningObjects.Clear();
|
|
|
|
if (lightningCoroutine != null)
|
|
StopCoroutine(lightningCoroutine);
|
|
|
|
if (jupiterGlow != null)
|
|
{
|
|
var e = jupiterGlow.emission;
|
|
e.rateOverTime = 100f;
|
|
jupiterGlow.Play();
|
|
}
|
|
|
|
enabled = false;
|
|
}
|
|
}
|