- Home /
Offset textures individually into specific world directions
I am new to Unity and experimenting a bit with shaders, but now I'm stuck. So I hope you nice people can help me out.
My Scene has a simple cube with a texture assigned to it. So what I am doing is changing the offset of the texture over time, so the texture is moving down (easy, I know). But in that situation I do NOT want the top and bottom texture of the cube to move, only those on the side which are affected by lets say "world gravity", that drags them down. Is it possible to make some textures at the sides move and some (at top and bottom) not?
This even gets more complicated, when the cube is being rotated. The "gravity" effect would need to change for the textures, so they are being dragged into the right (world) direction (that means still down). (On the other hand, I don't want the textures to rotate with the object, but that's far less important to me at the moment. But any hint is also appreciated.) I thought it would be better to illustrate what I mean.
So I have no idea how to make the texture behave different for each side of the cube. Any help is appreciated!
Answer by Dolgsthrasir · Jun 08, 2012 at 03:53 PM
OK I think I've got it, at least it looks correct. Was even easier than I thought.
float3 dir = normalize(mul( _World2Object, float4(0, 1, 0, 0) ).xyz);
temp.xyzw = v.vertex.xyzw + Offset * float4(dir,0);
Answer by Paulius-Liekis · Jun 06, 2012 at 05:58 PM
It is posible to do it in several ways:
First: simply keep a single "offset" vector and then project that offset on each plane in the vertex shader. Simply taking normal of each vertex into account will give you an offset of UV on the vertex. It is easy to implement, but it might be nor exactly what you want, because each plane will be unable to keep it's ofset once the object rotates.
Second: put different materials on each side and calculate texture offset in the script. Offset of each side is calcualted in the same way. This will allow you to keep offset on each side.
I think, at the moment, the first solution would be sufficient, since the object won't rotate over time, but the rotation is only performed by hand in the editor. I also thought about taking normals into account, but it still sounds difficult to a beginner like me :). Is there some tutorial on how to get the normals of each vertex in unity's shaders? I was searching for something like that, but couldn't find anything that was any helpful for my problem.
A material for each side would work for cubes, but later I want to transfer the shader to other structures as well. So I think it's better for me to try the first suggestion.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
renderer.material.mainTextureOffset based on player movement 0 Answers
Help with understanding UV's in script created mesh 0 Answers
How to hide a part of a GUITexture that rotate ? 1 Answer
Is using texture offset with displacement shader possible? 0 Answers