Skip to content

Instantly share code, notes, and snippets.

@larssteenhoff
Forked from joshcamas/DistantPixelize.cs
Created September 23, 2023 23:06
Show Gist options
  • Save larssteenhoff/5817367aa56a57882f19d9f13a2b1f10 to your computer and use it in GitHub Desktop.
Save larssteenhoff/5817367aa56a57882f19d9f13a2b1f10 to your computer and use it in GitHub Desktop.
Distant Pixelize Renderer (Using SCPE)
#if URP
using UnityEngine.Rendering.Universal;
#endif
using System;
using UnityEngine;
using UnityEngine.Rendering;
using SCPE;
namespace Ardenfall.Effects
{
#if URP
[Serializable, VolumeComponentMenu("Ardenfall/Distant Pixelize")]
public sealed class DistantPixelize : VolumeComponent, IPostProcessComponent
{
public ClampedFloatParameter maxSize = new ClampedFloatParameter(0f, 0f, 1f);
public ClampedFloatParameter minSize = new ClampedFloatParameter(0f, 0f, 1f);
public ClampedFloatParameter maxDistance = new ClampedFloatParameter(0f, 0f, 300f);
public bool IsActive() => maxSize.value > 0f && this.active;
public bool IsTileCompatible() => false;
#endif
}
}
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering;
using UnityEngine;
using SCPE;
namespace Ardenfall.Effects
{
#if URP
public class DistantPixelizeRenderer : ScriptableRendererFeature
{
class DistantPixelizeRenderPass : PostEffectRenderer<DistantPixelize>
{
public DistantPixelizeRenderPass()
{
shaderName = "Ardenfall/Effects/DistantPixelize";
ProfilerTag = this.ToString();
}
public void Setup(ScriptableRenderer renderer)
{
this.cameraColorTarget = GetCameraTarget(renderer);
volumeSettings = VolumeManager.instance.stack.GetComponent<DistantPixelize>();
if(volumeSettings) renderer.EnqueuePass(this);
}
public override void ConfigurePass(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
{
if (!volumeSettings) return;
base.ConfigurePass(cmd, cameraTextureDescriptor);
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
if (!volumeSettings) return;
if (volumeSettings.IsActive() == false) return;
var cmd = CommandBufferPool.Get(ProfilerTag);
CopyTargets(cmd, renderingData);
Material.SetFloat("_MaxPixelSize", volumeSettings.maxSize.value * 0.1f);
Material.SetFloat("_MinPixelSize", volumeSettings.minSize.value * 0.1f);
Material.SetFloat("_MaxDistance", volumeSettings.maxDistance.value);
FinalBlit(this, context, cmd, mainTexID.id, cameraColorTarget, Material, 0);
}
}
public RenderPassEvent renderPassEvent;
private DistantPixelizeRenderPass m_ScriptablePass;
public override void Create()
{
m_ScriptablePass = new DistantPixelizeRenderPass();
// Configures where the render pass should be injected.
m_ScriptablePass.renderPassEvent = renderPassEvent;
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
m_ScriptablePass.Setup(renderer);
}
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment