made 3d models position themselves in center of UIObjectCamera's view so it does not go outside its field of view

This commit is contained in:
2018-09-28 17:16:47 +02:00
parent fbb247b8a2
commit 8530e15ff2
2 changed files with 18 additions and 11 deletions

View File

@@ -23,7 +23,7 @@ namespace RothenburgAR.UI
void FixedUpdate() void FixedUpdate()
{ {
if (_autoRotate == true) if (_autoRotate == true && CurrentModel != null)
{ {
NormalizeRotationSpeed(); NormalizeRotationSpeed();
CurrentModel.transform.Rotate(Vector3.up, _rotationSpeed); CurrentModel.transform.Rotate(Vector3.up, _rotationSpeed);

View File

@@ -28,7 +28,6 @@ namespace RothenburgAR.UI
get { return _currentDisplayedModelGo != null; } get { return _currentDisplayedModelGo != null; }
} }
private string modelToBeDisplayed = null;
private GameObject _currentDisplayedModelGo; private GameObject _currentDisplayedModelGo;
public void SetText(TextElement el) public void SetText(TextElement el)
@@ -53,8 +52,6 @@ namespace RothenburgAR.UI
public IEnumerator SetModelCoroutine(PoiData data) public IEnumerator SetModelCoroutine(PoiData data)
{ {
modelToBeDisplayed = data.ID;
if (!data.IsModelLoaded) if (!data.IsModelLoaded)
{ {
StartCoroutine(data.LoadModel()); StartCoroutine(data.LoadModel());
@@ -74,21 +71,31 @@ namespace RothenburgAR.UI
try try
{ {
if (modelToBeDisplayed == data.ID) SetModel(data.InstantiateModelPrefab());
SetModel(data.InstantiateModelPrefab());
} }
catch (Exception) catch (Exception)
{ {
// ignore, just do not display an object // ignore, just do not display an object
_currentDisplayedModelGo = null; RemoveModel();
modelToBeDisplayed = null;
} }
} }
public void SetModel(GameObject modelGo) public void SetModel(GameObject modelGo)
{ {
modelGo.transform.SetParent(UIManager.Instance.UIObjectCamera.transform, false); modelGo.transform.SetParent(UIManager.Instance.UIObjectCamera.transform, false);
modelGo.transform.localPosition += new Vector3(0, 0, 450);
var meshFilter = modelGo.GetComponentInChildren<MeshFilter>();
if (meshFilter == null) throw new Exception("No MeshFilter in Object!");
var mesh = meshFilter.mesh;
if (mesh == null) throw new Exception("No Mesh in Model!");
//TODO remove when model metadata rotation is no longer a thing
mesh.RecalculateBounds();
var scale = 1f / Mathf.Max(mesh.bounds.size.x, mesh.bounds.size.y, mesh.bounds.size.z);
modelGo.transform.localScale = new Vector3(scale, scale, scale);
modelGo.transform.localPosition = mesh.bounds.center * scale + new Vector3(0, 0, 1.8f);
_currentDisplayedModelGo = modelGo; _currentDisplayedModelGo = modelGo;
DetailsModel.CurrentModel = _currentDisplayedModelGo; DetailsModel.CurrentModel = _currentDisplayedModelGo;
@@ -102,7 +109,7 @@ namespace RothenburgAR.UI
{ {
DetailsModel.gameObject.SetActive(false); DetailsModel.gameObject.SetActive(false);
DetailsModel.CurrentModel = null; DetailsModel.CurrentModel = null;
modelToBeDisplayed = null;
if (HasModel) if (HasModel)
{ {
Destroy(_currentDisplayedModelGo); Destroy(_currentDisplayedModelGo);