Skip to content

Instantly share code, notes, and snippets.

@thelebaron
Created July 7, 2020 00:40
Show Gist options
  • Select an option

  • Save thelebaron/4add72dfb2c9199a5b9b20334816550f to your computer and use it in GitHub Desktop.

Select an option

Save thelebaron/4add72dfb2c9199a5b9b20334816550f to your computer and use it in GitHub Desktop.
pixel effect
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace RenderPasses
{
/// <summary>
///
/// </summary>
public class RetroBroFeature : ScriptableRendererFeature
{
class BroPass : ScriptableRenderPass
{
public bool Enabled;
public int Height = 320;
public int Width = 240;
private int m_Width;
const string k_profilerTag = "DrawPixelPass";
private RenderTargetIdentifier m_ColorSource;
private RenderTargetHandle m_PixelHandle = RenderTargetHandle.CameraTarget;
private RenderTextureDescriptor m_BaseDescriptor;
private RenderTargetIdentifier Source { get; set; }
private RenderTargetHandle Destination { get; set; }
public void Setup(RenderTargetIdentifier source, RenderTargetHandle destination)
{
this.Source = source;
this.Destination = destination;
}
// This method is called before executing the render pass.
// It can be used to configure render targets and their clear state. Also to create temporary render target textures.
// When empty this render pass will render to the active camera render target.
// You should never call CommandBuffer.SetRenderTarget. Instead call <c>ConfigureTarget</c> and <c>ConfigureClear</c>.
// The render pipeline will ensure target setup and clearing happens in an performance manner.
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
{
m_PixelHandle.Init("_TempPixelTexture");
RenderTextureDescriptor opaqueDesc = cameraTextureDescriptor;
var ratio = opaqueDesc.width / (float)opaqueDesc.height;
m_Width = Mathf.RoundToInt(Height * ratio);
//opaqueDesc.msaaSamples = (int)MSAASamples.None;
//opaqueDesc.height = Height;
//opaqueDesc.width = Width;//m_Width;
//cmd.GetTemporaryRT(m_PixelHandle.id, opaqueDesc, FilterMode.Point);
//R8G8B8A8_UNorm
//ARGB4444
// good working format
var format = GraphicsFormat.R16G16B16A16_UNorm; // no banding
format = GraphicsFormat.B8G8R8A8_UNorm;// has banding
//format = GraphicsFormat.R16G16B16A16_UNorm;// ?
cmd.GetTemporaryRT(m_PixelHandle.id, Height, Width,24, FilterMode.Point, format, (int)MSAASamples.None);
}
// Here you can implement the rendering logic.
// Use <c>ScriptableRenderContext</c> to issue drawing commands or execute command buffers
// https://docs.unity3d.com/ScriptReference/Rendering.ScriptableRenderContext.html
// You don't have to call ScriptableRenderContext.submit, the render pipeline will call it at specific points in the pipeline.
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
CommandBuffer cmd = CommandBufferPool.Get(k_profilerTag);
RenderTextureDescriptor opaqueDesc = renderingData.cameraData.cameraTargetDescriptor;
//opaqueDesc.height = 320;
//opaqueDesc.width = 240;
Destination = RenderTargetHandle.CameraTarget;
cmd.GetTemporaryRT(m_PixelHandle.id, opaqueDesc, FilterMode.Point);
Blit(cmd, Source, m_PixelHandle.Identifier());
Blit(cmd, m_PixelHandle.Identifier(), Source);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
/// Cleanup any allocated resources that were created during the execution of this render pass.
public override void OnCameraCleanup(CommandBuffer cmd)
{
if (Destination == RenderTargetHandle.CameraTarget)
cmd.ReleaseTemporaryRT(m_PixelHandle.id);
}
}
BroPass m_ScriptablePass;
public override void Create()
{
m_ScriptablePass = new BroPass();
m_ScriptablePass.Enabled = settings.enabled;
m_ScriptablePass.Height = settings.height;
m_ScriptablePass.Width = settings.width;
// Configures where the render pass should be injected.
m_ScriptablePass.renderPassEvent = settings.passEvent;
}
// Here you can inject one or multiple render passes in the renderer.
// This method is called when setting up the renderer once per-camera.
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
if(!m_ScriptablePass.Enabled)
return;
var src = renderer.cameraColorTarget;
var dest = RenderTargetHandle.CameraTarget;
m_ScriptablePass.Setup(src, dest);
renderer.EnqueuePass(m_ScriptablePass);
}
[System.Serializable]
public class BroSettings
{
public RenderPassEvent passEvent = RenderPassEvent.BeforeRenderingPostProcessing;
public int height = 320;
public int width = 280;
public bool enabled;
}
public BroSettings settings = new BroSettings();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment