Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add curvature to CRT shader effect #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Assets/SimpleCRTShader/Editor/CRTPostEffecterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public override void OnInspectorGUI()
CRTPostEffecter effect = target as CRTPostEffecter;
effect.material = (Material)EditorGUILayout.ObjectField("Effect Material", effect.material, typeof(Material), false);

effect.curvatureScale = EditorGUILayout.FloatField("Curvature", effect.curvatureScale);

using (new HorizontalScope(GUI.skin.box))
{
effect.whiteNoiseFrequency = EditorGUILayout.IntField("White Noise Freaquency (x/1000)", effect.whiteNoiseFrequency);
Expand Down
5 changes: 5 additions & 0 deletions Assets/SimpleCRTShader/Script/CRTPostEffecter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
public class CRTPostEffecter : MonoBehaviour
{
public Material material;
public float curvatureScale = -0.1f;
public int whiteNoiseFrequency = 1;
public float whiteNoiseLength = 0.1f;
private float whiteNoiseTimeLeft;
Expand Down Expand Up @@ -54,6 +55,7 @@ public enum LeterBoxType
public Vector2Int resolutions;

#region Properties in shader
private int _CurvatureScale;
private int _WhiteNoiseOnOff;
private int _ScanlineOnOff;
private int _MonochormeOnOff;
Expand Down Expand Up @@ -83,6 +85,7 @@ public enum LeterBoxType

private void Start()
{
_CurvatureScale = Shader.PropertyToID("_CurvatureScale");
_WhiteNoiseOnOff = Shader.PropertyToID("_WhiteNoiseOnOff");
_ScanlineOnOff = Shader.PropertyToID("_ScanlineOnOff");
_MonochormeOnOff = Shader.PropertyToID("_MonochormeOnOff");
Expand Down Expand Up @@ -127,6 +130,8 @@ private void OnRenderImage(RenderTexture src, RenderTexture dest)
}
//////

material.SetFloat(_CurvatureScale, curvatureScale);

material.SetInteger(_LetterBoxOnOff, isLetterBox ? 0 : 1);
//material.SetInteger(_LetterBoxEdgeBlurOnOff, isLetterBoxEdgeBlur ? 0 : 1);
material.SetInteger(_LetterBoxType, (int)letterBoxType);
Expand Down
8 changes: 6 additions & 2 deletions Assets/SimpleCRTShader/material/CRT.mat
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
serializedVersion: 8
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: CRT
m_Shader: {fileID: 4800000, guid: fd1d99cddfb7f5644bbd3d844f92ab11, type: 3}
m_ShaderKeywords:
m_Parent: {fileID: 0}
m_ModifiedSerializedProperties: 0
m_ValidKeywords: []
m_InvalidKeywords: []
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_LockedProperties:
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
Expand Down
19 changes: 17 additions & 2 deletions Assets/SimpleCRTShader/shader/CRT.shader
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Shader "Simple CRT"
float4 _MainTex_ST;
float4 _MainTex_TexelSize;

float _CurvatureScale;

int _WhiteNoiseOnOff;
int _ScanlineOnOff;
int _MonochormeOnOff;
Expand Down Expand Up @@ -75,7 +77,16 @@ Shader "Simple CRT"
float GetRandom(float x);
float EaseIn(float t0, float t1, float t);

v2f vert (appdata v)
float2 Distort(float2 uv)
{
float2 center = float2(0.5, 0.5);
uv = uv - center;
float len = length(uv);
uv = uv / (1.0 + len * _CurvatureScale);
return uv * (1 + (_CurvatureScale * 0.5)) + center;
}

v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
Expand All @@ -86,7 +97,11 @@ Shader "Simple CRT"

fixed4 frag (v2f i) : SV_Target
{
float2 uv = i.uv;
float2 uv = Distort(i.uv);
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0)
{
return fixed4(0.0, 0.0, 0.0, 1.0); // Return black color for outbound pixels
}

/////Jump noise
uv.y = frac(uv.y + _ScreenJumpLevel);
Expand Down