diff --git a/vr-configurator/Assets/Scenes/SampleScene.unity b/vr-configurator/Assets/Scenes/SampleScene.unity index 8c621d2..3b0829b 100644 --- a/vr-configurator/Assets/Scenes/SampleScene.unity +++ b/vr-configurator/Assets/Scenes/SampleScene.unity @@ -7852,6 +7852,7 @@ MonoBehaviour: baseModel: {fileID: 1272917964} text: {fileID: 1785144091} cms: {fileID: 1509914124} + explode: 0 --- !u!114 &465093188 MonoBehaviour: m_ObjectHideFlags: 0 @@ -12851,6 +12852,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: ModelManager: {fileID: 1519836175} + Exploder: {fileID: 465093187} meshFilter: {fileID: 698127113} meshRenderer: {fileID: 698127112} meshCollider: {fileID: 698127117} @@ -21753,6 +21755,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: ModelManager: {fileID: 1519836175} + Exploder: {fileID: 465093187} meshFilter: {fileID: 1272917971} meshRenderer: {fileID: 1272917970} meshCollider: {fileID: 1272917966} diff --git a/vr-configurator/Assets/Scripts/Models/ModelBehaviour.cs b/vr-configurator/Assets/Scripts/Models/ModelBehaviour.cs index 2f46bd8..ac90bbb 100644 --- a/vr-configurator/Assets/Scripts/Models/ModelBehaviour.cs +++ b/vr-configurator/Assets/Scripts/Models/ModelBehaviour.cs @@ -9,6 +9,7 @@ using UnityEngine; public class ModelBehaviour : MonoBehaviour, IResettable { public ModelManager ModelManager; + public Exploder Exploder; Model _model; internal Model Model @@ -21,6 +22,7 @@ public class ModelBehaviour : MonoBehaviour, IResettable { UpdateModel(value); _model = value; + LateUpdateModel(); } } readonly List _children = new List(); @@ -82,7 +84,7 @@ public class ModelBehaviour : MonoBehaviour, IResettable { return; } - interactable.WhenSelect.AddListener(OnSelect); + interactable.WhenSelect.AddListener(OnSelect); //TODO: the root of all "frequently called", which is wrong lateInited = true; } } @@ -137,19 +139,26 @@ public class ModelBehaviour : MonoBehaviour, IResettable SpawnChildPorts(newModel); } } - - public void UpdateChild(int childNum, string id) + + void LateUpdateModel() { - if (childNum >= _children.Count) + Debug.Log($"LateUpdateModel"); + Exploder.HandleModelChange(); + } + + public void UpdateChild(int portNum, string id) //onclick change model + { + if (portNum >= _children.Count) { - Debug.LogWarning($"Index {childNum} out of bounds {_children.Count}"); + Debug.LogWarning($"Index {portNum} out of bounds {_children.Count}"); } - Model.Ports[childNum].Unapply(_children[childNum].transform); + Model.Ports[portNum].Unapply(_children[portNum].transform); //set new - var cmb = _children[childNum].GetComponent(); + var cmb = _children[portNum].GetComponent(); cmb.ChildModel = ModelManager.GetById(id); cmb.Parent = this; - Model.Ports[childNum].Apply(_children[childNum].transform); + Model.Ports[portNum].Apply(_children[portNum].transform); + Exploder.HandleModelChange(); } /// diff --git a/vr-configurator/Assets/Scripts/Transform/Exploder.cs b/vr-configurator/Assets/Scripts/Transform/Exploder.cs index 72caf42..63502af 100644 --- a/vr-configurator/Assets/Scripts/Transform/Exploder.cs +++ b/vr-configurator/Assets/Scripts/Transform/Exploder.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using TMPro; using UnityEngine; @@ -13,8 +14,8 @@ public class Exploder : MonoBehaviour, IResettable, IPointerDownHandler List models = new List(); - static readonly int finalTick = 60 * 2; // 60fps * 2sek - bool explode = false; + static readonly int finalTick = 60 * 2; // 60fps * 2sek, quest3 isn't 60fps but doesn't matter + public bool explode = false; int tick = finalTick + 1; public void OnPointerDown(PointerEventData eventData) @@ -28,23 +29,91 @@ public class Exploder : MonoBehaviour, IResettable, IPointerDownHandler void Update() { - if (tick <= finalTick) + try { - if(explode) - for (int i = 0; i < models.Count; i++) - { - models[i].tf.localPosition = - MathUtil.EaseBoth(models[i].initPos, models[i].goalPos, + if (tick <= finalTick) + { + if (explode) + for (int i = 0; i < models.Count; i++) + { + models[i].tf.localPosition = + MathUtil.EaseBoth(models[i].initPos, + models[i].goalPos, (tick + 0f) / finalTick); //0f for floating point div - } - else - for (int i = 0; i < models.Count; i++) - { - models[i].tf.localPosition = - MathUtil.EaseBoth(models[i].goalPos, models[i].initPos, + } + else + for (int i = 0; i < models.Count; i++) + { + models[i].tf.localPosition = + MathUtil.EaseBoth(models[i].goalPos, + models[i].initPos, (tick + 0f) / finalTick); //0f for floating point div + } + tick++; + } + } + catch (Exception e) + { + Debug.LogError($"Fixing ModelList Explode after {e}"); //happens when user changes model while animation + CleanModelList(); + } + + } + + void PopulateModelList() + { + models.Clear(); + foreach (Transform child in baseModel.GetComponentsInChildren()) + { + var cmb = child.GetComponent(); + if (cmb == null) + { + continue; //only for basemodel + } + Port port = cmb.GetParentPort(); + models.Add(new ExploderModel(port, cmb.ChildModel, cmb)); + } + } + void UpdateModelList() + { + List modelss = new List(); + foreach (var exploModel in models) + { + if (exploModel.cmb == null) + { + continue; //skip dead ends + } + if (exploModel.model != exploModel.cmb.ChildModel) //update + { + Debug.LogWarning($"Updating Model List from {exploModel.model.NameId} to {exploModel.cmb.ChildModel.NameId}"); + // add children + itself + foreach (Transform child in exploModel.cmb.GetComponentsInChildren()) + { + var cmb = child.GetComponent(); + if (cmb == null) + { + continue; + } + Port port = cmb.GetParentPort(); + modelss.Add(new ExploderModel(port, cmb.ChildModel, cmb)); } - tick++; + continue; + } + modelss.Add(exploModel); + } + models = modelss; + } + + void CleanModelList() + { + models.RemoveAll(model => model.cmb == null || model.tf == null); + } + + void ForceSetPos() + { + foreach (var exploModel in models) + { + exploModel.tf.localPosition = explode ? exploModel.goalPos : exploModel.initPos; } } @@ -52,24 +121,23 @@ public class Exploder : MonoBehaviour, IResettable, IPointerDownHandler { if (!explode) // before switching to true { - models.Clear(); - foreach (Transform child in baseModel.GetComponentsInChildren()) - { - var cmb = child.GetComponent(); - if (cmb == null) - { - continue; //only for basemodel - } - Port port = cmb.GetParentPort(); - models.Add(new ExploderModel(port, cmb.ChildModel, child)); - } + PopulateModelList(); + } + else + { + CleanModelList(); } text.text = explode ? "Explode" : "Unexplode"; - //cms.SetActive(explode); explode = !explode; tick = 0; } + public void HandleModelChange() + { + UpdateModelList(); + ForceSetPos(); + } + public void ResetThis() { if (explode) diff --git a/vr-configurator/Assets/Scripts/Transform/ExploderModel.cs b/vr-configurator/Assets/Scripts/Transform/ExploderModel.cs index 4f1b02d..6b294ff 100644 --- a/vr-configurator/Assets/Scripts/Transform/ExploderModel.cs +++ b/vr-configurator/Assets/Scripts/Transform/ExploderModel.cs @@ -1,19 +1,28 @@ -using UnityEngine; +using Oculus.Interaction; +using UnityEngine; public class ExploderModel { public Port port; public ChildModel model; - public Transform tf; //pointer to cmb pos + public ChildModelBehaviour cmb; + + public Transform tf; public Vector3 initPos; public Vector3 goalPos; - public ExploderModel(Port port, ChildModel model, Transform tf) + public ExploderModel(Port port, ChildModel model, ChildModelBehaviour cmb) { this.port = port; this.model = model; - this.tf = tf; + this.cmb = cmb; + CalculatePos(); + } + + public void CalculatePos() + { + tf = cmb.transform; initPos = tf.localPosition; goalPos = tf.localPosition + port.ExplodeDirection; }