Guest User

Untitled

a guest
Sep 26th, 2025
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | Source Code | 0 0
  1. // OverlayWidget.h
  2. #pragma once
  3.  
  4. #include "Blueprint/UserWidget.h"
  5. #include "CoreMinimal.h"
  6. #include "OverlayWidget.generated.h"
  7.  
  8. class UImage;
  9.  
  10. UCLASS()
  11. class OVERLAYTEST_API UOverlayWidget : public UUserWidget {
  12.     GENERATED_BODY()
  13.  
  14. protected:
  15.     UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
  16.     UImage* m_imageOverlay;
  17.  
  18. public:
  19.     UFUNCTION(BlueprintCallable)
  20.     void UpdateOverlay();
  21. };
  22.  
  23.  
  24.  
  25.  
  26. // OverlayWidget.cpp
  27.  
  28. #include "OverlayWidget.h"
  29. #include "Components/Image.h"
  30.  
  31. static const FName matrixRowNames[] = {
  32.     TEXT("ViewProjectionMatrixRow0"),
  33.     TEXT("ViewProjectionMatrixRow1"),
  34.     TEXT("ViewProjectionMatrixRow2"),
  35.     TEXT("ViewProjectionMatrixRow3"),
  36. };
  37.  
  38. void UOverlayWidget::UpdateOverlay()
  39. {
  40.     const APlayerController* player = GetOwningPlayer();
  41.  
  42.     auto dynamicMaterial = m_imageOverlay->GetDynamicMaterial();
  43.  
  44.     if (!player || !dynamicMaterial) {
  45.         m_imageOverlay->SetVisibility(ESlateVisibility::Hidden);
  46.         return;
  47.     }
  48.  
  49.     FSceneViewProjectionData ProjectionData;
  50.     ULocalPlayer* const LP = player->GetLocalPlayer();
  51.  
  52.     if (!(LP && LP->ViewportClient && LP->GetProjectionData(LP->ViewportClient->Viewport, ProjectionData))) {
  53.         m_imageOverlay->SetVisibility(ESlateVisibility::Hidden);
  54.         return;
  55.     }
  56.  
  57.     FMatrix projectionInverse = ProjectionData.ProjectionMatrix.Inverse();
  58.  
  59.     FMatrix viewMatrixInverse = ProjectionData.ViewRotationMatrix.Inverse();
  60.     viewMatrixInverse.SetOrigin(ProjectionData.ViewOrigin);
  61.  
  62.     FMatrix modelMatrixInverse = FMatrix::Identity; // it does nothing for now, object is in the world origin
  63.  
  64.     FMatrix pvm = projectionInverse * viewMatrixInverse * modelMatrixInverse;
  65.  
  66.     for (int i = 0; i < 4; ++i) {
  67.         dynamicMaterial->SetVectorParameterValue(matrixRowNames[i],
  68.             FLinearColor(pvm.M[i][0], pvm.M[i][1], pvm.M[i][2], pvm.M[i][3]));
  69.     }
  70.  
  71.     if (GEngine) {
  72.         GEngine->AddOnScreenDebugMessage(size_t(this), .1f, FColor::Red, pvm.ToString(), true);
  73.     }
  74.  
  75.     m_imageOverlay->SetVisibility(ESlateVisibility::HitTestInvisible);
  76. }
  77.  
  78.  
  79.  
  80.  
  81. // M_Overlay (matrix and uv to RO and RD)
  82.  
  83. float4x4 mat = float4x4(
  84.     ViewProjectionMatrixRow0,
  85.     ViewProjectionMatrixRow1,
  86.     ViewProjectionMatrixRow2,
  87.     ViewProjectionMatrixRow3
  88. );
  89.  
  90. uv = uv * float2(2,-2) + float2(-1,1);
  91.  
  92. float4 ro4 = mul(float4(uv, -1., 1.), mat);
  93. ro = ro4.rgb / ro4.a;
  94.  
  95. float4 rd4 = mul(float4(uv, 1., 1.), mat);
  96. rd = rd4.rgb / rd4.a - ro;
  97.  
  98. rd = normalize(rd); // yes
  99.  
  100. return float3(1, 1, 1);
  101.  
  102.  
  103.  
  104.  
  105. // M_Overlay (RO and RD to Sphere4 shape)
  106.  
  107. float ra = 100.0;
  108. float r2 = ra*ra;
  109.  
  110. float3 d2 = rd*rd; float3 d3 = d2*rd;
  111. float3 o2 = ro*ro; float3 o3 = o2*ro;
  112.  
  113. float ka = 1.0/dot(d2,d2);
  114.  
  115. float k3 = ka* dot(ro,d3);
  116. float k2 = ka* dot(o2,d2);
  117. float k1 = ka* dot(o3,rd);
  118. float k0 = ka*(dot(o2,o2) - r2*r2);
  119.  
  120. float c2 = k2 - k3*k3;
  121. float c1 = k1 + 2.0*k3*k3*k3 - 3.0*k3*k2;
  122. float c0 = k0 - 3.0*k3*k3*k3*k3 + 6.0*k3*k3*k2 - 4.0*k3*k1;
  123.  
  124. float p = c2*c2 + c0/3.0;
  125. float q = c2*c2*c2 - c2*c0 + c1*c1;
  126.  
  127. float h = q*q - p*p*p;
  128.  
  129. if( h<0.0 ) return -1.0;
  130.  
  131. float sh = sqrt(h);
  132.  
  133. float s = sign(q+sh)*pow(abs(q+sh),1.0/3.0);
  134. float t = sign(q-sh)*pow(abs(q-sh),1.0/3.0);
  135. float2  w = float2( s+t,s-t );
  136.  
  137. float2  v = float2( w.x+c2*4.0, w.y*sqrt(3.0) )*0.5;
  138. float r = length(v);
  139. return -abs(v.y)/sqrt(r+v.x) - c1/r - k3;
  140.  
  141.  
  142.  
Advertisement
Add Comment
Please, Sign In to add comment