BVB Source Codes

CRYENGINE Show ParticleSystem.cpp Source code

Return Download CRYENGINE: download ParticleSystem.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  Created:     06/04/2014 by Filipe amim
  5. //  Description:
  6. // -------------------------------------------------------------------------
  7. //
  8. ////////////////////////////////////////////////////////////////////////////
  9.  
  10. #include "StdAfx.h"
  11. #include <CrySerialization/IArchiveHost.h>
  12. #include <CrySystem/ZLib/IZLibCompressor.h>
  13. #include "CryExtension/CryCreateClassInstance.h"
  14. #include <CryString/StringUtils.h>
  15. #include "ParticleSystem.h"
  16. #include "ParticleEffect.h"
  17. #include "ParticleEmitter.h"
  18.  
  19. CRY_PFX2_DBG
  20.  
  21. namespace pfx2
  22. {
  23.  
  24. CRYREGISTER_SINGLETON_CLASS(CParticleSystem)
  25.  
  26. std::vector<SParticleFeatureParams> &GetFeatureParams()
  27. {
  28.         static std::vector<SParticleFeatureParams> featureParams;
  29.         return featureParams;
  30. }
  31.  
  32. CParticleSystem* GetPSystem()
  33. {
  34.         FUNCTION_PROFILER(GetISystem(), PROFILE_PARTICLE);
  35.         static std::shared_ptr<IParticleSystem> pSystem(GetIParticleSystem());
  36.         return static_cast<CParticleSystem*>(pSystem.get());
  37. };
  38.  
  39. CParticleSystem::CParticleSystem()
  40.         : m_memHeap(gEnv->pJobManager->GetNumWorkerThreads() + 1)
  41. {
  42. }
  43.  
  44. PParticleEffect CParticleSystem::CreateEffect()
  45. {
  46.         return new pfx2::CParticleEffect();
  47. }
  48.  
  49. void CParticleSystem::RenameEffect(PParticleEffect pEffect, cstr name)
  50. {
  51.         CRY_PFX2_ASSERT(pEffect.get());
  52.         CParticleEffect* pCEffect = CastEffect(pEffect);
  53.  
  54.         if (pCEffect->GetName())
  55.                 m_effects[pCEffect->GetName()] = nullptr;
  56.         pCEffect->SetName(name);
  57.         m_effects[pCEffect->GetName()] = pCEffect;
  58. }
  59.  
  60. PParticleEffect CParticleSystem::FindEffect(cstr name)
  61. {
  62.         auto it = m_effects.find(name);
  63.         if (it != m_effects.end())
  64.                 return it->second;
  65.         return LoadEffect(name);
  66. }
  67.  
  68. PParticleEmitter CParticleSystem::CreateEmitter(PParticleEffect pEffect)
  69. {
  70.         MEMSTAT_CONTEXT(EMemStatContextTypes::MSC_Other, 0, "pfx2::ParticleEmitter");
  71.         PARTICLE_LIGHT_PROFILER();
  72.  
  73.         CRY_PFX2_ASSERT(pEffect.get());
  74.         CParticleEffect* pCEffect = CastEffect(pEffect);
  75.         pCEffect->Compile();
  76.  
  77.         _smart_ptr<CParticleEmitter> pEmitter = new CParticleEmitter(m_nextEmitterId++);
  78.         pEmitter->SetCEffect(pCEffect);
  79.         m_emitters.push_back(pEmitter);
  80.         return pEmitter;
  81. }
  82.  
  83. uint CParticleSystem::GetNumFeatureParams() const
  84. {
  85.         return uint(GetFeatureParams().size());
  86. }
  87.  
  88. SParticleFeatureParams& CParticleSystem::GetFeatureParam(uint featureIdx) const
  89. {
  90.         return GetFeatureParams()[featureIdx];
  91. }
  92.  
  93. void CParticleSystem::OnFrameStart()
  94. {
  95. }
  96.  
  97. void CParticleSystem::TrimEmitters()
  98. {
  99.         for (auto& pEmitter : m_emitters)
  100.         {
  101.                 bool independent = pEmitter->IsIndependent();
  102.                 bool hasParticles = pEmitter->HasParticles();
  103.                 bool isAlive = pEmitter->IsAlive();
  104.                 bool expired = isAlive && independent && !hasParticles;
  105.                 if (!expired)
  106.                         continue;
  107.                 pEmitter = 0;
  108.         }
  109.  
  110.         m_emitters.erase(
  111.           std::remove(m_emitters.begin(), m_emitters.end(), _smart_ptr<CParticleEmitter>(0)),
  112.           m_emitters.end());
  113. }
  114.  
  115. void CParticleSystem::Update()
  116. {
  117.         FUNCTION_PROFILER(GetISystem(), PROFILE_PARTICLE);
  118.         PARTICLE_LIGHT_PROFILER();
  119.  
  120.         const CCamera& camera = gEnv->p3DEngine->GetRenderingCamera();
  121.         const QuatT currentCameraPose = QuatT(camera.GetMatrix());
  122.         if (m_cameraMotion.q.GetLength() == 0.0f)
  123.         {
  124.                 m_cameraMotion = IDENTITY;
  125.         }
  126.         else
  127.         {
  128.                 m_cameraMotion.t = currentCameraPose.t - m_lastCameraPose.t;
  129.                 m_cameraMotion.q = currentCameraPose.q * m_lastCameraPose.q.GetInverted();
  130.         }
  131.         m_lastCameraPose = currentCameraPose;
  132.  
  133.         if (GetCVars()->e_Particles)
  134.         {
  135.                 auto gpuMan = gEnv->pRenderer->GetGpuParticleManager();
  136.                 gpuMan->BeginFrame();
  137.  
  138.                 for (auto& it : m_memHeap)
  139.                         CRY_PFX2_ASSERT(it.GetTotalMemory().nUsed == 0);  // some emitter leaked memory on mem stack
  140.  
  141.                 m_counts = SParticleCounts();
  142.                 for (auto& pEmitter : m_emitters)
  143.                         pEmitter->AccumCounts(m_counts);
  144.                 m_profiler.Display();
  145.  
  146.                 TrimEmitters();
  147.  
  148.                 for (auto& pEmitter : m_emitters)
  149.                 {
  150.                         pEmitter->Update();
  151.                         m_jobManager.AddEmitter(pEmitter);
  152.                 }
  153.  
  154.                 for (auto& pEmitter : m_emitters)
  155.                         UpdateGpuRuntimesForEmitter(pEmitter);
  156.  
  157.                 m_jobManager.KernelUpdateAll();
  158.         }
  159. }
  160.  
  161. void CParticleSystem::SyncronizeUpdateKernels()
  162. {
  163.         FUNCTION_PROFILER_3DENGINE;
  164.  
  165.         m_jobManager.SynchronizeUpdate();
  166.         for (auto& pEmitter : m_emitters)
  167.                 pEmitter->PostUpdate();
  168. }
  169.  
  170. void CParticleSystem::DeferredRender()
  171. {
  172.         m_jobManager.DeferredRender();
  173.         DebugParticleSystem(m_emitters);
  174. }
  175.  
  176. void CParticleSystem::ClearRenderResources()
  177. {
  178.         m_emitters.clear();
  179.         for (auto it = m_effects.begin(); it != m_effects.end(); )
  180.         {
  181.                 if (!it->second || it->second->Unique())
  182.                         it = m_effects.erase(it);
  183.                 else
  184.                         ++it;
  185.         }
  186. }
  187.  
  188. void CParticleSystem::Reset()
  189. {
  190. }
  191.  
  192. void CParticleSystem::Serialize(TSerialize ser)
  193. {
  194. }
  195.  
  196. PParticleEffect CParticleSystem::LoadEffect(cstr effectName)
  197. {
  198.         MEMSTAT_CONTEXT(EMemStatContextTypes::MSC_Other, 0, "pfx2::LoadEffect");
  199.         MEMSTAT_CONTEXT_FMT(EMemStatContextTypes::MSC_ParticleLibrary, 0, "Particle effect (%s)", effectName);
  200.  
  201.         if (gEnv->pCryPak->IsFileExist(effectName))
  202.         {
  203.                 PParticleEffect pEffect = CreateEffect();
  204.                 if (Serialization::LoadJsonFile(*CastEffect(pEffect), effectName))
  205.                 {
  206.                         RenameEffect(pEffect, effectName);
  207.                         return pEffect;
  208.                 }
  209.         }
  210.  
  211.         m_effects[effectName] = _smart_ptr<CParticleEffect>();
  212.         return PParticleEffect();
  213. }
  214.  
  215. void CParticleSystem::UpdateGpuRuntimesForEmitter(CParticleEmitter* pEmitter)
  216. {
  217.         FUNCTION_PROFILER_3DENGINE
  218.  
  219.         const auto& runtimeRefs = pEmitter->GetRuntimes();
  220.  
  221.         for (uint i = 0; i < runtimeRefs.size(); ++i)
  222.         {
  223.                 auto pGpuRuntime = runtimeRefs[i].pRuntime->GetGpuRuntime();
  224.                 if (!pGpuRuntime)
  225.                         continue;
  226.                 const bool isActive = pGpuRuntime->IsActive();
  227.                 if (isActive)
  228.                 {
  229.                         gpu_pfx2::SEnvironmentParameters params;
  230.                         params.physAccel = pEmitter->GetPhysicsEnv().m_UniformForces.vAccel;
  231.                         params.physWind = pEmitter->GetPhysicsEnv().m_UniformForces.vWind;
  232.                         pGpuRuntime->SetEnvironmentParameters(params);
  233.                         pGpuRuntime->SetEmitterData(pEmitter);
  234.                 }
  235.         }
  236. }
  237.  
  238. void CParticleSystem::GetCounts(SParticleCounts& counts)
  239. {
  240.         counts = m_counts;
  241. }
  242.  
  243. void CParticleSystem::GetMemoryUsage(ICrySizer* pSizer) const
  244. {
  245. }
  246.  
  247. ILINE CParticleEffect* CParticleSystem::CastEffect(const PParticleEffect& pEffect) const
  248. {
  249.         return static_cast<CParticleEffect*>(pEffect.get());
  250. }
  251.  
  252. ILINE CParticleEmitter* CParticleSystem::CastEmitter(const PParticleEmitter& pEmitter) const
  253. {
  254.         return static_cast<CParticleEmitter*>(pEmitter.get());
  255. }
  256.  
  257. uint GetVersion(Serialization::IArchive& ar)
  258. {
  259.         SSerializationContext* pContext = ar.context<SSerializationContext>();
  260.         if (!pContext)
  261.                 return gCurrentVersion;
  262.         return pContext->m_documentVersion;
  263. }
  264.  
  265. }
  266.  
downloadParticleSystem.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