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!