Scrolling textures

Hi,

I am trying to make scrolling texture for fogs, clouds, etc (I am making 2D game). I have made scroll like this:

renderer.material.mainTextureOffset = Vector2(offset, 0);

and put it in Update function. But when I am trying to launch it on iPad, fps drops. Is there any hint to solve my problem?

What drops the framerate? Is the framerate bad if you disable the script?

One thing you should do, if you’re running that every frame, is cache the material.

var material : Material;  //assign in Inspector

material.mainTextureOffset = Vector2(offset, 0);

The most important part is to cache the Renderer. I don’t know if caching the material itself yields a performance benefit on top of that, but I do it anyway to avoid clutter.

also be careful to not allow that offset value to increase to very large numbers. It does work if you just keep adding to your offset value and will auto-wrap but you will find that the textures will start to degrade in detail until they are almost non-existent and a mass of flashing polygons on some iPhone and iPod models. We found in our case that setting the offset to be modulo 16 units was a seamless way to reset the offset at the repeat point but this value would obviously vary for your own scene.

I do this religiously, but when I read this, I checked my texture scrolling script and realised I’d missed it. Thanks for the reminder :smile:

	while (on)
	{
		xOffset = Time.time * animSpeed;
		waterMat.mainTextureOffset = Vector2(xOffset,xOffset);
		if (xOffset > 1) xOffset -= 1; // This prevents inaccuracies when you go over 1.0
		yield;
	}

This is a one off special purpose script, hence why the x and y offset both use the same variable :slight_smile:

Thanks for answers,

I am try to make some hints to increase my FPS. And I got strange result, my fps counter(which I got from Unity wiki) in EMPTY scene, with only main camera shows me 29 frames per seconds on my iPad. Is it ok?

Bump

Reading through the whole iPhone-specific manual is a good idea. Read the beginning of the page “Tuning Main Loop Performance”.

Oh… Thanks, I am dumb :expressionless:

I doubt that, but either way, you’re more knowledgeable now! :slight_smile:

Unity by default is clocked to 30fps isn’t it? Unless you tweak the Application.mm to make it faster :slight_smile:

See! This guy obviously wrote the beginning of that page. :stuck_out_tongue:

Does ‘SetTextureOffset’ break batching?

It depends on how you use it. Renderer.material will, but Renderer.sharedMaterial won’t. Also, if you do it like I did in the first post (which I don’t necessarily recommend*), that won’t break batching either.

Also, keep in mind that Material’s texture offset and scale functions are just easier-to-work-with subsets of what you can do with Material.SetMatrix.

  • Assigning things in the Inspector is the fastest way to start the game, but it’s probably a difference of fractions of milliseconds. It’s probably less hassle to set it up like this at the beginning of the game:
material = GetComponent<Renderer>().sharedMaterial;

I actually almost never “cache” properties like you guys are talking about and I don’t have any script performance issues, even with a lot going on.

I mean, the only overhead is probably just a call to a getter, which then returns a private variable, right?
So yeah, if you do that in a loop, it might cause performance problems, but a few times each frame shouldn’t be that much of a problem.

I do use this technique to avoid having to call GetComponent or similar functions at runtime.

Or am I missing something? (maybe it’s just a UnityScript thing).

That’s exactly what all those shortcuts are, in the variables section. Joachim tells you that here. Be aware that there’s more to what’s going on than just framerate. When we’re talking iPhone, there’s battery to worry about, too. I’d be interested to see numbers for battery life when caching GetComponent calls or not.

It’s not.

Ok, cool.
So if my framerate is now good (I usually let my games run at 50fps), I can probably get it to great (60fps) with this trick. :slight_smile:
Thanks!

That’s also bad for battery. If you can’t get it to 60, cap it at 30. Anything more is just wasted battery.

Hmm, but it looks a lot smoother at 50 than at 30…
I’ll check out that link when I get off work. Thanks!

Perfect solution for me. Thanks a lot.

I believe that the info in the book is old and outdated, I don’t know if anything less than 60fps is actually 30 as I also have a scrolling background etc and it is a lot smoother when I remove the 30fps cap, (i cap it at 60). The FPS readout I have says it drops to 56 or so every so often. I also don’t know how accurate that thing is.

There is a ton of old old iOS info floating around that is really no longer valid. Stuff like how many draw calls, how many tris etc etc. Anything pre iOS 4 and pertaining to less than 3GS is pretty much dead. I have an old 3G sitting in a speaker dock to use as an internet radio.