Hello everyone!
I’m currently implementing a plugin and I need to transform some coordinates in operating system screen space to the active viewport.
I’ve gotten as far as finding a reference to the viewport I’m interested in:
GEngine->GameViewport->Viewport
I’m guessing this will always resolve to the viewport in question (disregarding any potential situations where it might be null). The question then becomes; How do I transform my screen coordinates to local coordinates in this viewport?
I first approached the question from the mouse angle. Since the unreal engine somehow is able to transform the mouse coordinates (expressed in “operating system screen space”) to viewport space coordinates, I should be able to piggyback on that code to transform my coordinates… right?
Well it turned out to not be that easy. I think I’ve found the part of the code that does this, in SceneViewport.cpp:
void FSceneViewport::GetMousePos( FIntPoint& MousePosition, const bool bLocalPosition )
{
if (bLocalPosition)
{
MousePosition = CachedMousePos;
}
else
{
const FVector2D AbsoluteMousePos = CachedGeometry.LocalToAbsolute(FVector2D(CachedMousePos.X, CachedMousePos.Y));
MousePosition.X = AbsoluteMousePos.X;
MousePosition.Y = AbsoluteMousePos.Y;
}
}
This class is the actual type of GEngine->GameViewport->Viewport it seems.
The problem here is that CachedGeometry is private, and I can’t find a good way to get access to it.
I’m currently trying to find a way to intercept CachedGeometry before it reaches the SceneViewport class, but it’s proving to be hard.
I’ve tried a few hacks to subvert the encapsulation system to see if it could work, and it seems like it, but it’s hardly a good solution.
Does anyone know of a better solution, or of a good way to make this work?
UPDATE:
Rama suggested using the:
/** Transforms a point from world-space to the view's screen-space. */
FVector4 WorldToScreen(const FVector& WorldPoint) const;
/** Transforms a point from the view's screen-space to world-space. */
FVector ScreenToWorld(const FVector4& ScreenPoint) const;
/** Transforms a point from the view's screen-space into pixel coordinates relative to the view's X,Y. */
bool ScreenToPixel(const FVector4& ScreenPoint,FVector2D& OutPixelLocation) const;
/** Transforms a point from pixel coordinates relative to the view's X,Y (left, top) into the view's screen-space. */
FVector4 PixelToScreen(float X,float Y,float Z) const;
/** Transforms a point from the view's world-space into pixel coordinates relative to the view's X,Y (left, top). */
bool WorldToPixel(const FVector& WorldPoint,FVector2D& OutPixelLocation) const;
/** Transforms a point from pixel coordinates relative to the view's X,Y (left, top) into the view's world-space. */
FVector4 PixelToWorld(float X,float Y,float Z) const;
Class of functions on FSceneView, but the screen referenced here is just another coordinate system inside the viewport itself, and are not related to the O/S screen pixels.
Best regards,
Temaran