Files
ScienceLab.TimeAndSpace/Assets/Scripts/Countries/GlobeBuilder.cs
2026-03-17 18:10:00 +02:00

78 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GlobeBuilder : MonoBehaviour
{
public float globeRadius = 5f;
public float rotationSpeed = 3f;
public bool autoRotate = true;
public Material countryMaterial;
private static readonly Color[] Palette = new Color[]
{
new Color(0.98f, 0.85f, 0.65f),
new Color(0.65f, 0.85f, 0.72f),
new Color(0.72f, 0.82f, 0.95f),
new Color(0.95f, 0.78f, 0.72f),
new Color(0.88f, 0.95f, 0.68f),
new Color(0.95f, 0.90f, 0.65f),
new Color(0.78f, 0.88f, 0.95f),
new Color(0.95f, 0.72f, 0.85f),
new Color(0.72f, 0.95f, 0.88f),
new Color(0.90f, 0.80f, 0.95f),
new Color(0.95f, 0.88f, 0.78f),
new Color(0.75f, 0.92f, 0.75f),
};
void Awake()
{
SetupCountries();
}
void Update()
{
if (autoRotate)
transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime);
}
void SetupCountries()
{
CountryData[] countries = GetComponentsInChildren<CountryData>();
for (int i = 0; i < countries.Length; i++)
{
CountryData country = countries[i];
Renderer rend = country.GetComponent<Renderer>();
if (rend == null) rend = country.GetComponentInChildren<Renderer>();
if (rend == null) continue;
Material mat = countryMaterial != null
? new Material(countryMaterial)
: new Material(rend.sharedMaterial);
rend.material = mat;
country.Init(mat);
Color baseColor = Palette[i % Palette.Length];
if (country.hasAnomaly)
baseColor = Color.Lerp(baseColor, new Color(1f, 0.5f, 0.3f), 0.3f);
country.SetDefaultColor(baseColor);
MeshFilter mf = country.GetComponent<MeshFilter>();
if (mf == null) mf = country.GetComponentInChildren<MeshFilter>();
if (mf != null && mf.sharedMesh != null)
{
Vector3 worldCenter = country.transform.TransformPoint(mf.sharedMesh.bounds.center);
Vector3 localCenter = transform.InverseTransformPoint(worldCenter);
country.centerOnGlobe = localCenter.normalized * globeRadius;
}
else
{
Vector3 localPos = transform.InverseTransformPoint(country.transform.position);
country.centerOnGlobe = localPos.normalized * globeRadius;
}
}
}
public void SetRotation(bool enabled) => autoRotate = enabled;
}