Advertisement
Guest User

LightDetector.H/.CPP

a guest
Jan 15th, 2019
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. .H
  2. #pragma once
  3.  
  4. #include <memory>
  5.  
  6. #include "CoreMinimal.h"
  7. #include "GameFramework/Actor.h"
  8. #include "Engine/TextureRenderTarget2D.h"
  9. #include "UnrealClient.h"
  10. #include "LightDetector.generated.h"
  11.  
  12. UCLASS()
  13. class STEALTHMETEREXAMPLE_API ALightDetector : public AActor
  14. {
  15. GENERATED_BODY()
  16.  
  17. UFUNCTION(BlueprintCallable, Category = "LightDetection")
  18. float CalculateBrightness();
  19.  
  20. void ProcessRenderTexture(UTextureRenderTarget2D *texture);
  21.  
  22. TArray<FColor> pixelStorage;
  23. float pixelChannelR{ 0 };
  24. float pixelChannelG{ 0 };
  25. float pixelChannelB{ 0 };
  26. float brightnessOutput{ 0 };
  27. float currentPixelBrightness{ 0 };
  28. FRenderTarget *fRenderTarget;
  29.  
  30. // The Render Textures we will be passing into the CalculateBrightness() method
  31. UPROPERTY(EditAnywhere)
  32. UTextureRenderTarget2D *detectorTextureTop;
  33. UPROPERTY(EditAnywhere)
  34. UTextureRenderTarget2D *detectorTextureBottom;
  35.  
  36. public:
  37. // Sets default values for this actor's properties
  38. ALightDetector();
  39.  
  40. protected:
  41. // Called when the game starts or when spawned
  42. virtual void BeginPlay() override;
  43.  
  44. public:
  45. // Called every frame
  46. virtual void Tick(float DeltaTime) override;
  47. };
  48.  
  49.  
  50. .CPP
  51. #include "LightDetector.h"
  52.  
  53.  
  54. // Sets default values
  55. ALightDetector::ALightDetector()
  56. {
  57. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  58. PrimaryActorTick.bCanEverTick = true;
  59.  
  60. }
  61.  
  62. // Called when the game starts or when spawned
  63. void ALightDetector::BeginPlay()
  64. {
  65. Super::BeginPlay();
  66.  
  67. }
  68.  
  69. // Called every frame
  70. void ALightDetector::Tick(float DeltaTime)
  71. {
  72. Super::Tick(DeltaTime);
  73.  
  74. }
  75.  
  76. void ALightDetector::ProcessRenderTexture(UTextureRenderTarget2D *detectorTexture) {
  77. // Read the pixels from our RenderTexture and store the data into our color array
  78. // Note: ReadPixels is allegedly a very slow operation
  79. fRenderTarget = detectorTexture->GameThread_GetRenderTargetResource();
  80. fRenderTarget->ReadPixels(pixelStorage);
  81.  
  82. // We iterate through every pixel we retrieved and find the brightest pixel
  83. for (int pixelNum = 0; pixelNum < pixelStorage.Num(); pixelNum++) {
  84. pixelChannelR = pixelStorage[pixelNum].R;
  85. pixelChannelG = pixelStorage[pixelNum].G;
  86. pixelChannelB = pixelStorage[pixelNum].B;
  87.  
  88. // Use a formula to determine brightness based on pixel color values. Source for Formula used:
  89. // www.stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color
  90. currentPixelBrightness = ((0.299 * pixelChannelR) + (0.587 * pixelChannelG) + (0.114 * pixelChannelB));
  91.  
  92. // If the current pixel we just processed is brighter than the previously brightest pixel, replace it with the new pixel
  93. if (currentPixelBrightness >= brightnessOutput) {
  94. brightnessOutput = currentPixelBrightness;
  95. }
  96. }
  97. }
  98.  
  99.  
  100. float ALightDetector::CalculateBrightness() {
  101. // Ensure that the user has actually supplied us with RenderTextures
  102. if (detectorTextureTop == nullptr || detectorTextureBottom == nullptr) {
  103. return 0.0f;
  104. }
  105. // Reset our values for the next brightness test
  106. currentPixelBrightness = 0;
  107. brightnessOutput = 0;
  108.  
  109. // Process our top and bottom RenderTextures
  110. ProcessRenderTexture(detectorTextureTop);
  111. ProcessRenderTexture(detectorTextureBottom);
  112.  
  113.  
  114. // At the end we return the brightest pixel we found in the RenderTextures
  115. return brightnessOutput;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement