ソーベルフィルタ
https://gyazo.com/167d94c688a450ad2740598d82b5ef66
code:SobelPass.cs
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering;
public class SobelPass : CustomPass
{
public Color outlineColor = Color.black;
public Color baseColor = Color.white;
public float threshold = 1;
public float thickness = 1;
public float senga = 0;
public float nega = 0;
public float lines = 0;
Shader sobelShader;
Material fullscreenMaterial;
MaterialPropertyBlock materialProperties;
ShaderTagId[] shaderTags;
RTHandle rtBuffer;
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
sobelShader = Shader.Find("FullScreen/SobelPass");
fullscreenMaterial = CoreUtils.CreateEngineMaterial(sobelShader);
materialProperties = new MaterialPropertyBlock();
// List all the materials that will be replaced in the frame
shaderTags = new ShaderTagId3 {
new ShaderTagId("Forward"),
new ShaderTagId("ForwardOnly"),
new ShaderTagId("SRPDefaultUnlit"),
};
rtBuffer = RTHandles.Alloc(
Vector2.one, TextureXR.slices, dimension: TextureXR.dimension,
colorFormat: GraphicsFormat.B10G11R11_UFloatPack32,
useDynamicScale: true, name: "Sobel Buffer"
);
}
void DrawMeshes(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
{
var result = new RendererListDesc(shaderTags, cullingResult, hdCamera.camera)
{
// We need the lighting render configuration to support rendering lit objects
rendererConfiguration = PerObjectData.LightProbe | PerObjectData.LightProbeProxyVolume | PerObjectData.Lightmaps,
renderQueueRange = RenderQueueRange.all,
sortingCriteria = SortingCriteria.BackToFront,
excludeObjectMotionVectors = false,
layerMask = 0,
};
CoreUtils.SetRenderTarget(cmd, rtBuffer, ClearFlag.Color);
HDUtils.DrawRendererList(renderContext, cmd, RendererList.Create(result));
}
protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
{
DrawMeshes(renderContext, cmd, hdCamera, cullingResult);
SetCameraRenderTarget(cmd);
materialProperties.SetColor("_OutlineColor", outlineColor);
materialProperties.SetColor("_BaseColor", baseColor);
materialProperties.SetTexture("_OutlineBuffer", rtBuffer);
materialProperties.SetFloat("_Threshold", threshold);
materialProperties.SetFloat("_Thickness", thickness);
materialProperties.SetFloat("_Senga", senga);
materialProperties.SetFloat("_Nega", nega);
materialProperties.SetFloat("_Lines", lines);
CoreUtils.DrawFullScreen(cmd, fullscreenMaterial, materialProperties, shaderPassId: 0);
}
protected override void Cleanup()
{
CoreUtils.Destroy(fullscreenMaterial);
rtBuffer.Release();
}
}
code:SobelPass.shader
Shader "FullScreen/SobelPass"
{
HLSLINCLUDE
#pragma only_renderers d3d11 playstation xboxone vulkan metal switch #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassCommon.hlsl" float4 _OutlineColor;
float4 _BaseColor;
float _Threshold;
float _Thickness;
float _Senga;
float _Nega;
float _Lines;
float _Lumin;
{
float2(1, 1),
float2(0, 1),
float2(-1, 1),
float2(-1, 0),
float2(-1, -1),
float2(0, -1),
float2(1, -1),
float2(1, 0),
};
{
float2(-1, 1),
float2(0, 2),
float2(1, 1),
float2(2, 0),
float2(1, -1),
float2(0, -2),
float2(-1, -1),
float2(-2, 0),
};
float4 SamplePix(float2 uv) {
return float4(CustomPassSampleCameraColor(uv, 0), 1);
}
float4 FullScreenPass(Varyings varyings) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(varyings);
float depth = LoadCameraDepth(varyings.positionCS.xy);
PositionInputs posInput = GetPositionInput(varyings.positionCS.xy, _ScreenSize.zw, depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V);
float4 color = float4(0.0, 0.0, 0.0, 0.0);
// Load the camera color buffer at the mip 0 if we're not at the before rendering injection point
if (_CustomPassInjectionPoint != CUSTOMPASSINJECTIONPOINT_BEFORE_RENDERING)
color = float4(CustomPassSampleCameraColor(posInput.positionNDC.xy, 0), 1);
// When sampling RTHandle texture, always use _RTHandleScale.xy to scale your UVs first.
float2 uv = posInput.positionNDC.xy;
float sobel = 0;
float4 sh = 0, sv = 0;
for (int i = 0; i < SAMPLES; i++)
{
float2 uvN = uv + _ScreenSize.zw * samplesi * _Thickness; float4 neighbour = SamplePix(uvN);
if (_Senga < 1) {
sh += min(max(neighbour, 0), 0) * sampleci.x; sv += min(max(neighbour, 0), 0) * sampleci.y; }
else {
sh += neighbour * sampleci.x; sv += neighbour * sampleci.y; }
}
if (_Senga < 1) {
sobel = sqrt(sh.r * sh.r + sv.r * sv.r);
}
else {
sobel = sqrt(pow(sh.r * sh.r, 2) + pow(sv.r * sv.r, 2)) * 128;
}
if (_Nega >= 1)
color.rgb = lerp(_OutlineColor.rgb, color.rgb, sobel);
else if (_Lines >= 1)
color.rgb = lerp(_BaseColor.rgb, color.rgb, sobel);
else
color.rgb = lerp(color.rgb, _OutlineColor.rgb, sobel);
return color;
}
ENDHLSL
SubShader
{
Pass
{
Name "Sobel Pass"
ZWrite Off
ZTest Always
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
HLSLPROGRAM
ENDHLSL
}
}
Fallback Off
}