Page 1 of 1

Material creation during runtime

Posted: Thu Apr 22, 2021 3:47 pm
by dragostej
Ogre Version: :?: 1.11.6
Operating System: :?: Win 10
Render System: :?: Directx 11

Hello guys,

Recently i have ported my app from DirectX9 to DirectX11, where the RTSS is new for me and i ran into some issues.
The RTSS works fine but some time i got errors.

Before the porting i used the following code, to create and edit materials during runtime:

Code: Select all

Ogre::MaterialPtr origMat = Ogre::MaterialManager::getSingletonPtr()->getByName( fullName );
Ogre::MaterialPtr copyMat = Ogre::MaterialManager::getSingletonPtr()->createOrRetrieve( copyName, group ).first.staticCast<Ogre::Material>();

But actually after the porting a always got crash when i want to render objects with copyMat materials in void ShaderGenerator::SGTechnique::buildTargetRenderState() function in that call: mat->getNumTechniques();

I could figure out that it was because of copyDetailsTo function. But i dont know the exact reason.
I solve the problem by the following code:

Code: Select all

Ogre::MaterialPtr copyMat = Ogre::MaterialManager::getSingletonPtr()->getByName(copyName);
	if (copyMat.isNull())
		copyMat = origMat->clone(copyName, true, group);
		Ogre::MaterialManager::getSingletonPtr()->remove(copyName, group);
		copyMat = origMat->clone(copyName, true, group);
Could you guys tell me what is the exact problem of the crash described above, and what are the possible solutions? My solution could work?

Re: Material creation during runtime

Posted: Thu Apr 22, 2021 4:51 pm
by paroj
you probably need to also use this: ... 72ae2aa1e3

Re: Material creation during runtime

Posted: Fri Apr 23, 2021 8:45 pm
by dragostej
Thank you some much!

Cloning materials works fine, but in some parts of my code, i cant use cloning because i want to edit my materials without deleting the existing one.
I could figure out that in my original code

Code: Select all

copyDetailsTo function is copying technique(s) from origMat to copyMat, but actually it is deleting everything from the destination material first, thus it is deleting RTSS generated codes from copyMat, i think thats why i got error.

How can i preserve the original state of copyMat and just "update" values from origMat to copyMat.
Is there any idea for that?

One more thing, can i think right, that if i using RTSS i have to call ShaderGenerator::invalidateMaterial() function always if i edit material during runtime?