Reputation: 39194
I'm writing Cg
shaders using Unity3D
.
I'm trying to use fmod function to repeat a texture along an axis (basically the same effect I can achieve by setting the texture scale in Material
with TextureWrapMode.Repeat
).
This is the fragment shader
code that can reproduce the error:
half4 frag (v2f i) : COLOR
{
float u_sample_coord = fmod(i.uv.x ,period) /period;
half4 col =tex2D(myTexture,float2(u_sample_coord, i.uv.y));
return col;
}
Basically it seems to work but it produces some sort of aliasing (a strip pattern) in correspondance with 0's of fmod function. Here's some screenshot:
The image above shows how texture repeats correctly. Here's a zoom on the strip aliasing pattern emerged:
I tried to debug it but I can't figure out what's going on exactly. Anyone could tell me what's the problem and eventually how to solve it?
EDIT: I found out that disabling mipmap generation solve this problem. Btw I'd like to use mipmap to avoid minification aliasing problem while the distance increase. Anyone has any idea?
Upvotes: 1
Views: 2875
Reputation: 3305
You'll need to explicitly set the u and v derivatives in the tex2D() function. But: why are you using fmod? If the sampler's repeat mode is set to wrap, then you can let the UV coordinates roam far beyond the 0-1 range. It will wrap by itself.
Upvotes: 2