- Home /
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:
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:
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:
CGPROGRAM
#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;
}
ENDCG
Thanks for any feedback!
Yes and no. I am laso trying to lower the resolution of the shadow so it has a 'pixely' look, but I'm not using any directional lights to calculate shadows. Actually, looking at your post, I am calculating light the same way $$anonymous$$inecraft does. Heh.
The heart of the issue is that there are many light values per one pixel grid square. So, the question is, how does one average out the light in that one grid cell? And to make it even more difficult, how do you do that from the shader? :O From what I understand of shaders (not a lot), you are unaware of neighboring pixel values, so how can you average them out?
Answer by SunnyChow · Jan 20, 2017 at 02:59 AM
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
Edit
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
$$anonymous$$y apologies for that.
LIGHT_ATTENUATION uses a lookup texture, right? If so, I'd prefer not to use lookup textures because I'm changing the light so frequently. It's much quicker for me to change the uv2.y value when generating my mesh rather than generate textures.
Do you know if it's possible to look up the uv values at specific vertices from within the frag function? If so, I believe I could simply use that to manually interpolate my light values.
if it's a real time shadow, the shadow texture is already being generated in every frame.