I need to separate the UV to grids and draw a texture in each grid.
It looks like using tilling and offset to make a texture repeat. But I want to make the texture scale in its grid.
Shader "Custom/NewSurfaceShader" {
Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Scale ("Scale", Range(0,5)) = 1.0
_SegmentX("Segment x", Float )=5
_SegmentY("Segment y", Float )=5
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
float _SegmentX;
float _SegmentY;
float _Scale;
void surf (Input IN, inout SurfaceOutputStandard o) {
float2 _Diameter=fixed2(1/ceil(_SegmentX),1/ceil(_SegmentY));
float2 center = float2(_Diameter.x*floor(IN.uv_MainTex.x/_Diameter.x), _Diameter.y*floor(IN.uv_MainTex.y/_Diameter.y));
center+=_Diameter/2;
float2 _rd=_Scale*_Diameter/2;
fixed2 uvMask=fixed2((IN.uv_MainTex.x-center.x),(IN.uv_MainTex.y-center.y));
uvMask=(uvMask+_rd)/(2*_rd);
uvMask=saturate(uvMask);
o.Albedo = tex2D (_MainTex, uvMask);
}
ENDCG
}
FallBack "Diffuse"
}
when I change the scale, a strange line appears at the edge of grids, which the value of uvmask jumps from 1 to 0. The lines are more clear when the scale is less than 1. It works well when drawing colors in each grid.
The only way I found to remove that line is to mirror the UV in every other grid.
fixed2 uvMask=fixed2((IN.uv_MainTex.x-center.x),(IN.uv_MainTex.y-center.y));
fixed2 indexXY=fixed2(floor(IN.uv_MainTex.x/_Diameter.x),floor(IN.uv_MainTex.y/_Diameter.y));
fixed2 dir=1-step(fmod(indexXY,2),0.1)*2;
uvMask*=dir;
uvMask=(uvMask+_rd)/(2*_rd);
Have you guys get some other ideas?
Does a fragment shader really run in each pixel?