cloudLayer.cpp 15 KB

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