initial commit
This commit is contained in:
390
Assets/Scripts/SaturnStratosphereEffects.cs
Normal file
390
Assets/Scripts/SaturnStratosphereEffects.cs
Normal file
@@ -0,0 +1,390 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class SaturnStratosphereEffects : MonoBehaviour
|
||||
{
|
||||
[Header("Íàëàøòóâàííÿ")]
|
||||
public Material fogMaterial;
|
||||
public Material cloudMaterial;
|
||||
public Material lightningMaterial;
|
||||
public float planetRadius = 58f;
|
||||
|
||||
[Range(0f, 1f)]
|
||||
public float stratosphereLevel = 1f;
|
||||
public float previousLevel = 1f;
|
||||
|
||||
private ParticleSystem fogSystem;
|
||||
private List<ParticleSystem> cloudClusters = new List<ParticleSystem>();
|
||||
private ParticleSystem chemicalGlowSystem;
|
||||
private List<GameObject> lightningObjects = new List<GameObject>();
|
||||
private Coroutine lightningCoroutine;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
public void InitState()
|
||||
{
|
||||
if (fogSystem != null) Destroy(fogSystem.gameObject);
|
||||
foreach (var ps in cloudClusters)
|
||||
if (ps != null) Destroy(ps.gameObject);
|
||||
cloudClusters.Clear();
|
||||
if (chemicalGlowSystem != null) Destroy(chemicalGlowSystem.gameObject);
|
||||
foreach (var obj in lightningObjects)
|
||||
if (obj != null) Destroy(obj);
|
||||
lightningObjects.Clear();
|
||||
if (lightningCoroutine != null) StopCoroutine(lightningCoroutine);
|
||||
|
||||
fogSystem = null;
|
||||
chemicalGlowSystem = null;
|
||||
|
||||
stratosphereLevel = 1f;
|
||||
previousLevel = 1f;
|
||||
|
||||
CreateFog();
|
||||
CreateClouds();
|
||||
CreateChemicalGlow();
|
||||
lightningCoroutine = StartCoroutine(SpawnLightning());
|
||||
}
|
||||
|
||||
void CreateFog()
|
||||
{
|
||||
GameObject obj = new GameObject("SaturnFog");
|
||||
obj.transform.parent = null;
|
||||
obj.transform.position = transform.position;
|
||||
|
||||
fogSystem = obj.AddComponent<ParticleSystem>();
|
||||
|
||||
var main = fogSystem.main;
|
||||
main.loop = true;
|
||||
main.playOnAwake = true;
|
||||
main.maxParticles = 4000;
|
||||
main.startLifetime = new ParticleSystem.MinMaxCurve(8f, 15f);
|
||||
main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f);
|
||||
main.startSize = new ParticleSystem.MinMaxCurve(3f, 8f);
|
||||
main.startColor = new ParticleSystem.MinMaxGradient(
|
||||
new Color(0.92f, 0.87f, 0.7f, 0.15f),
|
||||
new Color(0.85f, 0.8f, 0.62f, 0.08f)
|
||||
);
|
||||
main.simulationSpace = ParticleSystemSimulationSpace.World;
|
||||
main.gravityModifier = 0f;
|
||||
|
||||
var emission = fogSystem.emission;
|
||||
emission.rateOverTime = 200f;
|
||||
|
||||
var shape = fogSystem.shape;
|
||||
shape.enabled = true;
|
||||
shape.shapeType = ParticleSystemShapeType.Sphere;
|
||||
shape.radius = planetRadius * 1.12f;
|
||||
shape.radiusThickness = 0.08f;
|
||||
|
||||
var vel = fogSystem.velocityOverLifetime;
|
||||
vel.enabled = true;
|
||||
vel.space = ParticleSystemSimulationSpace.World;
|
||||
vel.orbitalX = new ParticleSystem.MinMaxCurve(0f);
|
||||
vel.orbitalY = new ParticleSystem.MinMaxCurve(0.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 = fogSystem.colorOverLifetime;
|
||||
colorOverLifetime.enabled = true;
|
||||
Gradient gradient = new Gradient();
|
||||
gradient.SetKeys(
|
||||
new GradientColorKey[] {
|
||||
new GradientColorKey(new Color(0.92f, 0.87f, 0.7f), 0f),
|
||||
new GradientColorKey(new Color(0.88f, 0.83f, 0.66f), 0.5f),
|
||||
new GradientColorKey(new Color(0.82f, 0.77f, 0.6f), 1f)
|
||||
},
|
||||
new GradientAlphaKey[] {
|
||||
new GradientAlphaKey(0f, 0f),
|
||||
new GradientAlphaKey(0.15f, 0.2f),
|
||||
new GradientAlphaKey(0.1f, 0.8f),
|
||||
new GradientAlphaKey(0f, 1f)
|
||||
}
|
||||
);
|
||||
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);
|
||||
|
||||
var noise = fogSystem.noise;
|
||||
noise.enabled = true;
|
||||
noise.strength = 0.3f;
|
||||
noise.frequency = 0.1f;
|
||||
noise.scrollSpeed = 0.05f;
|
||||
|
||||
var renderer = fogSystem.GetComponent<ParticleSystemRenderer>();
|
||||
renderer.renderMode = ParticleSystemRenderMode.Billboard;
|
||||
renderer.sortingFudge = 2f;
|
||||
if (fogMaterial != null)
|
||||
renderer.material = fogMaterial;
|
||||
|
||||
fogSystem.Play();
|
||||
}
|
||||
|
||||
void CreateClouds()
|
||||
{
|
||||
float[] latitudes = { -35f, -12f, 12f, 35f };
|
||||
Color[] colors =
|
||||
{
|
||||
new Color(0.82f, 0.75f, 0.55f),
|
||||
new Color(0.88f, 0.82f, 0.62f),
|
||||
new Color(0.85f, 0.78f, 0.58f),
|
||||
new Color(0.8f, 0.73f, 0.52f)
|
||||
};
|
||||
|
||||
for (int i = 0; i < latitudes.Length; i++)
|
||||
{
|
||||
GameObject obj = new GameObject("SaturnCloud_" + 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(2f, 5f);
|
||||
main.startColor = new ParticleSystem.MinMaxGradient(
|
||||
new Color(colors[i].r, colors[i].g, colors[i].b, 0.3f),
|
||||
new Color(colors[i].r * 0.85f, colors[i].g * 0.85f, colors[i].b * 0.85f, 0.15f)
|
||||
);
|
||||
main.simulationSpace = ParticleSystemSimulationSpace.World;
|
||||
main.gravityModifier = 0f;
|
||||
|
||||
var emission = ps.emission;
|
||||
emission.rateOverTime = 60f;
|
||||
|
||||
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 ? 1.5f : -1.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 = ps.colorOverLifetime;
|
||||
colorOverLifetime.enabled = true;
|
||||
Gradient gradient = new Gradient();
|
||||
gradient.SetKeys(
|
||||
new GradientColorKey[] {
|
||||
new GradientColorKey(colors[i], 0f),
|
||||
new GradientColorKey(colors[i] * 0.85f, 0.5f),
|
||||
new GradientColorKey(colors[i] * 0.65f, 1f)
|
||||
},
|
||||
new GradientAlphaKey[] {
|
||||
new GradientAlphaKey(0f, 0f),
|
||||
new GradientAlphaKey(0.3f, 0.15f),
|
||||
new GradientAlphaKey(0.2f, 0.8f),
|
||||
new GradientAlphaKey(0f, 1f)
|
||||
}
|
||||
);
|
||||
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);
|
||||
|
||||
var noise = ps.noise;
|
||||
noise.enabled = true;
|
||||
noise.strength = 0.3f;
|
||||
noise.frequency = 0.15f;
|
||||
noise.scrollSpeed = 0.08f;
|
||||
|
||||
var renderer = ps.GetComponent<ParticleSystemRenderer>();
|
||||
renderer.renderMode = ParticleSystemRenderMode.Billboard;
|
||||
renderer.sortingFudge = 3f;
|
||||
if (cloudMaterial != null)
|
||||
renderer.material = cloudMaterial;
|
||||
|
||||
ps.Play();
|
||||
cloudClusters.Add(ps);
|
||||
}
|
||||
}
|
||||
|
||||
void CreateChemicalGlow()
|
||||
{
|
||||
GameObject obj = new GameObject("SaturnChemicalGlow");
|
||||
obj.transform.parent = null;
|
||||
obj.transform.position = transform.position;
|
||||
|
||||
chemicalGlowSystem = obj.AddComponent<ParticleSystem>();
|
||||
|
||||
var main = chemicalGlowSystem.main;
|
||||
main.loop = true;
|
||||
main.playOnAwake = true;
|
||||
main.maxParticles = 2000;
|
||||
main.startLifetime = new ParticleSystem.MinMaxCurve(5f, 10f);
|
||||
main.startSpeed = new ParticleSystem.MinMaxCurve(0f, 0f);
|
||||
main.startSize = new ParticleSystem.MinMaxCurve(1f, 3f);
|
||||
main.startColor = new ParticleSystem.MinMaxGradient(
|
||||
new Color(0.9f, 0.85f, 0.5f, 0.2f),
|
||||
new Color(0.8f, 0.75f, 0.4f, 0.1f)
|
||||
);
|
||||
main.simulationSpace = ParticleSystemSimulationSpace.World;
|
||||
main.gravityModifier = 0f;
|
||||
|
||||
var emission = chemicalGlowSystem.emission;
|
||||
emission.rateOverTime = 100f;
|
||||
|
||||
var shape = chemicalGlowSystem.shape;
|
||||
shape.enabled = true;
|
||||
shape.shapeType = ParticleSystemShapeType.Sphere;
|
||||
shape.radius = planetRadius * 1.08f;
|
||||
shape.radiusThickness = 0.03f;
|
||||
|
||||
var vel = chemicalGlowSystem.velocityOverLifetime;
|
||||
vel.enabled = true;
|
||||
vel.space = ParticleSystemSimulationSpace.World;
|
||||
vel.orbitalX = new ParticleSystem.MinMaxCurve(0f);
|
||||
vel.orbitalY = new ParticleSystem.MinMaxCurve(1f);
|
||||
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 = chemicalGlowSystem.colorOverLifetime;
|
||||
colorOverLifetime.enabled = true;
|
||||
Gradient gradient = new Gradient();
|
||||
gradient.SetKeys(
|
||||
new GradientColorKey[] {
|
||||
new GradientColorKey(new Color(0.9f, 0.85f, 0.5f), 0f),
|
||||
new GradientColorKey(new Color(0.85f, 0.78f, 0.45f), 0.5f),
|
||||
new GradientColorKey(new Color(0.75f, 0.68f, 0.35f), 1f)
|
||||
},
|
||||
new GradientAlphaKey[] {
|
||||
new GradientAlphaKey(0f, 0f),
|
||||
new GradientAlphaKey(0.2f, 0.15f),
|
||||
new GradientAlphaKey(0.1f, 0.8f),
|
||||
new GradientAlphaKey(0f, 1f)
|
||||
}
|
||||
);
|
||||
colorOverLifetime.color = new ParticleSystem.MinMaxGradient(gradient);
|
||||
|
||||
var renderer = chemicalGlowSystem.GetComponent<ParticleSystemRenderer>();
|
||||
renderer.renderMode = ParticleSystemRenderMode.Billboard;
|
||||
renderer.sortingFudge = 1f;
|
||||
if (fogMaterial != null)
|
||||
renderer.material = fogMaterial;
|
||||
|
||||
chemicalGlowSystem.Play();
|
||||
}
|
||||
|
||||
IEnumerator SpawnLightning()
|
||||
{
|
||||
while (enabled)
|
||||
{
|
||||
if (stratosphereLevel < 1f)
|
||||
{
|
||||
float interval = Mathf.Lerp(0.2f, 2f, stratosphereLevel);
|
||||
yield return new WaitForSeconds(interval);
|
||||
|
||||
Vector3 dir = Random.onUnitSphere;
|
||||
Vector3 pos = transform.position + dir * planetRadius * 1.05f;
|
||||
|
||||
GameObject lightObj = new GameObject("SaturnLightning");
|
||||
lightObj.transform.position = pos;
|
||||
Light light = lightObj.AddComponent<Light>();
|
||||
light.type = LightType.Point;
|
||||
light.color = new Color(0.9f, 0.85f, 0.6f);
|
||||
light.intensity = Mathf.Lerp(30f, 150f, 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 UpdateFog()
|
||||
{
|
||||
if (fogSystem == null) return;
|
||||
var emission = fogSystem.emission;
|
||||
|
||||
if (stratosphereLevel >= 1f)
|
||||
emission.rateOverTime = 200f;
|
||||
else if (stratosphereLevel >= 0.7f)
|
||||
emission.rateOverTime = Mathf.Lerp(200f, 120f, Mathf.InverseLerp(1f, 0.7f, stratosphereLevel));
|
||||
else if (stratosphereLevel >= 0.5f)
|
||||
emission.rateOverTime = Mathf.Lerp(120f, 50f, Mathf.InverseLerp(0.7f, 0.5f, stratosphereLevel));
|
||||
else if (stratosphereLevel >= 0.3f)
|
||||
emission.rateOverTime = Mathf.Lerp(50f, 10f, Mathf.InverseLerp(0.5f, 0.3f, stratosphereLevel));
|
||||
else
|
||||
emission.rateOverTime = 0f;
|
||||
}
|
||||
|
||||
void UpdateClouds()
|
||||
{
|
||||
foreach (var ps in cloudClusters)
|
||||
{
|
||||
if (ps == null) continue;
|
||||
var emission = ps.emission;
|
||||
|
||||
if (stratosphereLevel >= 1f)
|
||||
emission.rateOverTime = 60f;
|
||||
else if (stratosphereLevel >= 0.7f)
|
||||
emission.rateOverTime = Mathf.Lerp(60f, 35f, Mathf.InverseLerp(1f, 0.7f, stratosphereLevel));
|
||||
else if (stratosphereLevel >= 0.5f)
|
||||
emission.rateOverTime = Mathf.Lerp(35f, 15f, Mathf.InverseLerp(0.7f, 0.5f, stratosphereLevel));
|
||||
else if (stratosphereLevel >= 0.3f)
|
||||
emission.rateOverTime = Mathf.Lerp(15f, 5f, Mathf.InverseLerp(0.5f, 0.3f, stratosphereLevel));
|
||||
else
|
||||
emission.rateOverTime = 0f;
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateChemicalGlow()
|
||||
{
|
||||
if (chemicalGlowSystem == null) return;
|
||||
var emission = chemicalGlowSystem.emission;
|
||||
float t = Mathf.InverseLerp(1f, 0f, stratosphereLevel);
|
||||
emission.rateOverTime = Mathf.Lerp(100f, 0f, t);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (!Application.isPlaying) return;
|
||||
|
||||
if (stratosphereLevel >= 1f && previousLevel < 1f)
|
||||
InitState();
|
||||
|
||||
previousLevel = stratosphereLevel;
|
||||
UpdateFog();
|
||||
UpdateClouds();
|
||||
UpdateChemicalGlow();
|
||||
}
|
||||
|
||||
public void CleanUp()
|
||||
{
|
||||
if (fogSystem != null) Destroy(fogSystem.gameObject);
|
||||
foreach (var ps in cloudClusters)
|
||||
if (ps != null) Destroy(ps.gameObject);
|
||||
cloudClusters.Clear();
|
||||
if (chemicalGlowSystem != null) Destroy(chemicalGlowSystem.gameObject);
|
||||
foreach (var obj in lightningObjects)
|
||||
if (obj != null) Destroy(obj);
|
||||
lightningObjects.Clear();
|
||||
if (lightningCoroutine != null) StopCoroutine(lightningCoroutine);
|
||||
|
||||
fogSystem = null;
|
||||
chemicalGlowSystem = null;
|
||||
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user