- Home /
Manipulate Texture UVs with Shader
How can we manipulate a texture's UVs from a shader?
I've taken a look at the Shader Reference, but am still quite new to shaders and would like to be pointed in the right direction. Edit: Your help is greatly appreciated, sorry if the question was too vague.
Answer by ScroodgeM · Sep 07, 2012 at 12:56 PM
which kind of shader are you asking about?
for example, for surface shaders, you can modify IN.uv_TexName parameter.
this is usual line to read rgb value from tex to model surface:
o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
this will swap X and Y of texture's UV:
o.Albedo = tex2D(_MainTex, float2(IN.uv_MainTex.y, IN.uv_MainTex.x)).rgb;
What I'm trying to do is manipulate the UVs of a texture that will be drawn OnGUI with Graphics.DrawTexture and a material with a custom shader. Thanks for the information, I think I'll have to do some more reading on shaders to get a better grasp of how they work.
why don't you want to change texture mapping by changing it's offset and tiling in material? if you have only one object that draws with some material you can use these settings ins$$anonymous$$d of creating own shader
That's a good point, but the effect I want to achieve is more than just offsetting or tiling. Say for example you wanted to animate a texture's UVs to produce different distortion effects.
Surface shaders are for pleebs. We want to know how to do this with a vert and frag shader setup. ;)
Answer by Markus_T · Feb 18, 2020 at 11:26 AM
It might be easier than you think. You really just need to be creative with even simple vertex/fragment shaders. If you look at the link below, then whenever you see
o.uv =...
you're really just dealing with a vector2. Manipulate the values of that vector in creative ways (translate them, multiply them, swap them) and you'll see you get interesting results.
https://docs.unity3d.com/Manual/SL-VertexFragmentShaderExamples.html
For example
// make your texture appear half as big
o.uv = v.uv * 2;
// use your object's vertices as UV data
o.uv = mul(unity_ObjectToWorld, v.vertex).xz;
// creates a weird tartan pattern
float bentU = sin(v.uv.x + v.vertex.x * 2);
float bentV = sin(v.uv.y + v.vertex.z * 3);
o.uv = float2(bentU, bentV);
So, just be creative with your math!
Your answer
Follow this Question
Related Questions
Blending between two parts of a texture atlas, using another texture atlas as an Alpha Channel 0 Answers
Getting projector shader to conform to UVs 0 Answers
How to edit each texture of a cubemap in a shader? 0 Answers
How do I set up eye pupil specific UV animation in Unity? 2 Answers
What's the Unity shader equivalent of gl_FragCoord? 0 Answers