OldBlueWater/BlueWater/Assets/Distant Lands/Cozy Weather/Contents/Scripts/Modules/CozySatelliteModule.cs

293 lines
11 KiB
C#
Raw Normal View History

2023-09-13 05:07:40 +00:00
using System.Collections;
using System.Collections.Generic;
// Distant Lands 2022.
using UnityEngine;
using DistantLands.Cozy.Data;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
#endif
#if COZY_URP
using UnityEngine.Rendering;
#endif
namespace DistantLands.Cozy
{
[ExecuteAlways]
2024-01-03 06:34:33 +00:00
public class CozySatelliteModule : CozyModule
2023-09-13 05:07:40 +00:00
{
public SatelliteProfile[] satellites = new SatelliteProfile[0];
[HideInInspector]
public Transform satHolder = null;
public bool hideInHierarchy = true;
2024-01-03 06:34:33 +00:00
private Light moonLight;
2023-09-13 05:07:40 +00:00
// Start is called before the first frame update
void Awake()
{
UpdateSatellites();
}
// Update is called once per frame
void Update()
{
if (weatherSphere.freezeUpdateInEditMode && !Application.isPlaying)
return;
if (satHolder == null)
{
UpdateSatellites();
}
if (satHolder.hideFlags == (HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild) && hideInHierarchy)
UpdateSatellites();
if (satHolder.hideFlags != (HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild) && !hideInHierarchy)
UpdateSatellites();
if (weatherSphere.cozyCamera)
satHolder.position = weatherSphere.cozyCamera.transform.position;
if (satellites != null)
foreach (SatelliteProfile sat in satellites)
{
if (!sat)
break;
if (sat.orbitRef == null)
UpdateSatellites();
if (sat.changedLastFrame == true)
UpdateSatellites();
2024-01-03 06:34:33 +00:00
if (sat.linkToDay && weatherSphere.timeModule)
{
float dec = sat.declination * Mathf.Sin(Mathf.PI * 2 * ((weatherSphere.modifiedDayPercentage + (float)((weatherSphere.timeModule.currentDay + sat.declinationPeriodOffset + weatherSphere.timeModule.GetDaysPerYear() * weatherSphere.timeModule.currentYear)) % sat.declinationPeriod) / sat.declinationPeriod));
sat.orbitRef.localEulerAngles = new Vector3(0, weatherSphere.sunDirection + sat.satelliteDirection, sat.satellitePitch + dec);
sat.satelliteRotation = (weatherSphere.modifiedDayPercentage + (float)((weatherSphere.timeModule.currentDay + sat.rotationPeriodOffset + weatherSphere.timeModule.GetDaysPerYear() * weatherSphere.timeModule.currentYear)) % sat.rotationPeriod) / sat.rotationPeriod * 360;
sat.orbitRef.GetChild(0).localEulerAngles = Vector3.right * ((360 * weatherSphere.modifiedDayPercentage) + sat.satelliteRotation + sat.orbitOffset);
}
else
{
sat.orbitRef.localEulerAngles = new Vector3(0, weatherSphere.sunDirection + sat.satelliteDirection, sat.satellitePitch);
sat.orbitRef.GetChild(0).localEulerAngles = Vector3.right * ((360 * weatherSphere.dayPercentage) + sat.orbitOffset);
sat.satelliteRotation += Time.deltaTime * sat.satelliteRotateSpeed;
sat.moonRef.localEulerAngles = sat.initialRotation + sat.satelliteRotateAxis.normalized * sat.satelliteRotation;
}
2023-09-13 05:07:40 +00:00
if (sat.useLight)
{
2024-01-03 06:34:33 +00:00
float moonBrightness = Mathf.Clamp01(Mathf.Sin((weatherSphere.dayPercentage + 0.25f) * 2 * Mathf.PI) + 0.25f);
sat.lightRef.color = weatherSphere.moonlightColor * weatherSphere.sunFilter * sat.lightColorMultiplier * moonBrightness;
#if COZY_URP
if (sat.flareRef)
{
sat.flareRef.intensity = moonBrightness;
}
#endif
sat.lightRef.enabled = weatherSphere.moonlightColor.grayscale > 0.05f;
2023-09-13 05:07:40 +00:00
}
}
}
public void UpdateSatellites()
{
if (weatherSphere == null)
weatherSphere = CozyWeather.instance;
Transform oldHolder = null;
if (satHolder)
{
oldHolder = satHolder;
}
satHolder = new GameObject("Cozy Satellites").transform;
if (hideInHierarchy)
satHolder.gameObject.hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild | HideFlags.HideInHierarchy;
else
satHolder.gameObject.hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild;
bool j = true;
if (satellites != null)
foreach (SatelliteProfile i in satellites)
{
InitializeSatellite(i, j);
j = false;
}
if (oldHolder)
DestroyImmediate(oldHolder.gameObject);
}
public void DestroySatellites()
{
if (satHolder)
DestroyImmediate(satHolder.gameObject);
}
public void DestroySatellite(SatelliteProfile sat)
{
if (sat.orbitRef)
DestroyImmediate(sat.orbitRef.gameObject);
}
2024-01-03 06:34:33 +00:00
public override void DeinitializeModule()
2023-09-13 05:07:40 +00:00
{
DestroySatellites();
2024-01-03 06:34:33 +00:00
Shader.SetGlobalVector("CZY_MoonDirection", Vector3.down);
2023-09-13 05:07:40 +00:00
}
public void InitializeSatellite(SatelliteProfile sat, bool mainMoon)
{
float dist = 0;
if (weatherSphere.lockToCamera != CozyWeather.LockToCameraStyle.DontLockToCamera && weatherSphere.cozyCamera)
dist = .92f * weatherSphere.cozyCamera.farClipPlane * sat.distance;
else
dist = .92f * 1000 * sat.distance * weatherSphere.transform.localScale.x;
sat.orbitRef = new GameObject(sat.name).transform;
sat.orbitRef.parent = satHolder;
sat.orbitRef.transform.localPosition = Vector3.zero;
var orbitArm = new GameObject("Orbit Arm");
orbitArm.transform.parent = sat.orbitRef;
orbitArm.transform.localPosition = Vector3.zero;
orbitArm.transform.localEulerAngles = Vector3.zero;
sat.moonRef = Instantiate(sat.satelliteReference, Vector3.forward * dist, Quaternion.identity, sat.orbitRef.GetChild(0)).transform;
sat.moonRef.transform.localPosition = -Vector3.forward * dist;
sat.moonRef.transform.localEulerAngles = sat.initialRotation;
sat.moonRef.transform.localScale = sat.satelliteReference.transform.localScale * sat.size * dist / 1000;
sat.orbitRef.localEulerAngles = new Vector3(0, sat.satelliteDirection, sat.satellitePitch);
2024-01-03 06:34:33 +00:00
sat.orbitRef.GetChild(0).localEulerAngles = Vector3.right * ((360 * weatherSphere.dayPercentage) + sat.orbitOffset);
2023-09-13 05:07:40 +00:00
if (sat.useLight)
{
var obj = new GameObject("Light");
obj.transform.parent = sat.orbitRef.GetChild(0);
sat.lightRef = obj.AddComponent<Light>();
sat.lightRef.transform.localEulerAngles = new Vector3(0, 0, 0);
2024-01-03 06:34:33 +00:00
sat.lightRef.transform.localPosition = -Vector3.forward * dist;
2023-09-13 05:07:40 +00:00
sat.lightRef.type = LightType.Directional;
sat.lightRef.shadows = sat.castShadows;
#if COZY_URP
2024-01-03 06:34:33 +00:00
if (sat.flare.flare)
2023-09-13 05:07:40 +00:00
{
2024-01-03 06:34:33 +00:00
sat.flareRef = obj.AddComponent<LensFlareComponentSRP>();
sat.flareRef.intensity = sat.flare.intensity;
sat.flareRef.lensFlareData = sat.flare.flare;
sat.flareRef.allowOffScreen = sat.flare.allowOffscreen;
sat.flareRef.radialScreenAttenuationCurve = sat.flare.screenAttenuation;
sat.flareRef.distanceAttenuationCurve = sat.flare.screenAttenuation;
sat.flareRef.scale = sat.flare.scale;
sat.flareRef.occlusionRadius = sat.flare.occlusionRadius;
sat.flareRef.useOcclusion = sat.flare.useOcclusion;
2023-09-13 05:07:40 +00:00
}
#else
if (sat.flare)
sat.lightRef.flare = sat.flare;
#endif
}
if (mainMoon)
{
sat.orbitRef.GetChild(0).gameObject.AddComponent<CozySetMoonDirection>();
}
sat.changedLastFrame = false;
}
void Reset()
{
List<SatelliteProfile> profiles = new List<SatelliteProfile>();
2024-01-03 06:34:33 +00:00
profiles.Add(Resources.Load("Profiles/Satellites/Stylized Moon") as SatelliteProfile);
2023-09-13 05:07:40 +00:00
satellites = profiles.ToArray();
}
}
#if UNITY_EDITOR
2024-01-03 06:34:33 +00:00
[CustomEditor(typeof(CozySatelliteModule))]
2023-09-13 05:07:40 +00:00
[CanEditMultipleObjects]
public class E_SatelliteManager : E_CozyModule
{
2024-01-03 06:34:33 +00:00
CozySatelliteModule t;
2023-09-13 05:07:40 +00:00
static bool manageSatellites;
private void OnEnable()
{
2024-01-03 06:34:33 +00:00
t = (CozySatelliteModule)target;
2023-09-13 05:07:40 +00:00
}
public override GUIContent GetGUIContent()
{
return new GUIContent(" Satellites", (Texture)Resources.Load("CozyMoon"), "Manage satellites and moons within the COZY system.");
}
2024-01-03 06:34:33 +00:00
public override void OpenDocumentationURL()
2023-09-13 05:07:40 +00:00
{
2024-01-03 06:34:33 +00:00
Application.OpenURL("https://distant-lands.gitbook.io/cozy-stylized-weather-documentation/how-it-works/modules/satellite-module");
2023-09-13 05:07:40 +00:00
}
2024-01-03 06:34:33 +00:00
2023-09-13 05:07:40 +00:00
public override void DisplayInCozyWindow()
{
serializedObject.Update();
2024-01-03 06:34:33 +00:00
manageSatellites = EditorGUILayout.BeginFoldoutHeaderGroup(manageSatellites, new GUIContent(" Manage Satellites"), EditorUtilities.FoldoutStyle);
2023-09-13 05:07:40 +00:00
EditorGUILayout.EndFoldoutHeaderGroup();
if (manageSatellites)
{
EditorGUILayout.Space();
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(serializedObject.FindProperty("satellites"), true);
EditorGUILayout.Space();
EditorGUILayout.PropertyField(serializedObject.FindProperty("hideInHierarchy"));
EditorGUI.indentLevel--;
serializedObject.ApplyModifiedProperties();
}
EditorGUI.indentLevel++;
if (t.satellites != null)
foreach (SatelliteProfile i in t.satellites)
{
if (i)
(CreateEditor(i) as E_SatelliteProfile).NestedGUI();
}
EditorGUI.indentLevel--;
EditorGUILayout.Space();
if (GUILayout.Button("Refresh Satellites"))
2024-01-03 06:34:33 +00:00
((CozySatelliteModule)target).UpdateSatellites();
2023-09-13 05:07:40 +00:00
serializedObject.ApplyModifiedProperties();
}
}
#endif
}