Initial commit

This commit is contained in:
gunnbr committed 2021-01-11 22:20:43 -08:00
1 parent ee9615239e
commit 16feac691f
4093 files changed
+1030491

No files matched your search

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 05af7bf41f38afe409054c460ba51593
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,21 @@
using UnityEditor;
namespace Gaia
{
[CustomEditor(typeof(GaiaPlanarReflections))]
public class GaiaPlanarReflectionsEditor : Editor
{
private const string m_helpText = "Gaia Planar Reflections is the reflection system for SRP this system only work in URP/HDRP. This system uses the SRP camera commands to render reflections in realtime. This system also supports shadows, custom render distances and unity layers.";
private GaiaPlanarReflections m_reflections;
public override void OnInspectorGUI()
{
if (m_reflections == null)
{
m_reflections = (GaiaPlanarReflections) target;
}
//Initialization
EditorGUILayout.HelpBox(m_helpText, MessageType.Info);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bb560212dad63024b9985a1d559addcd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,102 @@
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using PWCommon4;
using Gaia.Internal;
using Gaia;
namespace ProceduralWorlds.WaterSystem
{
/// <summary>
/// Editor for the PWS_WaterReflections
/// </summary>
[CustomEditor(typeof(PWS_WaterSystem))]
public class PWS_WaterReflectionsEditor : PWEditor
{
private EditorUtils m_editorUtils;
private PWS_WaterSystem WaterReflections;
private void OnEnable()
{
WaterReflections = (PWS_WaterSystem)target;
if (m_editorUtils == null)
{
// Get editor utils for this
m_editorUtils = PWApp.GetEditorUtils(this);
}
}
#region Inspector Region
/// <summary>
/// Custom editor for PWS_WaterReflections
/// </summary
public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
//Initialization
m_editorUtils.Initialize(); // Do not remove this!
if (WaterReflections == null)
{
WaterReflections = (PWS_WaterSystem)target;
}
m_editorUtils.Panel("GlobalSettings", GlobalSettings, true);
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(WaterReflections);
}
}
#endregion
#region Panel
/// <summary>
/// Global Main Panel
/// </summary>
/// <param name="helpEnabled"></param>
private void GlobalSettings(bool helpEnabled)
{
m_editorUtils.Heading("Setup");
EditorGUI.indentLevel++;
WaterReflections.SunLight = (Light)m_editorUtils.ObjectField("SunLight", WaterReflections.SunLight, typeof(Light), true, helpEnabled);
WaterReflections.m_player = (Transform)m_editorUtils.ObjectField("Player", WaterReflections.m_player, typeof(Transform), true, helpEnabled);
WaterReflections.SeaLevel = m_editorUtils.FloatField("SeaLevel", WaterReflections.SeaLevel, helpEnabled);
WaterReflections.InfiniteMode = m_editorUtils.Toggle("InfiniteMode", WaterReflections.InfiniteMode, helpEnabled);
EditorGUI.indentLevel--;
/*m_editorUtils.Heading("SurfaceSettings");
EditorGUI.indentLevel++;
WaterReflections.m_minSurfaceLight = m_editorUtils.FloatField("MinSurfaceLight", WaterReflections.m_minSurfaceLight, helpEnabled);
if (WaterReflections.m_minSurfaceLight > WaterReflections.m_maxSurfaceLight)
{
WaterReflections.m_minSurfaceLight = WaterReflections.m_maxSurfaceLight - 0.1f;
}
if (WaterReflections.m_minSurfaceLight < 0f)
{
WaterReflections.m_minSurfaceLight = 0f;
}
WaterReflections.m_maxSurfaceLight = m_editorUtils.FloatField("MaxSurfaceLight", WaterReflections.m_maxSurfaceLight, helpEnabled);
if (WaterReflections.m_maxSurfaceLight < 0.1f)
{
WaterReflections.m_maxSurfaceLight = 0.1f;
}
EditorGUI.indentLevel--;*/
if (m_editorUtils.Button("EditReflectionSettings"))
{
GaiaUtils.FocusWaterProfile();
}
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8b83c234d2445d946be3b2aaa34e0ae2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,85 @@
using System.Collections;
using System.Collections.Generic;
using Gaia.Internal;
using PWCommon4;
using UnityEditor;
using UnityEngine;
namespace Gaia
{
[CustomEditor(typeof(ReflectionMasker))]
public class ReflectionMaskerEditor : PWEditor
{
private EditorUtils m_editorUtils;
private ReflectionMasker m_profile;
private Color GUIBackground;
private void OnEnable()
{
if (m_editorUtils == null)
{
// Get editor utils for this
m_editorUtils = PWApp.GetEditorUtils(this);
}
}
/// <summary>
/// Setup on destroy
/// </summary>
private void OnDestroy()
{
if (m_editorUtils != null)
{
m_editorUtils.Dispose();
}
}
public override void OnInspectorGUI()
{
//Initialization
m_editorUtils.Initialize(); // Do not remove this!
if (m_profile == null)
{
m_profile = (ReflectionMasker) target;
}
m_editorUtils.Panel("GlobalSettings", GlobalSettings, true);
}
private void GlobalSettings(bool helpEnabled)
{
EditorGUI.BeginChangeCheck();
GUIBackground = GUI.backgroundColor;
m_profile.Player = (Transform)m_editorUtils.ObjectField("Player", m_profile.Player, typeof(Transform), true, helpEnabled, GUILayout.MaxHeight(16f));
if (m_profile.Player == null)
{
m_profile.Player = m_profile.GetPlayer();
}
if (m_profile.ReflectionMaskerData.m_maskingTexture == null)
{
EditorGUILayout.HelpBox("The mask texture is null. This system needs a mask texture to function please provide one.", MessageType.Error);
GUI.backgroundColor = Color.red;
}
else
{
if (!m_profile.ReflectionMaskerData.m_maskingTexture.isReadable)
{
EditorGUILayout.HelpBox("The mask texture " + m_profile.ReflectionMaskerData.m_maskingTexture.name + " is not Read/Write enabled please go to the texture import settings and enable this setting then click apply.", MessageType.Error);
GUI.backgroundColor = Color.red;
}
}
m_profile.ReflectionMaskerData.m_maskingTexture = (Texture2D)m_editorUtils.ObjectField("MaskTexture", m_profile.ReflectionMaskerData.m_maskingTexture, typeof(Texture2D), false, helpEnabled, GUILayout.MaxHeight(16f));
GUI.backgroundColor = GUIBackground;
EditorGUI.indentLevel++;
m_profile.ReflectionMaskerData.m_channelSelection = (ReflectionMaskerChannelSelection)m_editorUtils.EnumPopup("ChannelSelection", m_profile.ReflectionMaskerData.m_channelSelection, helpEnabled);
m_profile.ReflectionMaskerData.m_minValue = m_editorUtils.Slider("MinValue",m_profile.ReflectionMaskerData.m_minValue, 0f, m_profile.ReflectionMaskerData.m_maxValue - 0.01f, helpEnabled);
m_profile.ReflectionMaskerData.m_maxValue = m_editorUtils.Slider("MaxValue",m_profile.ReflectionMaskerData.m_maxValue, m_profile.ReflectionMaskerData.m_minValue + 0.01f, 1f, helpEnabled);
EditorGUI.indentLevel--;
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(m_profile);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 77b936a1f8700f5418980af13502f681
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,424 @@
using System;
using ProceduralWorlds.WaterSystem;
using UnityEngine;
using UnityEngine.Rendering;
#if UPPipeline
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering.Universal;
#endif
namespace Gaia
{
[ExecuteAlways]
public class GaiaPlanarReflections : MonoBehaviour
{
[Serializable]
public class PlanarReflectionSettings
{
public bool m_disableSkyboxReflections = false;
public bool m_enableReflections = true;
public GaiaConstants.ResolutionMulltiplier m_ResolutionMultiplier = GaiaConstants.ResolutionMulltiplier.Half;
public float m_ClipPlaneOffset = 0f;
public LayerMask m_ReflectLayers = -1;
public bool m_Shadows = false;
public bool m_enableRenderDistance = false;
public bool m_enableRenderDistances = false;
public float m_customRenderDistance = 500f;
public float[] m_customRenderDistances = new float[32];
public int m_textureResolution = 512;
}
public class PlanarReflectionSettingData
{
private bool _fog;
private int _maxLod;
private float _lodBias;
public void Get()
{
_fog = RenderSettings.fog;
_maxLod = QualitySettings.maximumLODLevel;
_lodBias = QualitySettings.lodBias;
}
public void Set()
{
GL.invertCulling = true;
RenderSettings.fog = true; // disable fog for now as it's incorrect with projection
QualitySettings.maximumLODLevel = 1;
QualitySettings.lodBias = _lodBias * 0.5f;
}
public void Restore()
{
GL.invertCulling = false;
RenderSettings.fog = _fog;
QualitySettings.maximumLODLevel = _maxLod;
QualitySettings.lodBias = _lodBias;
}
}
public PlanarReflectionSettings m_settings = new PlanarReflectionSettings();
public PlanarReflectionSettingData m_reflectionData = new PlanarReflectionSettingData();
public static event Action<ScriptableRenderContext, Camera> BeginPlanarReflections;
public Camera m_gameCamera;
[SerializeField]
private static Camera m_reflectionCamera;
[SerializeField]
private RenderTexture m_reflectionTexture = null;
[SerializeField]
private float[] m_distances = new float[32];
#region Unity Functions
/// <summary>
/// Load on enable
/// </summary>
private void OnEnable()
{
StartReflectionSystem();
}
/// <summary>
/// Execute on disable
/// </summary>
private void OnDisable()
{
Cleanup();
}
/// <summary>
/// Execute when the object is destroyed
/// </summary>
private void OnDestroy()
{
Cleanup();
}
#endregion
#region Planar Reflection Functions
/// <summary>
/// Calculates the matrix of the reflection plane view
/// </summary>
/// <param name="reflectionMat"></param>
/// <param name="plane"></param>
private static void CalculateReflectionMatrix(ref Matrix4x4 reflectionMat, Vector4 plane)
{
reflectionMat.m00 = (1f - 2f * plane[0] * plane[0]);
reflectionMat.m01 = (-2f * plane[0] * plane[1]);
reflectionMat.m02 = (-2f * plane[0] * plane[2]);
reflectionMat.m03 = (-2f * plane[3] * plane[0]);
reflectionMat.m10 = (-2f * plane[1] * plane[0]);
reflectionMat.m11 = (1f - 2f * plane[1] * plane[1]);
reflectionMat.m12 = (-2f * plane[1] * plane[2]);
reflectionMat.m13 = (-2f * plane[3] * plane[1]);
reflectionMat.m20 = (-2f * plane[2] * plane[0]);
reflectionMat.m21 = (-2f * plane[2] * plane[1]);
reflectionMat.m22 = (1f - 2f * plane[2] * plane[2]);
reflectionMat.m23 = (-2f * plane[3] * plane[2]);
reflectionMat.m30 = 0f;
reflectionMat.m31 = 0f;
reflectionMat.m32 = 0f;
reflectionMat.m33 = 1f;
}
/// <summary>
/// Updates position
/// </summary>
/// <param name="pos"></param>
/// <returns></returns>
private static Vector3 ReflectPosition(Vector3 pos)
{
var newPos = new Vector3(pos.x, -pos.y, pos.z);
return newPos;
}
/// <summary>
/// Updates the camera planes based on it's current view
/// </summary>
/// <param name="cam"></param>
/// <param name="pos"></param>
/// <param name="normal"></param>
/// <param name="sideSign"></param>
/// <returns></returns>
private Vector4 CameraSpacePlane(Camera cam, Vector3 pos, Vector3 normal, float sideSign)
{
var offsetPos = pos + normal * m_settings.m_ClipPlaneOffset;
var m = cam.worldToCameraMatrix;
var cameraPosition = m.MultiplyPoint(offsetPos);
var cameraNormal = m.MultiplyVector(normal).normalized * sideSign;
return new Vector4(cameraNormal.x, cameraNormal.y, cameraNormal.z, -Vector3.Dot(cameraPosition, cameraNormal));
}
/// <summary>
/// Builds the reflection texture if null or new resolution has been set
/// </summary>
private void PlanarReflectionTexture()
{
if (m_reflectionTexture == null)
{
#if UPPipeline
var res = ReflectionResolution(m_settings.m_textureResolution, UniversalRenderPipeline.asset.renderScale);
const RenderTextureFormat hdrFormat = RenderTextureFormat.DefaultHDR;
m_reflectionTexture = RenderTexture.GetTemporary(res.x, res.y, 24, GraphicsFormatUtility.GetGraphicsFormat(hdrFormat, true));
#endif
}
else
{
if (m_reflectionTexture.width != m_settings.m_textureResolution)
{
#if UPPipeline
//
var res = ReflectionResolution(m_settings.m_textureResolution, UniversalRenderPipeline.asset.renderScale);
const RenderTextureFormat hdrFormat = RenderTextureFormat.DefaultHDR;
m_reflectionTexture = RenderTexture.GetTemporary(res.x, res.y, 24, GraphicsFormatUtility.GetGraphicsFormat(hdrFormat, true));
#endif
}
}
m_reflectionCamera.targetTexture = m_reflectionTexture;
}
/// <summary>
/// Gets the reflection resolution
/// </summary>
/// <param name="textureSize"></param>
/// <param name="scale"></param>
/// <returns></returns>
private Vector2Int ReflectionResolution(int textureSize, float scale)
{
var x = (int)(textureSize * scale * GetScaleValue());
var y = (int)(textureSize * scale * GetScaleValue());
return new Vector2Int(x, y);
}
/// <summary>
/// Executes the planar reflections functions
/// </summary>
/// <param name="context"></param>
/// <param name="cam"></param>
public void ExecutePlanarReflections(ScriptableRenderContext context, Camera cam)
{
if (m_settings.m_disableSkyboxReflections)
{
return;
}
if (cam.cameraType == CameraType.Reflection || cam.cameraType == CameraType.Preview)
{
return;
}
UpdateReflectionCamera(cam);
PlanarReflectionTexture();
if (m_reflectionData == null)
{
m_reflectionData = new PlanarReflectionSettingData();
}
m_reflectionData.Get();
m_reflectionData.Set();
BeginPlanarReflections?.Invoke(context, m_reflectionCamera);
#if UPPipeline
UniversalRenderPipeline.RenderSingleCamera(context, m_reflectionCamera);
#endif
PWS_WaterSystem.Instance.SetReflectionTexture(m_reflectionTexture);
m_reflectionData.Restore();
}
#endregion
#region Utils
/// <summary>
/// Cleans up planar reflections
/// </summary>
private void Cleanup()
{
RenderPipelineManager.beginCameraRendering -= ExecutePlanarReflections;
if (m_reflectionCamera != null)
{
m_reflectionCamera.targetTexture = null;
SafeDestroy(m_reflectionCamera.gameObject);
}
if (m_reflectionTexture != null)
{
RenderTexture.ReleaseTemporary(m_reflectionTexture);
}
}
private void StartReflectionSystem()
{
RenderPipelineManager.beginCameraRendering += ExecutePlanarReflections;
m_gameCamera = GaiaUtils.GetCamera();
}
public void SetReflectionState(bool state)
{
m_settings.m_enableReflections = state;
}
/// <summary>
/// Creates the reflection camera
/// </summary>
/// <returns></returns>
private Camera CreateMirrorObjects()
{
var go = new GameObject("Gaia Planar Reflections Camera",typeof(Camera));
#if UPPipeline
var cameraData = go.AddComponent(typeof(UniversalAdditionalCameraData)) as UniversalAdditionalCameraData;
cameraData.requiresColorOption = CameraOverrideOption.Off;
cameraData.requiresDepthOption = CameraOverrideOption.Off;
cameraData.SetRenderer(0);
#endif
var t = transform;
var reflectionCamera = go.GetComponent<Camera>();
reflectionCamera.transform.SetPositionAndRotation(t.position, t.rotation);
reflectionCamera.depth = -10;
reflectionCamera.enabled = false;
go.hideFlags = HideFlags.HideAndDontSave;
return reflectionCamera;
}
/// <summary>
/// Destory the object
/// </summary>
/// <param name="obj"></param>
private static void SafeDestroy(GameObject obj)
{
if (Application.isEditor)
{
DestroyImmediate(obj);
}
else
{
Destroy(obj);
}
}
/// <summary>
/// Updates the reflection camera settings
/// </summary>
/// <param name="src"></param>
/// <param name="dest"></param>
private void UpdateCamera(Camera src, Camera dest)
{
if (dest == null) return;
dest.CopyFrom(src);
dest.useOcclusionCulling = true;
SetCameraBackgroundType(dest, CameraClearFlags.SolidColor);
#if UPPipeline
if (dest.gameObject.TryGetComponent(out UniversalAdditionalCameraData camData))
{
camData.SetRenderer(0);
camData.renderShadows = m_settings.m_Shadows; // turn off shadows for the reflection camera
}
#endif
}
/// <summary>
///
/// </summary>
/// <param name="camera"></param>
private void SetCameraBackgroundType(Camera camera, CameraClearFlags backgroundRenderMode)
{
if (camera == null)
{
return;
}
camera.clearFlags = backgroundRenderMode;
camera.backgroundColor = RenderSettings.fogColor;
}
/// <summary>
/// Updates the reflection camera for rendering
/// </summary>
/// <param name="realCamera"></param>
private void UpdateReflectionCamera(Camera realCamera)
{
if (m_reflectionCamera == null)
{
m_reflectionCamera = CreateMirrorObjects();
}
Vector3 pos = Vector3.zero;
Vector3 normal = Vector3.up;
UpdateCamera(realCamera, m_reflectionCamera);
var d = Vector3.Dot(normal, pos) - m_settings.m_ClipPlaneOffset;
var reflectionPlane = new Vector4(normal.x, normal.y, normal.z, d);
var reflection = Matrix4x4.identity;
reflection = Matrix4x4.Scale(new Vector3(1, 1, 1));
CalculateReflectionMatrix(ref reflection, reflectionPlane);
var oldPosition = realCamera.transform.position - new Vector3(0, pos.y, 0);
var newPosition = ReflectPosition(oldPosition);
m_reflectionCamera.transform.forward = Vector3.Scale(realCamera.transform.forward, new Vector3(1, 1, 1));
m_reflectionCamera.worldToCameraMatrix = realCamera.worldToCameraMatrix * reflection;
var clipPlane = CameraSpacePlane(m_reflectionCamera, pos - Vector3.up * 0.1f, normal, 1.0f);
var projection = realCamera.CalculateObliqueMatrix(clipPlane);
m_reflectionCamera.projectionMatrix = projection;
if (m_settings.m_enableReflections)
{
m_reflectionCamera.cullingMask = ~(1 << 4) & m_settings.m_ReflectLayers;
}
else
{
m_reflectionCamera.cullingMask = 0;
}
if (m_distances.Length != 32)
{
m_distances = new float[32];
}
if (m_settings.m_enableRenderDistance)
{
if (m_settings.m_enableRenderDistances)
{
m_reflectionCamera.layerCullDistances = m_settings.m_customRenderDistances;
m_reflectionCamera.layerCullSpherical = true;
}
else
{
for (int idx = 0; idx < m_distances.Length; idx++)
{
m_distances[idx] = m_settings.m_customRenderDistance;
}
m_reflectionCamera.layerCullDistances = m_distances;
m_reflectionCamera.layerCullSpherical = true;
}
}
else
{
for (int idx = 0; idx < m_distances.Length; idx++)
{
m_distances[idx] = 0f;
}
m_reflectionCamera.layerCullDistances = m_distances;
m_reflectionCamera.layerCullSpherical = true;
}
m_reflectionCamera.transform.position = newPosition;
}
/// <summary>
/// Gets the multiplier scale
/// </summary>
/// <returns></returns>
private float GetScaleValue()
{
switch(m_settings.m_ResolutionMultiplier)
{
case GaiaConstants.ResolutionMulltiplier.Full:
return 1f;
case GaiaConstants.ResolutionMulltiplier.Half:
return 0.5f;
case GaiaConstants.ResolutionMulltiplier.Third:
return 0.33f;
case GaiaConstants.ResolutionMulltiplier.Quarter:
return 0.25f;
default:
return 0.5f;
}
}
#endregion
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5362db1f5573731479e71ccee9de582b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 121c7ad962667ca409c1cefbc44c67f9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,288 @@
using System;
using UnityEngine;
namespace Gaia
{
public enum ReflectionMaskerChannelSelection { R, G, B, A, RGBA }
[Serializable]
public class ReflectionMaskerData
{
public Texture2D m_maskingTexture;
public ReflectionMaskerChannelSelection m_channelSelection = ReflectionMaskerChannelSelection.R;
public float m_minValue = 0.15f;
public float m_maxValue = 1f;
public bool m_enableReflections;
public bool m_allowMSAA;
public bool m_enableHDR;
public bool m_enableHeightFeatures;
public float m_disableHeightValue;
public bool m_enableCustomDistance;
public bool m_enableMultiLayerDistances;
public float m_reflectionDistance;
public float[] m_reflectionDistances;
public GaiaPlanarReflections m_planarReflections;
public void Process(SceneProfile sceneProfile, bool state)
{
Get(sceneProfile);
SetState(sceneProfile, state);
}
public void Get(SceneProfile profile)
{
if (profile == null)
{
return;
}
m_enableReflections = profile.m_enableReflections;
m_allowMSAA = profile.m_allowMSAA;
m_enableHDR = profile.m_useHDR;
m_enableHeightFeatures = profile.m_enableDisabeHeightFeature;
m_disableHeightValue = profile.m_disableHeight;
m_enableCustomDistance = profile.m_useCustomRenderDistance;
m_enableMultiLayerDistances = profile.m_enableLayerDistances;
m_reflectionDistance = profile.m_customRenderDistance;
m_reflectionDistances = profile.m_customRenderDistances;
}
public void Set(SceneProfile profile)
{
if (profile == null)
{
return;
}
profile.m_enableReflections = m_enableReflections;
profile.m_allowMSAA = m_allowMSAA;
profile.m_useHDR = m_enableHDR;
profile.m_enableDisabeHeightFeature = m_enableHeightFeatures;
profile.m_disableHeight = m_disableHeightValue;
profile.m_useCustomRenderDistance = m_enableCustomDistance;
profile.m_enableLayerDistances = m_enableMultiLayerDistances;
profile.m_customRenderDistance = m_reflectionDistance;
profile.m_customRenderDistances = m_reflectionDistances;
}
public void SetState(SceneProfile profile, bool state)
{
if (profile == null)
{
return;
}
profile.m_enableReflections = state;
profile.m_enableDisabeHeightFeature = state;
#if UPPipeline
if (m_planarReflections != null)
{
m_planarReflections.SetReflectionState(state);
}
else
{
m_planarReflections = GameObject.FindObjectOfType<GaiaPlanarReflections>();
if (m_planarReflections != null)
{
m_planarReflections.SetReflectionState(state);
}
}
#endif
}
}
public class ReflectionMasker : MonoBehaviour
{
public Transform Player;
public ReflectionMaskerData ReflectionMaskerData
{
get { return m_reflectionMaskerData; }
set
{
if (m_reflectionMaskerData != value)
{
m_reflectionMaskerData = value;
if (Application.isPlaying)
{
if (GaiaUtils.CheckIfSceneProfileExists())
{
Execute();
}
}
}
}
}
[SerializeField]
private ReflectionMaskerData m_reflectionMaskerData = new ReflectionMaskerData();
private bool m_needUpdating = false;
[SerializeField]
private Vector3 m_oldPosition;
private void Start()
{
Initilize();
Execute();
}
private void Update()
{
if (DoesNeedExecute())
{
Execute();
}
}
public void Execute()
{
if (Player != null)
{
bool checkValue = CheckPoint(ReflectionMaskerData.m_maskingTexture, Player.position, ReflectionMaskerData.m_minValue, ReflectionMaskerData.m_maxValue);
if (DoesNeedUpdating(checkValue))
{
m_needUpdating = checkValue;
if (GaiaUtils.CheckIfSceneProfileExists())
{
ReflectionMaskerData.Process(GaiaGlobal.Instance.SceneProfile, checkValue);
}
}
}
}
private bool CheckPoint(Texture2D maskTexture, Vector3 position, float min, float max)
{
try
{
if (maskTexture == null)
{
return false;
}
if (!maskTexture.isReadable)
{
Debug.LogError("Texture is not marked as readable. Please set is readable on the texture import settings on texture " + maskTexture.name);
}
Terrain terrain = TerrainHelper.GetTerrain(position);
if (terrain != null)
{
float scalerX = (float)(position.x - terrain.transform.position.x) / (float)terrain.terrainData.size.x;
float scalerZ = (float)(position.z - terrain.transform.position.z) / (float)terrain.terrainData.size.z;
Color value = Color.white;
value = maskTexture.GetPixel(Mathf.RoundToInt(scalerX * maskTexture.width), Mathf.RoundToInt(scalerZ * maskTexture.height));
switch (ReflectionMaskerData.m_channelSelection)
{
case ReflectionMaskerChannelSelection.R:
if (value.r >= min && value.r <= max)
{
return false;
}
break;
case ReflectionMaskerChannelSelection.G:
if (value.g >= min && value.g <= max)
{
return false;
}
break;
case ReflectionMaskerChannelSelection.B:
if (value.b >= min && value.b <= max)
{
return false;
}
break;
case ReflectionMaskerChannelSelection.A:
if (value.a >= min && value.a <= max)
{
return false;
}
break;
case ReflectionMaskerChannelSelection.RGBA:
if (value.r >= min && value.r <= max)
{
return false;
}
if (value.g >= min && value.g <= max)
{
return false;
}
if (value.b >= min && value.b <= max)
{
return false;
}
if (value.a >= min && value.a <= max)
{
return false;
}
break;
}
}
return true;
}
catch (Exception e)
{
Debug.LogError("Issues happened when trying to check the point " + e.Message + " Happened here " + e.StackTrace);
return true;
}
}
private bool DoesNeedUpdating(bool newValue)
{
if (m_needUpdating != newValue)
{
return true;
}
else
{
return false;
}
}
private bool DoesNeedExecute()
{
if (Player != null)
{
if (Player.position != m_oldPosition)
{
m_oldPosition = Player.position;
return true;
}
else
{
return false;
}
}
return false;
}
private void Initilize()
{
if (GaiaUtils.CheckIfSceneProfileExists())
{
m_needUpdating = GaiaGlobal.Instance.SceneProfile.m_enableReflections;
}
if (Player == null)
{
Player = GetPlayer();
}
}
public Transform GetPlayer()
{
Transform transform = null;
if (GaiaUtils.CheckIfSceneProfileExists())
{
if (GaiaGlobal.Instance.m_mainCamera != null)
{
transform = GaiaGlobal.Instance.m_mainCamera.transform;
}
}
if (transform == null)
{
transform = GaiaUtils.GetPlayerTransform();
}
return transform;
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 01d1cbcea7cb01e458a6b08e7c1bf04e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: