Changing material parameters on the fly

Problems building or running the engine, queries about how to use features etc.
Post Reply
jtsagata
Gnoblar
Posts: 3
Joined: Sun Feb 24, 2013 8:13 pm
x 1

Changing material parameters on the fly

Post 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]
paroj
OGRE Team Member
OGRE Team Member
Posts: 1995
Joined: Sun Mar 30, 2014 2:51 pm
x 1075
Contact:

Re: Changing material parameters on the fly

Post by paroj »

with GL3+ (RTSS), you must use getBestTechnique() instead of getTechnique(0)
jtsagata
Gnoblar
Posts: 3
Joined: Sun Feb 24, 2013 8:13 pm
x 1

Re: Changing material parameters on the fly

Post 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);
  }
paroj
OGRE Team Member
OGRE Team Member
Posts: 1995
Joined: Sun Mar 30, 2014 2:51 pm
x 1075
Contact:

Re: Changing material parameters on the fly

Post by paroj »

setTextureName
Post Reply