SHARE
TWEET

Untitled

a guest Feb 27th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "GameProject_precompiled.h"
  2. #pragma optimize("", off)
  3.  
  4.  
  5.  
  6. #include <AzCore/Component/Entity.h>
  7. #include <AzCore/Serialization/SerializeContext.h>
  8. #include <AzCore/Serialization/EditContext.h>
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10.  
  11. /// EBUS
  12. #include <AzCore/Component/TransformBus.h>
  13. #include <AzCore/Component/ComponentApplicationBus.h>
  14.  
  15. #include <AzFramework/Entity/EntityContextBus.h>
  16. #include <AzFramework/Entity/GameEntityContextBus.h>
  17.  
  18.  
  19. /// Game Headers
  20. #include "TerraformingComponent.h"
  21.  
  22. using namespace GameProject;
  23.  
  24. void TerraformingComponent::Reflect(AZ::ReflectContext * context)
  25. {
  26.     AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  27.  
  28.     if (serialize)
  29.     {
  30.         serialize->Class<TerraformingComponent, AZ::Component >()
  31.             ->Version(1)
  32.             ->Field("radius", &TerraformingComponent::m_radius)
  33.             ->Field("inland", &TerraformingComponent::m_inland)
  34.             ->Field("wholeTerrainNoise", &TerraformingComponent::m_wholeTerrainNoise)
  35.             ->Field("saveAndRestoreOriginalTerrain", &TerraformingComponent::m_saveAndRestoreOriginalTerrain)
  36.             ;
  37.  
  38.         AZ::EditContext* edit = serialize->GetEditContext();
  39.         if (edit)
  40.         {
  41.             edit->Class<TerraformingComponent>("TerraformingComponent", "")
  42.                 ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  43.                 ->Attribute(AZ::Edit::Attributes::Category, CATEGORY)
  44.                 ->Attribute("Icon", "Editor/Icons/Components/ComponentPlaceholder.png")
  45.                 ->Attribute("ViewportIcon", "Editor/Icons/Components/Viewport/ComponentPlaceholder.png")
  46.                 ->Attribute("AutoExpand", true)
  47.                 ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game", 0x232b318c))
  48.                 ->DataElement(AZ::Edit::UIHandlers::Default, &TerraformingComponent::m_saveAndRestoreOriginalTerrain, "Save and restore original terrain", "")
  49.                 ->DataElement(AZ::Edit::UIHandlers::Default, &TerraformingComponent::m_radius, "Radius", "Crater radius around entity")
  50.                 ->DataElement(AZ::Edit::UIHandlers::Default, &TerraformingComponent::m_inland, "Inland", "How deep it's should be")
  51.                 ->DataElement(AZ::Edit::UIHandlers::Default, &TerraformingComponent::m_wholeTerrainNoise, "Set noise on whole terrain", "")
  52.  
  53.                 ;
  54.         }
  55.     }
  56. }
  57.  
  58. void TerraformingComponent::Init()
  59. {
  60.  
  61. }
  62.  
  63. void TerraformingComponent::Activate()
  64. {
  65.     //AZ::EntityBus::Handler::BusConnect(GetEntityId());
  66.     AzFramework::GameEntityContextEventBus::Handler::BusConnect();
  67.     TerraformingBus::Handler::BusConnect(GetEntityId());
  68. }
  69.  
  70. void TerraformingComponent::Deactivate()
  71. {
  72.  
  73.  
  74.     //AZ::TickBus::Handler::BusDisconnect();
  75.     //AZ::EntityBus::Handler::BusDisconnect(GetEntityId());
  76.     AzFramework::GameEntityContextEventBus::Handler::BusDisconnect();
  77.     TerraformingBus::Handler::BusDisconnect();
  78. }
  79.  
  80. void TerraformingComponent::OnTick(float deltaTime, AZ::ScriptTimePoint time)
  81. {
  82.    
  83. }
  84.  
  85. void TerraformingComponent::OnGameEntitiesStarted()
  86. {
  87.     //if (m_saveAndRestoreOriginalTerrain)
  88.     SaveOriginalTerrain();
  89.  
  90.     //AZ::TickBus::Handler::BusConnect();
  91.     if (m_wholeTerrainNoise)
  92.         UpdateTerrain();
  93.     else
  94.         //UpdateTerrainUnderEntity(m_radius, m_inland);
  95.         UpdateTerrainUnderEntityOptimized(m_radius, m_inland);
  96. }
  97.  
  98. void TerraformingComponent::OnGameEntitiesReset()
  99. {
  100.     if (m_saveAndRestoreOriginalTerrain)
  101.         RestoreOriginalTerrain();
  102.  
  103.     if (pOriginalTerrain)
  104.     {
  105.         delete[] pOriginalTerrain;
  106.         pOriginalTerrain = nullptr;
  107.     }
  108.  
  109.     if (pCurTerrain)
  110.     {
  111.         delete[] pCurTerrain;
  112.         pCurTerrain = nullptr;
  113.     }
  114.  
  115.     if (pCurTerrainSW)
  116.     {
  117.         delete[] pCurTerrainSW;
  118.         pCurTerrainSW = nullptr;
  119.     }
  120. }
  121.  
  122. void TerraformingComponent::UpdateTerrain()
  123. {
  124.     // turn-on terrain deformations
  125.     ICVar* e_td = gEnv->pConsole->GetCVar("e_TerrainDeformations");
  126.     if (e_td)
  127.         e_td->Set(1.0f);
  128.  
  129.     AZ::u32 mapSize = gEnv->p3DEngine->GetTerrainSize() / gEnv->p3DEngine->GetHeightMapUnitSize(); // = 1024...
  130.     AZ::u32 sectorSize = gEnv->p3DEngine->GetTerrainSectorSize(); // = 32
  131.  
  132.     float* pTerrainBlock = new float[mapSize*mapSize];
  133.     ITerrain::SurfaceWeight* pTerrainSW  = new ITerrain::SurfaceWeight[mapSize*mapSize];
  134.     ITerrain::SurfaceWeight defaultSW; //= gEnv->p3DEngine->GetITerrain()->GetSurfaceWeight(512, 512); // Sample center of the map for SurfaceWeight for propagate
  135.  
  136.     // change whole map with random noise
  137.     for (int x = 0; x < mapSize; x++)
  138.     {
  139.         for (int y = 0; y < mapSize; y++)
  140.         {
  141.             pTerrainBlock[x * mapSize + y] = gEnv->p3DEngine->GetTerrainElevation((float)x, (float)y) + m_RndGenerator.GetRandomFloat() * 5.0f;
  142.             pTerrainSW[x * mapSize + y] = defaultSW;
  143.         }
  144.     }
  145.  
  146.     AZ::Vector3 pos;
  147.     AZ::TransformBus::EventResult(pos, GetEntityId(), &AZ::TransformBus::Events::GetWorldTranslation);
  148.     gEnv->p3DEngine->GetITerrain()->SetTerrainElevation(0, 0 , mapSize, pTerrainBlock, sizeof(pTerrainSW), pTerrainSW);
  149.    
  150.     delete[] pTerrainBlock;
  151.     delete[] pTerrainSW;
  152. }
  153.  
  154. //
  155. void TerraformingComponent::UpdateTerrainUnderEntity(float radius, float inland)
  156. {
  157.     // turn-on terrain deformations
  158.     ICVar* e_td = gEnv->pConsole->GetCVar("e_TerrainDeformations");
  159.     if (e_td)
  160.         e_td->Set(1.0f);
  161.  
  162.     AZ::u32 mapSize = gEnv->p3DEngine->GetTerrainSize() / gEnv->p3DEngine->GetHeightMapUnitSize(); // = 1024...
  163.     AZ::u32 sectorSize = gEnv->p3DEngine->GetTerrainSectorSize(); // = 32 (32 * 32 = 1024 mapsize)
  164.  
  165.     float* pTerrainBlock = new float[mapSize*mapSize];
  166.     ITerrain::SurfaceWeight* pTerrainSW = new ITerrain::SurfaceWeight[mapSize * mapSize];
  167.     ITerrain::SurfaceWeight defaultSW; //= gEnv->p3DEngine->GetITerrain()->GetSurfaceWeight(512, 512); // Sample center of the map for SurfaceWeight for propagate
  168.  
  169.     AZ::Vector3 pos;
  170.     AZ::TransformBus::EventResult(pos, GetEntityId(), &AZ::TransformBus::Events::GetWorldTranslation);
  171.  
  172.     for (int x = 0; x < mapSize; x++)
  173.     {
  174.         for (int y = 0; y < mapSize; y++)
  175.         {
  176.             float terrainZ = gEnv->p3DEngine->GetTerrainElevation((float)x, (float)y);
  177.             float distance = pos.GetDistance(AZ::Vector3((float)x, (float)y, terrainZ));
  178.             float normalizedAffectOnTerrainOverDistance = 1.0f - AZ::GetClamp<float>(distance / radius, 0.0f, 1.0f);
  179.  
  180.             pTerrainBlock[x * mapSize + y] = terrainZ + (-inland * normalizedAffectOnTerrainOverDistance);
  181.             pTerrainSW[x * mapSize + y] = defaultSW;
  182.         }
  183.     }
  184.  
  185.     gEnv->p3DEngine->GetITerrain()->SetTerrainElevation(0, 0, mapSize, pTerrainBlock, sizeof(pTerrainSW), pTerrainSW);
  186.     delete[] pTerrainBlock;
  187.     delete[] pTerrainSW;
  188. }
  189.  
  190. void TerraformingComponent::UpdateTerrainUnderEntityOptimized(float radius, float inland)
  191. {
  192.     // turn-on terrain deformations
  193.     ICVar* e_td = gEnv->pConsole->GetCVar("e_TerrainDeformations");
  194.     if (e_td)
  195.         e_td->Set(1.0f);
  196.  
  197.     AZ::u32 mapSize = gEnv->p3DEngine->GetTerrainSize() / gEnv->p3DEngine->GetHeightMapUnitSize(); // = 1024...
  198.     AZ::u32 sectorSize = gEnv->p3DEngine->GetTerrainSectorSize(); // = 32 (32 * 32 = 1024 mapsize)
  199.  
  200.     float* pTerrainBlock = new float[mapSize*mapSize];
  201.     //float* pTerrainBlock = new float[sectorSize*sectorSize];
  202.  
  203.     ITerrain::SurfaceWeight* pTerrainSW = new ITerrain::SurfaceWeight[mapSize * mapSize];
  204.     //ITerrain::SurfaceWeight* pTerrainSW = new ITerrain::SurfaceWeight[sectorSize * sectorSize];
  205.  
  206.  
  207.     ITerrain::SurfaceWeight defaultSW; //= gEnv->p3DEngine->GetITerrain()->GetSurfaceWeight(512, 512); // Sample center of the map for SurfaceWeight for propagate
  208.  
  209.     // get entity world pos
  210.     AZ::Vector3 pos;
  211.     AZ::TransformBus::EventResult(pos, GetEntityId(), &AZ::TransformBus::Events::GetWorldTranslation);
  212.  
  213.     // calc cell of map where is entity placed
  214.     int cx = (int)pos.GetX() / sectorSize;
  215.     int cy = (int)pos.GetY() / sectorSize;
  216.  
  217.     int ccx = cx * sectorSize;
  218.     int ccy = cy * sectorSize;
  219.  
  220.     for (int x = ccx; x < ccx + sectorSize; x++)
  221.     {
  222.         for (int y = ccy; y < ccy + sectorSize; y++)
  223.         {
  224.             float terrainZ = gEnv->p3DEngine->GetTerrainElevation((float)x, (float)y);
  225.             float distance = pos.GetDistance(AZ::Vector3((float)x, (float)y, terrainZ));
  226.             float normalizedAffectOnTerrainOverDistance = 1.0f - AZ::GetClamp<float>(distance / radius, 0.0f, 1.0f);
  227.  
  228.             pTerrainBlock[x * mapSize + y] = terrainZ + (-inland * normalizedAffectOnTerrainOverDistance);
  229.             pTerrainSW[x * mapSize + y] = defaultSW;
  230.         }
  231.     }
  232.  
  233.     gEnv->p3DEngine->GetITerrain()->SetTerrainElevation(ccx, ccy, 32, pTerrainBlock, sizeof(pTerrainSW), pTerrainSW);
  234.     delete[] pTerrainBlock;
  235.     delete[] pTerrainSW;
  236. }
  237.  
  238. void TerraformingComponent::UpdateTerrainCutout(AZ::Vector3 pos, float radius, bool cutout)
  239. {
  240.     ICVar* e_td = gEnv->pConsole->GetCVar("e_TerrainDeformations");
  241.     if (e_td)
  242.         e_td->Set(1.0f);
  243.  
  244.     //gEnv->p3DEngine->GetITerrain()->SetOceanWaterLevel(0.0f);
  245.  
  246.     AZ::u32 mapSize = gEnv->p3DEngine->GetTerrainSize() / gEnv->p3DEngine->GetHeightMapUnitSize(); // = 1024...
  247.     AZ::u32 sectorSize = gEnv->p3DEngine->GetTerrainSectorSize(); // = 32 (32 * 32 = 1024 mapsize)
  248.  
  249.     //float* pTerrainBlock = new float[mapSize*mapSize];
  250.     //float* pTerrainBlock = new float[sectorSize*sectorSize];
  251.  
  252.     //ITerrain::SurfaceWeight* pTerrainSW = new ITerrain::SurfaceWeight[mapSize * mapSize];
  253.     //ITerrain::SurfaceWeight* pTerrainSW = new ITerrain::SurfaceWeight[sectorSize * sectorSize];
  254.  
  255.  
  256.     ITerrain::SurfaceWeight defaultSW; //= gEnv->p3DEngine->GetITerrain()->GetSurfaceWeight(512, 512); // Sample center of the map for SurfaceWeight for propagate
  257.  
  258.     //AZ::Vector3 pos;
  259.     //AZ::TransformBus::EventResult(pos, GetEntityId(), &AZ::TransformBus::Events::GetWorldTranslation);
  260.  
  261.     int doubleRadius = (int)radius * 2;
  262.     int sectorSizeHalf = (int)sectorSize / 2;
  263.  
  264.     int cx = (int)(pos.GetX()) / sectorSize;
  265.     int cy = (int)(pos.GetY()) / sectorSize;
  266.  
  267.     int ccx = cx * sectorSize;
  268.     int ccy = cy * sectorSize;
  269.  
  270.     // cutout terrain
  271.     for (int x = ccx; x < (ccx + sectorSize); x++)
  272.     {
  273.         for (int y = ccy; y < (ccy + sectorSize); y++)
  274.         {
  275.             //float terrainZ = gEnv->p3DEngine->GetTerrainElevation((float)x, (float)y);
  276.             float terrainZ = pCurTerrain[x * mapSize + y];
  277.             float distance = pos.GetDistance(AZ::Vector3((float)x, (float)y, terrainZ));
  278.             float normalizedAffectOnTerrainOverDistance = 1.0f - AZ::GetClamp<float>(distance / radius, 0.0f, 1.0f);
  279.            
  280.             pCurTerrain[x * mapSize + y] = terrainZ + (-radius * normalizedAffectOnTerrainOverDistance);
  281.         }
  282.     }
  283.  
  284.     //// smooth terrain
  285.     //const int smoothHalfSize = 4;
  286.     //int tx = 0;
  287.     //int ty = 0;
  288.     //float total = 0.0f;
  289.     //int sampleCount = 0;
  290.    
  291.     //for (int x = ccx-doubleRadius; x < ccx + doubleRadius; x++)
  292.     //{
  293.     //  for (int y = ccy-doubleRadius; y < ccy + doubleRadius; y++)
  294.     //  {
  295.     //      total = 0.0f;
  296.     //      sampleCount = 0;
  297.     //      for (int xx = -smoothHalfSize; xx < smoothHalfSize; xx++)
  298.     //          for (int yy = -smoothHalfSize; yy < smoothHalfSize; yy++)
  299.     //          {
  300.     //              tx = x + xx;
  301.     //              ty = y + yy;
  302.     //              total +=pCurTerrain[tx * mapSize + tx];
  303.     //              sampleCount++;
  304.     //          }
  305.  
  306.     //      pCurTerrain[x * mapSize + y] = total / sampleCount;
  307.     //  }
  308.     //}
  309.  
  310.  
  311.  
  312.     gEnv->p3DEngine->GetITerrain()->SetTerrainElevation(ccx, ccy, sectorSize, pCurTerrain, sizeof(pCurTerrainSW), pCurTerrainSW);
  313.     //delete[] pTerrainBlock;
  314.     //delete[] pTerrainSW;
  315. }
  316.  
  317. void TerraformingComponent::SaveOriginalTerrain()
  318. {
  319.     AZ::u32 mapSize = gEnv->p3DEngine->GetTerrainSize() / gEnv->p3DEngine->GetHeightMapUnitSize(); // = 1024...
  320.  
  321.     //if (pOriginalTerrain)
  322.     //  delete[] pOriginalTerrain;
  323.    
  324.     if (!pOriginalTerrain)
  325.         pOriginalTerrain = new float[mapSize*mapSize];
  326.  
  327.     //SHotUpdateInfo hui;// xd
  328.     ///gEnv->p3DEngine->GetITerrain()->GetCompiledData((char*)pOriginalTerrain, mapSize*mapSize*4, nullptr, nullptr, nullptr, false, )
  329.    
  330.     if (!pCurTerrain)
  331.         pCurTerrain = new float[mapSize*mapSize];
  332.  
  333.     if (!pCurTerrainSW)
  334.         pCurTerrainSW = new ITerrain::SurfaceWeight[mapSize * mapSize];
  335.  
  336.     for (int x = 0; x < mapSize; x++)
  337.     {
  338.         for (int y = 0; y < mapSize; y++)
  339.         {
  340.             pOriginalTerrain[x * mapSize + y] = gEnv->p3DEngine->GetTerrainElevation((float)x, (float)y);
  341.             pCurTerrain[x * mapSize + y] = pOriginalTerrain[x * mapSize + y];
  342.             pCurTerrainSW[x * mapSize + y] = gEnv->p3DEngine->GetITerrain()->GetSurfaceWeight(x, y);
  343.             // Optionaly we also can save gEnv->p3DEngine->GetITerrain()->GetSurfaceWeight(x, y)
  344.         }
  345.     }
  346.  
  347.  
  348.  
  349.     //memcpy(pCurTerrain, pOriginalTerrain, sizeof(pOriginalTerrain));
  350. }
  351.  
  352. void TerraformingComponent::RestoreOriginalTerrain()
  353. {
  354.     AZ::u32 mapSize = gEnv->p3DEngine->GetTerrainSize() / gEnv->p3DEngine->GetHeightMapUnitSize(); // = 1024...
  355.     gEnv->p3DEngine->GetITerrain()->SetTerrainElevation(0, 0, mapSize, pOriginalTerrain, 0, nullptr);
  356. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top