using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class Grayscale : ScriptableRendererFeature
{
[System.Serializable]
public class GrayscaleSetting
{
// レンダリングの実行タイミング
public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
}
/// <summary>
/// Grayscale実行Pass
/// </summary>
class GrayScalePass : ScriptableRenderPass
{
private readonly string profilerTag = "GrayScale Pass";
public Material grayscaleMaterial; // グレースケール計算用マテリアル
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var cameraColorTarget = renderingData.cameraData.renderer.cameraColorTarget;
// コマンドバッファ
var cmd = CommandBufferPool.Get(profilerTag);
// マテリアル実行
cmd.Blit(cameraColorTarget, cameraColorTarget, grayscaleMaterial);
context.ExecuteCommandBuffer(cmd);
}
}
[SerializeField] private GrayscaleSetting settings = new GrayscaleSetting();
private GrayScalePass scriptablePass;
public override void Create()
{
var shader = Shader.Find("Unlit/MonoToneUnlitShader");
if (shader)
{
scriptablePass = new GrayScalePass();
scriptablePass.grayscaleMaterial = new Material(shader);
scriptablePass.renderPassEvent = settings.renderPassEvent;
}
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
if (scriptablePass != null && scriptablePass.grayscaleMaterial != null)
{
renderer.EnqueuePass(scriptablePass);
}
}
}
}
Shader "Unlit/MonoToneUnlitShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
fixed4 frag (v2f_img i) : COLOR
{
fixed4 color = tex2D(_MainTex,i.uv);
float grayColor = color.r * 0.3 + color.g * 0.6 + color.b * 0.1;
return fixed4(grayColor, grayColor, grayColor, color.a);
}
ENDCG
}
}
}