Created
March 17, 2021 12:28
-
-
Save EmmaEwert/9c333db6c987b5e10297e5cf6ce3c726 to your computer and use it in GitHub Desktop.
Distortion Blit in Universal Render Pipeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Rendering; | |
using UnityEngine.Rendering.Universal; | |
public class Blit : ScriptableRendererFeature { | |
public Settings settings = new Settings(); | |
private Pass pass; | |
public override void Create() => | |
pass = new Pass(settings.@event, settings.material, name); | |
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { | |
if (settings.material != null) { | |
pass.source = renderer.cameraColorTarget; | |
renderer.EnqueuePass(pass); | |
} | |
} | |
[System.Serializable] | |
public class Settings { | |
public RenderPassEvent @event = RenderPassEvent.AfterRenderingOpaques; | |
public Material material = null; | |
} | |
public class Pass : ScriptableRenderPass { | |
public Material material; | |
public RenderTargetIdentifier source; | |
private string name; | |
private RenderTargetHandle texture; | |
public Pass(RenderPassEvent @event, Material material, string name) { | |
this.renderPassEvent = @event; | |
this.material = material; | |
this.name = name; | |
texture.Init("_TemporaryColorTexture"); | |
} | |
public override void Configure(CommandBuffer buffer, RenderTextureDescriptor descriptor) => | |
buffer.GetTemporaryRT(texture.id, descriptor); | |
public override void Execute(ScriptableRenderContext context, ref RenderingData data) { | |
var buffer = CommandBufferPool.Get(name); | |
buffer.Clear(); | |
data.cameraData.cameraTargetDescriptor.depthBufferBits = 0; | |
Blit(buffer, source, destination: texture.Identifier(), material, passIndex: -1); | |
Blit(buffer, source: texture.Identifier(), destination: source); | |
context.ExecuteCommandBuffer(buffer); | |
CommandBufferPool.Release(buffer); | |
} | |
public override void FrameCleanup(CommandBuffer buffer) => | |
buffer.ReleaseTemporaryRT(texture.id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Features/Distortion" { | |
Properties { | |
[HideInInspector] _MainTex ("Texture", 2D) = "white" {} | |
_DistortionTex ("Distortion (Normalmap)", 2D) = "white" {} | |
_Properties ("Intensity (XY), Stretch (ZW)", Vector) = (0.5, 0.5, 0.5, 0.5) | |
_TimeFactor ("Time factor", Float) = 0.5 | |
} | |
SubShader { | |
Cull Off ZWrite Off ZTest Always | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata { | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f { | |
float2 uv : TEXCOORD0; | |
float4 vertex : SV_POSITION; | |
}; | |
v2f vert(appdata v) { | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
o.uv = v.uv; | |
return o; | |
} | |
sampler2D _MainTex; | |
sampler2D _DistortionTex; | |
float4 _Properties; | |
float _TimeFactor; | |
float4 frag(v2f i) : SV_Target { | |
float2 offset = (i.uv + _Time.xx * _TimeFactor) * _Properties.wz; | |
float2 distortion = tex2D(_DistortionTex, offset).xy - 0.5; | |
float4 color = tex2D(_MainTex, i.uv + distortion * _Properties.xy); | |
return color; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment