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

110 lines
4.1 KiB
C#

using UnityEngine;
[ExecuteAlways]
public class VenusAtmosphereSystem : MonoBehaviour
{
public float venusRadiusUnits = 6.0515f;
public float heightMultiplier = 0.5f;
public Texture2D cloudTexture;
void OnEnable()
{
if (!Application.isPlaying && transform.childCount == 0)
BuildLayers();
}
[ContextMenu("Ïåðåáóäóâàòè øàðè")]
public void BuildLayers()
{
for (int i = transform.childCount - 1; i >= 0; i--)
{
Transform child = transform.GetChild(i);
if (child.name.StartsWith("Layer_"))
{
if (Application.isPlaying) Destroy(child.gameObject);
else DestroyImmediate(child.gameObject);
}
}
CreateLayer("Layer_Troposphere", 300f, new Color(1.0f, 0.65f, 0.1f), new Color(0.9f, 0.4f, 0.0f), 2.5f, 0.8f, 0.05f, 0.01f, 0.7f);
CreateLayer("Layer_Mesosphere", 600f, new Color(1.0f, 0.75f, 0.2f), new Color(0.8f, 0.5f, 0.05f), 2.0f, 0.6f, 0.03f, 0.02f, 0.6f);
CreateLayer("Layer_Thermosphere", 900f, new Color(1.0f, 0.85f, 0.35f), new Color(0.7f, 0.45f, 0.1f), 1.5f, 0.4f, 0.02f, 0.005f, 0.4f);
CreateLayer("Layer_Exosphere", 1300f, new Color(1.0f, 0.9f, 0.5f), new Color(0.6f, 0.4f, 0.15f), 1.2f, 0.2f, 0.01f, 0.003f, 0.2f);
}
void CreateLayer(string layerName, float heightKm, Color rimColor, Color coreColor,
float rimIntensity, float coreIntensity,
float scrollX, float scrollY, float textureStrength)
{
float radiusUnits = venusRadiusUnits + (heightKm / 1000f) * heightMultiplier;
GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
obj.name = layerName;
obj.transform.parent = this.transform;
obj.transform.localPosition = Vector3.zero;
obj.transform.localScale = Vector3.one * radiusUnits * 2f;
if (Application.isPlaying) Destroy(obj.GetComponent<Collider>());
else DestroyImmediate(obj.GetComponent<Collider>());
Shader shader = cloudTexture != null
? Shader.Find("Custom/AtmosphereLayerTexture")
: Shader.Find("Custom/AtmosphereLayer");
Material mat = new Material(shader);
mat.SetColor("_RimColor", rimColor);
mat.SetColor("_CoreColor", coreColor);
mat.SetFloat("_RimPower", 1.8f);
mat.SetFloat("_RimIntensity", rimIntensity);
mat.SetFloat("_CoreIntensity", coreIntensity);
mat.SetFloat("_PulseSpeed", 0.5f);
mat.SetFloat("_PulseStrength", 0.2f);
if (cloudTexture != null)
{
mat.SetTexture("_MainTex", cloudTexture);
mat.SetFloat("_ScrollSpeedX", scrollX);
mat.SetFloat("_ScrollSpeedY", scrollY);
mat.SetFloat("_TextureStrength", textureStrength);
}
obj.GetComponent<Renderer>().material = mat;
}
public void SetLayerActive(string layerName, bool active)
{
Transform layer = transform.Find(layerName);
if (layer != null) layer.gameObject.SetActive(active);
}
public void SetLayerIntensity(string layerName, float level)
{
Transform layer = transform.Find(layerName);
if (layer == null) return;
Renderer r = layer.GetComponent<Renderer>();
if (r == null) return;
float rimBase = 0f;
float coreBase = 0f;
switch (layerName)
{
case "Layer_Troposphere": rimBase = 2.5f; coreBase = 0.8f; break;
case "Layer_Mesosphere": rimBase = 2.0f; coreBase = 0.6f; break;
case "Layer_Thermosphere": rimBase = 1.5f; coreBase = 0.4f; break;
case "Layer_Exosphere": rimBase = 1.2f; coreBase = 0.2f; break;
}
r.material.SetFloat("_RimIntensity", Mathf.Lerp(0f, rimBase, level));
r.material.SetFloat("_CoreIntensity", Mathf.Lerp(0f, coreBase, level));
}
public void SetIntensity(float level)
{
SetLayerIntensity("Layer_Troposphere", level);
SetLayerIntensity("Layer_Mesosphere", level);
SetLayerIntensity("Layer_Thermosphere", level);
SetLayerIntensity("Layer_Exosphere", level);
}
}