Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // OverlayWidget.h
- #pragma once
- #include "Blueprint/UserWidget.h"
- #include "CoreMinimal.h"
- #include "OverlayWidget.generated.h"
- class UImage;
- UCLASS()
- class OVERLAYTEST_API UOverlayWidget : public UUserWidget {
- GENERATED_BODY()
- protected:
- UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
- UImage* m_imageOverlay;
- public:
- UFUNCTION(BlueprintCallable)
- void UpdateOverlay();
- };
- // OverlayWidget.cpp
- #include "OverlayWidget.h"
- #include "Components/Image.h"
- static const FName matrixRowNames[] = {
- TEXT("ViewProjectionMatrixRow0"),
- TEXT("ViewProjectionMatrixRow1"),
- TEXT("ViewProjectionMatrixRow2"),
- TEXT("ViewProjectionMatrixRow3"),
- };
- void UOverlayWidget::UpdateOverlay()
- {
- const APlayerController* player = GetOwningPlayer();
- auto dynamicMaterial = m_imageOverlay->GetDynamicMaterial();
- if (!player || !dynamicMaterial) {
- m_imageOverlay->SetVisibility(ESlateVisibility::Hidden);
- return;
- }
- FSceneViewProjectionData ProjectionData;
- ULocalPlayer* const LP = player->GetLocalPlayer();
- if (!(LP && LP->ViewportClient && LP->GetProjectionData(LP->ViewportClient->Viewport, ProjectionData))) {
- m_imageOverlay->SetVisibility(ESlateVisibility::Hidden);
- return;
- }
- FMatrix projectionInverse = ProjectionData.ProjectionMatrix.Inverse();
- FMatrix viewMatrixInverse = ProjectionData.ViewRotationMatrix.Inverse();
- viewMatrixInverse.SetOrigin(ProjectionData.ViewOrigin);
- FMatrix modelMatrixInverse = FMatrix::Identity; // it does nothing for now, object is in the world origin
- FMatrix pvm = projectionInverse * viewMatrixInverse * modelMatrixInverse;
- for (int i = 0; i < 4; ++i) {
- dynamicMaterial->SetVectorParameterValue(matrixRowNames[i],
- FLinearColor(pvm.M[i][0], pvm.M[i][1], pvm.M[i][2], pvm.M[i][3]));
- }
- if (GEngine) {
- GEngine->AddOnScreenDebugMessage(size_t(this), .1f, FColor::Red, pvm.ToString(), true);
- }
- m_imageOverlay->SetVisibility(ESlateVisibility::HitTestInvisible);
- }
- // M_Overlay (matrix and uv to RO and RD)
- float4x4 mat = float4x4(
- ViewProjectionMatrixRow0,
- ViewProjectionMatrixRow1,
- ViewProjectionMatrixRow2,
- ViewProjectionMatrixRow3
- );
- uv = uv * float2(2,-2) + float2(-1,1);
- float4 ro4 = mul(float4(uv, -1., 1.), mat);
- ro = ro4.rgb / ro4.a;
- float4 rd4 = mul(float4(uv, 1., 1.), mat);
- rd = rd4.rgb / rd4.a - ro;
- rd = normalize(rd); // yes
- return float3(1, 1, 1);
- // M_Overlay (RO and RD to Sphere4 shape)
- float ra = 100.0;
- float r2 = ra*ra;
- float3 d2 = rd*rd; float3 d3 = d2*rd;
- float3 o2 = ro*ro; float3 o3 = o2*ro;
- float ka = 1.0/dot(d2,d2);
- float k3 = ka* dot(ro,d3);
- float k2 = ka* dot(o2,d2);
- float k1 = ka* dot(o3,rd);
- float k0 = ka*(dot(o2,o2) - r2*r2);
- float c2 = k2 - k3*k3;
- float c1 = k1 + 2.0*k3*k3*k3 - 3.0*k3*k2;
- float c0 = k0 - 3.0*k3*k3*k3*k3 + 6.0*k3*k3*k2 - 4.0*k3*k1;
- float p = c2*c2 + c0/3.0;
- float q = c2*c2*c2 - c2*c0 + c1*c1;
- float h = q*q - p*p*p;
- if( h<0.0 ) return -1.0;
- float sh = sqrt(h);
- float s = sign(q+sh)*pow(abs(q+sh),1.0/3.0);
- float t = sign(q-sh)*pow(abs(q-sh),1.0/3.0);
- float2 w = float2( s+t,s-t );
- float2 v = float2( w.x+c2*4.0, w.y*sqrt(3.0) )*0.5;
- float r = length(v);
- return -abs(v.y)/sqrt(r+v.x) - c1/r - k3;
Advertisement
Add Comment
Please, Sign In to add comment