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()
{
if (_autoRotate == true)
if (_autoRotate == true && CurrentModel != null)
{
NormalizeRotationSpeed();
CurrentModel.transform.Rotate(Vector3.up, _rotationSpeed);
@@ -71,7 +71,7 @@ namespace RothenburgAR.UI
{
if (CurrentModel == null) return;
var previousMousePosition = _previousMousePositions.Dequeue();
var endDistance = previousMousePosition.x - data.position.x;
_rotationSpeed = endDistance / 15;
_autoRotate = true;

View File

@@ -28,7 +28,6 @@ namespace RothenburgAR.UI
get { return _currentDisplayedModelGo != null; }
}
private string modelToBeDisplayed = null;
private GameObject _currentDisplayedModelGo;
public void SetText(TextElement el)
@@ -53,8 +52,6 @@ namespace RothenburgAR.UI
public IEnumerator SetModelCoroutine(PoiData data)
{
modelToBeDisplayed = data.ID;
if (!data.IsModelLoaded)
{
StartCoroutine(data.LoadModel());
@@ -74,21 +71,31 @@ namespace RothenburgAR.UI
try
{
if (modelToBeDisplayed == data.ID)
SetModel(data.InstantiateModelPrefab());
SetModel(data.InstantiateModelPrefab());
}
catch (Exception)
{
// ignore, just do not display an object
_currentDisplayedModelGo = null;
modelToBeDisplayed = null;
RemoveModel();
}
}
public void SetModel(GameObject modelGo)
{
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;
DetailsModel.CurrentModel = _currentDisplayedModelGo;
@@ -102,7 +109,7 @@ namespace RothenburgAR.UI
{
DetailsModel.gameObject.SetActive(false);
DetailsModel.CurrentModel = null;
modelToBeDisplayed = null;
if (HasModel)
{
Destroy(_currentDisplayedModelGo);