Skip to content

Instantly share code, notes, and snippets.

@wonkee-kim
Created August 11, 2020 00:31
Show Gist options
  • Save wonkee-kim/3632b9334bf0ade3add32327fdcc0b52 to your computer and use it in GitHub Desktop.
Save wonkee-kim/3632b9334bf0ade3add32327fdcc0b52 to your computer and use it in GitHub Desktop.
Unity URP Blit with comments
// https://github.com/Unity-Technologies/UniversalRenderingExamples/blob/master/Assets/Scripts/Runtime/RenderPasses/Blit.cshttps://samdriver.xyz/articles/scriptableRender.html
// https://samdriver.xyz/articles/scriptableRender.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.Universal;
namespace UnityEngine.Experimental.Rendering.Universal {
// https://github.com/Unity-Technologies/UniversalRenderingExamples/blob/master/Assets/Scripts/Runtime/RenderPasses/Blit.cs
// https://samdriver.xyz/articles/scriptableRender.htm
// The "pass" is what does the actual rendering work while the "feature" is how it interfaces with the rest of the scriptable pipeline.
public class Blit :ScriptableRendererFeature {
// Settings, we're free to put whatever we want here, public fields will be exposed in the inspector
[System.Serializable]
public class BlitSettings {
public bool isEnabled = true;
[HideInInspector] public bool isEnabledByIntensity = true;
[Range(0,1)] public float intensity = 0.5f;
public RenderPassEvent Event = RenderPassEvent.AfterRenderingOpaques;
public Material blitMaterial = null;
public int blitMaterialPassIndex = -1;
public Target destination = Target.Color;
public string textureId = "_BlitPassTexture";
}
public enum Target {
Color,
Texture
}
// MUST be names "settings"(lowercase) to be shown in the Render Features inspector
public BlitSettings settings = new BlitSettings();
RenderTargetHandle m_RenderTextureHandle;
BlitPass blitPass;
// called when OnValidate(), OnEnable() or GUIUtility(inspector) is changed
public override void Create() {
var passIndex = settings.blitMaterial != null ? settings.blitMaterial.passCount - 1 : 1;
settings.blitMaterialPassIndex = Mathf.Clamp(settings.blitMaterialPassIndex, -1, passIndex);
blitPass = new BlitPass(settings.Event, settings.blitMaterial, settings.blitMaterialPassIndex, name);
m_RenderTextureHandle.Init(settings.textureId);
settings.isEnabledByIntensity = (settings.intensity > 0);
}
// called every frame once per camera
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) {
if(!settings.isEnabled || !settings.isEnabledByIntensity) {
// we can do nothing this frame if we want
return;
}
if(settings.blitMaterial == null) {
Debug.LogWarningFormat("Missing Blit Material. {0} blit pass will not execute. Check for missing reference in the assigned renderer.", GetType().Name);
return;
}
settings.blitMaterial.SetFloat("_Intensity", settings.intensity);
// Gather up and pass any extra information our pass will need.
// In this case we're getting the camera's color buffer target
var src = renderer.cameraColorTarget;
//var src = renderer.cameraDepth;
var dest = (settings.destination == Target.Color) ? RenderTargetHandle.CameraTarget : m_RenderTextureHandle;
blitPass.Setup(src, dest);
// Ask the renderer to add our pass.
// Could queue up multiple passes and/or pick passes to use
renderer.EnqueuePass(blitPass);
}
} // public class Blit :ScriptableRendererFeature
} // namespace UnityEngine.Experimental.Rendering.Universal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment