void LIRenAttachmentEntity::replace_texture_now (const Ogre::String& name, Ogre::TexturePtr& texture)
{
	if (mesh.isNull () || entity == NULL)
		return;

	// Translate the name if already replaced before.
	std::map<Ogre::String, Ogre::String>::const_iterator iter;
	iter = applied_texture_replaces.find(name);
	Ogre::String real_name;
	if (iter != applied_texture_replaces.end())
		real_name = iter->second;
	else
		real_name = name;

	// Save the replaced name for future translations.
	applied_texture_replaces[name] = texture->getName ();

	// Replace in each submesh.
	for (size_t subent_idx = 0 ; subent_idx < entity->getNumSubEntities () ; ++subent_idx)
	{
		// Get the material of the subent.
		Ogre::SubEntity* subent = entity->getSubEntity (subent_idx);
		Ogre::MaterialPtr submat = subent->getMaterial ();
		if (submat.isNull ())
			continue;

		// Check if there are replaceable textures.
		if (!render->material_utils->has_overridable_texture (submat, real_name))
			continue;

		// Create a modified version of the material.
		Ogre::String new_name = render->id.next ();
		Ogre::MaterialPtr material = submat->clone (new_name, true, LIREN_RESOURCES_TEMPORARY);
		render->material_utils->replace_texture (material, real_name, texture->getName ());
		subent->setMaterial (material);
	}
}