skyBox.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "environment/skyBox.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/engineAPI.h"
  26. #include "scene/sceneRenderState.h"
  27. #include "renderInstance/renderPassManager.h"
  28. #include "gfx/primBuilder.h"
  29. #include "gfx/gfxTransformSaver.h"
  30. #include "core/stream/fileStream.h"
  31. #include "core/stream/bitStream.h"
  32. #include "materials/materialManager.h"
  33. #include "materials/materialFeatureTypes.h"
  34. #include "materials/sceneData.h"
  35. #include "T3D/gameFunctions.h"
  36. #include "renderInstance/renderBinManager.h"
  37. #include "materials/processedMaterial.h"
  38. #include "gfx/gfxDebugEvent.h"
  39. #include "math/util/matrixSet.h"
  40. IMPLEMENT_CO_NETOBJECT_V1( SkyBox );
  41. ConsoleDocClass( SkyBox,
  42. "@brief Represents the sky with an artist-created cubemap.\n\n"
  43. "SkyBox is not a directional light and should be used in conjunction with a Sun object.\n\n"
  44. "@ingroup Atmosphere"
  45. );
  46. SkyBox::SkyBox()
  47. {
  48. mTypeMask |= EnvironmentObjectType | StaticObjectType;
  49. mNetFlags.set(Ghostable | ScopeAlways);
  50. mMatName = "";
  51. mMatInstance = NULL;
  52. mIsVBDirty = false;
  53. mDrawBottom = true;
  54. mPrimCount = 0;
  55. mFogBandHeight = 0;
  56. mMatrixSet = reinterpret_cast<MatrixSet *>(dMalloc_aligned(sizeof(MatrixSet), 16));
  57. constructInPlace(mMatrixSet);
  58. mFogBandMat = NULL;
  59. mFogBandMatInst = NULL;
  60. }
  61. SkyBox::~SkyBox()
  62. {
  63. dFree_aligned(mMatrixSet);
  64. if( mMatInstance )
  65. SAFE_DELETE( mMatInstance );
  66. SAFE_DELETE( mFogBandMatInst );
  67. if ( mFogBandMat )
  68. {
  69. mFogBandMat->deleteObject();
  70. mFogBandMat = NULL;
  71. }
  72. }
  73. bool SkyBox::onAdd()
  74. {
  75. if ( !Parent::onAdd() )
  76. return false;
  77. setGlobalBounds();
  78. resetWorldBox();
  79. addToScene();
  80. if ( isClientObject() )
  81. {
  82. _initRender();
  83. _updateMaterial();
  84. }
  85. return true;
  86. }
  87. void SkyBox::onRemove()
  88. {
  89. removeFromScene();
  90. Parent::onRemove();
  91. }
  92. void SkyBox::initPersistFields()
  93. {
  94. addGroup( "Sky Box" );
  95. addField( "material", TypeMaterialName, Offset( mMatName, SkyBox ),
  96. "The name of a cubemap material for the sky box." );
  97. addField( "drawBottom", TypeBool, Offset( mDrawBottom, SkyBox ),
  98. "If false the bottom of the skybox is not rendered." );
  99. addField( "fogBandHeight", TypeF32, Offset( mFogBandHeight, SkyBox ),
  100. "The height (0-1) of the fog band from the horizon to the top of the SkyBox." );
  101. endGroup( "Sky Box" );
  102. Parent::initPersistFields();
  103. }
  104. void SkyBox::inspectPostApply()
  105. {
  106. Parent::inspectPostApply();
  107. _updateMaterial();
  108. }
  109. U32 SkyBox::packUpdate( NetConnection *conn, U32 mask, BitStream *stream )
  110. {
  111. U32 retMask = Parent::packUpdate( conn, mask, stream );
  112. stream->write( mMatName );
  113. stream->writeFlag( mDrawBottom );
  114. stream->write( mFogBandHeight );
  115. return retMask;
  116. }
  117. void SkyBox::unpackUpdate( NetConnection *conn, BitStream *stream )
  118. {
  119. Parent::unpackUpdate( conn, stream );
  120. String tmpString( "" );
  121. stream->read( &tmpString );
  122. if ( !tmpString.equal( mMatName, String::NoCase ) )
  123. {
  124. mMatName = tmpString;
  125. _updateMaterial();
  126. }
  127. bool drawBottom = stream->readFlag();
  128. F32 bandHeight = 0;
  129. stream->read( &bandHeight );
  130. // If this flag has changed
  131. // we need to update the vertex buffer.
  132. if ( drawBottom != mDrawBottom ||
  133. bandHeight != mFogBandHeight )
  134. {
  135. mDrawBottom = drawBottom;
  136. mFogBandHeight = bandHeight;
  137. mIsVBDirty = true;
  138. _initRender();
  139. }
  140. }
  141. void SkyBox::prepRenderImage( SceneRenderState *state )
  142. {
  143. PROFILE_SCOPE( SkyBox_prepRenderImage );
  144. if ( state->isShadowPass() ||
  145. mVB.isNull() ||
  146. mFogBandVB.isNull() ||
  147. !mMatInstance )
  148. return;
  149. mMatrixSet->setSceneView(GFX->getWorldMatrix());
  150. mMatrixSet->setSceneProjection(GFX->getProjectionMatrix());
  151. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  152. ri->renderDelegate.bind( this, &SkyBox::_renderObject );
  153. ri->type = RenderPassManager::RIT_Sky;
  154. ri->defaultKey = 10;
  155. ri->defaultKey2 = 0;
  156. state->getRenderPass()->addInst( ri );
  157. }
  158. void SkyBox::_renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *mi )
  159. {
  160. GFXDEBUGEVENT_SCOPE( SkyBox_RenderObject, ColorF::WHITE );
  161. GFXTransformSaver saver;
  162. GFX->setVertexBuffer( mVB );
  163. MatrixF worldMat = MatrixF::Identity;
  164. worldMat.setPosition( state->getCameraPosition() );
  165. SceneData sgData;
  166. sgData.init( state );
  167. sgData.objTrans = &worldMat;
  168. mMatrixSet->restoreSceneViewProjection();
  169. mMatrixSet->setWorld( worldMat );
  170. if ( state->isReflectPass() )
  171. mMatrixSet->setProjection( state->getSceneManager()->getNonClipProjection() );
  172. while ( mMatInstance->setupPass( state, sgData ) )
  173. {
  174. mMatInstance->setTransforms( *mMatrixSet, state );
  175. mMatInstance->setSceneInfo( state, sgData );
  176. GFX->drawPrimitive( GFXTriangleList, 0, mPrimCount );
  177. }
  178. // Draw render band.
  179. if ( mFogBandHeight > 0 && mFogBandMatInst )
  180. {
  181. const FogData &fog = state->getSceneManager()->getFogData();
  182. if ( mLastFogColor != fog.color )
  183. {
  184. mLastFogColor = fog.color;
  185. _initRender();
  186. }
  187. // Just need it to follow the camera... no rotation.
  188. MatrixF camPosMat( MatrixF::Identity );
  189. camPosMat.setPosition( worldMat.getPosition() );
  190. sgData.objTrans = &camPosMat;
  191. mMatrixSet->setWorld( *sgData.objTrans );
  192. while ( mFogBandMatInst->setupPass( state, sgData ) )
  193. {
  194. mFogBandMatInst->setTransforms( *mMatrixSet, state );
  195. mFogBandMatInst->setSceneInfo( state, sgData );
  196. GFX->setVertexBuffer( mFogBandVB );
  197. GFX->drawPrimitive( GFXTriangleList, 0, 16 );
  198. }
  199. }
  200. }
  201. void SkyBox::_initRender()
  202. {
  203. GFXVertexPNT *tmpVerts = NULL;
  204. U32 vertCount = 36;
  205. if ( !mDrawBottom )
  206. vertCount = 30;
  207. mPrimCount = vertCount / 3;
  208. // Create temp vertex pointer
  209. // so we can read from it
  210. // for generating the normals below.
  211. tmpVerts = new GFXVertexPNT[vertCount];
  212. // We don't bother sharing
  213. // vertices here, in order to
  214. // avoid using a primitive buffer.
  215. tmpVerts[0].point.set( -1, -1, 1 );
  216. tmpVerts[1].point.set( 1, -1, 1 );
  217. tmpVerts[2].point.set( 1, -1, -1 );
  218. tmpVerts[0].texCoord.set( 0, 0 );
  219. tmpVerts[1].texCoord.set( 1.0f, 0 );
  220. tmpVerts[2].texCoord.set( 1.0f, 1.0f );
  221. tmpVerts[3].point.set( -1, -1, 1 );
  222. tmpVerts[4].point.set( 1, -1, -1 );
  223. tmpVerts[5].point.set( -1, -1, -1 );
  224. tmpVerts[3].texCoord.set( 0, 0 );
  225. tmpVerts[4].texCoord.set( 1.0f, 1.0f );
  226. tmpVerts[5].texCoord.set( 0, 1.0f );
  227. tmpVerts[6].point.set( 1, -1, 1 );
  228. tmpVerts[7].point.set( 1, 1, 1 );
  229. tmpVerts[8].point.set( 1, 1, -1 );
  230. tmpVerts[6].texCoord.set( 0, 0 );
  231. tmpVerts[7].texCoord.set( 1.0f, 0 );
  232. tmpVerts[8].texCoord.set( 1.0f, 1.0f );
  233. tmpVerts[9].point.set( 1, -1, 1 );
  234. tmpVerts[10].point.set( 1, 1, -1 );
  235. tmpVerts[11].point.set( 1, -1, -1 );
  236. tmpVerts[9].texCoord.set( 0, 0 );
  237. tmpVerts[10].texCoord.set( 1.0f, 1.0f );
  238. tmpVerts[11].texCoord.set( 0, 1.0f );
  239. tmpVerts[12].point.set( -1, 1, 1 );
  240. tmpVerts[13].point.set( -1, -1, 1 );
  241. tmpVerts[14].point.set( -1, -1, -1 );
  242. tmpVerts[12].texCoord.set( 0, 0 );
  243. tmpVerts[13].texCoord.set( 1.0f, 0 );
  244. tmpVerts[14].texCoord.set( 1.0f, 1.0f );
  245. tmpVerts[15].point.set( -1, 1, 1 );
  246. tmpVerts[16].point.set( -1, -1, -1 );
  247. tmpVerts[17].point.set( -1, 1, -1 );
  248. tmpVerts[15].texCoord.set( 0, 0 );
  249. tmpVerts[16].texCoord.set( 1.0f, 1.0f );
  250. tmpVerts[17].texCoord.set( 1.0f, 0 );
  251. tmpVerts[18].point.set( 1, 1, 1 );
  252. tmpVerts[19].point.set( -1, 1, 1 );
  253. tmpVerts[20].point.set( -1, 1, -1 );
  254. tmpVerts[18].texCoord.set( 0, 0 );
  255. tmpVerts[19].texCoord.set( 1.0f, 0 );
  256. tmpVerts[20].texCoord.set( 1.0f, 1.0f );
  257. tmpVerts[21].point.set( 1, 1, 1 );
  258. tmpVerts[22].point.set( -1, 1, -1 );
  259. tmpVerts[23].point.set( 1, 1, -1 );
  260. tmpVerts[21].texCoord.set( 0, 0 );
  261. tmpVerts[22].texCoord.set( 1.0f, 1.0f );
  262. tmpVerts[23].texCoord.set( 0, 1.0f );
  263. tmpVerts[24].point.set( -1, -1, 1 );
  264. tmpVerts[25].point.set( -1, 1, 1 );
  265. tmpVerts[26].point.set( 1, 1, 1 );
  266. tmpVerts[24].texCoord.set( 0, 0 );
  267. tmpVerts[25].texCoord.set( 1.0f, 0 );
  268. tmpVerts[26].texCoord.set( 1.0f, 1.0f );
  269. tmpVerts[27].point.set( -1, -1, 1 );
  270. tmpVerts[28].point.set( 1, 1, 1 );
  271. tmpVerts[29].point.set( 1, -1, 1 );
  272. tmpVerts[27].texCoord.set( 0, 0 );
  273. tmpVerts[28].texCoord.set( 1.0f, 1.0f );
  274. tmpVerts[29].texCoord.set( 0, 1.0f );
  275. // Only set up these
  276. // vertices if the SkyBox
  277. // is set to render the bottom face.
  278. if ( mDrawBottom )
  279. {
  280. tmpVerts[30].point.set( 1, 1, -1 );
  281. tmpVerts[31].point.set( -1, 1, -1 );
  282. tmpVerts[32].point.set( -1, -1, -1 );
  283. tmpVerts[30].texCoord.set( 1.0f, 1.0f );
  284. tmpVerts[31].texCoord.set( 1.0f, 0 );
  285. tmpVerts[32].texCoord.set( 0, 0 );
  286. tmpVerts[33].point.set( 1, -1, -1 );
  287. tmpVerts[34].point.set( 1, 1, -1 );
  288. tmpVerts[35].point.set( -1, -1, -1 );
  289. tmpVerts[33].texCoord.set( 0, 1.0f );
  290. tmpVerts[34].texCoord.set( 1.0f, 1.0f );
  291. tmpVerts[35].texCoord.set( 0, 0 );
  292. }
  293. VectorF tmp( 0, 0, 0 );
  294. for ( U32 i = 0; i < vertCount; i++ )
  295. {
  296. //tmp = tmpVerts[i].point;
  297. //tmp.normalize();
  298. //tmpVerts[i].normal.set( tmp );
  299. // Note: SkyBox renders with a regular material, which uses the "Reflect Cube"
  300. // feature.
  301. //
  302. // This feature is really designed a cubemap representing a reflection
  303. // on an objects surface and therefore looks up into the cubemap with the
  304. // cubemap-space view vector reflected by the vert normal.
  305. //
  306. // Since we are actually viewing the skybox from "inside" not from
  307. // "outside" this reflection ends up making the cubemap appear upsidown.
  308. // Therefore we set the vert-normals to "zero" so that the reflection
  309. // operation returns the input, unreflected, vector.
  310. tmpVerts[i].normal.set( Point3F::Zero );
  311. }
  312. if ( mVB.isNull() || mIsVBDirty )
  313. {
  314. mVB.set( GFX, vertCount, GFXBufferTypeStatic );
  315. mIsVBDirty = false;
  316. }
  317. GFXVertexPNT *vertPtr = mVB.lock();
  318. if (!vertPtr)
  319. {
  320. delete[] tmpVerts;
  321. return;
  322. }
  323. dMemcpy(vertPtr, tmpVerts, sizeof( GFXVertexPNT) * vertCount);
  324. mVB.unlock();
  325. // Clean up temp verts.
  326. delete [] tmpVerts;
  327. if ( mFogBandVB.isNull() )
  328. mFogBandVB.set( GFX, 48, GFXBufferTypeStatic );
  329. GFXVertexPC *bandVertPtr = mFogBandVB.lock();
  330. if(!bandVertPtr) return;
  331. // Grab the fog color.
  332. ColorI fogColor( mLastFogColor.red * 255, mLastFogColor.green * 255, mLastFogColor.blue * 255 );
  333. ColorI fogColorAlpha( mLastFogColor.red * 255, mLastFogColor.green * 255, mLastFogColor.blue * 255, 0 );
  334. // Upper portion of band geometry.
  335. {
  336. bandVertPtr[0].point.set( -1, -1, mFogBandHeight );
  337. bandVertPtr[1].point.set( 1, -1, mFogBandHeight );
  338. bandVertPtr[2].point.set( 1, -1, 0 );
  339. bandVertPtr[0].color.set( fogColorAlpha );
  340. bandVertPtr[1].color.set( fogColorAlpha );
  341. bandVertPtr[2].color.set( fogColor );
  342. bandVertPtr[3].point.set( -1, -1, mFogBandHeight );
  343. bandVertPtr[4].point.set( 1, -1, 0 );
  344. bandVertPtr[5].point.set( -1, -1, 0 );
  345. bandVertPtr[3].color.set( fogColorAlpha );
  346. bandVertPtr[4].color.set( fogColor );
  347. bandVertPtr[5].color.set( fogColor );
  348. bandVertPtr[6].point.set( 1, -1, mFogBandHeight );
  349. bandVertPtr[7].point.set( 1, 1, mFogBandHeight );
  350. bandVertPtr[8].point.set( 1, 1, 0 );
  351. bandVertPtr[6].color.set( fogColorAlpha );
  352. bandVertPtr[7].color.set( fogColorAlpha );
  353. bandVertPtr[8].color.set( fogColor );
  354. bandVertPtr[9].point.set( 1, -1, mFogBandHeight );
  355. bandVertPtr[10].point.set( 1, 1, 0 );
  356. bandVertPtr[11].point.set( 1, -1, 0 );
  357. bandVertPtr[9].color.set( fogColorAlpha );
  358. bandVertPtr[10].color.set( fogColor );
  359. bandVertPtr[11].color.set( fogColor );
  360. bandVertPtr[12].point.set( -1, 1, mFogBandHeight );
  361. bandVertPtr[13].point.set( -1, -1, mFogBandHeight );
  362. bandVertPtr[14].point.set( -1, -1, 0 );
  363. bandVertPtr[12].color.set( fogColorAlpha );
  364. bandVertPtr[13].color.set( fogColorAlpha );
  365. bandVertPtr[14].color.set( fogColor );
  366. bandVertPtr[15].point.set( -1, 1, mFogBandHeight );
  367. bandVertPtr[16].point.set( -1, -1, 0 );
  368. bandVertPtr[17].point.set( -1, 1, 0 );
  369. bandVertPtr[15].color.set( fogColorAlpha );
  370. bandVertPtr[16].color.set( fogColor );
  371. bandVertPtr[17].color.set( fogColor );
  372. bandVertPtr[18].point.set( 1, 1, mFogBandHeight );
  373. bandVertPtr[19].point.set( -1, 1, mFogBandHeight );
  374. bandVertPtr[20].point.set( -1, 1, 0 );
  375. bandVertPtr[18].color.set( fogColorAlpha );
  376. bandVertPtr[19].color.set( fogColorAlpha );
  377. bandVertPtr[20].color.set( fogColor );
  378. bandVertPtr[21].point.set( 1, 1, mFogBandHeight );
  379. bandVertPtr[22].point.set( -1, 1, 0 );
  380. bandVertPtr[23].point.set( 1, 1, 0 );
  381. bandVertPtr[21].color.set( fogColorAlpha );
  382. bandVertPtr[22].color.set( fogColor );
  383. bandVertPtr[23].color.set( fogColor );
  384. }
  385. // Lower portion of band geometry.
  386. {
  387. bandVertPtr[24].point.set( -1, -1, 0 );
  388. bandVertPtr[25].point.set( 1, -1, 0 );
  389. bandVertPtr[26].point.set( 1, -1, -1 );
  390. bandVertPtr[24].color.set( fogColor );
  391. bandVertPtr[25].color.set( fogColor );
  392. bandVertPtr[26].color.set( fogColor );
  393. bandVertPtr[27].point.set( -1, -1, 0 );
  394. bandVertPtr[28].point.set( 1, -1, -1 );
  395. bandVertPtr[29].point.set( -1, -1, -1 );
  396. bandVertPtr[27].color.set( fogColor );
  397. bandVertPtr[28].color.set( fogColor );
  398. bandVertPtr[29].color.set( fogColor );
  399. bandVertPtr[30].point.set( 1, -1, 0 );
  400. bandVertPtr[31].point.set( 1, 1, 0 );
  401. bandVertPtr[32].point.set( 1, 1, -1 );
  402. bandVertPtr[30].color.set( fogColor );
  403. bandVertPtr[31].color.set( fogColor );
  404. bandVertPtr[32].color.set( fogColor );
  405. bandVertPtr[33].point.set( 1, -1, 0 );
  406. bandVertPtr[34].point.set( 1, 1, -1 );
  407. bandVertPtr[35].point.set( 1, -1, -1 );
  408. bandVertPtr[33].color.set( fogColor );
  409. bandVertPtr[34].color.set( fogColor );
  410. bandVertPtr[35].color.set( fogColor );
  411. bandVertPtr[36].point.set( -1, 1, 0 );
  412. bandVertPtr[37].point.set( -1, -1, 0 );
  413. bandVertPtr[38].point.set( -1, -1, -1 );
  414. bandVertPtr[36].color.set( fogColor );
  415. bandVertPtr[37].color.set( fogColor );
  416. bandVertPtr[38].color.set( fogColor );
  417. bandVertPtr[39].point.set( -1, 1, 0 );
  418. bandVertPtr[40].point.set( -1, -1, -1 );
  419. bandVertPtr[41].point.set( -1, 1, -1 );
  420. bandVertPtr[39].color.set( fogColor );
  421. bandVertPtr[40].color.set( fogColor );
  422. bandVertPtr[41].color.set( fogColor );
  423. bandVertPtr[42].point.set( 1, 1, 0 );
  424. bandVertPtr[43].point.set( -1, 1, 0 );
  425. bandVertPtr[44].point.set( -1, 1, -1 );
  426. bandVertPtr[42].color.set( fogColor );
  427. bandVertPtr[43].color.set( fogColor );
  428. bandVertPtr[44].color.set( fogColor );
  429. bandVertPtr[45].point.set( 1, 1, 0 );
  430. bandVertPtr[46].point.set( -1, 1, -1 );
  431. bandVertPtr[47].point.set( 1, 1, -1 );
  432. bandVertPtr[45].color.set( fogColor );
  433. bandVertPtr[46].color.set( fogColor );
  434. bandVertPtr[47].color.set( fogColor );
  435. }
  436. mFogBandVB.unlock();
  437. SAFE_DELETE( mFogBandMatInst );
  438. if ( mFogBandMat )
  439. {
  440. mFogBandMat->deleteObject();
  441. mFogBandMat = NULL;
  442. }
  443. // Setup the material for this imposter.
  444. mFogBandMat = MATMGR->allocateAndRegister( String::EmptyString );
  445. mFogBandMat->mAutoGenerated = true;
  446. mFogBandMat->mTranslucent = true;
  447. mFogBandMat->mVertColor[0] = true;
  448. mFogBandMat->mDoubleSided = true;
  449. mFogBandMat->mEmissive[0] = true;
  450. mFogBandMatInst = mFogBandMat->createMatInstance();
  451. mFogBandMatInst->init( MATMGR->getDefaultFeatures(), getGFXVertexFormat<GFXVertexPC>() );
  452. }
  453. void SkyBox::onStaticModified( const char *slotName, const char *newValue )
  454. {
  455. Parent::onStaticModified( slotName, newValue );
  456. if ( dStricmp( slotName, "material" ) == 0 )
  457. setMaskBits( 0xFFFFFFFF );
  458. }
  459. void SkyBox::_initMaterial()
  460. {
  461. if ( mMatInstance )
  462. SAFE_DELETE( mMatInstance );
  463. if ( mMaterial )
  464. mMatInstance = mMaterial->createMatInstance();
  465. else
  466. mMatInstance = MATMGR->createMatInstance( "WarningMaterial" );
  467. // We want to disable culling and z write.
  468. GFXStateBlockDesc desc;
  469. desc.setCullMode( GFXCullCW );
  470. desc.setZReadWrite( true, false );
  471. mMatInstance->addStateBlockDesc( desc );
  472. // Also disable lighting on the skybox material by default.
  473. FeatureSet features = MATMGR->getDefaultFeatures();
  474. features.removeFeature( MFT_RTLighting );
  475. features.removeFeature( MFT_Visibility );
  476. // Now initialize the material.
  477. mMatInstance->init(features, getGFXVertexFormat<GFXVertexPNT>());
  478. }
  479. void SkyBox::_updateMaterial()
  480. {
  481. if ( mMatName.isEmpty() )
  482. return;
  483. Material *pMat = NULL;
  484. if ( !Sim::findObject( mMatName, pMat ) )
  485. Con::printf( "SkyBox::_updateMaterial, failed to find Material of name %s!", mMatName.c_str() );
  486. else if ( isProperlyAdded() )
  487. {
  488. mMaterial = pMat;
  489. _initMaterial();
  490. }
  491. }
  492. BaseMatInstance* SkyBox::_getMaterialInstance()
  493. {
  494. if ( !mMaterial || !mMatInstance || mMatInstance->getMaterial() != mMaterial )
  495. _initMaterial();
  496. if ( !mMatInstance )
  497. return NULL;
  498. return mMatInstance;
  499. }
  500. DefineConsoleMethod( SkyBox, postApply, void, (), , "")
  501. {
  502. object->inspectPostApply();
  503. }