Material creation during runtime

Problems building or running the engine, queries about how to use features etc.
Post Reply
dragostej
Gnoblar
Posts: 11
Joined: Wed Mar 31, 2021 8:36 am
x 1

Material creation during runtime

Post 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>();

origMat->copyDetailsTo(copyMat);
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);
	else
	{
		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?
paroj
OGRE Team Member
OGRE Team Member
Posts: 1464
Joined: Sun Mar 30, 2014 2:51 pm
x 660
Contact:

Re: Material creation during runtime

Post by paroj »

you probably need to also use this:
https://ogrecave.github.io/ogre/api/lat ... 72ae2aa1e3
dragostej
Gnoblar
Posts: 11
Joined: Wed Mar 31, 2021 8:36 am
x 1

Re: Material creation during runtime

Post 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

origMat->copyDetailsTo(copyMat);
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?
Post Reply