You shouldn't use Pass::createTextureUnitState() here as it creates a new TextureUnitState every time it's called. Actually, if you set up the material once (e.g. through a script), all you would have to do in Fade() would be setting the alpha component of the diffuse colour as you do now. I.e.:
Code: Select all
for(int i = 0; i < object->getNumSubEntities(); ++i)
Pass *pass = object->getSubEntity(i)->getMaterial()->getTechnique(0)->getPass(0);
pass->setDiffuse(pass->getDiffuse().r, pass->getDiffuse().g, pass->getDiffuse().b, alpha);
Of course this requires, that your script contains the other options initially. For performance it may be a good idea to enable/disable scene blending on demand, though.
Also note, that if you have more entities using this modified material, they'll change too. So this will probably only work as expected, if the object you're fading has a unique material. There are ways around this, e.g. dynamically creating a copy of the material when fading is needed for an object, and removing it again when it's not needed anymore.