Hi! I’m a student and have been working on learning shaders and at the same time build a small library of cool shaders to use in different projects. A shader that I think it would be a good start is the pixelation effect some 3D games have (ex sokpop minigames), after finishing up the pixelation shader I saw that it was giving me half colours and in-betweens, which I don’t want. After that I made a nearest Neighbour scaling shader so it would fix the colour picking after the texture got squashed down from the first shader.
My problem now is: whenever I try to use both shaders at the same time, either one of them is overwritten by the other, or the output is a black screen to the main camera.
Here is my shaders:
Shader "Hidden/PixelShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
int _PixelDensity;
float2 _AspectRatioMultiplier;
fixed4 frag (v2f i) : SV_Target
{
float2 pixelScaling = _PixelDensity * _AspectRatioMultiplier;
i.uv = round(i.uv *pixelScaling) / pixelScaling;
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}
Shader "Hidden/PerfectPixel"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float4 _MainTex_TexelSize;
struct appdata
{
float4 vertex : POSITION;
fixed4 col : COLOR;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
fixed4 col : COLOR;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv * _MainTex_TexelSize.zw;
o.col = v.col;
return o;
}
sampler2D _MainTex;
float texelsPerPixel;
fixed4 frag (v2f i) : SV_Target
{
float2 locationWithinTexel = frac(i.uv);
float2 interpolationAmount = clamp(locationWithinTexel / texelsPerPixel, 0, .5)
+ clamp((locationWithinTexel - 1) / texelsPerPixel + .5, 0, .5);
float2 finalTextureCoords = (floor(i.uv) + interpolationAmount) / _MainTex_TexelSize.zw;
return tex2D(_MainTex, finalTextureCoords) * i.col;
}
ENDCG
}
}
}
And here is the script I made for the main camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class PixelEffect : MonoBehaviour
{
private Material pixelMaterial = null;
private Material _pixelPefectMat = null;
private float _texelsPerPixel;
private Camera _mainCamera;
[SerializeField] private int pixelDensity = 80;
void SetMaterial()
{
pixelMaterial = new Material(Shader.Find("Hidden/PixelShader"));
_pixelPefectMat = new Material(Shader.Find("Hidden/PerfectPixel"));
}
void OnEnable()
{
SetMaterial();
}
void OnDisable()
{
_pixelPefectMat = null;
pixelMaterial = null;
}
private void Start()
{
_mainCamera = GetComponent<Camera>();
float nativeAspectRatio = _mainCamera.pixelRect.x / _mainCamera.pixelRect.y;
float aspectRatio = Screen.width / Screen.height;
if (nativeAspectRatio > aspectRatio)
_texelsPerPixel = _mainCamera.pixelRect.y / Screen.height;
else
_texelsPerPixel = _mainCamera.pixelRect.x / Screen.width;
_pixelPefectMat.SetFloat("texelsPerPixel", _texelsPerPixel);
}
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (pixelMaterial == null)
{
Graphics.Blit(source, destination);
return;
}
Vector2 aspectRatioData;
if (Screen.height > Screen.width)
aspectRatioData = new Vector2((float)Screen.width / Screen.height, 1);
else
aspectRatioData = new Vector2(1, (float)Screen.height / Screen.width);
pixelMaterial.SetVector("_AspectRatioMultiplier", aspectRatioData);
pixelMaterial.SetInt("_PixelDensity", pixelDensity);
Graphics.Blit(source, pixelMaterial);
RenderTexture renderTemp = RenderTexture.GetTemporary(source.descriptor);
_pixelPefectMat.SetTexture("_MainTex", renderTemp);
Graphics.Blit(renderTemp, destination, _pixelPefectMat);
RenderTexture.ReleaseTemporary(renderTemp);
}
}
I almost sure the problem is in my camera script and not in any of the shaders, but you can never be too sure.
Thank’s for the help in advance!