add weather and time

This commit is contained in:
XuGaoFeng
2026-05-09 09:10:52 +08:00
parent 48e0dea5e1
commit 0ca1b49fa7
639 changed files with 121558 additions and 102 deletions

View File

@@ -0,0 +1,672 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
#if ENVIRO_HDRP
using UnityEngine.Rendering.HighDefinition;
#endif
namespace Enviro
{
[AddComponentMenu("Enviro 3/Reflection Probe")]
[RequireComponent(typeof(ReflectionProbe)), ExecuteInEditMode]
public class EnviroReflectionProbe : MonoBehaviour
{
#region Public Var
#region Standalone Settings
public bool standalone = false;
public bool updateReflectionOnGameTime = true;
public float reflectionsUpdateTreshhold = 0.025f;
public bool useTimeSlicing = true;
#endregion
public Camera renderCam;
[HideInInspector]
public ReflectionProbe myProbe;
public bool customRendering = false;
#if !ENVIRO_HDRP
private EnviroRenderer enviroRenderer;
#endif
public bool useFog = false;
#endregion
#region Private Var
// Privates
public Camera bakingCam;
public int renderId = -1;
private bool currentMode = false;
private int currentRes;
private RenderTexture cubemap;
private RenderTexture finalCubemap;
private RenderTexture mirrorTexture;
private RenderTexture renderTexture;
private GameObject renderCamObj;
private Material mirror = null;
private Material bakeMat = null;
private Material convolutionMat;
private Coroutine refreshing;
private int renderID;
#if ENVIRO_HDRP
public HDProbe hdprobe;
#endif
private static Quaternion[] orientations = new Quaternion[]
{
Quaternion.LookRotation(Vector3.right, Vector3.down),
Quaternion.LookRotation(Vector3.left, Vector3.down),
Quaternion.LookRotation(Vector3.up, Vector3.forward),
Quaternion.LookRotation(Vector3.down, Vector3.back),
Quaternion.LookRotation(Vector3.forward, Vector3.down),
Quaternion.LookRotation(Vector3.back, Vector3.down)
};
private double lastRelfectionUpdate;
#endregion
////////
void OnEnable()
{
myProbe = GetComponent<ReflectionProbe>();
#if ENVIRO_HDRP
if (EnviroManager.instance != null)
{
hdprobe = GetComponent<HDProbe>();
if(!standalone && myProbe != null)
myProbe.enabled = true;
if (customRendering)
{
if (hdprobe != null)
{
hdprobe.mode = ProbeSettings.Mode.Custom;
CreateCubemap();
CreateTexturesAndMaterial();
CreateRenderCamera();
currentRes = myProbe.resolution;
StartCoroutine(RefreshFirstTime());
}
}
else
{
if (hdprobe != null)
{
hdprobe.mode = ProbeSettings.Mode.Realtime;
hdprobe.realtimeMode = ProbeSettings.RealtimeMode.OnDemand;
hdprobe.RequestRenderNextUpdate();
}
}
}
#else
if (!standalone && myProbe != null)
myProbe.enabled = true;
if (customRendering)
{
myProbe.mode = ReflectionProbeMode.Custom;
myProbe.refreshMode = ReflectionProbeRefreshMode.ViaScripting;
CreateCubemap();
CreateTexturesAndMaterial();
CreateRenderCamera();
currentRes = myProbe.resolution;
StartCoroutine(RefreshFirstTime());
}
else
{
myProbe.mode = ReflectionProbeMode.Realtime;
myProbe.refreshMode = ReflectionProbeRefreshMode.ViaScripting;
//StartCoroutine(RefreshUnity());
renderId = myProbe.RenderProbe();
}
#endif
}
void OnDisable()
{
Cleanup();
if (!standalone && myProbe != null)
myProbe.enabled = false;
RenderSettings.defaultReflectionMode = UnityEngine.Rendering.DefaultReflectionMode.Skybox;
}
private void Cleanup()
{
if (refreshing != null)
StopCoroutine(refreshing);
if (cubemap != null)
{
if (renderCam != null)
renderCam.targetTexture = null;
DestroyImmediate(cubemap);
}
if (renderCamObj != null)
DestroyImmediate(renderCamObj);
if (mirrorTexture != null)
DestroyImmediate(mirrorTexture);
if (renderTexture != null)
DestroyImmediate(renderTexture);
}
// Creation
private void CreateRenderCamera()
{
if (renderCamObj == null)
{
renderCamObj = new GameObject();
renderCamObj.name = "Reflection Probe Cam";
renderCamObj.hideFlags = HideFlags.HideAndDontSave;
renderCam = renderCamObj.AddComponent<Camera>();
renderCam.gameObject.SetActive(true);
renderCam.cameraType = CameraType.Reflection;
renderCam.fieldOfView = 90;
renderCam.farClipPlane = myProbe.farClipPlane;
renderCam.nearClipPlane = myProbe.nearClipPlane;
renderCam.clearFlags = (CameraClearFlags)myProbe.clearFlags;
renderCam.backgroundColor = myProbe.backgroundColor;
renderCam.allowHDR = myProbe.hdr;
renderCam.targetTexture = cubemap;
renderCam.enabled = false;
#if VEGETATION_STUDIO_PRO
// VegetationStudioManager.Instance.AddCamera(renderCam);
#endif
#if !ENVIRO_HDRP
if (EnviroManager.instance != null)
{
enviroRenderer = renderCamObj.AddComponent<EnviroRenderer>();
}
#endif
}
}
private void UpdateCameraSettings()
{
if (renderCam != null)
{
renderCam.cullingMask = myProbe.cullingMask;
#if !ENVIRO_HDRP
if (EnviroManager.instance != null)
{
//Update Quality
}
#endif
}
}
private Camera CreateBakingCamera()
{
GameObject tempCam = new GameObject();
tempCam.name = "Reflection Probe Cam";
Camera cam = tempCam.AddComponent<Camera>();
cam.enabled = false;
cam.gameObject.SetActive(true);
cam.cameraType = CameraType.Reflection;
cam.fieldOfView = 90;
cam.farClipPlane = myProbe.farClipPlane;
cam.nearClipPlane = myProbe.nearClipPlane;
cam.cullingMask = myProbe.cullingMask;
cam.clearFlags = (CameraClearFlags)myProbe.clearFlags;
cam.backgroundColor = myProbe.backgroundColor;
cam.allowHDR = myProbe.hdr;
cam.targetTexture = cubemap;
#if !ENVIRO_HDRP
if (EnviroManager.instance != null)
{
enviroRenderer = renderCamObj.AddComponent<EnviroRenderer>();
}
#endif
tempCam.hideFlags = HideFlags.HideAndDontSave;
return cam;
}
private void CreateCubemap()
{
if (cubemap != null && myProbe.resolution == currentRes)
return;
if (cubemap != null)
{
cubemap.Release();
DestroyImmediate(cubemap);
}
if (finalCubemap != null)
{
finalCubemap.Release();
DestroyImmediate(finalCubemap);
}
int resolution = myProbe.resolution;
currentRes = resolution;
RenderTextureFormat format = myProbe.hdr ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32;
cubemap = new RenderTexture(resolution, resolution, 16, format, RenderTextureReadWrite.Linear);
cubemap.dimension = TextureDimension.Cube;
cubemap.useMipMap = true;
cubemap.autoGenerateMips = false;
cubemap.name = "Enviro Reflection Temp Cubemap";
cubemap.filterMode = FilterMode.Trilinear;
cubemap.Create();
finalCubemap = new RenderTexture(resolution, resolution, 16, format, RenderTextureReadWrite.Linear);
finalCubemap.dimension = TextureDimension.Cube;
finalCubemap.useMipMap = true;
finalCubemap.autoGenerateMips = false;
finalCubemap.name = "Enviro Reflection Final Cubemap";
finalCubemap.filterMode = FilterMode.Trilinear;
finalCubemap.Create();
}
//Create the textures
private void CreateTexturesAndMaterial()
{
if (mirror == null)
mirror = new Material(Shader.Find("Hidden/Enviro/ReflectionProbe"));
if (convolutionMat == null)
convolutionMat = new Material(Shader.Find("Hidden/EnviroCubemapBlur"));
int resolution = myProbe.resolution;
RenderTextureFormat format = myProbe.hdr ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32;
if (mirrorTexture == null || mirrorTexture.width != resolution || mirrorTexture.height != resolution)
{
if (mirrorTexture != null)
DestroyImmediate(mirrorTexture);
mirrorTexture = new RenderTexture(resolution, resolution, 16, format, RenderTextureReadWrite.Linear);
mirrorTexture.useMipMap = true;
mirrorTexture.autoGenerateMips = false;
mirrorTexture.name = "Enviro Reflection Mirror Texture";
mirrorTexture.Create();
}
if (renderTexture == null || renderTexture.width != resolution || renderTexture.height != resolution)
{
if (renderTexture != null)
DestroyImmediate(renderTexture);
renderTexture = new RenderTexture(resolution, resolution, 16, format, RenderTextureReadWrite.Linear);
renderTexture.useMipMap = true;
renderTexture.autoGenerateMips = false;
renderTexture.name = "Enviro Reflection Target Texture";
renderTexture.Create();
}
}
// Refresh Methods
public void RefreshReflection(bool timeSlice = false)
{
#if ENVIRO_HDRP
if (customRendering)
{
if (refreshing != null)
return;
CreateTexturesAndMaterial();
if (renderCam == null)
CreateRenderCamera();
UpdateCameraSettings();
renderCam.transform.position = transform.position;
renderCam.targetTexture = renderTexture;
if (Application.isPlaying)
{
if (!timeSlice)
refreshing = StartCoroutine(RefreshInstant(renderTexture, mirrorTexture));
else
refreshing = StartCoroutine(RefreshOvertime(renderTexture, mirrorTexture));
}
else
{
refreshing = StartCoroutine(RefreshInstant(renderTexture, mirrorTexture));
}
}
else
{
if(hdprobe != null)
hdprobe.RequestRenderNextUpdate();
}
#else
if (customRendering)
{
if (refreshing != null)
return;
CreateTexturesAndMaterial();
if (renderCam == null)
CreateRenderCamera();
UpdateCameraSettings();
renderCam.transform.position = transform.position;
renderCam.targetTexture = renderTexture;
if (Application.isPlaying)
{
if (!timeSlice)
refreshing = StartCoroutine(RefreshInstant(renderTexture, mirrorTexture));
else
refreshing = StartCoroutine(RefreshOvertime(renderTexture, mirrorTexture));
}
else
{
refreshing = StartCoroutine(RefreshInstant(renderTexture, mirrorTexture));
}
}
else
{
renderId = myProbe.RenderProbe();
}
#endif
}
IEnumerator RefreshFirstTime()
{
yield return null;
RefreshReflection(false);
RefreshReflection(false);
}
public IEnumerator RefreshUnity()
{
yield return null;
renderId = myProbe.RenderProbe();
}
public IEnumerator RefreshInstant(RenderTexture renderTex, RenderTexture mirrorTex)
{
CreateCubemap();
yield return null;
for (int face = 0; face < 6; face++)
{
renderCam.transform.rotation = orientations[face];
renderCam.Render();
if(mirrorTex != null)
{
Graphics.Blit(renderTex, mirrorTex, mirror);
Graphics.CopyTexture(mirrorTex, 0, 0, cubemap, face, 0);
}
}
ConvolutionCubemap();
#if ENVIRO_HDRP
if (hdprobe != null)
hdprobe.SetTexture(ProbeSettings.Mode.Custom, finalCubemap);
#else
myProbe.customBakedTexture = finalCubemap;
#endif
refreshing = null;
}
/// <summary>
/// Update Reflections with Time Slicing
/// </summary>
public IEnumerator RefreshOvertime(RenderTexture renderTex, RenderTexture mirrorTex)
{
CreateCubemap();
for (int face = 0; face < 6; face++)
{
yield return null;
renderCam.transform.rotation = orientations[face];
renderCam.Render();
if(mirrorTex != null)
{
Graphics.Blit(renderTex, mirrorTex, mirror);
Graphics.CopyTexture(mirrorTex, 0, 0, cubemap, face, 0);
}
//ClearTextures();
}
ConvolutionCubemap();
#if ENVIRO_HDRP
if (hdprobe != null)
hdprobe.SetTexture(ProbeSettings.Mode.Custom, finalCubemap);
#else
myProbe.customBakedTexture = finalCubemap;
#endif
refreshing = null;
}
/// <summary>
/// Bakes one face per time into a render texture
/// </summary>
/// <param name="face"></param>
/// <param name="res"></param>
/// <returns></returns>
public RenderTexture BakeCubemapFace(int face, int res)
{
if (bakeMat == null)
bakeMat = new Material(Shader.Find("Hidden/Enviro/BakeCubemap"));
if (bakingCam == null)
bakingCam = CreateBakingCamera();
bakingCam.transform.rotation = orientations[face];
RenderTexture temp = RenderTexture.GetTemporary(res, res, 0, RenderTextureFormat.ARGBFloat);
bakingCam.targetTexture = temp;
bakingCam.Render();
RenderTexture tex = new RenderTexture(res, res, 0, RenderTextureFormat.ARGBFloat);
Graphics.Blit(temp, tex, bakeMat);
RenderTexture.ReleaseTemporary(temp);
return tex;
}
private void ClearTextures()
{
RenderTexture rt = RenderTexture.active;
RenderTexture.active = renderTexture;
GL.Clear(true, true, Color.clear);
RenderTexture.active = mirrorTexture;
GL.Clear(true, true, Color.clear);
RenderTexture.active = rt;
}
//This is not a proper convolution and very hacky to get anywhere near of unity realtime reflection probe mip convolution.
private void ConvolutionCubemap()
{
int mipCount = 7;
GL.PushMatrix();
GL.LoadOrtho();
cubemap.GenerateMips();
float texel = 1f;
switch(finalCubemap.width)
{
case 16:
texel = 1f;
break;
case 32:
texel = 1f;
break;
case 64:
texel = 2f;
break;
case 128:
texel = 4f;
break;
case 256:
texel = 8f;
break;
case 512:
texel = 14f;
break;
case 1024:
texel = 30f;
break;
case 2048:
texel = 60f;
break;
}
float res = finalCubemap.width;
for (int mip = 0; mip < mipCount + 1; mip++)
{
//Copy each face
Graphics.CopyTexture(cubemap, 0, mip, finalCubemap, 0, mip);
Graphics.CopyTexture(cubemap, 1, mip, finalCubemap, 1, mip);
Graphics.CopyTexture(cubemap, 2, mip, finalCubemap, 2, mip);
Graphics.CopyTexture(cubemap, 3, mip, finalCubemap, 3, mip);
Graphics.CopyTexture(cubemap, 4, mip, finalCubemap, 4, mip);
Graphics.CopyTexture(cubemap, 5, mip, finalCubemap, 5, mip);
int dstMip = mip + 1;
if (dstMip == mipCount)
break;
float texelSize = (texel * dstMip) / res;
convolutionMat.SetTexture("_MainTex", finalCubemap);
convolutionMat.SetFloat("_Texel", texelSize);
convolutionMat.SetFloat("_Level", mip);
convolutionMat.SetPass(0);
res *= 0.75f;
// Positive X
Graphics.SetRenderTarget(cubemap, dstMip, CubemapFace.PositiveX);
GL.Begin(GL.QUADS);
GL.TexCoord3(1, 1, 1);
GL.Vertex3(0, 0, 1);
GL.TexCoord3(1, -1, 1);
GL.Vertex3(0, 1, 1);
GL.TexCoord3(1, -1, -1);
GL.Vertex3(1, 1, 1);
GL.TexCoord3(1, 1, -1);
GL.Vertex3(1, 0, 1);
GL.End();
// Negative X
Graphics.SetRenderTarget(cubemap, dstMip, CubemapFace.NegativeX);
GL.Begin(GL.QUADS);
GL.TexCoord3(-1, 1, -1);
GL.Vertex3(0, 0, 1);
GL.TexCoord3(-1, -1, -1);
GL.Vertex3(0, 1, 1);
GL.TexCoord3(-1, -1, 1);
GL.Vertex3(1, 1, 1);
GL.TexCoord3(-1, 1, 1);
GL.Vertex3(1, 0, 1);
GL.End();
// Positive Y
Graphics.SetRenderTarget(cubemap, dstMip, CubemapFace.PositiveY);
GL.Begin(GL.QUADS);
GL.TexCoord3(-1, 1, -1);
GL.Vertex3(0, 0, 1);
GL.TexCoord3(-1, 1, 1);
GL.Vertex3(0, 1, 1);
GL.TexCoord3(1, 1, 1);
GL.Vertex3(1, 1, 1);
GL.TexCoord3(1, 1, -1);
GL.Vertex3(1, 0, 1);
GL.End();
// Negative Y
Graphics.SetRenderTarget(cubemap, dstMip, CubemapFace.NegativeY);
GL.Begin(GL.QUADS);
GL.TexCoord3(-1, -1, 1);
GL.Vertex3(0, 0, 1);
GL.TexCoord3(-1, -1, -1);
GL.Vertex3(0, 1, 1);
GL.TexCoord3(1, -1, -1);
GL.Vertex3(1, 1, 1);
GL.TexCoord3(1, -1, 1);
GL.Vertex3(1, 0, 1);
GL.End();
// Positive Z
Graphics.SetRenderTarget(cubemap, dstMip, CubemapFace.PositiveZ);
GL.Begin(GL.QUADS);
GL.TexCoord3(-1, 1, 1);
GL.Vertex3(0, 0, 1);
GL.TexCoord3(-1, -1, 1);
GL.Vertex3(0, 1, 1);
GL.TexCoord3(1, -1, 1);
GL.Vertex3(1, 1, 1);
GL.TexCoord3(1, 1, 1);
GL.Vertex3(1, 0, 1);
GL.End();
// Negative Z
Graphics.SetRenderTarget(cubemap, dstMip, CubemapFace.NegativeZ);
GL.Begin(GL.QUADS);
GL.TexCoord3(1, 1, -1);
GL.Vertex3(0, 0, 1);
GL.TexCoord3(1, -1, -1);
GL.Vertex3(0, 1, 1);
GL.TexCoord3(-1, -1, -1);
GL.Vertex3(1, 1, 1);
GL.TexCoord3(-1, 1, -1);
GL.Vertex3(1, 0, 1);
GL.End();
}
GL.PopMatrix();
}
private void UpdateStandaloneReflection()
{
if ((EnviroManager.instance.Time.GetDateInHours() > lastRelfectionUpdate + reflectionsUpdateTreshhold ||EnviroManager.instance.Time.GetDateInHours() < lastRelfectionUpdate - reflectionsUpdateTreshhold) && updateReflectionOnGameTime)
{
lastRelfectionUpdate = EnviroManager.instance.Time.GetDateInHours();
RefreshReflection(!useTimeSlicing);
}
}
private void Update()
{
if (currentMode != customRendering)
{
currentMode = customRendering;
if (customRendering)
{
OnEnable();
}
else
{
OnEnable();
Cleanup();
}
}
if (EnviroManager.instance != null && standalone)
{
UpdateStandaloneReflection();
}
}
}
}

View File

@@ -0,0 +1,19 @@
fileFormatVersion: 2
guid: 766b1caed0279ea4281500fc502a2853
timeCreated: 1558916543
licenseType: Store
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 236601
packageName: Enviro 3 - Sky and Weather
packageVersion: 3.1.2
assetPath: Assets/Enviro 3 - Sky and Weather/Scripts/Runtime/Modules/Reflections/EnviroReflectionProbe.cs
uploadId: 660896

View File

@@ -0,0 +1,407 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using System;
namespace Enviro
{
[Serializable]
public class EnviroReflections
{
public enum GlobalReflectionResolution
{
R16,
R32,
R64,
R128,
R256,
R512,
R1024,
R2048
}
public bool globalReflections = true;
[Tooltip("Set if enviro reflection probe should use custom rendering setup. For example to include post effectsin birp.")]
public bool customRendering = true;
[Tooltip("Set to use custom timeslicing when rendered in custom mode.")]
public bool customRenderingTimeSlicing = true;
[Tooltip("Set if enviro reflection probe should update faces individual on different frames.")]
public ReflectionProbeTimeSlicingMode globalReflectionTimeSlicingMode = ReflectionProbeTimeSlicingMode.IndividualFaces;
[Tooltip("Enable/disable enviro reflection probe updates based on gametime changes..")]
public bool globalReflectionsUpdateOnGameTime = true;
[Tooltip("Enable/disable enviro reflection probe updates based on transform position changes..")]
public bool globalReflectionsUpdateOnPosition = true;
[Tooltip("Reflection probe intensity.")]
[Range(0f, 2f)]
public float globalReflectionsIntensity = 1.0f;
[Tooltip("Reflection probe update rate based on game time.")]
public float globalReflectionsTimeTreshold = 0.025f;
[Tooltip("Reflection probe update rate based on camera position.")]
public float globalReflectionsPositionTreshold = 0.5f;
[Tooltip("Reflection probe scale. Increase that one to increase the area where reflection probe will influence your scene.")]
[Range(10f, 10000f)]
public float globalReflectionsScale = 10000f;
[Tooltip("Reflection probe resolution.")]
public GlobalReflectionResolution globalReflectionResolution = GlobalReflectionResolution.R256;
[Tooltip("Reflection probe rendered Layers.")]
public LayerMask globalReflectionLayers;
[Tooltip("Enable this option to update the default reflection with global reflection probes cubemap. This can be needed for material that might not support direct reflection probes. (Instanced Indirect Rendering)")]
public bool updateDefaultEnvironmentReflections = true;
[Tooltip("Reflection cubemap used for default scene sky reflections in < Unity 2022.1 versions.")]
public Cubemap defaultSkyReflectionTex;
}
[Serializable]
[ExecuteInEditMode]
public class EnviroReflectionsModule : EnviroModule
{
public Enviro.EnviroReflections Settings;
public EnviroReflectionsModule preset;
// Inspector
public bool showReflectionControls;
public float lastReflectionUpdate;
public Vector3 lastReflectionUpdatePos;
private Coroutine renderReflectionCoroutine;
private Coroutine waitForProbeCoroutine;
private Coroutine copyDefaultReflectionCoroutine;
public override void Enable ()
{
if(EnviroManager.instance == null)
return;
Setup();
// Update global reflections once on enable.
if(EnviroManager.instance.Objects.globalReflectionProbe != null)
EnviroManager.instance.StartCoroutine(WaitToRefreshReflection());
}
public override void Disable ()
{
if(EnviroManager.instance == null)
return;
Cleanup();
}
private void Cleanup()
{
if(EnviroManager.instance == null)
return;
if(EnviroManager.instance.Objects.globalReflectionProbe != null)
DestroyImmediate(EnviroManager.instance.Objects.globalReflectionProbe.gameObject);
}
// Unity warns with "Attempting to update a disabled Reflection Probe" even though the probe is enabled.
// We have to wait a frame before interacting with reflection probes to allow Unity time to do any
// setup in its internal OnEnable(). Otherwise, we will receive a warning:
// "Attempting to update a disabled Reflection Probe. Action will be ignored."
private IEnumerator WaitToRefreshReflection()
{
yield return null;
RenderGlobalReflectionProbe(true, false);
UpdateDefaultReflectionTextureMode ();
}
private void Setup()
{
if(EnviroManager.instance.Objects.globalReflectionProbe == null)
{
GameObject newReflectionProbe = new GameObject();
newReflectionProbe.name = "Global Reflection Probe";
newReflectionProbe.transform.SetParent(EnviroManager.instance.transform);
newReflectionProbe.transform.localPosition = Vector3.zero;
EnviroManager.instance.Objects.globalReflectionProbe = newReflectionProbe.AddComponent<EnviroReflectionProbe>();
}
}
public override void UpdateModule ()
{
if(EnviroManager.instance == null)
return;
if(EnviroManager.instance.Objects.globalReflectionProbe != null)
UpdateReflection();
}
private void UpdateReflection()
{
if(!Settings.globalReflections)
{
EnviroManager.instance.Objects.globalReflectionProbe.myProbe.enabled = false;
UpdateDefaultReflectionTextureMode ();
return;
}
else
{
EnviroManager.instance.Objects.globalReflectionProbe.myProbe.enabled = true;
}
EnviroReflectionProbe probe = EnviroManager.instance.Objects.globalReflectionProbe;
SetupProbeSettings(probe);
if(EnviroManager.instance.Time != null)
{
if ((lastReflectionUpdate < EnviroManager.instance.Time.Settings.timeOfDay || lastReflectionUpdate > EnviroManager.instance.Time.Settings.timeOfDay + (Settings.globalReflectionsTimeTreshold + 0.01f)) && Settings.globalReflectionsUpdateOnGameTime)
{
RenderGlobalReflectionProbe(false,Settings.customRenderingTimeSlicing);
lastReflectionUpdate = EnviroManager.instance.Time.Settings.timeOfDay + Settings.globalReflectionsTimeTreshold;
}
}
if ((probe.transform.position.magnitude > lastReflectionUpdatePos.magnitude + Settings.globalReflectionsPositionTreshold || probe.transform.position.magnitude < lastReflectionUpdatePos.magnitude - Settings.globalReflectionsPositionTreshold) && Settings.globalReflectionsUpdateOnPosition)
{
RenderGlobalReflectionProbe(false,Settings.customRenderingTimeSlicing);
lastReflectionUpdatePos = probe.transform.position;
}
UpdateDefaultReflectionTextureMode ();
}
public void RenderGlobalReflectionProbe(bool forced = false, bool timeslice = false)
{
EnviroReflectionProbe probe = EnviroManager.instance.Objects.globalReflectionProbe;
if (probe == null)
return;
if(renderReflectionCoroutine != null)
{
EnviroManager.instance.StopCoroutine(renderReflectionCoroutine);
renderReflectionCoroutine = null;
}
#if !ENVIRO_HDRP
renderReflectionCoroutine = EnviroManager.instance.StartCoroutine(RenderGlobalReflectionProbeTimed(probe,timeslice));
if(Settings.updateDefaultEnvironmentReflections)
{
#if UNITY_2022_1_OR_NEWER
// We don't need to copy the texture to a cubemap anmyore
#else
// Prevent multiple coroutines from running at the same time
if (copyDefaultReflectionCoroutine != null)
{
EnviroManager.instance.StopCoroutine(copyDefaultReflectionCoroutine);
copyDefaultReflectionCoroutine = null;
}
if(Settings.customRendering)
copyDefaultReflectionCoroutine = EnviroManager.instance.StartCoroutine(CopyDefaultReflectionCustom(probe, timeslice));
else
CopyDefaultReflectionUnity(probe);
#endif
}
#else
renderReflectionCoroutine = EnviroManager.instance.StartCoroutine(RenderGlobalReflectionProbeTimed(probe,timeslice));
#endif
}
//Copy reflection probe to cubemap and assign as default reflections.
private void CopyDefaultReflectionCubemap (EnviroReflectionProbe probe)
{
if(Settings.defaultSkyReflectionTex == null || Settings.defaultSkyReflectionTex.height != probe.myProbe.texture.height || Settings.defaultSkyReflectionTex.width != probe.myProbe.texture.width)
{
if(Settings.defaultSkyReflectionTex != null)
DestroyImmediate(Settings.defaultSkyReflectionTex);
Settings.defaultSkyReflectionTex = new Cubemap(probe.myProbe.resolution, probe.myProbe.hdr ? TextureFormat.RGBAHalf : TextureFormat.RGBA32, true);
Settings.defaultSkyReflectionTex.name = "Enviro Default Sky Reflection";
}
if(probe.myProbe.texture != null)
Graphics.CopyTexture(probe.myProbe.texture, Settings.defaultSkyReflectionTex as Texture);
}
public void UpdateDefaultReflectionTextureMode ()
{
if(Settings.updateDefaultEnvironmentReflections && Settings.globalReflections)
{
RenderSettings.defaultReflectionMode = UnityEngine.Rendering.DefaultReflectionMode.Custom;
#if UNITY_2022_1_OR_NEWER
RenderSettings.customReflectionTexture = EnviroManager.instance.Objects.globalReflectionProbe.myProbe.texture;
#else
if(Settings.defaultSkyReflectionTex != null)
RenderSettings.customReflection = Settings.defaultSkyReflectionTex;
#endif
}
else
{
RenderSettings.defaultReflectionMode = UnityEngine.Rendering.DefaultReflectionMode.Skybox;
}
}
//Update all probe settings.
private void SetupProbeSettings(EnviroReflectionProbe probe)
{
int res = 128;
switch (Settings.globalReflectionResolution)
{
case EnviroReflections.GlobalReflectionResolution.R16:
res = 16;
break;
case EnviroReflections.GlobalReflectionResolution.R32:
res = 32;
break;
case EnviroReflections.GlobalReflectionResolution.R64:
res = 64;
break;
case EnviroReflections.GlobalReflectionResolution.R128:
res = 128;
break;
case EnviroReflections.GlobalReflectionResolution.R256:
res = 256;
break;
case EnviroReflections.GlobalReflectionResolution.R512:
res = 512;
break;
case EnviroReflections.GlobalReflectionResolution.R1024:
res = 1024;
break;
case EnviroReflections.GlobalReflectionResolution.R2048:
res = 2048;
break;
}
#if !ENVIRO_HDRP
probe.myProbe.cullingMask = Settings.globalReflectionLayers;
probe.myProbe.intensity = Settings.globalReflectionsIntensity;
probe.myProbe.size = new Vector3 (Settings.globalReflectionsScale,Settings.globalReflectionsScale,Settings.globalReflectionsScale);
probe.myProbe.resolution = res;
#if ENVIRO_URP
probe.customRendering = false;
probe.myProbe.timeSlicingMode = Settings.globalReflectionTimeSlicingMode;
#else
probe.customRendering = Settings.customRendering;
probe.useTimeSlicing = Settings.customRenderingTimeSlicing;
probe.myProbe.timeSlicingMode = Settings.globalReflectionTimeSlicingMode;
#endif
RenderSettings.reflectionIntensity = Settings.globalReflectionsIntensity;
#else
probe.customRendering = false;
probe.myProbe.resolution = res;
if(probe.hdprobe != null)
{
probe.hdprobe.settingsRaw.cameraSettings.culling.cullingMask = Settings.globalReflectionLayers;
probe.hdprobe.settingsRaw.influence.boxSize = new Vector3 (Settings.globalReflectionsScale,Settings.globalReflectionsScale,Settings.globalReflectionsScale);
probe.hdprobe.settingsRaw.influence.sphereRadius = Settings.globalReflectionsScale;
probe.hdprobe.settingsRaw.lighting.multiplier = Settings.globalReflectionsIntensity;
}
#endif
}
private IEnumerator CopyDefaultReflectionCustom(EnviroReflectionProbe probe, bool timeslice)
{
if (timeslice)
{
// Wait for seven frames so probe finished rendering
for (int i = 0; i < 8; i++)
{
yield return null;
}
CopyDefaultReflectionCubemap(probe);
}
else
{
yield return null;
yield return null;
CopyDefaultReflectionCubemap(probe);
}
}
private void CopyDefaultReflectionUnity(EnviroReflectionProbe probe)
{
if(probe.renderId == -1 || probe.myProbe.IsFinishedRendering(probe.renderId))
{
CopyDefaultReflectionCubemap(probe);
}
else
{
if (waitForProbeCoroutine != null) {
EnviroManager.instance.StopCoroutine(waitForProbeCoroutine);
waitForProbeCoroutine = null;
}
waitForProbeCoroutine = EnviroManager.instance.StartCoroutine(WaitForUnityProbe(probe));
}
}
private IEnumerator WaitForUnityProbe(EnviroReflectionProbe probe)
{
yield return null;
CopyDefaultReflectionUnity(probe);
}
private IEnumerator RenderGlobalReflectionProbeTimed (EnviroReflectionProbe probe, bool timeslice)
{
#if ENVIRO_HDRP
EnviroManager.instance.Lighting.UpdateDirectLightingHDRP ();
EnviroManager.instance.Lighting.UpdateAmbientLightingHDRP();
yield return null;
probe.RefreshReflection(timeslice);
yield return null;
EnviroManager.instance.Lighting.UpdateExposureHDRP ();
#else
if(EnviroManager.instance.Lighting != null)
{
//Force a lighting update before rendering the probe as it might has not updated yet.
EnviroManager.instance.Lighting.UpdateDirectLighting ();
EnviroManager.instance.Lighting.UpdateAmbientLighting(true);
yield return null;
probe.RefreshReflection(timeslice);
}
else
{
probe.RefreshReflection(timeslice);
}
#endif
}
//Save and Load
public void LoadModuleValues ()
{
if(preset != null)
{
Settings = JsonUtility.FromJson<Enviro.EnviroReflections>(JsonUtility.ToJson(preset.Settings));
}
else
{
Debug.Log("Please assign a saved module to load from!");
}
}
public void SaveModuleValues ()
{
#if UNITY_EDITOR
EnviroReflectionsModule t = ScriptableObject.CreateInstance<EnviroReflectionsModule>();
t.name = "Reflections Module";
t.Settings = JsonUtility.FromJson<Enviro.EnviroReflections>(JsonUtility.ToJson(Settings));
string assetPathAndName = UnityEditor.AssetDatabase.GenerateUniqueAssetPath(EnviroHelper.assetPath + "/New " + t.name + ".asset");
UnityEditor.AssetDatabase.CreateAsset(t, assetPathAndName);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
#endif
}
public void SaveModuleValues (EnviroReflectionsModule module)
{
module.Settings = JsonUtility.FromJson<Enviro.EnviroReflections>(JsonUtility.ToJson(Settings));
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(module);
UnityEditor.AssetDatabase.SaveAssets();
#endif
}
}
}

View File

@@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 153c2e7862a6cc74c80438e6d8e45aab
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 236601
packageName: Enviro 3 - Sky and Weather
packageVersion: 3.1.2
assetPath: Assets/Enviro 3 - Sky and Weather/Scripts/Runtime/Modules/Reflections/EnviroReflectionsModule.cs
uploadId: 660896

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: c513eea06098c7547b1d35f1e90e9a2a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,38 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 153c2e7862a6cc74c80438e6d8e45aab, type: 3}
m_Name: Default Reflections Preset
m_EditorClassIdentifier:
showModuleInspector: 0
showSaveLoad: 0
active: 1
Settings:
globalReflections: 1
customRendering: 1
customRenderingTimeSlicing: 1
globalReflectionTimeSlicingMode: 0
globalReflectionsUpdateOnGameTime: 1
globalReflectionsUpdateOnPosition: 0
globalReflectionsIntensity: 1
globalReflectionsTimeTreshold: 0.025
globalReflectionsPositionTreshold: 0.5
globalReflectionsScale: 10000
globalReflectionResolution: 3
globalReflectionLayers:
serializedVersion: 2
m_Bits: 0
updateDefaultEnvironmentReflections: 1
defaultSkyReflectionTex: {fileID: 0}
preset: {fileID: 0}
showReflectionControls: 0
lastReflectionUpdate: 0
lastReflectionUpdatePos: {x: 0, y: 0, z: 0}

View File

@@ -0,0 +1,16 @@
fileFormatVersion: 2
guid: da668770e38694d49af97ecfd05fd7eb
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:
AssetOrigin:
serializedVersion: 1
productId: 236601
packageName: Enviro 3 - Sky and Weather
packageVersion: 3.1.2
assetPath: Assets/Enviro 3 - Sky and Weather/Scripts/Runtime/Modules/Reflections/Presets/Default
Reflections Preset.asset
uploadId: 660896