214 lines
5.3 KiB
C#
214 lines
5.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
|
|
namespace Gaia
|
|
{
|
|
[RequireComponent(typeof(ReflectionProbe))]
|
|
public class GaiaReflectionProbeUpdate : MonoBehaviour
|
|
{
|
|
public enum ReflectionQuality
|
|
{
|
|
VeryLow = 0,
|
|
Low = 1,
|
|
Medium = 2,
|
|
High = 3,
|
|
VeryHigh = 4,
|
|
Ultra = 5
|
|
}
|
|
|
|
public enum RenderDistanceQuality
|
|
{
|
|
Close = 0,
|
|
Near = 1,
|
|
Far = 2,
|
|
VeryFar = 3,
|
|
ExtremelyFar = 4
|
|
}
|
|
|
|
[Header("Probe Configuration [Applied OnStart]")]
|
|
[Tooltip("Sets the probe reflection quality")]
|
|
public ReflectionQuality m_probeResolution = ReflectionQuality.Medium;
|
|
|
|
[Tooltip("Sets the probe render distance")]
|
|
public RenderDistanceQuality m_probeRenderDistance = RenderDistanceQuality.Near;
|
|
|
|
[Tooltip("If on the reflection probe will follow the camera position")]
|
|
public bool m_followCamera;
|
|
|
|
[Tooltip("Offset above the camera that probe will be adjusted by when following camera. Zero gives more accurate reflections, but captures surrounding trees and other objects.")]
|
|
public float m_followHeightOffset = 30f;
|
|
|
|
[Tooltip("Sets box projection")]
|
|
public bool m_boxProjection;
|
|
|
|
[Tooltip("Sets hdr on the reflection probe")]
|
|
public bool m_useHDR = true;
|
|
|
|
private int m_renderID;
|
|
|
|
private ReflectionProbe m_reflectionProbe;
|
|
|
|
private bool m_cameraIsMoving;
|
|
|
|
private Vector3 m_lastLocation;
|
|
|
|
public GameObject m_mainCameraObject;
|
|
|
|
private void Start()
|
|
{
|
|
m_mainCameraObject = GetOrCreateMainCamera();
|
|
m_reflectionProbe = base.gameObject.GetComponent<ReflectionProbe>();
|
|
SetProbeSettings();
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
if (m_mainCameraObject != null)
|
|
{
|
|
if (m_lastLocation != m_mainCameraObject.transform.position)
|
|
{
|
|
m_lastLocation = m_mainCameraObject.transform.position;
|
|
m_cameraIsMoving = true;
|
|
}
|
|
else
|
|
{
|
|
m_cameraIsMoving = false;
|
|
}
|
|
}
|
|
if (!m_cameraIsMoving)
|
|
{
|
|
if (m_reflectionProbe.mode == ReflectionProbeMode.Realtime)
|
|
{
|
|
m_reflectionProbe.refreshMode = ReflectionProbeRefreshMode.ViaScripting;
|
|
}
|
|
return;
|
|
}
|
|
if (m_followCamera)
|
|
{
|
|
ProbeFollow();
|
|
}
|
|
if (m_reflectionProbe.mode == ReflectionProbeMode.Realtime)
|
|
{
|
|
m_reflectionProbe.refreshMode = ReflectionProbeRefreshMode.EveryFrame;
|
|
}
|
|
}
|
|
|
|
public void SetProbeSettings()
|
|
{
|
|
ReflectionProbe component = base.gameObject.GetComponent<ReflectionProbe>();
|
|
component.timeSlicingMode = ReflectionProbeTimeSlicingMode.AllFacesAtOnce;
|
|
component.boxProjection = m_boxProjection;
|
|
component.hdr = m_useHDR;
|
|
component.intensity = 1f;
|
|
component.importance = 1;
|
|
component.clearFlags = ReflectionProbeClearFlags.Skybox;
|
|
component.center = new Vector3(0f, 0f, 0f);
|
|
if ((bool)GameObject.Find("Ambient Water Sample"))
|
|
{
|
|
component.size = new Vector3(125f, 10000f, 125f);
|
|
component.mode = ReflectionProbeMode.Realtime;
|
|
component.refreshMode = ReflectionProbeRefreshMode.ViaScripting;
|
|
}
|
|
switch (m_probeResolution)
|
|
{
|
|
case ReflectionQuality.VeryLow:
|
|
component.resolution = 32;
|
|
component.shadowDistance = 5f;
|
|
break;
|
|
case ReflectionQuality.Low:
|
|
component.resolution = 64;
|
|
component.shadowDistance = 10f;
|
|
break;
|
|
case ReflectionQuality.Medium:
|
|
component.resolution = 128;
|
|
component.shadowDistance = 25f;
|
|
break;
|
|
case ReflectionQuality.High:
|
|
component.resolution = 256;
|
|
component.shadowDistance = 75f;
|
|
break;
|
|
case ReflectionQuality.VeryHigh:
|
|
component.resolution = 512;
|
|
component.shadowDistance = 100f;
|
|
break;
|
|
case ReflectionQuality.Ultra:
|
|
component.resolution = 1024;
|
|
component.shadowDistance = 150f;
|
|
break;
|
|
}
|
|
switch (m_probeRenderDistance)
|
|
{
|
|
case RenderDistanceQuality.Close:
|
|
component.farClipPlane = 250f;
|
|
break;
|
|
case RenderDistanceQuality.Near:
|
|
component.farClipPlane = 500f;
|
|
break;
|
|
case RenderDistanceQuality.Far:
|
|
component.farClipPlane = 1000f;
|
|
break;
|
|
case RenderDistanceQuality.VeryFar:
|
|
component.farClipPlane = 2000f;
|
|
break;
|
|
case RenderDistanceQuality.ExtremelyFar:
|
|
component.farClipPlane = 4000f;
|
|
break;
|
|
}
|
|
if (component.IsFinishedRendering(m_renderID))
|
|
{
|
|
m_renderID = component.RenderProbe();
|
|
}
|
|
}
|
|
|
|
public void ProbeFollow()
|
|
{
|
|
if (m_mainCameraObject != null)
|
|
{
|
|
Vector3 position = m_mainCameraObject.transform.position;
|
|
position.y += m_followHeightOffset;
|
|
m_reflectionProbe.gameObject.transform.localPosition = position;
|
|
}
|
|
}
|
|
|
|
private static GameObject GetOrCreateMainCamera()
|
|
{
|
|
GameObject gameObject = GameObject.Find("Main Camera");
|
|
if (gameObject != null)
|
|
{
|
|
return gameObject;
|
|
}
|
|
gameObject = GameObject.Find("Camera");
|
|
if (gameObject != null)
|
|
{
|
|
return gameObject;
|
|
}
|
|
gameObject = GameObject.Find("FirstPersonCharacter");
|
|
if (gameObject != null)
|
|
{
|
|
return gameObject;
|
|
}
|
|
gameObject = GameObject.Find("FlyCam");
|
|
if (gameObject != null)
|
|
{
|
|
return gameObject;
|
|
}
|
|
if (Camera.main != null)
|
|
{
|
|
return Camera.main.gameObject;
|
|
}
|
|
Camera[] array = Object.FindObjectsOfType<Camera>();
|
|
int num = 0;
|
|
if (num < array.Length)
|
|
{
|
|
return array[num].gameObject;
|
|
}
|
|
gameObject = new GameObject("Main Camera");
|
|
gameObject.AddComponent<Camera>();
|
|
gameObject.AddComponent<FlareLayer>();
|
|
gameObject.AddComponent<AudioListener>();
|
|
gameObject.tag = "MainCamera";
|
|
return gameObject;
|
|
}
|
|
}
|
|
}
|