skyBox.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. INIT_ASSET(Material);
  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. INITPERSISTFIELD_MATERIALASSET(Material, SkyBox, "The name of a cubemap material for the sky box.");
  96. addField( "drawBottom", TypeBool, Offset( mDrawBottom, SkyBox ),
  97. "If false the bottom of the skybox is not rendered." );
  98. addField( "fogBandHeight", TypeF32, Offset( mFogBandHeight, SkyBox ),
  99. "The height (0-1) of the fog band from the horizon to the top of the SkyBox." );
  100. endGroup( "Sky Box" );
  101. Parent::initPersistFields();
  102. }
  103. void SkyBox::inspectPostApply()
  104. {
  105. Parent::inspectPostApply();
  106. _updateMaterial();
  107. }
  108. U32 SkyBox::packUpdate( NetConnection *conn, U32 mask, BitStream *stream )
  109. {
  110. U32 retMask = Parent::packUpdate( conn, mask, stream );
  111. PACK_ASSET(conn, Material);
  112. stream->writeFlag( mDrawBottom );
  113. stream->write( mFogBandHeight );
  114. return retMask;
  115. }
  116. void SkyBox::unpackUpdate( NetConnection *conn, BitStream *stream )
  117. {
  118. Parent::unpackUpdate( conn, stream );
  119. StringTableEntry oldMatName = getMaterial();
  120. UNPACK_ASSET(conn, Material);
  121. if (oldMatName != getMaterial())
  122. {
  123. _updateMaterial();
  124. }
  125. bool drawBottom = stream->readFlag();
  126. F32 bandHeight = 0;
  127. stream->read( &bandHeight );
  128. // If this flag has changed
  129. // we need to update the vertex buffer.
  130. if ( drawBottom != mDrawBottom ||
  131. bandHeight != mFogBandHeight )
  132. {
  133. mDrawBottom = drawBottom;
  134. mFogBandHeight = bandHeight;
  135. mIsVBDirty = true;
  136. _initRender();
  137. }
  138. }
  139. void SkyBox::prepRenderImage( SceneRenderState *state )
  140. {
  141. PROFILE_SCOPE( SkyBox_prepRenderImage );
  142. if ( state->isShadowPass() ||
  143. mVB.isNull() ||
  144. mFogBandVB.isNull() ||
  145. !mMatInstance )
  146. return;
  147. mMatrixSet->setSceneView(GFX->getWorldMatrix());
  148. mMatrixSet->setSceneProjection(GFX->getProjectionMatrix());
  149. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  150. ri->renderDelegate.bind( this, &SkyBox::_renderObject );
  151. ri->type = RenderPassManager::RIT_Sky;
  152. ri->defaultKey = 10;
  153. ri->defaultKey2 = 0;
  154. state->getRenderPass()->addInst( ri );
  155. }
  156. void SkyBox::_renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *mi )
  157. {
  158. GFXDEBUGEVENT_SCOPE( SkyBox_RenderObject, ColorI::WHITE );
  159. GFXTransformSaver saver;
  160. GFX->setVertexBuffer( mVB );
  161. MatrixF worldMat = MatrixF::Identity;
  162. worldMat.setPosition( state->getCameraPosition() );
  163. SceneData sgData;
  164. sgData.init( state );
  165. sgData.objTrans = &worldMat;
  166. mMatrixSet->restoreSceneViewProjection();
  167. mMatrixSet->setWorld( worldMat );
  168. if ( state->isReflectPass() )
  169. mMatrixSet->setProjection( state->getSceneManager()->getNonClipProjection() );
  170. while ( mMatInstance->setupPass( state, sgData ) )
  171. {
  172. mMatInstance->setTransforms( *mMatrixSet, state );
  173. mMatInstance->setSceneInfo( state, sgData );
  174. GFX->drawPrimitive( GFXTriangleList, 0, mPrimCount );
  175. }
  176. // Draw render band.
  177. if ( mFogBandHeight > 0 && mFogBandMatInst )
  178. {
  179. const FogData &fog = state->getSceneManager()->getFogData();
  180. if ( mLastFogColor != fog.color )
  181. {
  182. mLastFogColor = fog.color;
  183. _initRender();
  184. }
  185. // Just need it to follow the camera... no rotation.
  186. MatrixF camPosMat( MatrixF::Identity );
  187. camPosMat.setPosition( worldMat.getPosition() );
  188. sgData.objTrans = &camPosMat;
  189. mMatrixSet->setWorld( *sgData.objTrans );
  190. while ( mFogBandMatInst->setupPass( state, sgData ) )
  191. {
  192. mFogBandMatInst->setTransforms( *mMatrixSet, state );
  193. mFogBandMatInst->setSceneInfo( state, sgData );
  194. GFX->setVertexBuffer( mFogBandVB );
  195. GFX->drawPrimitive( GFXTriangleList, 0, 16 );
  196. }
  197. }
  198. }
  199. void SkyBox::_initRender()
  200. {
  201. GFXVertexPNT *tmpVerts = NULL;
  202. U32 vertCount = 36;
  203. if ( !mDrawBottom )
  204. vertCount = 30;
  205. mPrimCount = vertCount / 3;
  206. // Create temp vertex pointer
  207. // so we can read from it
  208. // for generating the normals below.
  209. tmpVerts = new GFXVertexPNT[vertCount];
  210. // We don't bother sharing
  211. // vertices here, in order to
  212. // avoid using a primitive buffer.
  213. tmpVerts[0].point.set( -1, -1, 1 );
  214. tmpVerts[1].point.set( 1, -1, 1 );
  215. tmpVerts[2].point.set( 1, -1, -1 );
  216. tmpVerts[0].texCoord.set( 0, 0 );
  217. tmpVerts[1].texCoord.set( 1.0f, 0 );
  218. tmpVerts[2].texCoord.set( 1.0f, 1.0f );
  219. tmpVerts[3].point.set( -1, -1, 1 );
  220. tmpVerts[4].point.set( 1, -1, -1 );
  221. tmpVerts[5].point.set( -1, -1, -1 );
  222. tmpVerts[3].texCoord.set( 0, 0 );
  223. tmpVerts[4].texCoord.set( 1.0f, 1.0f );
  224. tmpVerts[5].texCoord.set( 0, 1.0f );
  225. tmpVerts[6].point.set( 1, -1, 1 );
  226. tmpVerts[7].point.set( 1, 1, 1 );
  227. tmpVerts[8].point.set( 1, 1, -1 );
  228. tmpVerts[6].texCoord.set( 0, 0 );
  229. tmpVerts[7].texCoord.set( 1.0f, 0 );
  230. tmpVerts[8].texCoord.set( 1.0f, 1.0f );
  231. tmpVerts[9].point.set( 1, -1, 1 );
  232. tmpVerts[10].point.set( 1, 1, -1 );
  233. tmpVerts[11].point.set( 1, -1, -1 );
  234. tmpVerts[9].texCoord.set( 0, 0 );
  235. tmpVerts[10].texCoord.set( 1.0f, 1.0f );
  236. tmpVerts[11].texCoord.set( 0, 1.0f );
  237. tmpVerts[12].point.set( -1, 1, 1 );
  238. tmpVerts[13].point.set( -1, -1, 1 );
  239. tmpVerts[14].point.set( -1, -1, -1 );
  240. tmpVerts[12].texCoord.set( 0, 0 );
  241. tmpVerts[13].texCoord.set( 1.0f, 0 );
  242. tmpVerts[14].texCoord.set( 1.0f, 1.0f );
  243. tmpVerts[15].point.set( -1, 1, 1 );
  244. tmpVerts[16].point.set( -1, -1, -1 );
  245. tmpVerts[17].point.set( -1, 1, -1 );
  246. tmpVerts[15].texCoord.set( 0, 0 );
  247. tmpVerts[16].texCoord.set( 1.0f, 1.0f );
  248. tmpVerts[17].texCoord.set( 1.0f, 0 );
  249. tmpVerts[18].point.set( 1, 1, 1 );
  250. tmpVerts[19].point.set( -1, 1, 1 );
  251. tmpVerts[20].point.set( -1, 1, -1 );
  252. tmpVerts[18].texCoord.set( 0, 0 );
  253. tmpVerts[19].texCoord.set( 1.0f, 0 );
  254. tmpVerts[20].texCoord.set( 1.0f, 1.0f );
  255. tmpVerts[21].point.set( 1, 1, 1 );
  256. tmpVerts[22].point.set( -1, 1, -1 );
  257. tmpVerts[23].point.set( 1, 1, -1 );
  258. tmpVerts[21].texCoord.set( 0, 0 );
  259. tmpVerts[22].texCoord.set( 1.0f, 1.0f );
  260. tmpVerts[23].texCoord.set( 0, 1.0f );
  261. tmpVerts[24].point.set( -1, -1, 1 );
  262. tmpVerts[25].point.set( -1, 1, 1 );
  263. tmpVerts[26].point.set( 1, 1, 1 );
  264. tmpVerts[24].texCoord.set( 0, 0 );
  265. tmpVerts[25].texCoord.set( 1.0f, 0 );
  266. tmpVerts[26].texCoord.set( 1.0f, 1.0f );
  267. tmpVerts[27].point.set( -1, -1, 1 );
  268. tmpVerts[28].point.set( 1, 1, 1 );
  269. tmpVerts[29].point.set( 1, -1, 1 );
  270. tmpVerts[27].texCoord.set( 0, 0 );
  271. tmpVerts[28].texCoord.set( 1.0f, 1.0f );
  272. tmpVerts[29].texCoord.set( 0, 1.0f );
  273. // Only set up these
  274. // vertices if the SkyBox
  275. // is set to render the bottom face.
  276. if ( mDrawBottom )
  277. {
  278. tmpVerts[30].point.set( 1, 1, -1 );
  279. tmpVerts[31].point.set( -1, 1, -1 );
  280. tmpVerts[32].point.set( -1, -1, -1 );
  281. tmpVerts[30].texCoord.set( 1.0f, 1.0f );
  282. tmpVerts[31].texCoord.set( 1.0f, 0 );
  283. tmpVerts[32].texCoord.set( 0, 0 );
  284. tmpVerts[33].point.set( 1, -1, -1 );
  285. tmpVerts[34].point.set( 1, 1, -1 );
  286. tmpVerts[35].point.set( -1, -1, -1 );
  287. tmpVerts[33].texCoord.set( 0, 1.0f );
  288. tmpVerts[34].texCoord.set( 1.0f, 1.0f );
  289. tmpVerts[35].texCoord.set( 0, 0 );
  290. }
  291. VectorF tmp( 0, 0, 0 );
  292. for ( U32 i = 0; i < vertCount; i++ )
  293. {
  294. //tmp = tmpVerts[i].point;
  295. //tmp.normalize();
  296. //tmpVerts[i].normal.set( tmp );
  297. // Note: SkyBox renders with a regular material, which uses the "Reflect Cube"
  298. // feature.
  299. //
  300. // This feature is really designed a cubemap representing a reflection
  301. // on an objects surface and therefore looks up into the cubemap with the
  302. // cubemap-space view vector reflected by the vert normal.
  303. //
  304. // Since we are actually viewing the skybox from "inside" not from
  305. // "outside" this reflection ends up making the cubemap appear upsidown.
  306. // Therefore we set the vert-normals to "zero" so that the reflection
  307. // operation returns the input, unreflected, vector.
  308. tmpVerts[i].normal.set( Point3F::Zero );
  309. }
  310. if ( mVB.isNull() || mIsVBDirty )
  311. {
  312. mVB.set( GFX, vertCount, GFXBufferTypeStatic );
  313. mIsVBDirty = false;
  314. }
  315. GFXVertexPNT *vertPtr = mVB.lock();
  316. if (!vertPtr)
  317. {
  318. delete[] tmpVerts;
  319. return;
  320. }
  321. dMemcpy(vertPtr, tmpVerts, sizeof( GFXVertexPNT) * vertCount);
  322. mVB.unlock();
  323. // Clean up temp verts.
  324. delete [] tmpVerts;
  325. if ( mFogBandVB.isNull() )
  326. mFogBandVB.set( GFX, 48, GFXBufferTypeStatic );
  327. GFXVertexPC *bandVertPtr = mFogBandVB.lock();
  328. if(!bandVertPtr) return;
  329. // Grab the fog color.
  330. ColorI fogColor( mLastFogColor.red * 255, mLastFogColor.green * 255, mLastFogColor.blue * 255 );
  331. ColorI fogColorAlpha( mLastFogColor.red * 255, mLastFogColor.green * 255, mLastFogColor.blue * 255, 0 );
  332. // Upper portion of band geometry.
  333. {
  334. bandVertPtr[0].point.set( -1, -1, mFogBandHeight );
  335. bandVertPtr[1].point.set( 1, -1, mFogBandHeight );
  336. bandVertPtr[2].point.set( 1, -1, 0 );
  337. bandVertPtr[0].color.set( fogColorAlpha );
  338. bandVertPtr[1].color.set( fogColorAlpha );
  339. bandVertPtr[2].color.set( fogColor );
  340. bandVertPtr[3].point.set( -1, -1, mFogBandHeight );
  341. bandVertPtr[4].point.set( 1, -1, 0 );
  342. bandVertPtr[5].point.set( -1, -1, 0 );
  343. bandVertPtr[3].color.set( fogColorAlpha );
  344. bandVertPtr[4].color.set( fogColor );
  345. bandVertPtr[5].color.set( fogColor );
  346. bandVertPtr[6].point.set( 1, -1, mFogBandHeight );
  347. bandVertPtr[7].point.set( 1, 1, mFogBandHeight );
  348. bandVertPtr[8].point.set( 1, 1, 0 );
  349. bandVertPtr[6].color.set( fogColorAlpha );
  350. bandVertPtr[7].color.set( fogColorAlpha );
  351. bandVertPtr[8].color.set( fogColor );
  352. bandVertPtr[9].point.set( 1, -1, mFogBandHeight );
  353. bandVertPtr[10].point.set( 1, 1, 0 );
  354. bandVertPtr[11].point.set( 1, -1, 0 );
  355. bandVertPtr[9].color.set( fogColorAlpha );
  356. bandVertPtr[10].color.set( fogColor );
  357. bandVertPtr[11].color.set( fogColor );
  358. bandVertPtr[12].point.set( -1, 1, mFogBandHeight );
  359. bandVertPtr[13].point.set( -1, -1, mFogBandHeight );
  360. bandVertPtr[14].point.set( -1, -1, 0 );
  361. bandVertPtr[12].color.set( fogColorAlpha );
  362. bandVertPtr[13].color.set( fogColorAlpha );
  363. bandVertPtr[14].color.set( fogColor );
  364. bandVertPtr[15].point.set( -1, 1, mFogBandHeight );
  365. bandVertPtr[16].point.set( -1, -1, 0 );
  366. bandVertPtr[17].point.set( -1, 1, 0 );
  367. bandVertPtr[15].color.set( fogColorAlpha );
  368. bandVertPtr[16].color.set( fogColor );
  369. bandVertPtr[17].color.set( fogColor );
  370. bandVertPtr[18].point.set( 1, 1, mFogBandHeight );
  371. bandVertPtr[19].point.set( -1, 1, mFogBandHeight );
  372. bandVertPtr[20].point.set( -1, 1, 0 );
  373. bandVertPtr[18].color.set( fogColorAlpha );
  374. bandVertPtr[19].color.set( fogColorAlpha );
  375. bandVertPtr[20].color.set( fogColor );
  376. bandVertPtr[21].point.set( 1, 1, mFogBandHeight );
  377. bandVertPtr[22].point.set( -1, 1, 0 );
  378. bandVertPtr[23].point.set( 1, 1, 0 );
  379. bandVertPtr[21].color.set( fogColorAlpha );
  380. bandVertPtr[22].color.set( fogColor );
  381. bandVertPtr[23].color.set( fogColor );
  382. }
  383. // Lower portion of band geometry.
  384. {
  385. bandVertPtr[24].point.set( -1, -1, 0 );
  386. bandVertPtr[25].point.set( 1, -1, 0 );
  387. bandVertPtr[26].point.set( 1, -1, -1 );
  388. bandVertPtr[24].color.set( fogColor );
  389. bandVertPtr[25].color.set( fogColor );
  390. bandVertPtr[26].color.set( fogColor );
  391. bandVertPtr[27].point.set( -1, -1, 0 );
  392. bandVertPtr[28].point.set( 1, -1, -1 );
  393. bandVertPtr[29].point.set( -1, -1, -1 );
  394. bandVertPtr[27].color.set( fogColor );
  395. bandVertPtr[28].color.set( fogColor );
  396. bandVertPtr[29].color.set( fogColor );
  397. bandVertPtr[30].point.set( 1, -1, 0 );
  398. bandVertPtr[31].point.set( 1, 1, 0 );
  399. bandVertPtr[32].point.set( 1, 1, -1 );
  400. bandVertPtr[30].color.set( fogColor );
  401. bandVertPtr[31].color.set( fogColor );
  402. bandVertPtr[32].color.set( fogColor );
  403. bandVertPtr[33].point.set( 1, -1, 0 );
  404. bandVertPtr[34].point.set( 1, 1, -1 );
  405. bandVertPtr[35].point.set( 1, -1, -1 );
  406. bandVertPtr[33].color.set( fogColor );
  407. bandVertPtr[34].color.set( fogColor );
  408. bandVertPtr[35].color.set( fogColor );
  409. bandVertPtr[36].point.set( -1, 1, 0 );
  410. bandVertPtr[37].point.set( -1, -1, 0 );
  411. bandVertPtr[38].point.set( -1, -1, -1 );
  412. bandVertPtr[36].color.set( fogColor );
  413. bandVertPtr[37].color.set( fogColor );
  414. bandVertPtr[38].color.set( fogColor );
  415. bandVertPtr[39].point.set( -1, 1, 0 );
  416. bandVertPtr[40].point.set( -1, -1, -1 );
  417. bandVertPtr[41].point.set( -1, 1, -1 );
  418. bandVertPtr[39].color.set( fogColor );
  419. bandVertPtr[40].color.set( fogColor );
  420. bandVertPtr[41].color.set( fogColor );
  421. bandVertPtr[42].point.set( 1, 1, 0 );
  422. bandVertPtr[43].point.set( -1, 1, 0 );
  423. bandVertPtr[44].point.set( -1, 1, -1 );
  424. bandVertPtr[42].color.set( fogColor );
  425. bandVertPtr[43].color.set( fogColor );
  426. bandVertPtr[44].color.set( fogColor );
  427. bandVertPtr[45].point.set( 1, 1, 0 );
  428. bandVertPtr[46].point.set( -1, 1, -1 );
  429. bandVertPtr[47].point.set( 1, 1, -1 );
  430. bandVertPtr[45].color.set( fogColor );
  431. bandVertPtr[46].color.set( fogColor );
  432. bandVertPtr[47].color.set( fogColor );
  433. }
  434. mFogBandVB.unlock();
  435. SAFE_DELETE( mFogBandMatInst );
  436. if ( mFogBandMat )
  437. {
  438. mFogBandMat->deleteObject();
  439. mFogBandMat = NULL;
  440. }
  441. // Setup the material for this imposter.
  442. mFogBandMat = MATMGR->allocateAndRegister( String::EmptyString );
  443. mFogBandMat->mAutoGenerated = true;
  444. mFogBandMat->mTranslucent = true;
  445. mFogBandMat->mVertColor[0] = true;
  446. mFogBandMat->mDoubleSided = true;
  447. mFogBandMat->mEmissive[0] = true;
  448. FeatureSet features = MATMGR->getDefaultFeatures();
  449. features.addFeature(MFT_isBackground);
  450. mFogBandMatInst = mFogBandMat->createMatInstance();
  451. mFogBandMatInst->init(features, 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( GFXCullNone );
  470. desc.setBlend( true );
  471. desc.setZReadWrite( true, false );
  472. desc.zFunc = GFXCmpLessEqual;
  473. mMatInstance->addStateBlockDesc( desc );
  474. // Also disable lighting on the skybox material by default.
  475. FeatureSet features = MATMGR->getDefaultFeatures();
  476. features.removeFeature( MFT_RTLighting );
  477. features.removeFeature( MFT_Visibility );
  478. features.addFeature(MFT_isBackground);
  479. features.addFeature(MFT_SkyBox);
  480. // Now initialize the material.
  481. mMatInstance->init(features, getGFXVertexFormat<GFXVertexPNT>());
  482. }
  483. void SkyBox::_updateMaterial()
  484. {
  485. if (!getMaterialResource().isValid())
  486. {
  487. //If our materialDef isn't valid, try setting it
  488. _setMaterial(getMaterial());
  489. }
  490. if (getMaterialResource().isValid())
  491. {
  492. _initMaterial();
  493. }
  494. }
  495. BaseMatInstance* SkyBox::_getMaterialInstance()
  496. {
  497. if ( !mMaterial || !mMatInstance || mMatInstance->getMaterial() != mMaterial )
  498. _initMaterial();
  499. if ( !mMatInstance )
  500. return NULL;
  501. return mMatInstance;
  502. }
  503. DefineEngineMethod( SkyBox, postApply, void, (), , "")
  504. {
  505. object->inspectPostApply();
  506. }