cloudLayer.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 "platform/profiler.h"
  24. #include "console/consoleTypes.h"
  25. #include "cloudLayer.h"
  26. #include "gfx/gfxTransformSaver.h"
  27. #include "gfx/gfxTextureManager.h"
  28. #include "core/stream/fileStream.h"
  29. #include "core/stream/bitStream.h"
  30. #include "scene/sceneRenderState.h"
  31. #include "renderInstance/renderPassManager.h"
  32. #include "gfx/primBuilder.h"
  33. #include "materials/materialManager.h"
  34. #include "materials/customMaterialDefinition.h"
  35. #include "materials/shaderData.h"
  36. #include "lighting/lightInfo.h"
  37. #include "math/mathIO.h"
  38. ConsoleDocClass( CloudLayer,
  39. "@brief A layer of clouds which change shape over time and are affected by scene lighting.\n\n"
  40. "%CloudLayer always renders overhead, following the camera. It is intended "
  41. "as part of the background of your level, rendering in front of Sky/Sun "
  42. "type objects and behind everything else.\n\n"
  43. "The illusion of clouds forming and changing over time is controlled by the "
  44. "normal/opacity texture and the three sets of texture animation parameters. "
  45. "The texture is sampled three times. The first sample defines overall cloud "
  46. "density, where clouds are likely to form and their general size and shape. "
  47. "The second two samples control how it changes over time; they are "
  48. "combined and used as modifiers to the first sample.\n\n"
  49. "%CloudLayer is affected by scene lighting and is designed to be used in "
  50. "scenes with dynamic lighting or time of day changes.\n\n"
  51. "@ingroup Atmosphere"
  52. );
  53. GFXImplementVertexFormat( GFXCloudVertex )
  54. {
  55. addElement( "POSITION", GFXDeclType_Float3 );
  56. addElement( "NORMAL", GFXDeclType_Float3 );
  57. addElement( "BINORMAL", GFXDeclType_Float3 );
  58. addElement( "TANGENT", GFXDeclType_Float3 );
  59. addElement( "TEXCOORD", GFXDeclType_Float2, 0 );
  60. }
  61. U32 CloudLayer::smVertStride = 50;
  62. U32 CloudLayer::smStrideMinusOne = smVertStride - 1;
  63. U32 CloudLayer::smVertCount = smVertStride * smVertStride;
  64. U32 CloudLayer::smTriangleCount = smStrideMinusOne * smStrideMinusOne * 2;
  65. CloudLayer::CloudLayer()
  66. : mBaseColor( 0.9f, 0.9f, 0.9f, 1.0f ),
  67. mCoverage( 0.5f ),
  68. mExposure( 1.0f ),
  69. mWindSpeed( 1.0f ),
  70. mLastTime( 0 )
  71. {
  72. mTypeMask |= EnvironmentObjectType | StaticObjectType;
  73. mNetFlags.set(Ghostable | ScopeAlways);
  74. mTexScale[0] = 1.0;
  75. mTexScale[1] = 1.0;
  76. mTexScale[2] = 1.0;
  77. mTexDirection[0].set( 1.0f, 0.0f );
  78. mTexDirection[1].set( 0.0f, 1.0f );
  79. mTexDirection[2].set( 0.5f, 0.0f );
  80. mTexSpeed[0] = 0.005f;
  81. mTexSpeed[1] = 0.005f;
  82. mTexSpeed[2] = 0.005f;
  83. mTexOffset[0] = mTexOffset[1] = mTexOffset[2] = Point2F::Zero;
  84. mHeight = 4.0f;
  85. }
  86. IMPLEMENT_CO_NETOBJECT_V1( CloudLayer );
  87. // ConsoleObject...
  88. bool CloudLayer::onAdd()
  89. {
  90. if ( !Parent::onAdd() )
  91. return false;
  92. setGlobalBounds();
  93. resetWorldBox();
  94. addToScene();
  95. if ( isClientObject() )
  96. {
  97. _initTexture();
  98. _initBuffers();
  99. // Find ShaderData
  100. ShaderData *shaderData;
  101. mShader = Sim::findObject( "CloudLayerShader", shaderData ) ?
  102. shaderData->getShader() : NULL;
  103. if ( !mShader )
  104. {
  105. Con::errorf( "CloudLayer::onAdd - could not find CloudLayerShader" );
  106. return false;
  107. }
  108. // Create ShaderConstBuffer and Handles
  109. mShaderConsts = mShader->allocConstBuffer();
  110. mModelViewProjSC = mShader->getShaderConstHandle( "$modelView" );
  111. mEyePosWorldSC = mShader->getShaderConstHandle( "$eyePosWorld" );
  112. mSunVecSC = mShader->getShaderConstHandle( "$sunVec" );
  113. mTexOffsetSC[0] = mShader->getShaderConstHandle( "$texOffset0" );
  114. mTexOffsetSC[1] = mShader->getShaderConstHandle( "$texOffset1" );
  115. mTexOffsetSC[2] = mShader->getShaderConstHandle( "$texOffset2" );
  116. mTexScaleSC = mShader->getShaderConstHandle( "$texScale" );
  117. mAmbientColorSC = mShader->getShaderConstHandle( "$ambientColor" );
  118. mSunColorSC = mShader->getShaderConstHandle( "$sunColor" );
  119. mCoverageSC = mShader->getShaderConstHandle( "$cloudCoverage" );
  120. mExposureSC = mShader->getShaderConstHandle( "$cloudExposure" );
  121. mBaseColorSC = mShader->getShaderConstHandle( "$cloudBaseColor" );
  122. // Create StateBlocks
  123. GFXStateBlockDesc desc;
  124. desc.setCullMode( GFXCullNone );
  125. desc.setBlend( true );
  126. desc.setZReadWrite( false, false );
  127. desc.samplersDefined = true;
  128. desc.samplers[0].addressModeU = GFXAddressWrap;
  129. desc.samplers[0].addressModeV = GFXAddressWrap;
  130. desc.samplers[0].addressModeW = GFXAddressWrap;
  131. desc.samplers[0].magFilter = GFXTextureFilterLinear;
  132. desc.samplers[0].minFilter = GFXTextureFilterLinear;
  133. desc.samplers[0].mipFilter = GFXTextureFilterLinear;
  134. desc.samplers[0].textureColorOp = GFXTOPModulate;
  135. mStateblock = GFX->createStateBlock( desc );
  136. }
  137. return true;
  138. }
  139. void CloudLayer::onRemove()
  140. {
  141. removeFromScene();
  142. Parent::onRemove();
  143. }
  144. void CloudLayer::initPersistFields()
  145. {
  146. addGroup( "CloudLayer" );
  147. addField( "texture", TypeImageFilename, Offset( mTextureName, CloudLayer ),
  148. "An RGBA texture which should contain normals and opacity (density)." );
  149. addArray( "Textures", TEX_COUNT );
  150. addField( "texScale", TypeF32, Offset( mTexScale, CloudLayer ), TEX_COUNT,
  151. "Controls the texture repeat of this slot." );
  152. addField( "texDirection", TypePoint2F, Offset( mTexDirection, CloudLayer ), TEX_COUNT,
  153. "Controls the direction this slot scrolls." );
  154. addField( "texSpeed", TypeF32, Offset( mTexSpeed, CloudLayer ), TEX_COUNT,
  155. "Controls the speed this slot scrolls." );
  156. endArray( "Textures" );
  157. addField( "baseColor", TypeColorF, Offset( mBaseColor, CloudLayer ),
  158. "Base cloud color before lighting." );
  159. addField( "exposure", TypeF32, Offset( mExposure, CloudLayer ),
  160. "Brightness scale so CloudLayer can be overblown if desired." );
  161. addField( "coverage", TypeF32, Offset( mCoverage, CloudLayer ),
  162. "Fraction of sky covered by clouds 0-1." );
  163. addField( "windSpeed", TypeF32, Offset( mWindSpeed, CloudLayer ),
  164. "Overall scalar to texture scroll speed." );
  165. addField( "height", TypeF32, Offset( mHeight, CloudLayer ),
  166. "Abstract number which controls the curvature and height of the dome mesh." );
  167. endGroup( "CloudLayer" );
  168. Parent::initPersistFields();
  169. }
  170. void CloudLayer::inspectPostApply()
  171. {
  172. Parent::inspectPostApply();
  173. setMaskBits( CloudLayerMask );
  174. }
  175. // NetObject...
  176. U32 CloudLayer::packUpdate( NetConnection *conn, U32 mask, BitStream *stream )
  177. {
  178. U32 retMask = Parent::packUpdate( conn, mask, stream );
  179. stream->write( mTextureName );
  180. for ( U32 i = 0; i < TEX_COUNT; i++ )
  181. {
  182. stream->write( mTexScale[i] );
  183. stream->write( mTexSpeed[i] );
  184. mathWrite( *stream, mTexDirection[i] );
  185. }
  186. stream->write( mBaseColor );
  187. stream->write( mCoverage );
  188. stream->write( mExposure );
  189. stream->write( mWindSpeed );
  190. stream->write( mHeight );
  191. return retMask;
  192. }
  193. void CloudLayer::unpackUpdate( NetConnection *conn, BitStream *stream )
  194. {
  195. Parent::unpackUpdate( conn, stream );
  196. String oldTextureName = mTextureName;
  197. stream->read( &mTextureName );
  198. for ( U32 i = 0; i < TEX_COUNT; i++ )
  199. {
  200. stream->read( &mTexScale[i] );
  201. stream->read( &mTexSpeed[i] );
  202. mathRead( *stream, &mTexDirection[i] );
  203. }
  204. stream->read( &mBaseColor );
  205. F32 oldCoverage = mCoverage;
  206. stream->read( &mCoverage );
  207. stream->read( &mExposure );
  208. stream->read( &mWindSpeed );
  209. F32 oldHeight = mHeight;
  210. stream->read( &mHeight );
  211. if ( isProperlyAdded() )
  212. {
  213. if ( ( oldTextureName != mTextureName ) || ( ( oldCoverage == 0.0f ) != ( mCoverage == 0.0f ) ) )
  214. _initTexture();
  215. if ( oldHeight != mHeight )
  216. _initBuffers();
  217. }
  218. }
  219. // SceneObject...
  220. void CloudLayer::prepRenderImage( SceneRenderState *state )
  221. {
  222. PROFILE_SCOPE( CloudLayer_prepRenderImage );
  223. if ( mCoverage <= 0.0f )
  224. return;
  225. if ( state->isDiffusePass() )
  226. {
  227. // Scroll textures...
  228. U32 time = Sim::getCurrentTime();
  229. F32 delta = (F32)( time - mLastTime ) / 1000.0f;
  230. mLastTime = time;
  231. for ( U32 i = 0; i < 3; i++ )
  232. {
  233. mTexOffset[i] += mTexDirection[i] * mTexSpeed[i] * delta * mWindSpeed;
  234. }
  235. }
  236. // This should be sufficient for most objects that don't manage zones, and
  237. // don't need to return a specialized RenderImage...
  238. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  239. ri->renderDelegate.bind( this, &CloudLayer::renderObject );
  240. ri->type = RenderPassManager::RIT_Sky;
  241. ri->defaultKey = 0;
  242. ri->defaultKey2 = 0;
  243. state->getRenderPass()->addInst( ri );
  244. }
  245. void CloudLayer::renderObject( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *mi )
  246. {
  247. GFXTransformSaver saver;
  248. const Point3F &camPos = state->getCameraPosition();
  249. MatrixF xfm(true);
  250. xfm.setPosition(camPos);
  251. GFX->multWorld(xfm);
  252. if ( state->isReflectPass() )
  253. GFX->setProjectionMatrix( state->getSceneManager()->getNonClipProjection() );
  254. GFX->setShader( mShader );
  255. GFX->setShaderConstBuffer( mShaderConsts );
  256. GFX->setStateBlock( mStateblock );
  257. // Set all the shader consts...
  258. MatrixF xform(GFX->getProjectionMatrix());
  259. xform *= GFX->getViewMatrix();
  260. xform *= GFX->getWorldMatrix();
  261. mShaderConsts->setSafe( mModelViewProjSC, xform );
  262. mShaderConsts->setSafe( mEyePosWorldSC, camPos );
  263. LightInfo *lightinfo = LIGHTMGR->getSpecialLight(LightManager::slSunLightType);
  264. const ColorF &sunlight = state->getAmbientLightColor();
  265. Point3F ambientColor( sunlight.red, sunlight.green, sunlight.blue );
  266. mShaderConsts->setSafe( mAmbientColorSC, ambientColor );
  267. const ColorF &sunColor = lightinfo->getColor();
  268. Point3F data( sunColor.red, sunColor.green, sunColor.blue );
  269. mShaderConsts->setSafe( mSunColorSC, data );
  270. mShaderConsts->setSafe( mSunVecSC, lightinfo->getDirection() );
  271. for ( U32 i = 0; i < TEX_COUNT; i++ )
  272. mShaderConsts->setSafe( mTexOffsetSC[i], mTexOffset[i] );
  273. Point3F scale( mTexScale[0], mTexScale[1], mTexScale[2] );
  274. mShaderConsts->setSafe( mTexScaleSC, scale );
  275. Point3F color;
  276. color.set( mBaseColor.red, mBaseColor.green, mBaseColor.blue );
  277. mShaderConsts->setSafe( mBaseColorSC, color );
  278. mShaderConsts->setSafe( mCoverageSC, mCoverage );
  279. mShaderConsts->setSafe( mExposureSC, mExposure );
  280. GFX->setTexture( 0, mTexture );
  281. GFX->setVertexBuffer( mVB );
  282. GFX->setPrimitiveBuffer( mPB );
  283. GFX->drawIndexedPrimitive( GFXTriangleList, 0, 0, smVertCount, 0, smTriangleCount );
  284. }
  285. // CloudLayer Internal Methods....
  286. void CloudLayer::_initTexture()
  287. {
  288. if ( mCoverage <= 0.0f )
  289. {
  290. mTexture = NULL;
  291. return;
  292. }
  293. if ( mTextureName.isNotEmpty() )
  294. mTexture.set( mTextureName, &GFXDefaultStaticDiffuseProfile, "CloudLayer" );
  295. if ( mTexture.isNull() )
  296. mTexture.set( GFXTextureManager::getWarningTexturePath(), &GFXDefaultStaticDiffuseProfile, "CloudLayer" );
  297. }
  298. void CloudLayer::_initBuffers()
  299. {
  300. // Vertex Buffer...
  301. Point3F vertScale( 16.0f, 16.0f, mHeight );
  302. F32 zOffset = -( mCos( mSqrt( 1.0f ) ) + 0.01f );
  303. mVB.set( GFX, smVertCount, GFXBufferTypeStatic );
  304. GFXCloudVertex *pVert = mVB.lock();
  305. if(!pVert) return;
  306. for ( U32 y = 0; y < smVertStride; y++ )
  307. {
  308. F32 v = ( (F32)y / (F32)smStrideMinusOne - 0.5f ) * 2.0f;
  309. for ( U32 x = 0; x < smVertStride; x++ )
  310. {
  311. F32 u = ( (F32)x / (F32)smStrideMinusOne - 0.5f ) * 2.0f;
  312. F32 sx = u;
  313. F32 sy = v;
  314. F32 sz = mCos( mSqrt( sx*sx + sy*sy ) ) + zOffset;
  315. //F32 sz = 1.0f;
  316. pVert->point.set( sx, sy, sz );
  317. pVert->point *= vertScale;
  318. // The vert to our right.
  319. Point3F rpnt;
  320. F32 ru = ( (F32)( x + 1 ) / (F32)smStrideMinusOne - 0.5f ) * 2.0f;
  321. F32 rv = v;
  322. rpnt.x = ru;
  323. rpnt.y = rv;
  324. rpnt.z = mCos( mSqrt( rpnt.x*rpnt.x + rpnt.y*rpnt.y ) ) + zOffset;
  325. rpnt *= vertScale;
  326. // The vert to our front.
  327. Point3F fpnt;
  328. F32 fu = u;
  329. F32 fv = ( (F32)( y + 1 ) / (F32)smStrideMinusOne - 0.5f ) * 2.0f;
  330. fpnt.x = fu;
  331. fpnt.y = fv;
  332. fpnt.z = mCos( mSqrt( fpnt.x*fpnt.x + fpnt.y*fpnt.y ) ) + zOffset;
  333. fpnt *= vertScale;
  334. Point3F fvec = fpnt - pVert->point;
  335. fvec.normalize();
  336. Point3F rvec = rpnt - pVert->point;
  337. rvec.normalize();
  338. pVert->normal = mCross( fvec, rvec );
  339. pVert->normal.normalize();
  340. pVert->binormal = fvec;
  341. pVert->tangent = rvec;
  342. pVert->texCoord.set( u, v );
  343. pVert++;
  344. }
  345. }
  346. mVB.unlock();
  347. // Primitive Buffer...
  348. mPB.set( GFX, smTriangleCount * 3, smTriangleCount, GFXBufferTypeStatic );
  349. U16 *pIdx = NULL;
  350. mPB.lock(&pIdx);
  351. U32 curIdx = 0;
  352. for ( U32 y = 0; y < smStrideMinusOne; y++ )
  353. {
  354. for ( U32 x = 0; x < smStrideMinusOne; x++ )
  355. {
  356. U32 offset = x + y * smVertStride;
  357. pIdx[curIdx] = offset;
  358. curIdx++;
  359. pIdx[curIdx] = offset + 1;
  360. curIdx++;
  361. pIdx[curIdx] = offset + smVertStride + 1;
  362. curIdx++;
  363. pIdx[curIdx] = offset;
  364. curIdx++;
  365. pIdx[curIdx] = offset + smVertStride + 1;
  366. curIdx++;
  367. pIdx[curIdx] = offset + smVertStride;
  368. curIdx++;
  369. }
  370. }
  371. mPB.unlock();
  372. }