How can I make shadows pixelated in fragment function?

Hello! I’m pretty novice when it comes to graphics programming, so I’d love some advice on the best way to solve this problem. Let me give you some context.

Take a look at this image:

Alt Text

The above image represents how I light a square mesh. The values at the corners of the square represent a lighting value. 1 being fully shadowed, 0 being fully lit. Don’t worry about how I get this value. I put this value into the uv2 channel of the mesh component. So, the bottom left vertex value gets stored as (0.0, 1.0f). I’m not using the x, just y in this case.

On to the shader:

Okay, so, I’m currently using this value in my frag function as a light multiplier. uv *= uv2.y (Remember, I’m only using the y-axis from uv2, it’s the light value!) This creates a nice light blend, as the 1 light value lerps to 0.

Unfortunately, I’m using a pixel art texture that harshly contrasts the lighting gradient. See below:

Alt Text

It may be hard to see, but the pixel art is most apparent at the top right of the image. I want the shadow to have that same look, if possible.

Would it be possible to use the uv coord to snap this light value to a pixel grid?

Here is my current code:

			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

			struct app_data
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float2 uv2 : TEXCOORD1;

			struct v2f
				float4 vertex : SV_POSITION;
				float2 uv : TEXCOORD0;
				float2 uv2 : TEXCOORD1;

			sampler2D _MainTex;
			float4 _MainTex_ST;
			v2f vert (app_data v)
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				o.uv2 = v.uv2;

				return o;
			fixed4 frag (v2f input) : SV_Target
				fixed4 col = tex2D(_MainTex, input.uv);
                col.rgb *= input.uv2.y;
                return col;

Thanks for any feedback!

I am not sure what you want, because i can’t see your image. But to manually render shadow, you need to use LIGHT_ATTENUATION() in AutoLight.cginc. I guess you can make it pixelated by working on the value _ShadowCoord


Ok i think i get what you want. You are not talking about the shadow of unity lighting system. You are talking about some light data baked in to vertex. Try something like this:

 col.rgb *= round(input.uv2.y*4)/4;

it doesn’t have to be 4