First Commit
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
using Golems.RenderFeatures;
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
|
||||
namespace Golems
|
||||
{
|
||||
public class AlbertGhostMemoryComposite : SourceToDestinationBlitRenderFeature
|
||||
{
|
||||
[Header("Compose things")]
|
||||
[SerializeField] private bool m_IsEnabled = true;
|
||||
|
||||
[SerializeField] private AlbertGhostMemoryCompositeSettings m_FallbackSettings;
|
||||
private AlbertGhostMemoryCompositePass m_Pass;
|
||||
|
||||
private AlbertGhostMemoryCompositeSettings m_SettingsLoader;
|
||||
|
||||
[SerializeField]
|
||||
private float m_TimeBeforeSettingsFailureLog = 10.0f;
|
||||
|
||||
private float m_CreationTime;
|
||||
private bool m_LoggedSettingsFailure;
|
||||
|
||||
public override void Create()
|
||||
{
|
||||
m_LoggedSettingsFailure = false;
|
||||
m_CreationTime = Time.unscaledTime;
|
||||
bool logFallback = true;
|
||||
#if UNITY_EDITOR
|
||||
if (!EditorApplication.isPlayingOrWillChangePlaymode)
|
||||
{
|
||||
//
|
||||
// Cannot use Addressables in editor outside of playmode so use fallback
|
||||
//
|
||||
logFallback = false;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
m_SettingsLoader = m_FallbackSettings;
|
||||
|
||||
DoCreate();
|
||||
|
||||
m_Pass = new AlbertGhostMemoryCompositePass(m_SettingsLoader);
|
||||
|
||||
}
|
||||
private void DebugSetEnabledState(bool state)
|
||||
{
|
||||
m_IsEnabled = state;
|
||||
}
|
||||
private bool DebugGetEnabledState()
|
||||
{
|
||||
return m_IsEnabled;
|
||||
}
|
||||
|
||||
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
|
||||
{
|
||||
if (m_Pass == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// If the source and destination are not the same then have to at least perform a copy.
|
||||
//
|
||||
var doRender = !IsSourceAndDestinationSame();
|
||||
|
||||
if (!doRender)
|
||||
{
|
||||
//
|
||||
// Source and destination are different so
|
||||
// see if there is something to render.
|
||||
//
|
||||
if (m_IsEnabled)
|
||||
{
|
||||
doRender = renderingData.cameraData.camera.isActiveAndEnabled &&
|
||||
GhostObjectCollection.Instance.NumberOfObjectsRendered > 0 &&
|
||||
GhostObjectCollection.Instance.HasSomethingToRender();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (!doRender)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_Pass.Setup(m_IsEnabled, m_WhenToInsert, renderer.cameraColorTarget,
|
||||
m_SourceTextureSourceType, m_SourceNamedTemporaryNameHash,
|
||||
m_DestinationTextureSourceType, m_DestinationNamedTemporaryNameHash,
|
||||
m_TempCopyNamedTemporaryNameHash);
|
||||
renderer.EnqueuePass(m_Pass);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cd24c7c60313ff546ba5e93072933a44
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,167 @@
|
||||
using Golems.RenderFeatures;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
using UnityEngine.XR;
|
||||
using static Golems.ScriptableRenderPassHelper;
|
||||
|
||||
namespace Golems
|
||||
{
|
||||
public class AlbertGhostMemoryCompositePass : SourceToDestinationBlitRenderPass
|
||||
{
|
||||
private const string k_Shader = "Hidden/AlbertGhostMemoryComposite";
|
||||
private const string k_ProfilerIdent = "CompositePass";
|
||||
private AlbertGhostMemoryCompositeSettings m_Settings;
|
||||
private bool m_SettingsSet = false;
|
||||
private Material m_CompositeMat = null;
|
||||
private float m_Time = 0.0f;
|
||||
private bool m_IsEnabled;
|
||||
|
||||
#region Shader Properties
|
||||
private readonly int RandomValueID = Shader.PropertyToID("RandomValue");
|
||||
private readonly int TimeLapseID = Shader.PropertyToID("TimeLapse");
|
||||
private readonly int EdgeValueID = Shader.PropertyToID("EdgeValue");
|
||||
private readonly int GlobalAlphaID = Shader.PropertyToID("GlobalAlpha");
|
||||
private readonly int OffsetRedXID = Shader.PropertyToID("OffsetRedX");
|
||||
private readonly int OffsetGreenXID = Shader.PropertyToID("OffsetGreenX");
|
||||
private readonly int OffsetBlueXID = Shader.PropertyToID("OffsetBlueX");
|
||||
private readonly int OffsetRedYID = Shader.PropertyToID("OffsetRedY");
|
||||
private readonly int OffsetGreenYID = Shader.PropertyToID("OffsetGreenY");
|
||||
private readonly int OffsetBlueYID = Shader.PropertyToID("OffsetBlueY");
|
||||
private readonly int BrightnessID = Shader.PropertyToID("Brightness");
|
||||
private readonly int ContrastID = Shader.PropertyToID("Contrast");
|
||||
#endregion
|
||||
|
||||
public AlbertGhostMemoryCompositePass(AlbertGhostMemoryCompositeSettings settings)
|
||||
{
|
||||
SetSettings(settings);
|
||||
CreateMaterial(k_Shader, out m_CompositeMat);
|
||||
m_Time = 0.0f;
|
||||
}
|
||||
|
||||
public void SetSettings(AlbertGhostMemoryCompositeSettings settings)
|
||||
{
|
||||
m_Settings = settings;
|
||||
m_SettingsSet = m_Settings != null;
|
||||
}
|
||||
|
||||
public bool HasSettings()
|
||||
{
|
||||
return m_SettingsSet;
|
||||
}
|
||||
|
||||
public void Setup(bool isEnabled, RenderPassEvent whenToRender, RenderTargetIdentifier cameraColourTargetIdentifier,
|
||||
RenderFeatures.TextureSourceType sourceSourceType, int tempSourceTextureHash,
|
||||
RenderFeatures.TextureSourceType destinationSourceType, int tempDestinationTextureHash,
|
||||
int sameSourceDestTextureHash)
|
||||
{
|
||||
m_IsEnabled = isEnabled;
|
||||
DoSetup(whenToRender, cameraColourTargetIdentifier,
|
||||
sourceSourceType, tempSourceTextureHash,
|
||||
destinationSourceType, tempDestinationTextureHash,
|
||||
sameSourceDestTextureHash);
|
||||
}
|
||||
|
||||
|
||||
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
|
||||
{
|
||||
DoOnCameraSetup(cmd, ref renderingData);
|
||||
}
|
||||
|
||||
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
||||
{
|
||||
if (!m_GoodToExecute)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// If we do not have settings yet we at least need to do a copy so later
|
||||
// stages have something to work with.
|
||||
//
|
||||
var forceCopy = !m_SettingsSet;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
//
|
||||
// In Editor at edit time the material will get destroyed on scene load
|
||||
//
|
||||
if (m_CompositeMat == null)
|
||||
{
|
||||
CreateMaterial(k_Shader, out m_CompositeMat);
|
||||
}
|
||||
#endif
|
||||
|
||||
bool doCopyOnly = forceCopy || !m_IsEnabled || GhostObjectCollection.Instance.NumberOfObjectsRendered <= 0;
|
||||
if (doCopyOnly)
|
||||
{
|
||||
//
|
||||
// nothing to render
|
||||
//
|
||||
if (!m_SourceAndDestinationSame)
|
||||
{
|
||||
//
|
||||
// Copy the source to the destination
|
||||
//
|
||||
var copyCmd = CommandBufferPool.Get(k_ProfilerIdent);
|
||||
copyCmd.Blit(m_SourceTargetIdentifier, m_DestinationTargetIdentifier);
|
||||
context.ExecuteCommandBuffer(copyCmd);
|
||||
CommandBufferPool.Release(copyCmd);
|
||||
}
|
||||
return;
|
||||
}
|
||||
SetAllShaderParameters();
|
||||
CommandBuffer cmd = CommandBufferPool.Get(k_ProfilerIdent);
|
||||
|
||||
Compose(cmd);
|
||||
|
||||
context.ExecuteCommandBuffer(cmd);
|
||||
CommandBufferPool.Release(cmd);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Setup all the required values for
|
||||
/// the composition shader pass
|
||||
/// </summary>
|
||||
private void SetAllShaderParameters ()
|
||||
{
|
||||
m_Time += Time.deltaTime;
|
||||
|
||||
float random = Random.Range(0.0f, 1.0f);
|
||||
|
||||
m_CompositeMat.SetFloat(RandomValueID, random);
|
||||
m_CompositeMat.SetFloat(TimeLapseID, m_Time);
|
||||
m_CompositeMat.SetFloat(EdgeValueID, m_Settings.EdgeValue);
|
||||
m_CompositeMat.SetFloat(GlobalAlphaID, m_Settings.Alpha);
|
||||
|
||||
float ca_sin = m_Settings.ChromaticAberration * Mathf.Sin(m_Time * m_Settings.ChromaticAberrationSpeed);
|
||||
float ca_cos = m_Settings.ChromaticAberration * Mathf.Cos(m_Time * m_Settings.ChromaticAberrationSpeed);
|
||||
float ca_combo = (ca_sin + ca_cos);
|
||||
|
||||
m_CompositeMat.SetFloat(OffsetRedXID, ca_sin);
|
||||
m_CompositeMat.SetFloat(OffsetGreenXID, ca_combo);
|
||||
m_CompositeMat.SetFloat(OffsetBlueXID, ca_cos);
|
||||
m_CompositeMat.SetFloat(OffsetRedYID, ca_sin);
|
||||
m_CompositeMat.SetFloat(OffsetGreenYID, ca_combo);
|
||||
m_CompositeMat.SetFloat(OffsetBlueYID, -ca_cos);
|
||||
|
||||
m_CompositeMat.SetFloat(BrightnessID, m_Settings.Brightness);
|
||||
m_CompositeMat.SetFloat(ContrastID, m_Settings.Contrast);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compose the results of AlbertGhostMemoryPass into the cameraColorTarget
|
||||
/// </summary>
|
||||
private void Compose(in CommandBuffer cmd)
|
||||
{
|
||||
if (m_SourceAndDestinationSame)
|
||||
{
|
||||
cmd.Blit(m_SourceTargetIdentifier, m_TempCopyTargetIdentifier, m_CompositeMat);
|
||||
cmd.Blit(m_TempCopyTargetIdentifier, m_DestinationTargetIdentifier);
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd.Blit(m_SourceTargetIdentifier, m_DestinationTargetIdentifier, m_CompositeMat);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8382ede0b6a95594ba9841fab6b21364
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,52 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Golems
|
||||
{
|
||||
|
||||
[CreateAssetMenu(fileName = "New AlbertGhostMemoryCompositeSettings", menuName = "Golems/Scriptable Render Features/AlbertGhostMemoryCompositeSettings")]
|
||||
public class AlbertGhostMemoryCompositeSettings : ScriptableObject
|
||||
{
|
||||
[SerializeField] private float m_EdgeValue = 0.15f;
|
||||
public float EdgeValue
|
||||
{
|
||||
get => m_EdgeValue;
|
||||
set => m_EdgeValue = value;
|
||||
}
|
||||
|
||||
[SerializeField] private float m_ChromaticAberration = 0.0005f;
|
||||
public float ChromaticAberration
|
||||
{
|
||||
get => m_ChromaticAberration;
|
||||
set => m_ChromaticAberration = value;
|
||||
}
|
||||
|
||||
[SerializeField] private float m_ChromaticAberrationSpeed = 0.5f;
|
||||
public float ChromaticAberrationSpeed
|
||||
{
|
||||
get => m_ChromaticAberrationSpeed;
|
||||
set => m_ChromaticAberrationSpeed = value;
|
||||
}
|
||||
|
||||
[SerializeField] private float m_Brightness = 0.5f;
|
||||
public float Brightness
|
||||
{
|
||||
get => m_Brightness;
|
||||
set => m_Brightness = value;
|
||||
}
|
||||
|
||||
[SerializeField] private float m_Contrast = 1.0f;
|
||||
public float Contrast
|
||||
{
|
||||
get => m_Contrast;
|
||||
set => m_Contrast = value;
|
||||
}
|
||||
|
||||
[Range(0.0f, 1.0f)] public float m_Alpha = .5f;
|
||||
public float Alpha
|
||||
{
|
||||
get => m_Alpha;
|
||||
set => m_Alpha = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2f4087f258649684b9620322674cf675
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
/*
|
||||
using System;
|
||||
using UnityEngine.AddressableAssets;
|
||||
|
||||
namespace Golems
|
||||
{
|
||||
[Serializable]
|
||||
public class AlbertGhostMemoryCompositeSettingsRef : AssetReferenceT<AlbertGhostMemoryCompositeSettings>
|
||||
{
|
||||
public AlbertGhostMemoryCompositeSettingsRef(string guid) : base(guid)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1d87c60592bc40edbfcc3e1067ae34e5
|
||||
timeCreated: 1631024695
|
||||
Reference in New Issue
Block a user