I know that I can modify UV mapping in script by modifying mesh.uv. However, how can I do it in Shader?
The gray part in the screenshot is transparent.
In my shader, I want to slice this texture into 9 pieces, and only render one of them each time under certain conditions. In script, for each rectangle, i can do
mesh.uv = Vector2[ ] topLeftCellUV {
new Vector2 (0f, 2f/3f), //bottom left
new Vector2 (oneThird, 2f/3f), //bottom right
new Vector2 (0f, 1f), //top left
new Vector2(1f/3f, 1f) //top right
};
But i have no idea how do to it in shader.
The feature i’m trying to implement is using projector to highlight my grid (not the whole grid, just the grid under selected objects).
This is the shader i’m referring to:
Shader "Projector/AdditiveTint" {
Properties {
_Color ("Tint Color", Color) = (1,1,1,1)
_Attenuation ("Falloff", Range(0.0, 1.0)) = 1.0
_ShadowTex ("Cookie", 2D) = "gray" {}
}
Subshader {
Tags {"Queue"="Transparent"}
Pass {
ZWrite Off
ColorMask RGB
Blend SrcAlpha One // Additive blending
Offset -1, -1
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 uvShadow : TEXCOORD0;
float4 pos : SV_POSITION;
};
float4x4 unity_Projector;
float4x4 unity_ProjectorClip;
v2f vert (float4 vertex : POSITION)
{
v2f o;
o.pos = UnityObjectToClipPos (vertex);
o.uvShadow = mul (unity_Projector, vertex);
return o;
}
sampler2D _ShadowTex;
fixed4 _Color;
float _Attenuation;
fixed4 frag (v2f i) : SV_Target
{
// Apply tint & alpha mask
fixed4 texCookie = tex2Dproj (_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
fixed4 outColor = _Color * texCookie.a;
// Distance attenuation
float depth = i.uvShadow.z; // [-1(near), 1(far)]
return outColor * clamp(1.0 - abs(depth) + _Attenuation, 0.0, 1.0);
}
ENDCG
}
}
Thanks!!!
