Unity2021.3.15f1. URP 12.1.8
※URP14の場合はこちら
PostProcess 用のスクリプトをたまに忘れるのでこちらにメモしておきます
using UnityEngine; using UnityEngine.Rendering; using UnityEngine.Rendering.Universal; [Serializable] public class SampleRendererFeature : ScriptableRendererFeature { [SerializeField] private Shader _shader; private SampleRendererPass _pass; public override void Create() { _pass = new SampleRendererPass(); _pass.SetUp(_shader); } public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { renderer.EnqueuePass(_pass); } } public class SampleRendererPass : ScriptableRenderPass { private Material _material; private const string _name = nameof(SampleRendererPass); public void SetUp(Shader shader) { if (shader == null) return; renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing; _material = CoreUtils.CreateEngineMaterial(shader); } public override void Execute(ScriptableRenderContext context, ref RenderingData data) { if (_material == null || !data.cameraData.postProcessEnabled || data.cameraData.cameraType == CameraType.SceneView ) { return; } var commandBuffer = CommandBufferPool.Get(_name); using (new ProfilingScope(commandBuffer, new ProfilingSampler(_name))) { Blit(commandBuffer, ref data, _material); } context.ExecuteCommandBuffer(commandBuffer); CommandBufferPool.Release(commandBuffer); } }
PostProcess 用の 色反転 Shader
Shader "Hidden/SampleImageEffectShader" { SubShader { Cull Off ZWrite Off ZTest Always Pass { HLSLPROGRAM #pragma vertex FullscreenVert #pragma fragment frag #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl" TEXTURE2D_X(_SourceTex); SAMPLER(sampler_SourceTex); half4 frag(Varyings i) : SV_Target { half4 col = SAMPLE_TEXTURE2D_X( _SourceTex, sampler_SourceTex, i.uv); col.rgb = 1 - col.rgb; return col; } ENDHLSL } } }
FullscreenVert の中身はこのようになっています
Varyings FullscreenVert(Attributes input) { Varyings output; UNITY_SETUP_INSTANCE_ID(input); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); #if _USE_DRAW_PROCEDURAL output.positionCS = GetQuadVertexPosition(input.vertexID); output.positionCS.xy = output.positionCS.xy * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f); //convert to -1..1 output.uv = GetQuadTexCoord(input.vertexID) * _ScaleBias.xy + _ScaleBias.zw; #else output.positionCS = TransformObjectToHClip(input.positionOS.xyz); output.uv = input.uv; #endif return output; }