56 lines
1.2 KiB
C#
56 lines
1.2 KiB
C#
|
|
using Godot;
|
|
|
|
|
|
namespace Rokojori
|
|
{
|
|
[Tool][GlobalClass ]
|
|
public partial class SetScreenTransform : Action
|
|
{
|
|
[Export]
|
|
public Camera3D source;
|
|
|
|
[Export]
|
|
public Node3D target;
|
|
|
|
[Export]
|
|
public Vector3 screenPosition;
|
|
|
|
|
|
protected override void _OnTrigger()
|
|
{
|
|
var applyingSource = source;
|
|
|
|
if ( Engine.IsEditorHint() )
|
|
{
|
|
#if TOOLS
|
|
|
|
applyingSource = EditorInterface.Singleton.GetEditorViewport3D().GetCamera3D();
|
|
|
|
#endif
|
|
}
|
|
|
|
if ( source == null )
|
|
{
|
|
return;
|
|
}
|
|
|
|
target.GlobalPosition = NormalizedScreenToWorld( applyingSource, screenPosition.XY(), screenPosition.Z );
|
|
target.SetGlobalQuaternion( applyingSource.GlobalQuaternion() );
|
|
|
|
}
|
|
|
|
public static Vector3 NormalizedScreenToWorld( Camera3D camera, Vector2 normalizedPos, float depth )
|
|
{
|
|
var viewPort = camera.GetViewport();
|
|
var viewportSize = viewPort.GetVisibleRect().Size;
|
|
|
|
var screenPixelPos = new Vector2(
|
|
normalizedPos.X * viewportSize.X,
|
|
normalizedPos.Y * viewportSize.Y
|
|
);
|
|
|
|
return camera.ProjectPosition(screenPixelPos, depth);
|
|
}
|
|
}
|
|
} |