Page 1 of 1

Changing material parameters on the fly

Posted: Tue Apr 02, 2019 9:51 am
by jtsagata
Hello, i'am trying to change some materials parameters on the fly.
I like to change a texture image parameters and colour values. This is what i got so far

Image

I can change the viewport colour, but didn't manage to change the sphere colour.

Code: Select all

    if (!ImGui::CollapsingHeader("Ambient")) {
        auto ent = app.mSphere.object->getSubEntity(0);
        auto mat = ent->getMaterial();

        auto bgColor = mat->getTechnique(0)->getPass(0)->getAmbient();

        static auto color_ref2 = ImVec4(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
        static ImVec4 color = color_ref2;

        int flags = 0 | ImGuiColorEditFlags_NoLabel;
        ImGui::ColorPicker4("AbColor", (float *)&color, flags, &color_ref2.x);

        auto newColor = Ogre::ColourValue(color.x, color.y, color.z, color.w);
        mat->getTechnique(0)->getPass(0)->setAmbient(newColor);
        ent->setMaterial(mat);
    }

I have also try to clone the material with no luck. This will generate tons of unused materials. How can i get rid of them;

Code: Select all

        static auto nameGen = Ogre::NameGenerator("SphereMaterial");
        auto newmatName = nameGen.generate();
        auto mat2 = ent->getMaterial()->clone(newmatName);
        auto newColor = Ogre::ColourValue(color.x, color.y, color.z, color.w);
        mat2->getTechnique(0)->getPass(0)->setAmbient(newColor);
        ent->setMaterialName(newmatName);
Thanks
Ogre Version: 1.11.5
Operating System: Linux
Render System: OpenGL[3]

Re: Changing material parameters on the fly

Posted: Tue Apr 02, 2019 2:52 pm
by paroj
with GL3+ (RTSS), you must use getBestTechnique() instead of getTechnique(0)

Re: Changing material parameters on the fly

Posted: Mon Apr 08, 2019 9:43 am
by jtsagata
Thanks that works. But does not work with textures.

For example in BasicTutorial2.cpp i made these changes

Code: Select all

  //! [planesetmat]
  MaterialPtr material =
      MaterialManager::getSingleton().create("MyBackground", "General");
  material->getTechnique(0)->getPass(0)->createTextureUnitState("rockwall.tga");
  groundEntity->setMaterialName("MyBackground");
  //! [planesetmat]
And then i try to change some texture parameters

Code: Select all

  if (evt.keysym.sym == '1') {
    auto material = groundEntity->getSubEntity(0)->getMaterial();
    auto pass = material->getBestTechnique(0)->getPass(0);
    auto state = pass->getTextureUnitState(0);
    state->setTextureRotate(Degree(45));
    state->setTextureUScale(0.5);
    // How ?
    // state->setTextureFilename("leaf.png");
    groundEntity->setMaterial(material);
  }

Re: Changing material parameters on the fly

Posted: Mon Apr 08, 2019 11:31 pm
by paroj
setTextureName