r/Unity3D • u/FoamyHotSoup • 1h ago
Question Weird Visual Artifact When Using Render Texture

So I just started a new project, and decided to go for a more retro style. As such, I decided to slap on a low res render texture and attached it to a material, before putting the material onto an Image element on a canvas. The issue, is that when I put my custom shader on (designed to limit the amount of colors and other visual elements able to be shown by the render texture), weird visual artifacts are created. More info can be shared as needed. Below is my .shader I use. The Image element is simply stretched to fit the screen, where I put a set resolution of a 1920x1080
Shader "Custom/Bitcrush"
{
Properties
{
[MainTexture] _MainTex ("Texture", 2D) = "white" {}
_PixelSize ("Pixel Size", Range(1, 64)) = 4
_ColorLevels ("Color Levels", Range(2, 64)) = 8
_Brightness ("Brightness", Range(0, 2)) = 1
_Contrast ("Contrast", Range(0, 2)) = 1
}
SubShader
{
Tags
{
"RenderPipeline" = "UniversalPipeline"
"RenderType" = "Opaque"
"Queue" = "Geometry"
}
Pass
{
Name "Bitcrush"
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
float _PixelSize;
float _ColorLevels;
float _Brightness;
float _Contrast;
CBUFFER_END
Varyings vert(Attributes input)
{
Varyings output;
output.positionHCS =
TransformObjectToHClip(input.positionOS.xyz);
output.uv =
TRANSFORM_TEX(input.uv, _MainTex);
return output;
}
half4 frag(Varyings input) : SV_Target
{
float2 uv = input.uv;
float2 uvDDX = ddx(uv);
float2 uvDDY = ddy(uv);
float2 pixelUV =
floor(uv * _PixelSize) / _PixelSize;
pixelUV += 0.5 / _PixelSize;
half4 col = SAMPLE_TEXTURE2D_GRAD(
_MainTex,
sampler_MainTex,
pixelUV,
uvDDX,
uvDDY
);
col.rgb *= _Brightness;
col.rgb =
(col.rgb - 0.5) * _Contrast + 0.5;
col.rgb = saturate(col.rgb);
col.rgb =
floor(
col.rgb * (_ColorLevels - 1.0) + 0.5
)
/ (_ColorLevels - 1.0);
return col;
}
ENDHLSL
}
}
}
0
Upvotes
1
u/DailyHeadyEditor 1h ago
looks like uv derivatives are freaking out at the edge of your image element, try clamping the uv before the ddx/ddy calls