x


Changing objects texture causes high draw call count!

Part of the game I am working on is avoiding bombs. If you hit a bomb it will "burn" everything around it. I am doing the burn by changing the texture atlas out for a burnt style atlas. The problem is that each time the texture swap happens, it seems to add 3-15 draw calls pending on how many objects are being hit. The bomb instantiates graphic for the explosion. This graphic is what triggers the texture switch. Here is the code on the graphic.

    #pragma strict

var object : GameObject[]; object = GameObject.FindGameObjectsWithTag("pig"); var texture : Texture; var test = false;

function Awake () { //destroy after .1 seconds Destroy(gameObject, .1); test = true;

} //look at the camera function Update(){

transform.LookAt(transform.position + Camera.main.transform.rotation * Vector3.down,Camera.main.transform.rotation * Vector3.back);

if(test == true){ test = false; for (var obj: GameObject in object){ obj.renderer.material.mainTexture = texture; } }

}

With near max objects on screen I am only at 2 draw calls. When I detonate a bomb it jumps to 15 instantly. The draw calls go down as the items destroy themselves after being hit by the bomb.

I cannot figure out why swapping the textures would cause such a problem. Would it be a better idea to have the explosion graphic change a variable on the objects hit by the explosion, then that variable controls the texture change? Then each object hit changes its own texture, instead of one object changing +10 textures.

more ▼

asked Jul 15 '12 at 03:07 PM

gwarsh41 gravatar image

gwarsh41
1 1 1

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Your problem is that as soon as your set the texture you are getting a copy of the material for each of the objects - none of them batch after that as they all have different materials.

I suggest you have a burned material which you set on the objects rather than changing the texture, this material would already have it. Then maybe set the sharedMaterial (though I can't quite remember whether you need to do that).

more ▼

answered Jul 15 '12 at 03:10 PM

whydoidoit gravatar image

whydoidoit
33.1k 12 23 101

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2489
x2210
x472
x414

asked: Jul 15 '12 at 03:07 PM

Seen: 481 times

Last Updated: Jul 15 '12 at 03:10 PM