- Home /
Scale and repeat texture without tilling
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?
Answer by hectorux · Nov 08, 2018 at 03:08 PM
It may sound clumsy, but do you have your texture in repeat?, maybe, if it is in clamp it may be tearing it
The strange line exists no matter the texture is repeat or clamp. It seems that the UV of these pixels near the edge lerp from 0 to 1, so I guess the strange line is actually a narrow mirror texture. Is that right? or how can I deal with it :( Really confused.
Your answer
Follow this Question
Related Questions
Seamless Water Material on a Plane using Shader Graph 3 Answers
Shader Outline scale problem since U5 1 Answer
CG shader tile issue 1 Answer
Non scale texture? 1 Answer
How to make a localToWorld matrix for shader parameters? 1 Answer