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

120 lines
3.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VenusDeath : MonoBehaviour
{
public float troposphereLevel = 1f;
public float mesosphereLevel = 1f;
public Texture2D deadTexture;
public Transform sunTransform;
private Renderer planetRenderer;
private Material mat;
private float prevTropo = -1f;
private float prevMeso = -1f;
private bool deadApplied = false;
private Texture2D originalTexture;
public float thermosphereLevel = 1f;
private float prevThermo = -1f;
void Start()
{
planetRenderer = GetComponent<Renderer>();
if (planetRenderer == null)
planetRenderer = GetComponentInChildren<Renderer>();
if (planetRenderer != null)
{
mat = planetRenderer.material;
originalTexture = (Texture2D)mat.GetTexture("_Texture2D");
}
}
void Update()
{
if (mat == null) return;
if (sunTransform != null)
{
Vector3 sunDir = (sunTransform.position - transform.position).normalized;
mat.SetVector("_SunDirection", new Vector4(sunDir.x, sunDir.y, sunDir.z, 0f));
}
if (!Mathf.Approximately(thermosphereLevel, prevThermo))
{
prevThermo = thermosphereLevel;
mat.SetFloat("_ThermosphereLevel", thermosphereLevel);
}
if (thermosphereLevel >= 14f && !deadApplied)
{
deadApplied = true;
if (deadTexture != null)
mat.SetTexture("_Texture2D", deadTexture);
mat.SetFloat("_TroposphereLevel", 0f);
return;
}
if (mesosphereLevel <= 0f && !deadApplied)
{
deadApplied = true;
if (deadTexture != null)
mat.SetTexture("_Texture2D", deadTexture);
mat.SetFloat("_TroposphereLevel", 0f);
return;
}
if (Mathf.Approximately(troposphereLevel, prevTropo) &&
Mathf.Approximately(mesosphereLevel, prevMeso)) return;
prevTropo = troposphereLevel;
prevMeso = mesosphereLevel;
float heatLevel = 1f;
if (troposphereLevel >= 1f)
heatLevel = 1f;
else if (troposphereLevel >= 0.7f)
heatLevel = Mathf.Lerp(1f, 0.7f, Mathf.InverseLerp(1f, 0.7f, troposphereLevel));
else if (troposphereLevel >= 0.5f)
heatLevel = Mathf.Lerp(0.7f, 0.4f, Mathf.InverseLerp(0.7f, 0.5f, troposphereLevel));
else if (troposphereLevel >= 0.3f)
heatLevel = Mathf.Lerp(0.4f, 0.1f, Mathf.InverseLerp(0.5f, 0.3f, troposphereLevel));
else
heatLevel = Mathf.Lerp(0.1f, 0f, Mathf.InverseLerp(0.3f, 0f, troposphereLevel));
mat.SetFloat("_TroposphereLevel", heatLevel);
}
public void ApplyDeadColors()
{
if (mat == null) return;
deadApplied = true;
if (deadTexture != null)
mat.SetTexture("_Texture2D", deadTexture);
mat.SetFloat("_TroposphereLevel", 0f);
}
public void ResetTexture()
{
troposphereLevel = 1f;
mesosphereLevel = 1f;
prevTropo = -1f;
prevMeso = -1f;
deadApplied = false;
if (mat != null)
{
if (originalTexture != null)
mat.SetTexture("_Texture2D", originalTexture);
mat.SetFloat("_TroposphereLevel", 1f);
mat.SetColor("DayColor", new Color(1f, 0.4f, 0.05f));
mat.SetColor("NightColor", new Color(0.02f, 0.02f, 0.08f));
}
thermosphereLevel = 1f;
prevThermo = -1f;
mat.SetFloat("_ThermosphereLevel", 0f);
}
}