BVB Source Codes

CRYENGINE Show WaterRippleManager.cpp Source code

Return Download CRYENGINE: download WaterRippleManager.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "StdAfx.h"
  4. #include "WaterRippleManager.h"
  5.  
  6. int CWaterRippleManager::OnEventPhysCollision(const EventPhys* pEvent)
  7. {
  8.         EventPhysCollision* pEventPhysArea = (EventPhysCollision*)pEvent;
  9.         if (!pEventPhysArea)
  10.                 return 1;
  11.  
  12.         // only add nearby hits
  13.         if (pEventPhysArea->idmat[1] == gEnv->pPhysicalWorld->GetWaterMat())
  14.         {
  15.                 // Compute the momentum of the object.
  16.                 // Clamp the mass so that particles and other "massless" objects still cause ripples.
  17.                 const float v = pEventPhysArea->vloc[0].GetLength();
  18.                 float fMomentum = v * max(pEventPhysArea->mass[0], 0.025f);
  19.  
  20.                 // Removes small velocity collisions because too many small collisions cause too strong ripples.
  21.                 const float velRampMin = 1.0f;
  22.                 const float velRampMax = 10.0f;
  23.                 const float t = min(1.0f, max(0.0f, (v - velRampMin) / (velRampMax - velRampMin)));
  24.                 const float smoothstep = t * t * (3.0f - 2.0f * t);
  25.                 fMomentum *= smoothstep;
  26.  
  27.                 if(gEnv->p3DEngine && fMomentum > 0.0f)
  28.                 {
  29.                         gEnv->p3DEngine->AddWaterRipple(pEventPhysArea->pt, 1.0f, fMomentum);
  30.                 }
  31.         }
  32.  
  33.         return 1;
  34. }
  35.  
  36. CWaterRippleManager::CWaterRippleManager()
  37. {
  38. }
  39.  
  40. CWaterRippleManager::~CWaterRippleManager()
  41. {
  42.         Finalize();
  43. }
  44.  
  45. void CWaterRippleManager::GetMemoryUsage(ICrySizer* pSizer) const
  46. {
  47.         pSizer->AddObject(this, sizeof(*this));
  48.         pSizer->AddObject(m_waterRippleInfos);
  49. }
  50.  
  51. void CWaterRippleManager::Initialize()
  52. {
  53.         if (gEnv->pPhysicalWorld)
  54.         {
  55.                 gEnv->pPhysicalWorld->AddEventClient(EventPhysCollision::id, &OnEventPhysCollision, 1);
  56.         }
  57. }
  58.  
  59. void CWaterRippleManager::Finalize()
  60. {
  61.         if (gEnv->pPhysicalWorld)
  62.         {
  63.                 gEnv->pPhysicalWorld->RemoveEventClient(EventPhysCollision::id, &OnEventPhysCollision, 1);
  64.         }
  65. }
  66.  
  67. void CWaterRippleManager::OnFrameStart()
  68. {
  69.         m_waterRippleInfos.clear();
  70. }
  71.  
  72. void CWaterRippleManager::Render(const SRenderingPassInfo& passInfo)
  73. {
  74.         auto* pRenderView = passInfo.GetIRenderView();
  75.  
  76.         if (pRenderView && passInfo.IsRecursivePass())
  77.         {
  78.                 return;
  79.         }
  80.  
  81.         const float simGridSize = static_cast<float>(SWaterRippleInfo::WaveSimulationGridSize);
  82.         const float simGridRadius = 1.41421356f * 0.5f * simGridSize;
  83.         const float looseSimGridRadius = simGridRadius * 1.5f;
  84.         const float looseSimGridRadius2 = looseSimGridRadius * looseSimGridRadius;
  85.         const Vec3 camPos3D = passInfo.GetCamera().GetPosition();
  86.         const Vec2 camPos2D(camPos3D.x, camPos3D.y);
  87.  
  88.         for (auto& ripple : m_waterRippleInfos)
  89.         {
  90.                 // add a ripple which is approximately in the water simulation grid.
  91.                 const Vec2 pos2D(ripple.position.x, ripple.position.y);
  92.                 if ((pos2D - camPos2D).GetLength2() <= looseSimGridRadius2)
  93.                 {
  94.                         pRenderView->AddWaterRipple(ripple);
  95.                 }
  96.         }
  97. }
  98.  
  99. void CWaterRippleManager::AddWaterRipple(const Vec3 position, float scale, float strength)
  100. {
  101.         if (m_waterRippleInfos.size() < SWaterRippleInfo::MaxWaterRipplesInScene)
  102.         {
  103.                 m_waterRippleInfos.emplace_back(position, scale, strength);
  104.         }
  105. }
  106.  
downloadWaterRippleManager.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top