Is recomended to use SetTextureOffset to create a background scrolling effect?

I have a big background ( 2048 X 1440 ) and I want to do a background scrolling effect for my game.

I can do 2 things:

  1. place the background in the scene and a copy of the same background next it, and move them creating the scrolling effect. when the first one leaves the left side of the camera, it appears next to the copy at the far right of the camera. And this goes on and on.

  2. Or I can use the SetTextureOffset with a certain offset speed and thats all.

I would like to reduce the work, but my concern is related to the speed of this solution … my image is kind of big.

Somebody knows if this is a viable option?

thanks in advance.

1 Answer

1

SetTextureOffset is plenty fast. The two copy method might give a tiny gap/blur where they meet (which might show up only certain places when they move.)

The graphics card keeps one copy of the texture in memory - it never scrolls or moves that around. Instead, it loves to look up the pixels, and will gladly look up partial areas, upside-down, wrapping around edges… . To it, every pixel lookup is the same. If you call it with pixelOffset 0.1 during a frame, then 0.11 the next, it won’t even notice you changed by 0.01. It didn’t “set up” the texture for a 0.1 lookup, or anything like that.

Note that offsets go from 0 to 1 over the texture and go backwards. So offset += Time.deltaTime*0.2f; will have the texture scroll R to L once/5 seconds.

This is kind of out question but ... how do you know that offset += Time.deltaTime*0.2f; will take 5 seconds to complete one cycle?

Take your wall and manually change the offset to 0.1, 0.2, 0.3 ... up to 1 (this is one of the few things you can't slide with the mouse.) You'll see it perform a full scroll as you go from 0 to 1 (unless you have an odd unwrap.) So, say you want a 5-second scroll time, need to add 0.2/second. Using Time.deltaTime*0.2f in Update is a trick to make it change by 0.2/second. It works for anything being changed in Update. Also, offset will happily be more than 1, or negative. Type in 37.1, 37.2 ... or -0.9, -0.8 ... and see.

Note that some platforms (such as iOS) get increasingly bad results as the UVs get too far away from the 0..1 range, so it's often better to do offset = (offset + Time.deltaTime*0.2f)%1.

Thanks people, your help is greatly appreciated