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

202 lines
5.9 KiB
C#
Raw Normal View History

2023-09-13 05:07:40 +00:00
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
2024-01-03 06:34:33 +00:00
#if COZY_URP
using UnityEngine.Rendering.Universal;
#endif
2023-09-13 05:07:40 +00:00
namespace DistantLands.Cozy
{
[ExecuteAlways]
2024-01-03 06:34:33 +00:00
public class CozyReflectionsModule : CozyModule
2023-09-13 05:07:40 +00:00
{
public enum UpdateFrequency { everyFrame, onAwake, viaScripting }
public UpdateFrequency updateFrequency;
public Cubemap reflectionCubemap;
public Camera reflectionCamera;
[Tooltip("How many frames should pass before the cubemap renders again? A value of 0 renders every frame and a value of 30 renders once every 30 frames.")]
[Range(0, 30)]
public int framesBetweenRenders = 10;
[Tooltip("What layers should be rendered into the skybox reflections?.")]
public LayerMask layerMask = 2048;
2024-01-03 06:34:33 +00:00
private int framesLeft;
#if COZY_URP
public int rendererOverride;
#endif
2023-09-13 05:07:40 +00:00
2024-01-03 06:34:33 +00:00
public override void InitializeModule()
2023-09-13 05:07:40 +00:00
{
2024-01-03 06:34:33 +00:00
base.InitializeModule();
2023-09-13 05:07:40 +00:00
reflectionCubemap = Resources.Load("Materials/Reflection Cubemap") as Cubemap;
RenderSettings.customReflection = reflectionCubemap;
RenderSettings.defaultReflectionMode = UnityEngine.Rendering.DefaultReflectionMode.Custom;
weatherSphere.fogMesh.gameObject.layer = ToLayer(layerMask);
weatherSphere.skyMesh.gameObject.layer = ToLayer(layerMask);
weatherSphere.cloudMesh.gameObject.layer = ToLayer(layerMask);
if (updateFrequency == UpdateFrequency.onAwake)
2024-01-03 06:34:33 +00:00
{
2023-09-13 05:07:40 +00:00
RenderReflections();
2024-01-03 06:34:33 +00:00
}
2023-09-13 05:07:40 +00:00
}
2024-01-03 06:34:33 +00:00
public override void CozyUpdateLoop()
2023-09-13 05:07:40 +00:00
{
if (weatherSphere == null)
{
2024-01-03 06:34:33 +00:00
base.InitializeModule();
2023-09-13 05:07:40 +00:00
}
2024-01-03 06:34:33 +00:00
2023-09-13 05:07:40 +00:00
if (weatherSphere.freezeUpdateInEditMode && !Application.isPlaying)
2024-01-03 06:34:33 +00:00
{
2023-09-13 05:07:40 +00:00
return;
2024-01-03 06:34:33 +00:00
}
2023-09-13 05:07:40 +00:00
if (updateFrequency == UpdateFrequency.everyFrame)
2024-01-03 06:34:33 +00:00
{
2023-09-13 05:07:40 +00:00
if (framesLeft < 0)
{
RenderReflections();
framesLeft = framesBetweenRenders;
}
else
2024-01-03 06:34:33 +00:00
{
2023-09-13 05:07:40 +00:00
framesLeft--;
2024-01-03 06:34:33 +00:00
}
}
2023-09-13 05:07:40 +00:00
}
public int ToLayer(LayerMask mask)
{
int value = mask.value;
if (value == 0)
{
return 0;
}
for (int l = 1; l < 32; l++)
{
if ((value & (1 << l)) != 0)
{
return l;
}
}
return -1;
}
2024-01-03 06:34:33 +00:00
public override void DeinitializeModule()
2023-09-13 05:07:40 +00:00
{
2024-01-03 06:34:33 +00:00
base.DeinitializeModule();
2023-09-13 05:07:40 +00:00
if (reflectionCamera)
2024-01-03 06:34:33 +00:00
{
2023-09-13 05:07:40 +00:00
DestroyImmediate(reflectionCamera.gameObject);
2024-01-03 06:34:33 +00:00
}
2023-09-13 05:07:40 +00:00
RenderSettings.customReflection = null;
}
public void RenderReflections()
{
if (!weatherSphere.cozyCamera)
{
Debug.LogError("COZY Reflections requires the cozy camera to be set in the settings tab!");
return;
}
if (reflectionCamera == null)
2024-01-03 06:34:33 +00:00
{
2023-09-13 05:07:40 +00:00
SetupCamera();
2024-01-03 06:34:33 +00:00
}
2023-09-13 05:07:40 +00:00
reflectionCamera.enabled = true;
reflectionCamera.transform.position = transform.position;
reflectionCamera.nearClipPlane = weatherSphere.cozyCamera.nearClipPlane;
reflectionCamera.farClipPlane = weatherSphere.cozyCamera.farClipPlane;
reflectionCamera.cullingMask = layerMask;
2024-01-03 06:34:33 +00:00
#if COZY_URP
if (reflectionCamera.GetComponent<UniversalAdditionalCameraData>())
reflectionCamera.GetComponent<UniversalAdditionalCameraData>().SetRenderer(rendererOverride);
#endif
2023-09-13 05:07:40 +00:00
reflectionCamera.RenderToCubemap(reflectionCubemap);
reflectionCamera.enabled = false;
}
public void SetupCamera()
{
2024-01-03 06:34:33 +00:00
GameObject i = new GameObject
{
name = "COZY Reflection Camera",
hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild | HideFlags.HideInHierarchy
};
2023-09-13 05:07:40 +00:00
reflectionCamera = i.AddComponent<Camera>();
reflectionCamera.depth = -50;
reflectionCamera.enabled = false;
}
}
#if UNITY_EDITOR
2024-01-03 06:34:33 +00:00
[CustomEditor(typeof(CozyReflectionsModule))]
2023-09-13 05:07:40 +00:00
[CanEditMultipleObjects]
public class E_CozyReflect : E_CozyModule
{
2024-01-03 06:34:33 +00:00
private CozyReflectionsModule reflect;
2023-09-13 05:07:40 +00:00
public override GUIContent GetGUIContent()
{
//Place your module's GUI content here.
return new GUIContent(" Reflections", (Texture)Resources.Load("Reflections"), "Sets up a cubemap for reflections with COZY.");
}
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/reflections-module");
2023-09-13 05:07:40 +00:00
}
public override void DisplayInCozyWindow()
{
if (reflect == null)
2024-01-03 06:34:33 +00:00
{
reflect = (CozyReflectionsModule)target;
}
2023-09-13 05:07:40 +00:00
serializedObject.Update();
EditorGUILayout.PropertyField(serializedObject.FindProperty("updateFrequency"));
2024-01-03 06:34:33 +00:00
if (reflect.updateFrequency == CozyReflectionsModule.UpdateFrequency.everyFrame)
{
2023-09-13 05:07:40 +00:00
EditorGUILayout.PropertyField(serializedObject.FindProperty("framesBetweenRenders"));
2024-01-03 06:34:33 +00:00
}
2023-09-13 05:07:40 +00:00
EditorGUILayout.PropertyField(serializedObject.FindProperty("reflectionCubemap"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("layerMask"));
2024-01-03 06:34:33 +00:00
#if COZY_URP
EditorGUILayout.PropertyField(serializedObject.FindProperty("rendererOverride"));
#endif
2023-09-13 05:07:40 +00:00
serializedObject.ApplyModifiedProperties();
}
}
#endif
}