BVB Source Codes

CRYENGINE Show FeatureField.cpp Source code

Return Download CRYENGINE: download FeatureField.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  Created:     14/01/2015 by Filipe amim
  5. //  Description:
  6. // -------------------------------------------------------------------------
  7. //
  8. ////////////////////////////////////////////////////////////////////////////
  9.  
  10. #include "StdAfx.h"
  11. #include <CrySerialization/SmartPtr.h>
  12. #include <CrySerialization/Math.h>
  13. #include "ParticleSystem/ParticleFeature.h"
  14. #include "ParamMod.h"
  15.  
  16. #include <CryRenderer/IGpuParticles.h>
  17.  
  18. CRY_PFX2_DBG
  19.  
  20. namespace pfx2
  21. {
  22.  
  23. EParticleDataType PDT(EPDT_Alpha, float, 1, BHasInit(true));
  24.  
  25. //////////////////////////////////////////////////////////////////////////
  26. // CFeatureFieldOpacity
  27.  
  28. class CFeatureFieldOpacity : public CParticleFeature
  29. {
  30. public:
  31.         CRY_PFX2_DECLARE_FEATURE
  32.  
  33.         CFeatureFieldOpacity()
  34.                 : m_alphaScale(0, 1)
  35.                 , m_clipLow(0, 0)
  36.                 , m_clipRange(1, 1)
  37.                 , CParticleFeature(gpu_pfx2::eGpuFeatureType_FieldOpacity)
  38.         {
  39.         }
  40.  
  41.         virtual void AddToComponent(CParticleComponent* pComponent, SComponentParams* pParams) override
  42.         {
  43.                 m_opacity.AddToComponent(pComponent, this, EPDT_Alpha);
  44.  
  45.                 pParams->m_shaderData.m_alphaTest[0][0] = m_alphaScale.x;
  46.                 pParams->m_shaderData.m_alphaTest[1][0] = m_alphaScale.y - m_alphaScale.x;
  47.                 pParams->m_shaderData.m_alphaTest[0][1] = m_clipLow.x;
  48.                 pParams->m_shaderData.m_alphaTest[1][1] = m_clipLow.y - m_clipLow.x;
  49.                 pParams->m_shaderData.m_alphaTest[0][2] = m_clipRange.x;
  50.                 pParams->m_shaderData.m_alphaTest[1][2] = m_clipRange.y - m_clipRange.x;
  51.  
  52.                 if (auto pInt = GetGpuInterface())
  53.                 {
  54.                         const int numSamples = gpu_pfx2::kNumModifierSamples;
  55.                         float samples[numSamples];
  56.                         m_opacity.Sample(samples, numSamples);
  57.                         gpu_pfx2::SFeatureParametersOpacity parameters;
  58.                         parameters.samples = samples;
  59.                         parameters.numSamples = numSamples;
  60.                         parameters.alphaScale = m_alphaScale;
  61.                         parameters.clipLow = m_clipLow;
  62.                         parameters.clipRange = m_clipRange;
  63.                         pInt->SetParameters(parameters);
  64.                 }
  65.                 pComponent->AddToUpdateList(EUL_Update, this);
  66.         }
  67.  
  68.         virtual void Serialize(Serialization::IArchive& ar) override
  69.         {
  70.                 CParticleFeature::Serialize(ar);
  71.                 ar(m_opacity, "value", "Value");
  72.                 ar(m_alphaScale, "AlphaScale", "Alpha Scale");
  73.                 ar(m_clipLow, "ClipLow", "Clip Low");
  74.                 ar(m_clipRange, "ClipRange", "Clip Range");
  75.         }
  76.  
  77.         virtual void InitParticles(const SUpdateContext& context) override
  78.         {
  79.                 CRY_PFX2_PROFILE_DETAIL;
  80.                 m_opacity.InitParticles(context, EPDT_Alpha);
  81.         }
  82.  
  83.         virtual void Update(const SUpdateContext& context) override
  84.         {
  85.                 CRY_PFX2_PROFILE_DETAIL;
  86.                 m_opacity.Update(context, EPDT_Alpha);
  87.         }
  88.  
  89. private:
  90.         CParamMod<SModParticleField, UUnitFloat> m_opacity;
  91.         Vec2 m_alphaScale;
  92.         Vec2 m_clipLow, m_clipRange;
  93. };
  94.  
  95. CRY_PFX2_IMPLEMENT_FEATURE(CParticleFeature, CFeatureFieldOpacity, "Field", "Opacity", colorField);
  96.  
  97. //////////////////////////////////////////////////////////////////////////
  98. // CFeatureFieldSize
  99.  
  100. EParticleDataType PDT(EPDT_Size, float, 1, BHasInit(true));
  101.  
  102. class CFeatureFieldSize : public CParticleFeature
  103. {
  104. public:
  105.         CRY_PFX2_DECLARE_FEATURE
  106.  
  107.         CFeatureFieldSize() : CParticleFeature(gpu_pfx2::eGpuFeatureType_FieldSize) {}
  108.  
  109.         virtual void AddToComponent(CParticleComponent* pComponent, SComponentParams* pParams) override
  110.         {
  111.                 m_size.AddToComponent(pComponent, this, EPDT_Size);
  112.                 pParams->m_maxParticleSize = max(pParams->m_maxParticleSize, m_size.GetValueRange().end);
  113.  
  114.                 if (auto gpuInt = GetGpuInterface())
  115.                 {
  116.                         const int numSamples = gpu_pfx2::kNumModifierSamples;
  117.                         float samples[numSamples];
  118.                         m_size.Sample(samples, numSamples);
  119.                         gpu_pfx2::SFeatureParametersSizeTable sizeTable;
  120.                         sizeTable.samples = samples;
  121.                         sizeTable.numSamples = numSamples;
  122.                         gpuInt->SetParameters(sizeTable);
  123.                 }
  124.         }
  125.  
  126.         virtual void Serialize(Serialization::IArchive& ar) override
  127.         {
  128.                 CParticleFeature::Serialize(ar);
  129.                 ar(m_size, "value", "Value");
  130.         }
  131.  
  132.         virtual EFeatureType GetFeatureType() override
  133.         {
  134.                 return EFT_Size;
  135.         }
  136.  
  137.         virtual void InitParticles(const SUpdateContext& context) override
  138.         {
  139.                 CRY_PFX2_PROFILE_DETAIL;
  140.                 m_size.InitParticles(context, EPDT_Size);
  141.         }
  142.  
  143.         virtual void Update(const SUpdateContext& context) override
  144.         {
  145.                 CRY_PFX2_PROFILE_DETAIL;
  146.                 m_size.Update(context, EPDT_Size);
  147.         }
  148.  
  149. private:
  150.         CParamMod<SModParticleField, UFloat10> m_size;
  151. };
  152.  
  153. CRY_PFX2_IMPLEMENT_FEATURE(CParticleFeature, CFeatureFieldSize, "Field", "Size", colorField);
  154.  
  155. //////////////////////////////////////////////////////////////////////////
  156. // CFeatureFieldPixelSize
  157.  
  158. class CFeatureFieldPixelSize : public CParticleFeature
  159. {
  160. public:
  161.         CRY_PFX2_DECLARE_FEATURE
  162.  
  163.         CFeatureFieldPixelSize()
  164.                 : m_minSize(4.0f)
  165.                 , m_maxSize(0.0f)
  166.                 , m_initAlphas(false)
  167.                 , m_affectOpacity(true)
  168.                 , CParticleFeature(gpu_pfx2::eGpuFeatureType_FieldPixelSize) {}
  169.  
  170.         virtual void AddToComponent(CParticleComponent* pComponent, SComponentParams* pParams) override
  171.         {
  172.                 pComponent->AddParticleData(EPVF_Position);
  173.                 pComponent->AddParticleData(EPDT_Size);
  174.                 if (m_affectOpacity)
  175.                 {
  176.                         m_initAlphas = !pComponent->UseParticleData(EPDT_Alpha);
  177.                         pComponent->AddParticleData(EPDT_Alpha);
  178.                 }
  179.                 pComponent->AddToUpdateList(EUL_Update, this);
  180.  
  181.                 if (auto gpuInt = GetGpuInterface())
  182.                 {
  183.                         gpu_pfx2::SFeatureParametersPixelSize params;
  184.                         params.minSize = m_minSize;
  185.                         params.maxSize = m_maxSize;
  186.                         params.affectOpacity = m_affectOpacity;
  187.                         gpuInt->SetParameters(params);
  188.                 }
  189.         }
  190.  
  191.         virtual void Serialize(Serialization::IArchive& ar) override
  192.         {
  193.                 CParticleFeature::Serialize(ar);
  194.                 ar(m_minSize, "MinSize", "Minimum Pixel Size");
  195.                 ar(m_maxSize, "MaxSize", "Maximum Pixel Size");
  196.                 ar(m_affectOpacity, "AffectOpacity", "Affect Opacity");
  197.         }
  198.  
  199.         virtual void Update(const SUpdateContext& context) override
  200.         {
  201.                 CRY_PFX2_PROFILE_DETAIL;
  202.  
  203.                 const CCamera& camera = gEnv->p3DEngine->GetRenderingCamera();
  204.                 const Planev projectionPlane = ToPlanev(Plane(camera.GetViewdir(), -camera.GetPosition().dot(camera.GetViewdir())));
  205.  
  206.                 const float height = float(gEnv->pRenderer->GetHeight());
  207.                 const float fov = camera.GetFov();
  208.                 const float minPixelSizeF = m_minSize > 0.0f ? float(m_minSize) : 0.0f;
  209.                 const floatv minPixelSize = ToFloatv(minPixelSizeF);
  210.                 const floatv invMinPixelSize = ToFloatv(minPixelSizeF != 0.0f ? 1.0f / minPixelSizeF : 1.0f);
  211.                 const floatv maxPixelSize = ToFloatv(m_maxSize > 0.0f ? float(m_maxSize) : std::numeric_limits<float>::max());
  212.                 const floatv minDrawPixels = ToFloatv(fov / height * 0.5f);
  213.                 const floatv epsilon = ToFloatv(1.0f / 1024.0f);
  214.  
  215.                 CParticleContainer& container = context.m_container;
  216.                 IVec3Stream positions = container.GetIVec3Stream(EPVF_Position);
  217.                 IOFStream sizes = container.GetIOFStream(EPDT_Size);
  218.                 IFStream inputAlphas = m_initAlphas ? IFStream(nullptr, 1.0f) : container.GetIFStream(EPDT_Alpha);
  219.                 IOFStream outputAlphas = container.GetIOFStream(EPDT_Alpha);
  220.  
  221.                 CRY_PFX2_FOR_ACTIVE_PARTICLESGROUP(context)
  222.                 {
  223.                         const Vec3v position = positions.Load(particleGroupId);
  224.                         const floatv size0 = sizes.Load(particleGroupId);
  225.                         const floatv distance = max(epsilon, projectionPlane.DistFromPlane(position));
  226.                         const floatv pixelSize0 = rcp_fast(distance * minDrawPixels) * size0;
  227.                         const floatv pixelSize1 = clamp(pixelSize0, minPixelSize, maxPixelSize);
  228.                         const floatv size1 = pixelSize1 * distance * minDrawPixels;
  229.                         sizes.Store(particleGroupId, size1);
  230.  
  231.                         if (m_affectOpacity)
  232.                         {
  233.                                 const floatv alpha0 = inputAlphas.SafeLoad(particleGroupId);
  234.                                 const floatv alpha1 = alpha0 * saturate(pixelSize0 * invMinPixelSize);
  235.                                 outputAlphas.Store(particleGroupId, alpha1);
  236.                         }
  237.                 }
  238.                 CRY_PFX2_FOR_END;
  239.         }
  240.  
  241. private:
  242.         UFloat10 m_minSize;
  243.         UFloat10 m_maxSize;
  244.         bool     m_affectOpacity;
  245.         bool     m_initAlphas;
  246. };
  247.  
  248. CRY_PFX2_IMPLEMENT_FEATURE(CParticleFeature, CFeatureFieldPixelSize, "Field", "PixelSize", colorField);
  249.  
  250. }
  251.  
downloadFeatureField.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