Making an Object Transparent [0-1] based on input value
-
- 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
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
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
-
- Halfling
- Posts: 46
- Joined: Tue Feb 18, 2014 10:49 pm
Re: Making an Object Transparent [0-1] based on input value
you can make your own datablock Material by code then change the alpha dynamically
-
- 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
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?
Do you have a small example how this can be done on a model?
Thanks
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");
}
Thanks
-
- 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
Code: Select all
Ogre::ColourValue c = ..;
c.a = val;
pass->setDiffuse(c);
pass->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA);
-
- 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
Ok that seems to work.
Here is what I did.
During creating object I created a material for transparency.
Then at runtime when I change the value of the alpha I do the following
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?
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();
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);
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?
-
- 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
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 goodGaribalde wrote:1. The colourvalue I set, What should the RGB values be set too?
yes! save the pass pointerGaribalde 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?
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.10Garibalde wrote:2. Does this seem right. (It works for a single test object in myscene). is it efficient?
-
- 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
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.
-
- 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
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).
Then at runtime if I want to change the transparency (f1) I do the following
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?
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);
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);
-
- 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
Ok writing that post I solved my issue... lol!
I have to add the transparency to the Material applied to the cube.
This fixed the issue.
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);
-
- 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
ok still have issues. I cant see the cause.
If i apply a material for my objects as below:
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) Figure b.jpg is with the matial applied and alpha (Tranparency set to 0.0) Here i have removed the material and only applied the Transparency material with transparency to 0.2 What am i doing wrong? any ideas is it lighting? or something?
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
}
}
}
}
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) Figure b.jpg is with the matial applied and alpha (Tranparency set to 0.0) Here i have removed the material and only applied the Transparency material with transparency to 0.2 What am i doing wrong? any ideas is it lighting? or something?