BVB Source Codes

CRYENGINE Show FeatureAppearance.cpp Source code

Return Download CRYENGINE: download FeatureAppearance.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  Created:     27/10/2014 by Filipe amim
  5. //  Description:
  6. // -------------------------------------------------------------------------
  7. //
  8. ////////////////////////////////////////////////////////////////////////////
  9.  
  10. #include "StdAfx.h"
  11. #include <CrySerialization/IArchive.h>
  12. #include <CrySerialization/Decorators/Resources.h>
  13. #include <CrySerialization/Enum.h>
  14. #include "ParticleSystem/ParticleFeature.h"
  15.  
  16. CRY_PFX2_DBG
  17.  
  18. namespace pfx2
  19. {
  20.  
  21. EParticleDataType PDT(EPDT_Tile, uint8);
  22.  
  23. SERIALIZATION_ENUM_DEFINE(EVariantMode, ,
  24.                           Random,
  25.                           Ordered
  26.                           )
  27.  
  28. class CFeatureAppearanceTextureTiling : public CParticleFeature
  29. {
  30. public:
  31.         CRY_PFX2_DECLARE_FEATURE
  32.  
  33.         CFeatureAppearanceTextureTiling()
  34.                 : m_variantMode(EVariantMode::Random)
  35.                 , CParticleFeature(gpu_pfx2::eGpuFeatureType_Dummy) {}
  36.  
  37.         uint VariantCount() const
  38.         {
  39.                 return m_tileCount / m_anim.m_frameCount;
  40.         }
  41.  
  42.         virtual void AddToComponent(CParticleComponent* pComponent, SComponentParams* pParams) override
  43.         {
  44.                 pParams->m_shaderData.m_tileSize[0] = 1.0f / float(m_tilesX);
  45.                 pParams->m_shaderData.m_tileSize[1] = 1.0f / float(m_tilesY);
  46.                 pParams->m_shaderData.m_frameCount = float(m_tileCount);
  47.                 pParams->m_shaderData.m_firstTile = float(m_firstTile);
  48.                 pParams->m_textureAnimation = m_anim;
  49.  
  50.                 if (m_anim.IsAnimating() && m_anim.m_frameBlending)
  51.                         pParams->m_renderStateFlags |= OS_ANIM_BLEND;
  52.  
  53.                 if (VariantCount() > 1)
  54.                 {
  55.                         pComponent->AddParticleData(EPDT_Tile);
  56.                         if (m_variantMode == EVariantMode::Ordered)
  57.                                 pComponent->AddParticleData(EPDT_SpawnId);
  58.                         pComponent->AddParticleData(EPDT_Tile);
  59.                         pComponent->AddToUpdateList(EUL_InitUpdate, this);
  60.                 }
  61.         }
  62.  
  63.         virtual void InitParticles(const SUpdateContext& context) override
  64.         {
  65.                 CRY_PROFILE_FUNCTION(PROFILE_PARTICLE);
  66.  
  67.                 if (m_variantMode == EVariantMode::Random)
  68.                         AssignTiles<EVariantMode::Random>(context);
  69.                 else
  70.                         AssignTiles<EVariantMode::Ordered>(context);
  71.         }
  72.  
  73.         virtual void Serialize(Serialization::IArchive& ar) override
  74.         {
  75.                 CParticleFeature::Serialize(ar);
  76.                 ar(m_tilesX, "TilesX", "Tiles X");
  77.                 ar(m_tilesY, "TilesY", "Tiles Y");
  78.                 ar(m_tileCount, "TileCount", "Tile Count");
  79.                 ar(m_firstTile, "FirstTile", "First Tile");
  80.                 ar(m_variantMode, "VariantMode", "Variant Mode");
  81.                 ar(m_anim, "Animation", "Animation");
  82.         }
  83.  
  84. private:
  85.         UBytePos          m_tilesX;
  86.         UBytePos          m_tilesY;
  87.         UBytePos          m_tileCount;
  88.         UByte             m_firstTile;
  89.         EVariantMode      m_variantMode;
  90.         STextureAnimation m_anim;
  91.  
  92.         template<EVariantMode mode>
  93.         void AssignTiles(const SUpdateContext& context)
  94.         {
  95.                 CParticleContainer& container = context.m_container;
  96.                 TIOStream<uint8> tiles = container.GetTIOStream<uint8>(EPDT_Tile);
  97.                 TIStream<uint> spawnIds = container.GetTIStream<uint>(EPDT_SpawnId);
  98.                 uint variantCount = VariantCount();
  99.  
  100.                 CRY_PFX2_FOR_SPAWNED_PARTICLES(context);
  101.                 uint32 tile;
  102.                 if (mode == EVariantMode::Random)
  103.                 {
  104.                         tile = context.m_spawnRng.Rand();
  105.                 }
  106.                 else if (mode == EVariantMode::Ordered)
  107.                 {
  108.                         tile = spawnIds.Load(particleId);
  109.                 }
  110.                 tile %= variantCount;
  111.                 tile *= m_anim.m_frameCount;
  112.                 tiles.Store(particleId, tile);
  113.                 CRY_PFX2_FOR_END;
  114.         }
  115.  
  116. };
  117.  
  118. CRY_PFX2_IMPLEMENT_FEATURE(CParticleFeature, CFeatureAppearanceTextureTiling, "Appearance", "Texture Tiling", colorAppearance);
  119.  
  120. class CFeatureAppearanceMaterial : public CParticleFeature
  121. {
  122. public:
  123.         CRY_PFX2_DECLARE_FEATURE
  124.  
  125.         CFeatureAppearanceMaterial()
  126.                 : CParticleFeature(gpu_pfx2::eGpuFeatureType_Dummy) {}
  127.  
  128.         virtual void AddToComponent(CParticleComponent* pComponent, SComponentParams* pParams) override
  129.         {
  130.                 if (!m_materialName.empty())
  131.                         pParams->m_pMaterial = gEnv->p3DEngine->GetMaterialManager()->LoadMaterial(m_materialName.c_str());
  132.                 if (!m_textureName.empty())
  133.                         pParams->m_diffuseMap = m_textureName;
  134.         }
  135.  
  136.         virtual void Serialize(Serialization::IArchive& ar) override
  137.         {
  138.                 using Serialization::MaterialPicker;
  139.                 using Serialization::TextureFilename;
  140.                 CParticleFeature::Serialize(ar);
  141.                 ar(MaterialPicker(m_materialName), "Material", "Material");
  142.                 ar(TextureFilename(m_textureName), "Texture", "Texture");
  143.         }
  144.  
  145. private:
  146.         string m_materialName;
  147.         string m_textureName;
  148. };
  149.  
  150. CRY_PFX2_IMPLEMENT_FEATURE(CParticleFeature, CFeatureAppearanceMaterial, "Appearance", "Material", colorAppearance);
  151.  
  152. class CFeatureAppearanceLighting : public CParticleFeature
  153. {
  154. public:
  155.         CRY_PFX2_DECLARE_FEATURE
  156.  
  157.         CFeatureAppearanceLighting()
  158.                 : m_albedo(100.0f)
  159.                 , m_backLight(0.0f)
  160.                 , m_emissive(0.0f)
  161.                 , m_curvature(0.0f)
  162.                 , m_receiveShadows(false)
  163.                 , m_affectedByFog(true)
  164.                 , CParticleFeature(gpu_pfx2::eGpuFeatureType_Dummy) {}
  165.  
  166.         virtual void AddToComponent(CParticleComponent* pComponent, SComponentParams* pParams) override
  167.         {
  168.                 // light energy normalizer for the light equation:
  169.                 //      L = max(0, cos(a)*(1-y)+y)
  170.                 //      cos(a) = dot(l, n)
  171.                 //      y = back lighting
  172.                 const float y = m_backLight.Get();
  173.                 const float energyNorm = (y < 0.5) ? (1 - y) : (1.0f / (4.0f * y));
  174.                 const float kiloScale = 1000.0f;
  175.                 const float toLightUnitScale = kiloScale / RENDERER_LIGHT_UNIT_SCALE;
  176.                 pParams->m_shaderData.m_diffuseLighting = (m_albedo * energyNorm) / 100.0f;
  177.                 pParams->m_shaderData.m_backLighting = m_backLight;
  178.                 pParams->m_shaderData.m_emissiveLighting = m_emissive * toLightUnitScale;
  179.                 pParams->m_shaderData.m_curvature = m_curvature;
  180.                 if (m_albedo >= FLT_EPSILON)
  181.                         pParams->m_renderObjectFlags |= FOB_LIGHTVOLUME;
  182.                 if (m_receiveShadows)
  183.                         pParams->m_renderObjectFlags |= FOB_INSHADOW;
  184.                 if (!m_affectedByFog)
  185.                         pParams->m_renderObjectFlags |= FOB_NO_FOG;
  186.         }
  187.  
  188.         virtual void Serialize(Serialization::IArchive& ar) override
  189.         {
  190.                 CParticleFeature::Serialize(ar);
  191.                 ar(m_albedo, "Albedo", "Albedo (%)");
  192.                 ar(m_backLight, "BackLight", "Back Light");
  193.                 ar(m_emissive, "Emissive", "Emissive (kcd/m2)");
  194.                 ar(m_curvature, "Curvature", "Curvature");
  195.                 ar(m_receiveShadows, "ReceiveShadows", "Receive Shadows");
  196.                 ar(m_affectedByFog, "AffectedByFog", "Affected by Fog");
  197.                 if (ar.isInput())
  198.                         VersionFix(ar);
  199.         }
  200.  
  201. private:
  202.         void VersionFix(Serialization::IArchive& ar)
  203.         {
  204.                 const float kiloScale = 1000.0f;
  205.                 const float toPhysicalScale = 1.0f / (kiloScale / RENDERER_LIGHT_UNIT_SCALE);
  206.                 float diffuse;
  207.                 switch (GetVersion(ar))
  208.                 {
  209.                 case 1:
  210.                         ar(diffuse, "Diffuse");
  211.                         m_albedo.Set(diffuse * 100.0f);
  212.                         m_emissive.Set(m_emissive * toPhysicalScale);
  213.                         break;
  214.                 }
  215.         }
  216.  
  217.         UFloat100  m_albedo;
  218.         UUnitFloat m_backLight;
  219.         UFloat10   m_emissive;
  220.         UUnitFloat m_curvature;
  221.         bool       m_receiveShadows;
  222.         bool       m_affectedByFog;
  223. };
  224.  
  225. CRY_PFX2_IMPLEMENT_FEATURE(CParticleFeature, CFeatureAppearanceLighting, "Appearance", "Lighting", colorAppearance);
  226.  
  227. SERIALIZATION_DECLARE_ENUM(EBlendMode,
  228.                            Opaque = 0,
  229.                            Alpha = OS_ALPHA_BLEND,
  230.                            Additive = OS_ADD_BLEND,
  231.                            Multiplicative = OS_MULTIPLY_BLEND
  232.                            )
  233.  
  234. class CFeatureAppearanceBlending : public CParticleFeature
  235. {
  236. public:
  237.         CRY_PFX2_DECLARE_FEATURE
  238.  
  239.         CFeatureAppearanceBlending()
  240.                 : m_blendMode(EBlendMode::Alpha)
  241.                 , CParticleFeature(gpu_pfx2::eGpuFeatureType_Dummy) {}
  242.  
  243.         virtual void AddToComponent(CParticleComponent* pComponent, SComponentParams* pParams) override
  244.         {
  245.                 pParams->m_renderStateFlags = (pParams->m_renderStateFlags & ~OS_TRANSPARENT) | (int)m_blendMode;
  246.         }
  247.  
  248.         virtual void Serialize(Serialization::IArchive& ar) override
  249.         {
  250.                 CParticleFeature::Serialize(ar);
  251.                 ar(m_blendMode, "BlendMode", "Blend Mode");
  252.         }
  253.  
  254. private:
  255.         EBlendMode m_blendMode;
  256. };
  257.  
  258. CRY_PFX2_IMPLEMENT_FEATURE(CParticleFeature, CFeatureAppearanceBlending, "Appearance", "Blending", colorAppearance);
  259.  
  260. class CFeatureAppearanceSoftIntersect : public CParticleFeature
  261. {
  262. public:
  263.         CRY_PFX2_DECLARE_FEATURE
  264.  
  265.         CFeatureAppearanceSoftIntersect()
  266.                 : m_softNess(1.0f)
  267.                 , CParticleFeature(gpu_pfx2::eGpuFeatureType_Dummy) {}
  268.  
  269.         virtual void AddToComponent(CParticleComponent* pComponent, SComponentParams* pParams) override
  270.         {
  271.                 if (m_softNess > 0.f)
  272.                 {
  273.                         pParams->m_renderObjectFlags |= FOB_SOFT_PARTICLE;
  274.                         pParams->m_shaderData.m_softnessMultiplier = m_softNess;
  275.                 }
  276.         }
  277.  
  278.         virtual void Serialize(Serialization::IArchive& ar) override
  279.         {
  280.                 CParticleFeature::Serialize(ar);
  281.                 ar(m_softNess, "Softness", "Softness");
  282.         }
  283.  
  284. private:
  285.         UFloat m_softNess;
  286. };
  287.  
  288. CRY_PFX2_IMPLEMENT_FEATURE(CParticleFeature, CFeatureAppearanceSoftIntersect, "Appearance", "SoftIntersect", colorAppearance);
  289.  
  290. class CFeatureAppearanceVisibility : public CParticleFeature, SVisibilityParams
  291. {
  292. public:
  293.         CRY_PFX2_DECLARE_FEATURE
  294.  
  295.         CFeatureAppearanceVisibility()
  296.                 : m_drawNear(false)
  297.                 , m_drawOnTop(false)
  298.         {}
  299.  
  300.         virtual void AddToComponent(CParticleComponent* pComponent, SComponentParams* pParams) override
  301.         {
  302.                 pParams->m_visibility = static_cast<const SVisibilityParams&>(*this);
  303.                 if (m_drawNear)
  304.                         pParams->m_renderObjectFlags |= FOB_NEAREST;
  305.                 if (m_drawOnTop)
  306.                         pParams->m_renderStateFlags |= OS_NODEPTH_TEST;
  307.         }
  308.  
  309.         virtual void Serialize(Serialization::IArchive& ar) override
  310.         {
  311.                 CParticleFeature::Serialize(ar);
  312.                 SERIALIZE_VAR(ar, m_maxScreenSize);
  313.                 SERIALIZE_VAR(ar, m_minCameraDistance);
  314.                 SERIALIZE_VAR(ar, m_maxCameraDistance);
  315.                 SERIALIZE_VAR(ar, m_viewDistanceMultiple);
  316.                 SERIALIZE_VAR(ar, m_indoorVisibility);
  317.                 SERIALIZE_VAR(ar, m_waterVisibility);
  318.                 SERIALIZE_VAR(ar, m_drawNear);
  319.                 SERIALIZE_VAR(ar, m_drawOnTop);
  320.         }
  321.  
  322. private:
  323.         bool m_drawNear;
  324.         bool m_drawOnTop;
  325. };
  326.  
  327. CRY_PFX2_IMPLEMENT_FEATURE(CParticleFeature, CFeatureAppearanceVisibility, "Appearance", "Visibility", colorAppearance);
  328.  
  329. }
  330.  
downloadFeatureAppearance.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