First Commit
This commit is contained in:
221
Assets/Scripts/RenderPasses/GhostObjectBehaviour.cs
Normal file
221
Assets/Scripts/RenderPasses/GhostObjectBehaviour.cs
Normal file
@@ -0,0 +1,221 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
namespace Golems
|
||||
{
|
||||
/// <summary>
|
||||
/// Ghost Object Behaviour sends all attached renderers via the AlbertGhostMemory effect
|
||||
/// NOTE:
|
||||
/// This is ExecuteInEditMode so that in the effect is visible in Editor at edit time. This
|
||||
/// helps Timeline authoring and similar as the alpha changes will be visible.
|
||||
/// </summary>
|
||||
[ExecuteInEditMode]
|
||||
public class GhostObjectBehaviour : MonoBehaviour
|
||||
{
|
||||
private static SRDebuggerGhostAreRealSetting m_SRDebuggerGhostAreRealSettingInstance = null;
|
||||
|
||||
/// <summary>
|
||||
/// Global Alpha applied to all the renders attached
|
||||
/// to this behaviour. This is applied POST drawing
|
||||
/// everything so no clipping through meshes
|
||||
/// N.B Variable is serialized to allow setting it, but requires public controller to be set via animations
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
[Range(0f,1f)]
|
||||
private float m_Alpha = 1.0f;
|
||||
public float Alpha => m_Alpha;
|
||||
|
||||
/// <summary>
|
||||
/// Flag to ignore the Global Alpha driven via the
|
||||
/// Alert Ghost Memory effect Settings. Will use m_Alpha if true
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
private bool m_IgnoreGlobalAlpha = false;
|
||||
public bool IgnoreGlobalAlpha => m_IgnoreGlobalAlpha;
|
||||
|
||||
/// <summary>
|
||||
/// Really this should now just be m_Renderers..
|
||||
/// See docs for GhostObject
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
private GhostObject m_GhostObject = new GhostObject();
|
||||
public GhostObject GhostObject => m_GhostObject;
|
||||
|
||||
[SerializeField]
|
||||
private List<Material> m_ReplacementMemoryMaterials;
|
||||
public List<Material> ReplacementMemoryMaterials => m_ReplacementMemoryMaterials;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
|
||||
GhostObjectCollection.Instance.AddGhostObjectBehaviour(this);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
|
||||
if (m_SRDebuggerGhostAreRealSettingInstance == null)
|
||||
{
|
||||
m_SRDebuggerGhostAreRealSettingInstance = new SRDebuggerGhostAreRealSetting();
|
||||
}
|
||||
|
||||
if (m_SRDebuggerGhostAreRealSettingInstance != null)
|
||||
{
|
||||
m_SRDebuggerGhostAreRealSettingInstance.MakeGhostsGhosty += MakeGhostGhosty;
|
||||
m_SRDebuggerGhostAreRealSettingInstance.MakeGhostsReal += MakeGhostReal;
|
||||
|
||||
if (m_SRDebuggerGhostAreRealSettingInstance.GetGhostsGhosty())
|
||||
{
|
||||
MakeGhostGhosty();
|
||||
}
|
||||
else
|
||||
{
|
||||
MakeGhostReal();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (m_SRDebuggerGhostAreRealSettingInstance != null)
|
||||
{
|
||||
m_SRDebuggerGhostAreRealSettingInstance.MakeGhostsGhosty -= MakeGhostGhosty;
|
||||
m_SRDebuggerGhostAreRealSettingInstance.MakeGhostsReal -= MakeGhostReal;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
|
||||
GhostObjectCollection.Instance.RemoveGhostObjectBehaviour(this);
|
||||
}
|
||||
|
||||
private void MakeGhostGhosty()
|
||||
{
|
||||
if (m_GhostObject != null)
|
||||
{
|
||||
foreach (var renderer in m_GhostObject.Renderers)
|
||||
{
|
||||
renderer.gameObject.layer = LayerMask.NameToLayer("Default");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void MakeGhostReal()
|
||||
{
|
||||
if (m_GhostObject != null)
|
||||
{
|
||||
foreach (var renderer in m_GhostObject.Renderers)
|
||||
{
|
||||
if (renderer != null)
|
||||
{
|
||||
renderer.gameObject.layer = LayerMask.NameToLayer("Ghost");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Set the local Alpha of the
|
||||
/// Ghost Object Behaviour to the passed value
|
||||
/// </summary>
|
||||
/// <param name="alpha"></param>
|
||||
public void SetAlpha(float alpha)
|
||||
{
|
||||
if (alpha <= 1 && alpha >= 0 )
|
||||
{
|
||||
m_Alpha = alpha;
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void GrabAllRenderers()
|
||||
{
|
||||
var renderList = new List<Renderer>();
|
||||
foreach(var rend in GetComponentsInChildren<Renderer>())
|
||||
{
|
||||
if (rend as ParticleSystemRenderer)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (rend as TrailRenderer)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (rend as LineRenderer)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
renderList.Add(rend);
|
||||
}
|
||||
|
||||
if (renderList.Count > 0)
|
||||
{
|
||||
m_GhostObject.SetRenderers(renderList.ToArray());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
class SRDebuggerGhostAreRealSetting
|
||||
{
|
||||
public UnityAction MakeGhostsReal;
|
||||
public UnityAction MakeGhostsGhosty;
|
||||
private bool m_IsGhosty = false;
|
||||
|
||||
|
||||
public bool GetGhostsGhosty()
|
||||
{
|
||||
return m_IsGhosty;
|
||||
}
|
||||
|
||||
public void SetGhostsGhosty(bool shouldBeGhosty)
|
||||
{
|
||||
if (shouldBeGhosty != m_IsGhosty)
|
||||
{
|
||||
if (shouldBeGhosty)
|
||||
{
|
||||
Debug.Log("make em Ghosty");
|
||||
MakeGhostsGhosty?.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("make em Real");
|
||||
MakeGhostsReal?.Invoke();
|
||||
}
|
||||
m_IsGhosty = shouldBeGhosty;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ghost Object is somewhat deprecated. However it remains as the Renderers in the scene
|
||||
/// are all hooked up to the instance of GhostObject via GhostObjectBehaviour.
|
||||
/// N.B We use to only pass around GhostObject but features got more demanding
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class GhostObject
|
||||
{
|
||||
/// <summary>
|
||||
/// Renderers to be drawn through Albert Ghost Memory Pass
|
||||
/// </summary>
|
||||
[SerializeField]
|
||||
private Renderer[] m_Renderers;
|
||||
|
||||
public Renderer[] Renderers => m_Renderers;
|
||||
|
||||
public void SetRenderers(Renderer[] renderers)
|
||||
{
|
||||
m_Renderers = renderers;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user