feat : add a cathodic efect on the player cam
This commit is contained in:
8
Assets/Code/Scripts/Rendering.meta
Normal file
8
Assets/Code/Scripts/Rendering.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 97a4ae8015df4732ac9524441048a765
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
173
Assets/Code/Scripts/Rendering/CRTRendererFeature.cs
Normal file
173
Assets/Code/Scripts/Rendering/CRTRendererFeature.cs
Normal file
@@ -0,0 +1,173 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Rendering.RenderGraphModule;
|
||||
using UnityEngine.Rendering.RenderGraphModule.Util;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
|
||||
public class CRTRendererFeature : ScriptableRendererFeature
|
||||
{
|
||||
[System.Serializable]
|
||||
public class CRTSettings
|
||||
{
|
||||
public bool EffectEnabled = true;
|
||||
public RenderPassEvent PassEvent = RenderPassEvent.AfterRenderingPostProcessing;
|
||||
public Shader CRTShader;
|
||||
|
||||
[Range(0f, 1f)] public float Intensity = 0.65f;
|
||||
[Range(0f, 2f)] public float ScanlineDensity = 1.2f;
|
||||
[Range(0f, 1f)] public float ScanlineStrength = 0.18f;
|
||||
[Range(0f, 0.2f)] public float Curvature = 0.04f;
|
||||
[Range(0f, 1f)] public float VignetteStrength = 0.28f;
|
||||
[Range(0f, 0.05f)] public float ChromaticAberration = 0.004f;
|
||||
[Range(0f, 0.2f)] public float NoiseStrength = 0.03f;
|
||||
[Range(0f, 0.1f)] public float FlickerStrength = 0.015f;
|
||||
}
|
||||
|
||||
class CRTPass : ScriptableRenderPass
|
||||
{
|
||||
private readonly ProfilingSampler m_ProfilingSampler = new("CRT Effect");
|
||||
|
||||
private Material m_Material;
|
||||
private CRTSettings m_Settings;
|
||||
private RTHandle m_TempColorTexture;
|
||||
|
||||
public void Setup(Material material, CRTSettings settings)
|
||||
{
|
||||
m_Material = material;
|
||||
m_Settings = settings;
|
||||
renderPassEvent = settings.PassEvent;
|
||||
requiresIntermediateTexture = true;
|
||||
}
|
||||
|
||||
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
|
||||
{
|
||||
if (m_Material == null || m_Settings == null || !m_Settings.EffectEnabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
|
||||
if (resourceData.isActiveTargetBackBuffer)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_Material.SetFloat("_Intensity", m_Settings.Intensity);
|
||||
m_Material.SetFloat("_ScanlineDensity", m_Settings.ScanlineDensity);
|
||||
m_Material.SetFloat("_ScanlineStrength", m_Settings.ScanlineStrength);
|
||||
m_Material.SetFloat("_Curvature", m_Settings.Curvature);
|
||||
m_Material.SetFloat("_VignetteStrength", m_Settings.VignetteStrength);
|
||||
m_Material.SetFloat("_ChromaticAberration", m_Settings.ChromaticAberration);
|
||||
m_Material.SetFloat("_NoiseStrength", m_Settings.NoiseStrength);
|
||||
m_Material.SetFloat("_FlickerStrength", m_Settings.FlickerStrength);
|
||||
|
||||
TextureHandle source = resourceData.activeColorTexture;
|
||||
TextureDesc destinationDesc = renderGraph.GetTextureDesc(source);
|
||||
destinationDesc.name = "CameraColor-CRT";
|
||||
destinationDesc.clearBuffer = false;
|
||||
|
||||
TextureHandle destination = renderGraph.CreateTexture(destinationDesc);
|
||||
RenderGraphUtils.BlitMaterialParameters blitParams = new(source, destination, m_Material, 0);
|
||||
renderGraph.AddBlitPass(blitParams, "CRT Effect");
|
||||
|
||||
resourceData.cameraColor = destination;
|
||||
}
|
||||
|
||||
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
|
||||
{
|
||||
cameraTextureDescriptor.depthBufferBits = 0;
|
||||
RenderingUtils.ReAllocateHandleIfNeeded(
|
||||
ref m_TempColorTexture,
|
||||
cameraTextureDescriptor,
|
||||
FilterMode.Bilinear,
|
||||
TextureWrapMode.Clamp,
|
||||
name: "_CRTTempColorTexture"
|
||||
);
|
||||
}
|
||||
|
||||
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
||||
{
|
||||
if (m_Material == null || m_Settings == null || !m_Settings.EffectEnabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (renderingData.cameraData.isSceneViewCamera)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CommandBuffer cmd = CommandBufferPool.Get();
|
||||
|
||||
using (new ProfilingScope(cmd, m_ProfilingSampler))
|
||||
{
|
||||
m_Material.SetFloat("_Intensity", m_Settings.Intensity);
|
||||
m_Material.SetFloat("_ScanlineDensity", m_Settings.ScanlineDensity);
|
||||
m_Material.SetFloat("_ScanlineStrength", m_Settings.ScanlineStrength);
|
||||
m_Material.SetFloat("_Curvature", m_Settings.Curvature);
|
||||
m_Material.SetFloat("_VignetteStrength", m_Settings.VignetteStrength);
|
||||
m_Material.SetFloat("_ChromaticAberration", m_Settings.ChromaticAberration);
|
||||
m_Material.SetFloat("_NoiseStrength", m_Settings.NoiseStrength);
|
||||
m_Material.SetFloat("_FlickerStrength", m_Settings.FlickerStrength);
|
||||
|
||||
RTHandle source = renderingData.cameraData.renderer.cameraColorTargetHandle;
|
||||
Blitter.BlitCameraTexture(cmd, source, m_TempColorTexture, m_Material, 0);
|
||||
Blitter.BlitCameraTexture(cmd, m_TempColorTexture, source);
|
||||
}
|
||||
|
||||
context.ExecuteCommandBuffer(cmd);
|
||||
CommandBufferPool.Release(cmd);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
m_TempColorTexture?.Release();
|
||||
m_TempColorTexture = null;
|
||||
}
|
||||
}
|
||||
|
||||
public CRTSettings Settings = new();
|
||||
|
||||
private CRTPass m_Pass;
|
||||
private Material m_Material;
|
||||
|
||||
public override void Create()
|
||||
{
|
||||
if (Settings.CRTShader == null)
|
||||
{
|
||||
Settings.CRTShader = Shader.Find("Hidden/HeadlessHazard/CRT");
|
||||
}
|
||||
|
||||
if (Settings.CRTShader != null)
|
||||
{
|
||||
m_Material = CoreUtils.CreateEngineMaterial(Settings.CRTShader);
|
||||
}
|
||||
|
||||
m_Pass ??= new CRTPass();
|
||||
}
|
||||
|
||||
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
|
||||
{
|
||||
if (m_Material == null || !Settings.EffectEnabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (renderingData.cameraData.cameraType != CameraType.Game)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_Pass.Setup(m_Material, Settings);
|
||||
renderer.EnqueuePass(m_Pass);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
m_Pass?.Dispose();
|
||||
m_Pass = null;
|
||||
|
||||
CoreUtils.Destroy(m_Material);
|
||||
m_Material = null;
|
||||
}
|
||||
}
|
||||
11
Assets/Code/Scripts/Rendering/CRTRendererFeature.cs.meta
Normal file
11
Assets/Code/Scripts/Rendering/CRTRendererFeature.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4f2de7a6cfbd47c8bc740d43bb991205
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
97
Assets/Code/Shaders/CRTScreenEffect.shader
Normal file
97
Assets/Code/Shaders/CRTScreenEffect.shader
Normal file
@@ -0,0 +1,97 @@
|
||||
Shader "Hidden/HeadlessHazard/CRT"
|
||||
{
|
||||
Properties
|
||||
{
|
||||
_Intensity ("Intensity", Range(0,1)) = 0.65
|
||||
_ScanlineDensity ("Scanline Density", Range(0,2)) = 1.2
|
||||
_ScanlineStrength ("Scanline Strength", Range(0,1)) = 0.18
|
||||
_Curvature ("Curvature", Range(0,0.2)) = 0.04
|
||||
_VignetteStrength ("Vignette Strength", Range(0,1)) = 0.28
|
||||
_ChromaticAberration ("Chromatic Aberration", Range(0,0.05)) = 0.004
|
||||
_NoiseStrength ("Noise Strength", Range(0,0.2)) = 0.03
|
||||
_FlickerStrength ("Flicker Strength", Range(0,0.1)) = 0.015
|
||||
}
|
||||
|
||||
SubShader
|
||||
{
|
||||
Tags { "RenderPipeline" = "UniversalPipeline" }
|
||||
|
||||
Pass
|
||||
{
|
||||
Name "CRT"
|
||||
ZWrite Off
|
||||
ZTest Always
|
||||
Cull Off
|
||||
Blend One Zero
|
||||
|
||||
HLSLPROGRAM
|
||||
#pragma vertex Vert
|
||||
#pragma fragment Frag
|
||||
#pragma target 3.5
|
||||
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
||||
#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
|
||||
|
||||
float _Intensity;
|
||||
float _ScanlineDensity;
|
||||
float _ScanlineStrength;
|
||||
float _Curvature;
|
||||
float _VignetteStrength;
|
||||
float _ChromaticAberration;
|
||||
float _NoiseStrength;
|
||||
float _FlickerStrength;
|
||||
|
||||
float Random01(float2 seed)
|
||||
{
|
||||
return frac(sin(dot(seed, float2(12.9898, 78.233))) * 43758.5453);
|
||||
}
|
||||
|
||||
float2 DistortUV(float2 uv, float curvature)
|
||||
{
|
||||
float2 center = uv * 2.0 - 1.0;
|
||||
float radius2 = dot(center, center);
|
||||
center *= 1.0 + (radius2 * curvature);
|
||||
return center * 0.5 + 0.5;
|
||||
}
|
||||
|
||||
half4 Frag(Varyings input) : SV_Target
|
||||
{
|
||||
float2 uv = input.texcoord;
|
||||
float2 curvedUV = DistortUV(uv, _Curvature);
|
||||
|
||||
if (curvedUV.x < 0.0 || curvedUV.x > 1.0 || curvedUV.y < 0.0 || curvedUV.y > 1.0)
|
||||
{
|
||||
return half4(0.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
|
||||
float2 fromCenter = curvedUV - 0.5;
|
||||
float2 aberrationOffset = fromCenter * _ChromaticAberration;
|
||||
|
||||
half red = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, curvedUV + aberrationOffset).r;
|
||||
half green = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, curvedUV).g;
|
||||
half blue = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, curvedUV - aberrationOffset).b;
|
||||
half3 color = half3(red, green, blue);
|
||||
|
||||
float scanlineWave = sin((curvedUV.y * _ScreenParams.y * 0.5 * _ScanlineDensity) + (_Time.y * 18.0));
|
||||
float scanlineMask = lerp(1.0, saturate(0.7 + 0.3 * scanlineWave), _ScanlineStrength);
|
||||
color *= scanlineMask;
|
||||
|
||||
float noise = Random01(curvedUV * _ScreenParams.xy + _Time.yy * 37.0) - 0.5;
|
||||
color += noise * _NoiseStrength;
|
||||
|
||||
float flicker = 1.0 - (_FlickerStrength * (0.5 + 0.5 * sin(_Time.y * 32.0)));
|
||||
color *= flicker;
|
||||
|
||||
float2 vignetteUV = curvedUV * (1.0 - curvedUV.yx);
|
||||
float vignette = saturate(pow(vignetteUV.x * vignetteUV.y * 18.0, 0.2));
|
||||
color *= lerp(1.0 - _VignetteStrength, 1.0, vignette);
|
||||
|
||||
half3 baseColor = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv).rgb;
|
||||
half3 finalColor = lerp(baseColor, color, _Intensity);
|
||||
|
||||
return half4(finalColor, 1.0);
|
||||
}
|
||||
ENDHLSL
|
||||
}
|
||||
}
|
||||
}
|
||||
10
Assets/Code/Shaders/CRTScreenEffect.shader.meta
Normal file
10
Assets/Code/Shaders/CRTScreenEffect.shader.meta
Normal file
@@ -0,0 +1,10 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a9f7eb85c2f4f9f8ec82c8565f4e8b1
|
||||
ShaderImporter:
|
||||
externalObjects: {}
|
||||
defaultTextures: []
|
||||
nonModifiableTextures: []
|
||||
preprocessorOverride: 0
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user