afxRenderHighlightMgr.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  25. // The afxRenderHighlightMgr class is adapted from the resource,
  26. // "Silhoute selection via postFX for Torque3D" posted by Konrad Kiss.
  27. // http://www.garagegames.com/community/resources/view/17821
  28. // Supporting code mods in other areas of the engine are marked as
  29. // "(selection-highlight)".
  30. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  31. #include "platform/platform.h"
  32. #include "afxRenderHighlightMgr.h"
  33. #include "scene/sceneManager.h"
  34. #include "scene/sceneRenderState.h"
  35. #include "materials/sceneData.h"
  36. #include "materials/matInstance.h"
  37. //#include "materials/materialFeatureTypes.h"
  38. #include "materials/processedMaterial.h"
  39. #include "postFx/postEffect.h"
  40. #include "gfx/gfxTransformSaver.h"
  41. #include "gfx/gfxDebugEvent.h"
  42. #include "math/util/matrixSet.h"
  43. IMPLEMENT_CONOBJECT( afxRenderHighlightMgr );
  44. afxRenderHighlightMgr::afxRenderHighlightMgr()
  45. : RenderTexTargetBinManager( RenderPassManager::RIT_Mesh,
  46. 1.0f,
  47. 1.0f,
  48. GFXFormatR8G8B8A8,
  49. Point2I( 512, 512 ) )
  50. {
  51. mNamedTarget.registerWithName( "highlight" );
  52. mTargetSizeType = WindowSize;
  53. }
  54. afxRenderHighlightMgr::~afxRenderHighlightMgr()
  55. {
  56. }
  57. PostEffect* afxRenderHighlightMgr::getSelectionEffect()
  58. {
  59. if ( !mSelectionEffect )
  60. mSelectionEffect = dynamic_cast<PostEffect*>( Sim::findObject( "afxHighlightPostFX" ) );
  61. return mSelectionEffect;
  62. }
  63. bool afxRenderHighlightMgr::isSelectionEnabled()
  64. {
  65. return getSelectionEffect() && getSelectionEffect()->isEnabled();
  66. }
  67. void afxRenderHighlightMgr::addElement( RenderInst *inst )
  68. {
  69. // Skip out if we don't have the selection post
  70. // effect enabled at this time.
  71. if ( !isSelectionEnabled() )
  72. return;
  73. // Skip it if we don't have a selection material.
  74. BaseMatInstance *matInst = getMaterial( inst );
  75. if ( !matInst || !matInst->needsSelectionHighlighting() )
  76. return;
  77. internalAddElement(inst);
  78. }
  79. void afxRenderHighlightMgr::render( SceneRenderState *state )
  80. {
  81. PROFILE_SCOPE( RenderSelectionMgr_Render );
  82. if ( !isSelectionEnabled() )
  83. return;
  84. const U32 binSize = mElementList.size();
  85. // If this is a non-diffuse pass or we have no objects to
  86. // render then tell the effect to skip rendering.
  87. if ( !state->isDiffusePass() || binSize == 0 )
  88. {
  89. getSelectionEffect()->setSkip( true );
  90. return;
  91. }
  92. GFXDEBUGEVENT_SCOPE( RenderSelectionMgr_Render, ColorI::GREEN );
  93. GFXTransformSaver saver;
  94. // Tell the superclass we're about to render, preserve contents
  95. const bool isRenderingToTarget = _onPreRender( state, true );
  96. // Clear all the buffers to black.
  97. //GFX->clear( GFXClearTarget, ColorI::BLACK, 1.0f, 0);
  98. GFX->clear( GFXClearTarget, ColorI::ZERO, 1.0f, 0);
  99. // Restore transforms
  100. MatrixSet &matrixSet = getRenderPass()->getMatrixSet();
  101. matrixSet.restoreSceneViewProjection();
  102. // init loop data
  103. SceneData sgData;
  104. sgData.init( state, SceneData::HighlightBin );
  105. for( U32 j=0; j<binSize; )
  106. {
  107. MeshRenderInst *ri = static_cast<MeshRenderInst*>(mElementList[j].inst);
  108. setupSGData( ri, sgData );
  109. BaseMatInstance *mat = ri->matInst;
  110. U32 matListEnd = j;
  111. while( mat && mat->setupPass( state, sgData ) )
  112. {
  113. U32 a;
  114. for( a=j; a<binSize; a++ )
  115. {
  116. MeshRenderInst *passRI = static_cast<MeshRenderInst*>(mElementList[a].inst);
  117. if ( newPassNeeded( ri, passRI ) )
  118. break;
  119. matrixSet.setWorld(*passRI->objectToWorld);
  120. matrixSet.setView(*passRI->worldToCamera);
  121. matrixSet.setProjection(*passRI->projection);
  122. mat->setTransforms(matrixSet, state);
  123. // Setup HW skinning transforms if applicable
  124. if (mat->usesHardwareSkinning())
  125. {
  126. mat->setNodeTransforms(passRI->mNodeTransforms, passRI->mNodeTransformCount);
  127. }
  128. mat->setSceneInfo(state, sgData);
  129. mat->setBuffers(passRI->vertBuff, passRI->primBuff);
  130. if ( passRI->prim )
  131. GFX->drawPrimitive( *passRI->prim );
  132. else
  133. GFX->drawPrimitive( passRI->primBuffIndex );
  134. }
  135. matListEnd = a;
  136. setupSGData( ri, sgData );
  137. }
  138. // force increment if none happened, otherwise go to end of batch
  139. j = ( j == matListEnd ) ? j+1 : matListEnd;
  140. }
  141. // Finish up.
  142. if ( isRenderingToTarget )
  143. _onPostRender();
  144. // Make sure the effect is gonna render.
  145. getSelectionEffect()->setSkip( false );
  146. }