Skip to content

Commit

Permalink
Added a grille effect to the CRT shader
Browse files Browse the repository at this point in the history
  • Loading branch information
Math-Man committed Jan 28, 2024
1 parent 0fd2115 commit 11752b7
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 174 deletions.
14 changes: 11 additions & 3 deletions URP-PSX/Assets/Scenes/Psx_PBR.unity
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ Light:
serializedVersion: 10
m_Type: 0
m_Shape: 0
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_Color: {r: 0.28040814, g: 0.49162373, b: 0.6415094, a: 1}
m_Intensity: 70.23
m_Range: 27.335835
m_SpotAngle: 32.74557
Expand Down Expand Up @@ -574,7 +574,7 @@ Transform:
m_GameObject: {fileID: 608079567}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0.7071068, z: 0, w: 0.7071068}
m_LocalPosition: {x: 9.22, y: -0.44, z: -0.02}
m_LocalPosition: {x: 7.09, y: 3.28, z: -0.02}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
Expand Down Expand Up @@ -614,7 +614,7 @@ Light:
serializedVersion: 10
m_Type: 2
m_Shape: 0
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_Color: {r: 0.2519677, g: 0.5767318, b: 0.8805031, a: 1}
m_Intensity: 70.23
m_Range: 27.335835
m_SpotAngle: 32.74557
Expand Down Expand Up @@ -1457,6 +1457,14 @@ MonoBehaviour:
chromaticRed: {x: 0.002, y: -0.002}
chromaticGreen: {x: -0.002, y: 0.002}
chromaticBlue: {x: 0, y: 0}
grilleOpacity: 0.2
grilleCounterOpacity: 0.3
grilleResolution: 640
grilleCounterResolution: 380
grilleBrightness: 16
grilleUvRotation: 90
grilleUvMidPoint: 0.5
grilleShift: {x: 1, y: 1, z: 1}
--- !u!1001 &1364880466
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down
20 changes: 19 additions & 1 deletion URP-PSX/Assets/Scripts/CRT/CRTEffectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public class CRTEffectController : MonoBehaviour
[SerializeField] protected Vector2 chromaticGreen = new Vector2();
[SerializeField] protected Vector2 chromaticBlue = new Vector2();

[SerializeField] protected float grilleOpacity = 0.4f;
[SerializeField] protected float grilleCounterOpacity = 0.2f;
[SerializeField] protected float grilleResolution = 360.0f;
[SerializeField] protected float grilleCounterResolution = 540.0f;
[SerializeField] protected float grilleBrightness = 15.0f;
[SerializeField] protected float grilleUvRotation = (90.0f);
[SerializeField] protected float grilleUvMidPoint = 0.5f;
[SerializeField] protected Vector3 grilleShift = new Vector3(1.0f, 1.0f, 1.0f);

protected void Update()
{
this.SetParams();
Expand Down Expand Up @@ -57,6 +66,15 @@ protected void SetParams()
this.crt.chromaticRed.value = this.chromaticRed;
this.crt.chromaticGreen.value = this.chromaticGreen;
this.crt.chromaticBlue.value = this.chromaticBlue;


this.crt.grilleOpacity.value = this.grilleOpacity;
this.crt.grilleCounterOpacity.value = this.grilleCounterOpacity;
this.crt.grilleResolution.value = this.grilleResolution;
this.crt.grilleCounterResolution.value = this.grilleCounterResolution;
this.crt.grilleBrightness.value = this.grilleBrightness;
this.crt.grilleUvRotation.value = this.grilleUvRotation;
this.crt.grilleUvMidPoint.value = this.grilleUvMidPoint;
this.crt.grilleShift.value = this.grilleShift;

}
}
20 changes: 19 additions & 1 deletion URP-PSX/Assets/Scripts/CRT/CRTRenderFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public class CRTPass : ScriptableRenderPass
static readonly int ChromaticRed = Shader.PropertyToID("_ChromaticRed");
static readonly int ChromaticGreen = Shader.PropertyToID("_ChromaticGreen");
static readonly int ChromaticBlue = Shader.PropertyToID("_ChromaticBlue");

static readonly int GrilleOpacity = Shader.PropertyToID("_GrilleOpacity");
static readonly int GrilleCounterOpacity = Shader.PropertyToID("_GrilleCounterOpacity");
static readonly int GrilleResolution = Shader.PropertyToID("_GrilleResolution");
static readonly int GrilleCounterResolution = Shader.PropertyToID("_GrilleCounterResolution");
static readonly int GrilleBrightness = Shader.PropertyToID("_GrilleBrightness");
static readonly int GrilleUvRotation = Shader.PropertyToID("_GrilleUvRotation");
static readonly int GrilleUvMidPoint = Shader.PropertyToID("_GrilleUvMidPoint");
static readonly int GrilleShift = Shader.PropertyToID("_GrilleShift");

Crt m_Crt;
Material crtMaterial;
Expand All @@ -61,7 +70,7 @@ public CRTPass(RenderPassEvent evt)
var shader = Shader.Find(shaderPath);
if (shader == null)
{
Debug.LogError("Shader not found lol (crt).");
Debug.LogError("Shader not found (crt).");
return;
}

Expand Down Expand Up @@ -133,6 +142,15 @@ void Render(CommandBuffer cmd, ref RenderingData renderingData)
this.crtMaterial.SetVector(ChromaticGreen, this.m_Crt.chromaticGreen.value);
this.crtMaterial.SetVector(ChromaticBlue, this.m_Crt.chromaticBlue.value);

this.crtMaterial.SetFloat(GrilleOpacity, this.m_Crt.grilleOpacity.value);
this.crtMaterial.SetFloat(GrilleCounterOpacity, this.m_Crt.grilleCounterOpacity.value);
this.crtMaterial.SetFloat(GrilleResolution, this.m_Crt.grilleResolution.value);
this.crtMaterial.SetFloat(GrilleCounterResolution, this.m_Crt.grilleCounterResolution.value);
this.crtMaterial.SetFloat(GrilleBrightness, this.m_Crt.grilleBrightness.value);
this.crtMaterial.SetFloat(GrilleUvRotation, this.m_Crt.grilleUvRotation.value);
this.crtMaterial.SetFloat(GrilleUvMidPoint, this.m_Crt.grilleUvMidPoint.value);
this.crtMaterial.SetVector(GrilleShift, this.m_Crt.grilleShift.value);

int shaderPass = 0;
cmd.SetGlobalTexture(MainTexId, source);
cmd.GetTemporaryRT(destination, w, h, 0, FilterMode.Point, RenderTextureFormat.Default);
Expand Down
19 changes: 16 additions & 3 deletions URP-PSX/Assets/Scripts/CRT/Crt.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Unity.Mathematics;
using System;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class Crt : VolumeComponent, IPostProcessComponent
{

public FloatParameter scanlinesWeight = new FloatParameter(1f);
public FloatParameter noiseWeight = new FloatParameter(1f);

public FloatParameter screenBendX = new FloatParameter(1000.0f);
public FloatParameter screenBendY = new FloatParameter(1000.0f);
public FloatParameter vignetteAmount = new FloatParameter(0.0f);
Expand All @@ -19,10 +19,23 @@ public class Crt : VolumeComponent, IPostProcessComponent
public FloatParameter scanlinesDensity = new FloatParameter(200.0f);
public FloatParameter scanlinesSpeed = new FloatParameter(-10.0f);
public FloatParameter noiseAmount = new FloatParameter(250.0f);

public Vector2Parameter chromaticRed = new Vector2Parameter(new Vector2());
public Vector2Parameter chromaticGreen = new Vector2Parameter(new Vector2());
public Vector2Parameter chromaticBlue = new Vector2Parameter(new Vector2());

// Grille Effect is a modified version of the shader from here:
// https://godotshaders.com/shader/vhs-and-crt-monitor-effect/
public FloatParameter grilleOpacity = new FloatParameter(0.4f);
public FloatParameter grilleCounterOpacity = new FloatParameter(0.2f);
public FloatParameter grilleResolution = new FloatParameter(360.0f);
public FloatParameter grilleCounterResolution = new FloatParameter(540.0f);
public FloatParameter grilleUvRotation = new FloatParameter((float)(90.0f));
public FloatParameter grilleBrightness = new FloatParameter(15.0f);
public FloatParameter grilleUvMidPoint = new FloatParameter(0.5f);
public Vector3Parameter grilleShift = new Vector3Parameter(new Vector3(1.0f, 1.0f, 1.0f));


//INTERFACE REQUIREMENT
public bool IsActive() => true;
public bool IsTileCompatible() => false;
Expand Down
Loading

0 comments on commit 11752b7

Please sign in to comment.