當Shader Code越來越多的時候,很容易會有需要共用一個全域參數的需求(管理上很方便),比方我的Terrain及物件都需要參考到這個AmbientRatio參數,來處理LightMap顯示強度。傳統的做法是每個Sharder都有自己一個AmbientRatio的參數,當調整這個參數的時候,就要記得改每個Shader的constant,這樣非常麻煩…還好Ogre有一個叫shared parameter的好東西:
1.材質設定法:
只要在program描述前先宣告這個:
shared_params LightMapParams
{
shared_param_named AmbientRatio float 1.0
}
然後再引用它即可:
fragment_program BaseLightMap_FP unified
{
delegate BaseLightMap_FP_GLSLES
delegate BaseLightMap_FP_GLSL
delegate BaseLightMap_FP_HLSL
default_params
{
shared_params_ref LightMapParams
}
}
2.程式碼設定法:
Ogre::GpuSharedParametersPtr spSharedParameter =
Ogre::GpuProgramManager::getSingleton().
createSharedParameters("LightMapParams");
//define the shared param structure
spSharedParameter->addConstantDefinition("AmbientRatio", Ogre::GCT_FLOAT1);
//set some initial values
spSharedParameter->setNamedConstant("AmbientRatio", 1.0f);
PS:材質設定法要注意的事,有可能因為parse檔案順序的問題導致有些shader參考不到,除非你能確定定義shared_params的檔案最先被parse,筆者目前是程用程式定義法比較保險。
以上兩種方法,之後都可以在Shader使用到這個AmbientRatio參數,而且程式執行時期一改,所有的參考到這個參數的Shader都會連動到。
Dream continues in...
沒有留言:
張貼留言