Making an Object Transparent [0-1] based on input value

Problems building or running the engine, queries about how to use features etc.
Post Reply
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

Making an Object Transparent [0-1] based on input value

Post by Garibalde »

I am looking for a way to change the transparency of an object on the fly. But setting a float between the value [0-1].
Using this value to set the 0 = solid and 1 = not visible and the rest inbetween semi transparent.

I have seen the transparency example I applied the example\waterStream to my object and it is transparent.. However
I cant change the level of transparency. How would one do this?

Thanks
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
Feanor16
Halfling
Posts: 46
Joined: Tue Feb 18, 2014 10:49 pm

Re: Making an Object Transparent [0-1] based on input value

Post by Feanor16 »

you can make your own datablock Material by code then change the alpha dynamically
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

Re: Making an Object Transparent [0-1] based on input value

Post by Garibalde »

I am not sure how to create the material. I looked at the transparency example in the Wiki.

So now I get it, you need to create a material In this case it was "yourMat" and assign it to your entity.
There is a function loadChromaKeyedTexture(). my understanding of this is that given a color it will
make the current texture transparent in areas its below the given threshold.

In my case. I am looking to make the complete object transparent by a value between [0-1] which I would
set at runtime.

1. How to I set the alpha of the material?
2. Do I need to create a material every time I change this transparency value and apply it to the entity? Or is there some other better way?
3. I think transparency adjustment at runtime is usually a common feature? Or am I making things more complicated than they should be?
4. Or is this feature called something else what I am trying to do?

Code: Select all

void createTranparentMaterial()
{
	// Create your material, e.g. like this:
	Ogre::MaterialPtr mmat = Ogre::MaterialManager::getSingleton().create("MyTransparentMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
	mmat->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_REPLACE);
	mmat->getTechnique(0)->getPass(0)->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 128);  [color=#FF4040]? What is this ? can I use this to make the material [0-1] transparent?[/color]
        mmat->getTechnique(0)->getPass(0)->setCullingMode(Ogre::CULL_NONE);
	mmat->getTechnique(0)->getPass(0)->setManualCullingMode(Ogre::MANUAL_CULL_NONE);

	Ogre::TextureUnitState*  t = mmat->getTechnique(0)->getPass(0)->createTextureUnitState(mtexName);
	t->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
}

Ogre::SceneNode* MyNode = mScnMgr->getSceneNode("MyTestObject");
if (MyNode)
{
	mEntity = mScnMgr->getEntity("Left_Lung_Cavity_Ribbed");
	mEntity->setMaterialName("MyTransparentMaterial");
}
Do you have a small example how this can be done on a model?

Thanks
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
paroj
OGRE Team Member
OGRE Team Member
Posts: 1994
Joined: Sun Mar 30, 2014 2:51 pm
x 1074
Contact:

Re: Making an Object Transparent [0-1] based on input value

Post by paroj »

Code: Select all

Ogre::ColourValue c = ..;
c.a = val;
pass->setDiffuse(c);
pass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
note that you might need to use getBestTechnique instead of technique 0 if you are using the RTSS.
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

Re: Making an Object Transparent [0-1] based on input value

Post by Garibalde »

Ok that seems to work.
Here is what I did.

During creating object I created a material for transparency.

Code: Select all

	mmat = Ogre::MaterialManager::getSingleton().create("MyTransparentMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
	mmat->compile();
Then at runtime when I change the value of the alpha I do the following

Code: Select all

			Ogre::Technique* MyMatTech = mmat->getBestTechnique();
			if (MyMatTech == NULL)
			{
				Ogre::Material::TechniqueIterator techItr = mmat->getTechniqueIterator();
				if (techItr.hasMoreElements())
				{
					MyMatTech = techItr.getNext();
				}
			}

			Ogre::Pass* MyPass = MyMatTech->getPass(0);
			Ogre::ColourValue c = Ogre::ColourValue(0.5,0.5,0.5,0.0);
			c.a = f1;
			MyPass->setDiffuse(c);
			MyPass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
			mEntity->setMaterial(mmat);
A few questions I am not clear on.

1. The colourvalue I set, What should the RGB values be set too?
2. Should I get the Ogre::Technique* MyMatTech and Ogre::Pass* MyPass everytime.. or can I get these in the setup phase the first time when I create the mmat (Material) and save the pointer?
2. Does this seem right. (It works for a single test object in myscene). is it efficient?
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
xrgo
OGRE Expert User
OGRE Expert User
Posts: 1148
Joined: Sat Jul 06, 2013 10:59 pm
Location: Chile
x 168

Re: Making an Object Transparent [0-1] based on input value

Post by xrgo »

Garibalde wrote:1. The colourvalue I set, What should the RGB values be set too?
usually you use the current value like pass->setDiffuse( Ogre::ColourValue( pass->getDiffuse().r,pass->getDiffuse().g,pass->getDiffuse().b, val ) ); But! maybe its a fixed value that you might know... or If you use a texture maybe white is good
Garibalde wrote:2. Should I get the Ogre::Technique* MyMatTech and Ogre::Pass* MyPass everytime.. or can I get these in the setup phase the first time when I create the mmat (Material) and save the pointer?
yes! save the pass pointer
Garibalde wrote:2. Does this seem right. (It works for a single test object in myscene). is it efficient?
at least in Ogre 1.9 many materials where very expensive, always keep material count as low as possible. In 2.1 you can have many without problem... but i don't know the answer for 1.10
paroj
OGRE Team Member
OGRE Team Member
Posts: 1994
Joined: Sun Mar 30, 2014 2:51 pm
x 1074
Contact:

Re: Making an Object Transparent [0-1] based on input value

Post by paroj »

in 1.10 materials are sorted by shader (whereas 1.9 sorted by texture) and there are caches to prevent re-submitting identical values. So it should be better then 1.9, but obviously not as good as 2.1 with a single shader and materials in UBOs.
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

Re: Making an Object Transparent [0-1] based on input value

Post by Garibalde »

ok I got it to work however. when I apply the transparency shader (I think that's what its called).
I destroy my current material I have set to the object.

How do I keep all the previous materials and change the transparency?

Current this is what I do.

When I create the object in setup (A cube).

Code: Select all

	mEntity = mScnMgr->createEntity("cc", "ColourCube");

	Ogre::MaterialPtr lMaterial = Ogre::MaterialManager::getSingleton().create("MYMaterial", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
	Ogre::Technique* lFirstTechnique = lMaterial->getTechnique(0);
	Ogre::Pass* lFirstPass = lFirstTechnique->getPass(0);

	lFirstPass->setDiffuse(0.8f, 0.8f, 0.8f, 1.0f);
	lFirstPass->setAmbient(0.3f, 0.3f, 0.3f);
	lFirstPass->setSpecular(1.0f, 1.0f, 1.0f, 1.0f);
	lFirstPass->setShininess(64.0f);
	lFirstPass->setSelfIllumination(0.1f, 0.1f, 0.1f);

	Ogre::TextureUnitState* lTextureUnit = lFirstPass->createTextureUnitState();
	lTextureUnit->setTextureName("clouds.jpg", Ogre::TEX_TYPE_2D);
	lTextureUnit->setTextureCoordSet(0);

	mEntity->setMaterialName("MYMaterial");
	mSceneNode = mScnMgr->getRootSceneNode()->createChildSceneNode();
	mSceneNode->setPosition(0, 0, 0);
	mSceneNode->attachObject(mEntity);
Then at runtime if I want to change the transparency (f1) I do the following

Code: Select all

			Ogre::Technique* MyMatTech = mmat->getBestTechnique();
			if (MyMatTech == NULL)
			{
				Ogre::Material::TechniqueIterator techItr = mmat->getTechniqueIterator();
				if (techItr.hasMoreElements())
				{
					MyMatTech = techItr.getNext();
				}
			}

			mPass = MyMatTech->getPass(0);
			Ogre::ColourValue c = Ogre::ColourValue(mPass->getDiffuse().r, mPass->getDiffuse().g, mPass->getDiffuse().b, 0.0);
			c.a = f1;
			mPass->setDiffuse(c);
			mPass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
			mEntity->setMaterial(mmat);
I am sure... I think somehow I have to take the existing material on the object and add a transparency pass to it... how do I achieve this?
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

Re: Making an Object Transparent [0-1] based on input value

Post by Garibalde »

Ok writing that post I solved my issue... lol!

I have to add the transparency to the Material applied to the cube.

Code: Select all

			Ogre::MaterialPtr MyMat = Ogre::MaterialManager::getSingleton().getByName("MYMaterial");

			Ogre::Technique* MyMatTech = MyMat->getBestTechnique();
			if (MyMatTech == NULL)
			{
				Ogre::Material::TechniqueIterator techItr = MyMat->getTechniqueIterator();
				if (techItr.hasMoreElements())
				{
					MyMatTech = techItr.getNext();
				}
			}

			mPass = MyMatTech->getPass(0);
			Ogre::ColourValue c = Ogre::ColourValue(mPass->getDiffuse().r, mPass->getDiffuse().g, mPass->getDiffuse().b, 0.0);
			c.a = f1;
			mPass->setDiffuse(c);
			mPass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
			mEntity->setMaterial(MyMat);
This fixed the issue.
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
Garibalde
Halfling
Posts: 69
Joined: Thu Apr 16, 2009 2:38 am
Location: Montreal, Quebec
x 2
Contact:

Re: Making an Object Transparent [0-1] based on input value

Post by Garibalde »

ok still have issues. I cant see the cause.

If i apply a material for my objects as below:

Code: Select all

material Examples/Trans
{
	technique
	{
		pass
		{
			ambient 0.5 0.5 0.5 1.0
			diffuse 1.0 1.0 1.0 1.0
			specular 0.0 0.0 0.0 1.0 12.5
			emissive 0.0 0.0 0.0 1.0

			texture_unit
			{
				texture BeachStones.jpg
				filtering trilinear
			}
		}
	}
}
I get the following when i apply the transparency on top of the material. as defined above. But when i reduce the transparency
my object becomes black not transparent!! Its like it makes the material transparent!!

Figure a.jpg is with the matial applied and alpha (Tranparency set to 1.0)
transparency 1.0
transparency 1.0
Figure b.jpg is with the matial applied and alpha (Tranparency set to 0.0)
transparency 0.0
transparency 0.0
Here i have removed the material and only applied the Transparency material with transparency to 0.2
d.jpg
What am i doing wrong? any ideas is it lighting? or something?
Garibalde
VIRMED Simulation Technologies Inc.
http://www.virmed.com
Post Reply