问题
The following script is supposed to enlarge a UI Canvas
menu in the world space, so as the user gets far, the menu grows in size and scale, and as the user approaches it, it shrinks proportionately.
We are actually trying to update the height of the menu to the camera's frustum height, so that the menu always takes up exactly the vertical height of the camera's view.
Is this the way towards a solution? What are we doing wrong that the new size does not match the cam' frustum height? We have put this script on the HoloLensCamera
in out app's scene...
public class Adjuster : MonoBehaviour
{
public RectTransform _rectTransform;
private float _ratio;
private float _pixPerUnit = 100f;
private Vector3 _gazeOrigin;
private Vector3 _uiOrigin;
private float _frustumHeight;
private float _distance;
public bool _reSizeCanvas;
private Camera _camera;
private float _canvasWidth;
private float _canvasHeight;
private void Start()
{
if (_reSizeCanvas)
{
Vector2 initialSize = _rectTransform.sizeDelta;
_ratio = _rectTransform.rect.width / _rectTransform.rect.height;
}
}
private void LateUpdate()
{
_gazeOrigin = transform.position;
_uiOrigin = _rectTransform.position;
_distance = Vector3.Distance(_gazeOrigin, _uiOrigin);
_frustumHeight = 2.0f * _distance * Mathf.Tan( this.GetComponent<Camera>().fieldOfView * 0.5f * Mathf.Deg2Rad );
_rectTransform.sizeDelta = new Vector2(_ratio * _frustumHeight * _pixPerUnit, _frustumHeight * _pixPerUnit);
}
}
回答1:
When applying a ratio to something. That ratio is calculated based on the comparison of two things. In this case u are comparing the distance between u and the menu to a baseline distance between u and the menu. So u need fixed distance to compare your actual distance to.
A frustum is literally A cone like shape that the camera spawns (with the tip starting at the camera) and it will render anything that is inside this cone. Its dimensions will always be the same if u don't mess with the camera. If u want to get frustum height involved u need to find out where the object is in that cone. Then find out the dimensions of that slice of the cone.
The easiest solution; Leave the frustum height out of this and your solution is very easy; Player gets closer increase scale. Vice Versa
来源:https://stackoverflow.com/questions/45563636/adjust-canvas-menu-size-and-scale-in-a-holoworld