Created
January 22, 2024 03:19
-
-
Save error454/7bc58a3cbb304d6bf1b24c82831112f7 to your computer and use it in GitHub Desktop.
Barebones example of texture math for minimap, just enough boilerplate to describe GetImageCoordsFromWorldLoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MinimapUserWidget.h" | |
#include "Kismet/GameplayStatics.h" | |
FVector2D UMinimapUserWidget::GetImageCoordsFromWorldLoc(FVector WorldLoc) const | |
{ | |
// Image coordinate system in UMG is: | |
// 0,0 ------> +x | |
// | | |
// | | |
// | | |
// +y | |
// Find the world location of the top left of the mini map | |
// MiniMapOffset gets us to the center of the texture while MiniMapOrthoWidth * 0.5f is half the size of the minimap. | |
const float HalfMapLength = MiniMapOrthoWidth * 0.5f; | |
const FVector2D TextureTopLeftWorld = MiniMapOffset - MiniMapWorldX * HalfMapLength + MiniMapWorldY * HalfMapLength; | |
const FVector2D TextureBottomRightWorld = TextureTopLeftWorld + MiniMapWorldX * MiniMapOrthoWidth - MiniMapWorldY * MiniMapOrthoWidth; | |
// Calculate our ranges for mapping | |
const FVector2D InputRangeWorldX = FVector2D(TextureTopLeftWorld.X, TextureBottomRightWorld.X); | |
const FVector2D InputRangeWorldY = FVector2D(TextureTopLeftWorld.Y, TextureBottomRightWorld.Y); | |
const FVector2D OutputRangePx = FVector2D(0.f, Image_MiniMapTexture->Brush.GetImageSize().X); | |
// Construct the final coordinates | |
const float PixelsX = FMath::GetMappedRangeValueClamped(InputRangeWorldX, OutputRangePx, WorldLoc.X); | |
const float PixelsY = FMath::GetMappedRangeValueClamped(InputRangeWorldY, OutputRangePx, WorldLoc.Y); | |
return FVector2D(PixelsX, PixelsY); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "CoreMinimal.h" | |
#include "Blueprint/UserWidget.h" | |
#include "MinimapUserWidget.generated.h" | |
/** | |
* | |
*/ | |
UCLASS() | |
class YOURGAME_API UMinimapUserWidget : public UUserWidget | |
{ | |
GENERATED_BODY() | |
public: | |
// The texture for the mini map, assumptions are that this image is square and origin is the center of the image | |
UPROPERTY(BlueprintReadWrite, meta = (BindWidget)) | |
UImage* Image_MiniMapTexture; | |
// The Ortho Width of the camera that captured the map image | |
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MiniMap") | |
float MiniMapOrthoWidth; | |
// The X/Y offset of the camera that captured the map in world space | |
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MiniMap") | |
FVector2D MiniMapOffset; | |
// When looking at the mini map this vector should point in the direction of +world X | |
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MiniMap") | |
FVector2D MiniMapWorldX; | |
// When looking at the mini map this vector should point in the direction of +world Y | |
// Y positive going down the texture | |
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MiniMap") | |
FVector2D MiniMapWorldY; | |
// Return image coordinates in texture space from a world location | |
// Image coordinate system in UMG is: | |
// 0,0 ------> +x | |
// | | |
// | | |
// | | |
// +y | |
UFUNCTION(BlueprintCallable) | |
FVector2D GetImageCoordsFromWorldLoc(FVector WorldLoc) const; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment