作者pleaselouis (LOUISN)
看板GameDesign
標題[請益] 請問關於 Ogre 材質 透明色 設定
時間Mon Feb 23 16:02:59 2009
大家好 我又來發問了 這次是關於 材質 的 透明色 設定問題
也就是小弟希望指定一個 ColourKey 讓圖片中和 ColourKey 一樣的像素 顯示為透明
而我是參考這篇文章 修改的 :
http://0rz.tw/FBjS8
附帶一提 我使用的Ogre版本為 1.49 而下面則是我的原始碼 :
-----
/*這邊是執行透明化運算的 Function*/
Ogre::String loadChromaKeyedTexture(const Ogre::String& filename,
const Ogre::ColourValue& keyCol,
const Ogre::String& resGroup =
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
const Ogre::String& prefix = "ck_" ,
int numMipmaps = Ogre::MIP_DEFAULT ,
float threshold = 0.003f )
{
using namespace Ogre;
using std::fabs;
Image chromaKeyedImg;
Image srcImg;
srcImg.load (filename, resGroup);
uint width = srcImg.getWidth (), height = srcImg.getHeight ();
uchar* pixelData = srcImg.getData();
Ogre::uint32* pBuf = new Ogre::uint32[width*height];
for(uint y = 0; y < height; ++y)
{
for(uint x = 0; x < width; ++x)
{
ColourValue pixCol = srcImg.getColourAt (x, y, 0);
if( pixCol.r==keyCol.r &&
pixCol.g==keyCol.g &&
pixCol.b==keyCol.b ) pixCol.a=0.5;
Ogre::PixelUtil::packColour (pixCol,
PF_A8R8G8B8, &pBuf[y*width+x] );
}
}
chromaKeyedImg.loadDynamicImage ( (Ogre::uchar*)pBuf,
width, height, 1, PF_A8R8G8B8, true);
String resName = prefix + filename;
TextureManager::getSingleton ().
loadImage( resName , resGroup,
chromaKeyedImg, TEX_TYPE_2D, numMipmaps );
return resName;
}
/*再來是做一個 Rectangle 用來把圖片貼上去*/
Ogre::Rectangle2D *miniScreen;
miniScreen = new Ogre::Rectangle2D(true);
miniScreen->setCorners(0.25, 0.5, 1.0, -1.0);
AxisAlignedBox aabInf;
aabInf.setInfinite();
miniScreen->setBoundingBox(aabInf);
miniScreen->setRenderQueueGroup(RENDER_QUEUE_OVERLAY-1);
miniScreenNode = mSceneMgr->getRootSceneNode()
->createChildSceneNode("MiniScreenNode");
miniScreenNode->attachObject(miniScreen);
/*最後則是呼叫執行透明化的 Function 絕大部分跟連結內的範例一樣*/
Ogre::String texName =
loadChromaKeyedTexture("test.bmp", Ogre::ColourValue(1,0,0) );
Ogre::MaterialPtr mat = Ogre::MaterialManager::getSingleton().create
("yourMat", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
mat->getTechnique(0)->getPass(0)->setSceneBlending (Ogre::SBT_REPLACE);
mat->getTechnique(0)->getPass(0)
->setAlphaRejectSettings(Ogre::CMPF_GREATER_EQUAL, 128);
mat->getTechnique(0)->getPass(0)->setCullingMode (Ogre::CULL_NONE);
mat->getTechnique(0)->getPass(0)->setManualCullingMode (Ogre::MANUAL_CULL_NONE);
Ogre::TextureUnitState* t = mat->getTechnique(0)
->getPass(0)->createTextureUnitState(texName);
t->setTextureAddressingMode(Ogre::TextureUnitState::TAM_CLAMP);
miniScreen->setMaterial("yourMat");
-----
程式執行是正常的 但是貼出來的圖片 就是沒有任何的 透明化特效 產生
甚至是我最後將 透明化Function 中迴圈的 if判斷 註解掉
強硬的把每一個 像素 的 Alpha 都設為 0.0f 或是 0.5f 結果仍然是一樣沒有透明化
因此才又上來這邊 跟各位前輩請教 請各位前輩多多幫忙 謝謝大家 <(_ _)>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 211.76.190.21
推 tainjoy:你可以使用oFusion,可以在3ds max中看到結果 02/23 17:15
→ tainjoy:也可以調參數,這樣會比較了解 02/23 17:16
→ pleaselouis:因為 oFusion 用於商業用途 要另外收費啊 <囧/ 02/23 17:34