diff --git a/vr-configurator/Assets/Scripts/Models/BaseModelBehaviour.cs b/vr-configurator/Assets/Scripts/Models/BaseModelBehaviour.cs index f8da07f..79ca37c 100644 --- a/vr-configurator/Assets/Scripts/Models/BaseModelBehaviour.cs +++ b/vr-configurator/Assets/Scripts/Models/BaseModelBehaviour.cs @@ -5,11 +5,11 @@ using UnityEngine; //Only one should exist at the same time public class BaseModelBehaviour : ModelBehaviour { - public BaseModel baseModel + public BaseModel BaseModel { get { - if (model is BaseModel bModel) + if (Model is BaseModel bModel) { return bModel; } @@ -17,7 +17,7 @@ public class BaseModelBehaviour : ModelBehaviour } set { - model = value; + Model = value; } } diff --git a/vr-configurator/Assets/Scripts/Models/ChildModelBehaviour.cs b/vr-configurator/Assets/Scripts/Models/ChildModelBehaviour.cs index c5ae16c..3955fb5 100644 --- a/vr-configurator/Assets/Scripts/Models/ChildModelBehaviour.cs +++ b/vr-configurator/Assets/Scripts/Models/ChildModelBehaviour.cs @@ -4,15 +4,15 @@ using UnityEngine; public class ChildModelBehaviour : ModelBehaviour { - public ChildModel childModel + public ChildModel ChildModel { get { - if (model == null) + if (Model == null) { return null; } - if (model is ChildModel cModel) + if (Model is ChildModel cModel) { return cModel; } @@ -20,7 +20,7 @@ public class ChildModelBehaviour : ModelBehaviour } internal set { - model = value; + Model = value; } } diff --git a/vr-configurator/Assets/Scripts/Models/Definitions.cs b/vr-configurator/Assets/Scripts/Models/Definitions.cs index 07e6528..9b3b100 100644 --- a/vr-configurator/Assets/Scripts/Models/Definitions.cs +++ b/vr-configurator/Assets/Scripts/Models/Definitions.cs @@ -1,4 +1,8 @@ -public class Definitions +/// +/// Class containing all PORT definitions +/// Can contain future IDs aswell +/// +public class Definitions { // BIKE PORTS public const string PORT_BIKE_SPROCKET = "bikeSprocket"; @@ -10,6 +14,7 @@ // ERLCAR PORTS + // TODO: rename all important ones public const string PORT_ERLBACH_0 = "portErlbach0"; public const string PORT_ERLBACH_1 = "portErlbach1"; public const string PORT_ERLBACH_2 = "portErlbach2"; diff --git a/vr-configurator/Assets/Scripts/Models/Model.cs b/vr-configurator/Assets/Scripts/Models/Model.cs index 66645e4..d27b1f6 100644 --- a/vr-configurator/Assets/Scripts/Models/Model.cs +++ b/vr-configurator/Assets/Scripts/Models/Model.cs @@ -37,62 +37,13 @@ public class Model this.scale = Vector3.one; } - public bool hasPorts() + public bool HasPorts() { return ports != null && ports.Count > 0; } - public void addPort(Port port) + public void AddPort(Port port) { ports.Add(port); } - - /// - /// Spawns all Port-GO's, should be called only once - /// - /// gameObject in the MonoBehaviour - /// All spawned GO's - public List spawnAllPorts(GameObject parent) - { - Debug.Log($"{this.nameId}: Spawning {parent.name} count of {ports.Count} Ports"); - List ret = new List(); - for (int i = 0; i < ports.Count; i++) - { - var port = ports[i]; - Debug.Log(i + ". Creating port " + port.port); - // bike:wheel:num - GameObject child = new GameObject(parent.name + ":" + port.port + ":" + i); - ChildModelBehaviour cmb = child.AddComponent(); - - var mm = Object.FindAnyObjectByType(); - if (mm == null) - { - Debug.LogError("ModelManager not found"); - } - var model = mm.getById(port.defaultId); - - if (model.mesh == null) - { - Debug.LogError("Default Mesh Not Found, destoying child"); - Object.Destroy(child); - continue; - } - Debug.Log($"Loaded Default Mesh: {model.mesh.name}"); - //set mesh - var meshFilter = child.AddComponent(); - meshFilter.mesh = model.mesh; - meshFilter.sharedMesh = model.mesh; - //set material - var meshRenderer = child.AddComponent(); - meshRenderer.material = model.material; - - child.transform.SetParent(parent.transform); //makes this an actual child - port.apply(child.transform); // move to correct position - ret.Add(child); // i debugged 1.5 hours for this - } - - return ret; - } - - } \ No newline at end of file diff --git a/vr-configurator/Assets/Scripts/Models/ModelBehaviour.cs b/vr-configurator/Assets/Scripts/Models/ModelBehaviour.cs index d5d8450..e7f84cb 100644 --- a/vr-configurator/Assets/Scripts/Models/ModelBehaviour.cs +++ b/vr-configurator/Assets/Scripts/Models/ModelBehaviour.cs @@ -10,7 +10,7 @@ public class ModelBehaviour : MonoBehaviour { private Model _model; - internal Model model + internal Model Model { get { @@ -18,68 +18,84 @@ public class ModelBehaviour : MonoBehaviour } set { - _model = value; UpdateModel(value); + _model = value; } } //should be verified - internal List children; + internal List Children = new List(); internal MeshFilter meshFilter; internal MeshRenderer meshRenderer; void Start() { - var filter = gameObject.GetComponent(); - if (filter == null) - { - filter = gameObject.AddComponent(); - } - meshFilter = filter; - - var renderer = gameObject.GetComponent(); - if (renderer == null) - { - renderer = gameObject.AddComponent(); - } - meshRenderer = renderer; + Init(); } - private void UpdateModel(Model _model) + void Init() // unity my beloved edge case { - //kill old children - foreach (var go in GetComponentsInChildren()) + if(meshFilter == null) + meshFilter = gameObject.GetOrAddComponent(); + if(meshRenderer == null) + meshRenderer = gameObject.GetOrAddComponent(); + } + + private void UpdateModel(Model newModel) + { + if (newModel is BaseModel) { - Debug.Log($"Destroying {go.name}"); - Destroy(go); // will be destroyed next frame + this.name = "BaseModel:" + newModel.nameId; + } else if (newModel is ChildModel cModel) + { + this.name = cModel.port + ":" + newModel.nameId; + } + Init(); + Debug.Log($"Model {name} update to new {newModel} Model."); + //unapply model offset + if (Model != null) + { + transform.localPosition -= Model.offset; + //rotation + scale is set, not modified + } + + //kill old children + foreach (var compo in GetComponentsInChildren()) + { + if (compo == this) // why does InChildren include the parent + { + continue; + } + Debug.Log($"Destroying {name}'s Child {compo.gameObject.name}"); + Destroy(compo.gameObject); // will be destroyed next frame } //spawn new childPorts - if (model.hasPorts()) + if (newModel.HasPorts()) { Debug.Log($"Spawning {gameObject.name}'s ports"); - children = model.spawnAllPorts(gameObject); + spawnChildPorts(newModel); } //change ourselves - meshFilter.mesh = _model.mesh; - meshFilter.sharedMesh = _model.mesh; - meshRenderer.material = _model.material; - meshRenderer.sharedMaterial = _model.material; - transform.rotation = _model.rotation; - transform.localScale = _model.scale; - transform.localPosition += _model.offset; + meshFilter.mesh = newModel.mesh; + meshFilter.sharedMesh = newModel.mesh; + meshRenderer.material = newModel.material; + meshRenderer.sharedMaterial = newModel.material; + transform.rotation = newModel.rotation; + transform.localScale = newModel.scale; + transform.localPosition += newModel.offset; } public void UpdateChild(int childNum, string id) { var newChildModel = FindAnyObjectByType().getById(id); - children[childNum].GetComponent().UpdateModel(newChildModel); + Children[childNum].GetComponent().UpdateModel(newChildModel); } - public List getChildrenWithPort(string port) + public List GetChildrenWithPort(string port) { List list = new List(); - for (int i = 0; i < children.Count; i++) + for (int i = 0; i < Children.Count; i++) { - if (children[i].GetComponent().childModel.port == port) + if (Children[i].GetComponent().ChildModel.port == port) { list.Add(i); } @@ -87,25 +103,61 @@ public class ModelBehaviour : MonoBehaviour return list; } - public bool setFirstWithPort(string port, ChildModel _childModel) + public bool SetFirstWithPort(string port, ChildModel childModel) { - Debug.Log($"{this.name}: has {children.Count} children"); - for (int i = 0; i < children.Count; i++) + Debug.Log($"{this.name}: has {Children.Count} children"); + for (int i = 0; i < Children.Count; i++) { - var cmb = children[i].GetComponent(); - Debug.Log($"{this.name}: {i} name {children[i].name} - port {cmb.childModel.port}"); - if(cmb.childModel.port == port) + var cmb = Children[i].GetComponent(); + Debug.Log($"{this.name}: {i} name {Children[i].name} - port {cmb.ChildModel.port}"); + if(cmb.ChildModel.port == port) { - if (_childModel == null) + if (childModel == null) { Debug.LogWarning($"{this.name}: Cant replace port {port} with model, model is null"); return false; } - cmb.UpdateModel(model); + cmb.UpdateModel(Model); return true; } } Debug.LogWarning($"{this.name}: Didnt find port {port}"); return false; } + + /// + /// Spawns all Port-GO's, should be called only on Model Switch after cleanup + /// + /// Model the New Model to spawn ports from + private void spawnChildPorts(Model newModel) + { + Debug.Log($"{newModel.nameId}: Spawning {this.name} count of {newModel.ports.Count} Ports"); + + for (int i = 0; i < newModel.ports.Count; i++) + { + var port = newModel.ports[i]; + Debug.Log(i + ". Creating port " + port.port); + // bike:wheel:num + GameObject child = new GameObject(this.name + ":" + port.port + ":" + i); + child.transform.SetParent(this.transform); //makes this an actual child + ChildModelBehaviour cmb = child.AddComponent(); + + var mm = FindAnyObjectByType(); + if (mm == null) + { + Debug.LogError("ModelManager not found"); + } + + var childModel = mm.getById(port.defaultId); + if (childModel.mesh == null) + { + Debug.LogError("Default Mesh Not Found, destoying child"); + Destroy(child); + continue; + } + cmb.ChildModel = childModel; + port.apply(child.transform); // move to correct position + Children.Add(child); + } + } } diff --git a/vr-configurator/Assets/Scripts/Models/ModelManager.cs b/vr-configurator/Assets/Scripts/Models/ModelManager.cs index 7c62f58..f49d27a 100644 --- a/vr-configurator/Assets/Scripts/Models/ModelManager.cs +++ b/vr-configurator/Assets/Scripts/Models/ModelManager.cs @@ -103,39 +103,26 @@ public class ModelManager : MonoBehaviour // Neues GameObject für das BaseModel erstellen this.baseModel = new GameObject(baseModel.nameId) { - name = baseModel.nameId + name = baseModel.nameId, }; this.baseModel.AddComponent(); // Positionieren - this.baseModel.transform.position = new Vector3(0, 0, 1); + this.baseModel.transform.position = new Vector3(0, 0, 1); this.baseModel.transform.rotation = Quaternion.Euler(0, 90, 0); this.baseModel.SetActive(true); } - this.baseModel.GetComponent().baseModel = baseModel; - this.baseModel.name = baseModel.nameId; + this.baseModel.GetComponent().BaseModel = baseModel; this.baseModel.SetActive(true); - Debug.Log($"Modell {baseModel.nameHuman} lock and loaded."); + Debug.Log($"Model {baseModel.nameHuman} lock and loaded."); } public void openSelector() { Debug.Log("Open Selector"); - // Altes BaseModel zerstören und ein neues leeres erstellen - - if (baseModel != null) - { - Destroy(baseModel); // Altes BaseModel zerstören - - - baseModel = new GameObject("BaseModel"); - baseModel.AddComponent(); - baseModel.transform.position = new Vector3(0,0,1); // Optional: Position setzen - Debug.Log("Neues leeres BaseModel erstellt."); - } - - baseModelSelector.Activate(); //actimel aktiviert ausw'hlkrafte + baseModel.SetActive(false); + baseModelSelector.Activate(); //actimel aktiviert auswahlkräfte } diff --git a/vr-configurator/Assets/Scripts/UI/ReturnButtonBehaviour.cs b/vr-configurator/Assets/Scripts/UI/ReturnButtonBehaviour.cs index fd44996..c6729f4 100644 --- a/vr-configurator/Assets/Scripts/UI/ReturnButtonBehaviour.cs +++ b/vr-configurator/Assets/Scripts/UI/ReturnButtonBehaviour.cs @@ -25,8 +25,7 @@ public class ReturnButtonBehaviour : MonoBehaviour { gameObject.SetActive(true); //black magic - var cameraTf = GameObject.Find("[BuildingBlock] Camera Rig").transform; - + //var cameraTf = GameObject.Find("[BuildingBlock] Camera Rig").transform; //gameObject.transform.position = cameraTf.position + new Vector3(5f,-0.5f,5f); //gameObject.transform.rotation = Quaternion.LookRotation(gameObject.transform.position - cameraTf.position); }