/// <summary> /// Get the coordinates represented by <paramref name="anchor3d" /> in local coordinate system. /// This method calculates the coordinates of the anchor so that it is projected to the correct position on the plane when viewed from the <paramref name="cameraPosition" />. /// </summary> /// <remarks> /// Assume that the camera is oriented perpendicular to the plane. /// </remarks> /// <param name="rectTransform"> /// <see cref="RectTransform" /> that is attached to the target plane /// </param> /// <param name="cameraPosition">The position of the camera represented in local coordinates of the plane</param> /// <param name="defaultDepth"> /// Depth value when the anchor's Z is 1.0. /// Depth here refers to the distance from the camera on the Z axis. /// </param> /// <param name="imageRotation">Counterclockwise rotation angle of the input image</param> /// <param name="isMirrored">Set to true if the original coordinates is mirrored</param> public static Vector3 GetLocalPosition(this RectTransform rectTransform, Anchor3d anchor3d, Vector3 cameraPosition, float defaultDepth, RotationAngle imageRotation = RotationAngle.Rotation0, bool isMirrored = false) { if (Mathf.Approximately(cameraPosition.z, 0.0f)) { throw new System.ArgumentException("Z value of the camera position must not be zero"); } var cameraDepth = Mathf.Abs(cameraPosition.z); var anchorPoint2d = rectTransform.GetLocalPosition(anchor3d, imageRotation, isMirrored); var anchorDepth = anchor3d.Z * defaultDepth; // Maybe it should be defined as a CameraCoordinate method var x = (anchorPoint2d.x - cameraPosition.x) * anchorDepth / cameraDepth + cameraPosition.x; var y = (anchorPoint2d.y - cameraPosition.y) * anchorDepth / cameraDepth + cameraPosition.y; var z = cameraPosition.z > 0 ? cameraPosition.z - anchorDepth : cameraPosition.z + anchorDepth; return(new Vector3(x, y, z)); }
/// <summary> /// Get the coordinates represented by <paramref name="anchor3d" /> in local coordinate system. /// </summary> /// <param name="rectTransform"> /// <see cref="RectTransform" /> to be used for calculating local coordinates /// </param> /// <param name="imageRotation">Counterclockwise rotation angle of the input image</param> /// <param name="isMirrored">Set to true if the original coordinates is mirrored</param> public static Vector2 GetLocalPosition(this RectTransform rectTransform, Anchor3d anchor3d, RotationAngle imageRotation = RotationAngle.Rotation0, bool isMirrored = false) { return(GetLocalPositionNormalized(rectTransform, anchor3d.X, anchor3d.Y, imageRotation, isMirrored)); }
/// <summary> /// Get the coordinates represented by <paramref name="anchor3d" /> in the local coordinate system. /// </summary> /// <param name="rectangle">Rectangle to get a point inside</param> /// <param name="imageRotation"> /// Counterclockwise rotation angle of the input image in the image coordinate system. /// In the local coordinate system, this value will often represent a clockwise rotation angle. /// </param> /// <param name="isMirrored">Set to true if the original coordinates is mirrored</param> public static Vector2 GetPoint(this UnityEngine.Rect rectangle, Anchor3d anchor3d, RotationAngle imageRotation = RotationAngle.Rotation0, bool isMirrored = false) { return(ImageNormalizedToPoint(rectangle, anchor3d.x, anchor3d.y, imageRotation, isMirrored)); }