decalInstance.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "T3D/decal/decalInstance.h"
  24. #include "scene/sceneRenderState.h"
  25. DecalInstance::DecalInstance()
  26. : mDataBlock(NULL),
  27. mRotAroundNormal(0.0f),
  28. mSize(0.0f),
  29. mCreateTime(0),
  30. mVisibility(1.0f),
  31. mLastAlpha(1.0f),
  32. mTextureRectIdx(0),
  33. mVerts(NULL),
  34. mIndices(NULL),
  35. mVertCount(0),
  36. mIndxCount(0),
  37. mFlags(0),
  38. mRenderPriority(0),
  39. mId(-1),
  40. mCustomTex(NULL)
  41. {}
  42. void DecalInstance::getWorldMatrix( MatrixF *outMat, bool flip )
  43. {
  44. outMat->setPosition( mPosition );
  45. Point3F fvec;
  46. mCross( mNormal, mTangent, &fvec );
  47. outMat->setColumn( 0, mTangent );
  48. outMat->setColumn( 1, fvec );
  49. outMat->setColumn( 2, mNormal );
  50. }
  51. F32 DecalInstance::calcPixelSize( U32 viewportHeight, const Point3F &cameraPos, F32 worldToScreenScaleY ) const
  52. {
  53. // If fadeStartPixelSize is set less than zero this is interpreted to mean
  54. // pixelSize based fading is disabled and the decal always renders.
  55. // Returning a value of F32_MAX should be treated by the caller as
  56. // meaning "big enough to render at normal quality, dont LOD me.".
  57. if ( mDataBlock->fadeStartPixelSize < 0.0f )
  58. return F32_MAX;
  59. // This is an approximation since mSize is actually the xyz size of the
  60. // cube we use to clip geometry for the decal. In this case we are assuming
  61. // it is appropriate to use the radius of the sphere 'inscribed' in that
  62. // cube as the equivalent of mShape->radius for purposes of calculating
  63. // the pixelScale ( see TSShapeInstance::setDetailFromDistance ).
  64. const F32 radius = mSize * 0.5f;
  65. // Approximate distance to the decal.
  66. const F32 distance = ( cameraPos - mPosition ).len() - radius;
  67. // We are inside the decal's volume. There is no useful pixelSize for us
  68. // to return, it is essentially the entire screen.
  69. if ( distance <= 0.0f )
  70. return F32_MAX;
  71. // Lod is relative to a viewport with a heigh of 300. Could prescale this into decalSize...
  72. const F32 pixelScale = viewportHeight / 300.0f;
  73. // Inline of SceneState::projectRadius.
  74. const F32 pixelSize = mSize / distance * worldToScreenScaleY * pixelScale;
  75. // Optionally scale pixelSize by a detail adjust value here...
  76. // eg. pixelSize *= TSDetailAdjust...
  77. return pixelSize;
  78. }