- Home /
Offset texture based on world space
I'm looking for a way to make an object's texture offset when I move it in the scene. Let's say I have a cube, a 1024x1024 texture atlas divided in 4 equal parts with 4 different colors (red, green, blue, and yellow; just as an example), and the cube's UV will cover one of those 4 sides, so it will show one of the four colors. How do I make the texture continuously offset while I move the cube in my scene, so that everytime I move it, it will show one of the colors on the texture atlas? And if I keep moving it, it will keep scrolling through the four colors, over and over again.
I was thinking of having the texture offset half way in a direction so it will show one of the other colors, but I don't know how to make that happen while the cube's position changes in my scene. Can someone point me in the right direction?
Thank you.
Do you want to do it with 3D object (Cube) or a plane? If it's Cube, it will be harder. I'd like to give it a try, but i just want to be sure that you want to do it with cube exactly??
I actually want to be able to do it with any kind of 3D model I have. So it doesn't matter if it's a cube, a wall, or a car. As long as I've got an unwrapped 3D model and a specific texture atlas for that model, it should work. i didn't find anything on google about changing textures, offset, or changing any kind of values while the object is moving in world space.
Answer by pslattery · Aug 29, 2017 at 01:12 AM
If you're using the built in Standard Shader (I'm sure some of the other shaders do this too, but I'm not in front of Unity and can't check) you can change the Offset property to move the texture.
Basically,
Store your current position as the last position you have 'stepped' the texture offset at
Every update, check to see if you've moved more than some threshold, in x and y each. If you have:
Update the objects material property Offset x or y (corresponding to which direction you moved in) to equal its current value + .5 (the distance across half the texture in UV space).
Hope this helps!
Isn't that going to work only at runtime? I want that continuous offset to happen while moving the object in your viewport and update in realtime, without needing to hit play. The effect should be happening immediatly as you drag and drop your model from the project to the scene and start moving it around (assu$$anonymous$$g it's got the script attached).