| 1419 | } |
| 1420 | |
| 1421 | std::string RenderManager::setMaterialOpacity(const std::string& materialName, float opacity) |
| 1422 | { |
| 1423 | if (opacity < 0.0f || opacity > 1.0f) |
| 1424 | return materialName; |
| 1425 | |
| 1426 | std::stringstream newMaterialName; |
| 1427 | newMaterialName.str(""); |
| 1428 | |
| 1429 | // Check whether the material name has alreay got an _alpha_ suffix and remove it. |
| 1430 | size_t alphaPos = materialName.find("_alpha_"); |
| 1431 | // Create the material name accordingly. |
| 1432 | if (alphaPos == std::string::npos) |
| 1433 | newMaterialName << materialName; |
| 1434 | else |
| 1435 | newMaterialName << materialName.substr(0, alphaPos); |
| 1436 | |
| 1437 | // Only precise the opactiy when its useful, otherwise give the original material name. |
| 1438 | if (opacity != 1.0f) |
| 1439 | newMaterialName << "_alpha_" << static_cast<int>(opacity * 255.0f); |
| 1440 | |
| 1441 | Ogre::MaterialPtr requestedMaterial = Ogre::MaterialManager::getSingleton().getByName(newMaterialName.str()); |
| 1442 | |
| 1443 | // If this texture has been copied and colourized, we can return |
| 1444 | if (!requestedMaterial.isNull()) |
| 1445 | return newMaterialName.str(); |
| 1446 | |
| 1447 | // If not yet, then do so |
| 1448 | Ogre::MaterialPtr oldMaterial = Ogre::MaterialManager::getSingleton().getByName(materialName); |
| 1449 | //std::cout << "\nMaterial does not exist, creating a new one."; |
| 1450 | Ogre::MaterialPtr newMaterial = oldMaterial->clone(newMaterialName.str()); |
| 1451 | bool cloned = mShaderGenerator->cloneShaderBasedTechniques(oldMaterial->getName(), oldMaterial->getGroup(), |
| 1452 | newMaterial->getName(), newMaterial->getGroup()); |
| 1453 | if(!cloned) |
| 1454 | { |
| 1455 | OD_LOG_ERR("Failed to clone rtss for material: " + materialName); |
| 1456 | } |
| 1457 | |
| 1458 | // Loop over the techniques for the new material |
| 1459 | for (auto i = 0; i < newMaterial->getNumTechniques(); ++i) |
| 1460 | { |
| 1461 | Ogre::Technique* technique = newMaterial->getTechnique(i); |
| 1462 | for(auto j = 0; j < technique->getNumPasses(); ++j) |
| 1463 | { |
| 1464 | // Set alpha value for all passes |
| 1465 | Ogre::Pass* pass = technique->getPass(j); |
| 1466 | Ogre::ColourValue color = pass->getEmissive(); |
| 1467 | color.a = opacity; |
| 1468 | pass->setEmissive(color); |
| 1469 | |
| 1470 | color = pass->getSpecular(); |
| 1471 | color.a = opacity; |
| 1472 | pass->setSpecular(color); |
| 1473 | |
| 1474 | color = pass->getAmbient(); |
| 1475 | color.a = opacity; |
| 1476 | pass->setAmbient(color); |
| 1477 | |
| 1478 | color = pass->getDiffuse(); |