decalSphere.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/decalSphere.h"
  24. #include "scene/zones/sceneZoneSpaceManager.h"
  25. #include "T3D/decal/decalInstance.h"
  26. F32 DecalSphere::smDistanceTolerance = 30.0f;
  27. F32 DecalSphere::smRadiusTolerance = 40.0f;
  28. //-----------------------------------------------------------------------------
  29. bool DecalSphere::tryAddItem( DecalInstance* inst )
  30. {
  31. // If we are further away from the center than our tolerance
  32. // allows, don't use this sphere.
  33. //
  34. // Note that we take the distance from the nearest point on the
  35. // bounding sphere rather than the distance to the center of the
  36. // decal. This takes decal sizes into account and generally works
  37. // better.
  38. const F32 distCenterToCenter = ( mWorldSphere.center - inst->mPosition ).len();
  39. const F32 distBoundsToCenter = distCenterToCenter - inst->mSize / 2.f;
  40. if( distBoundsToCenter > smDistanceTolerance )
  41. return false;
  42. // Also, if adding the current decal to the sphere would make
  43. // it larger than our radius tolerance, don't use it.
  44. const F32 newRadius = distCenterToCenter + inst->mSize / 2.f + 0.5f;
  45. if( newRadius > mWorldSphere.radius && newRadius > smRadiusTolerance )
  46. return false;
  47. // Otherwise, go with this sphere and add the item to it.
  48. mItems.push_back( inst );
  49. // Update the sphere bounds, if necessary.
  50. if( newRadius > mWorldSphere.radius )
  51. updateWorldSphere();
  52. return true;
  53. }
  54. //-----------------------------------------------------------------------------
  55. void DecalSphere::updateWorldSphere()
  56. {
  57. AssertFatal( mItems.size() >= 1, "DecalSphere::updateWorldSphere - Sphere is empty!" );
  58. Box3F aabb( mItems[ 0 ]->getWorldBox() );
  59. const U32 numItems = mItems.size();
  60. for( U32 i = 1; i < numItems; ++ i )
  61. aabb.intersect( mItems[ i ]->getWorldBox() );
  62. mWorldSphere = aabb.getBoundingSphere();
  63. // Clear the zoning data so that it gets recomputed.
  64. mZones.clear();
  65. }
  66. //-----------------------------------------------------------------------------
  67. void DecalSphere::updateZoning( SceneZoneSpaceManager* zoneManager )
  68. {
  69. mZones.clear();
  70. // If there's only a single zone in the world, it must be the
  71. // outdoor zone, so there's no point maintaining zoning information
  72. // on all the decalspheres.
  73. if( zoneManager->getNumZones() == 1 )
  74. return;
  75. // Otherwise query the scene graph for all zones that intersect with the
  76. // AABB around our world sphere.
  77. Box3F aabb( mWorldSphere.radius );
  78. aabb.setCenter( mWorldSphere.center );
  79. zoneManager->findZones( aabb, mZones );
  80. }