skyBox.cpp 19 KB

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