/// <summary> /// Transforms Screen coordinates into World coordinates. /// World space includes the camera transform of the camera closest to /// the input coordinate. /// In 2D setups, the world z coordinate is always set to 0. /// </summary> public float3 TranslateScreenToWorld(float2 screenCoord) { // TODO: this should not live in input, but keep it for now for compat var env = World.TinyEnvironment(); DisplayInfo di = env.GetConfigData <DisplayInfo>(); Rect screenRect = new Rect(0, 0, (float)di.width, (float)di.height); float3 result = float3.zero; float2 windowSize = new float2((float)di.width, (float)di.height); float bestdepth = Single.NegativeInfinity; Entities.ForEach((Entity e, ref Camera2D c2d, ref LocalToWorld xform) => { Rect cRect; if (c2d.rect.IsEmpty()) { cRect = new Rect(0, 0, 1, 1); } else { cRect = c2d.rect; } cRect = screenRect.Region(cRect); if (cRect.Contains(screenCoord)) { if (c2d.depth > bestdepth) { result = TransformHelpers.WindowToWorld(this, e, screenCoord, windowSize); bestdepth = c2d.depth; } } }); return(result); }
float3 GetWorldPosition(float2 screenPosition) { var env = World.TinyEnvironment(); var displayInfo = env.GetConfigData <DisplayInfo>(); var inputSystem = World.GetExistingSystem <InputSystem>(); var cameraEntity = Entity.Null; Entities.WithAll <Camera2D>().ForEach((Entity entity) => { cameraEntity = entity; }); var windowPosition = new float2(screenPosition.x, screenPosition.y); var windowSize = new float2(displayInfo.width, displayInfo.height); return(TransformHelpers.WindowToWorld(this, cameraEntity, windowPosition, windowSize)); }