public void Subdivide(LeModule.axis axis) { subdivisionAxis = axis; if (children == null) { children = new List <LeModule>(); } // Create modules if this is the first time we've been divided while (children.Count < 2) { var go = new GameObject(); go.transform.SetParent(this.transform); go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; var node = go.AddComponent <LeModule>(); children.Add(node); node.parent = this; var prefab = Resources.Load <GameObject>("Prefabs/Cube"); node.go = GameObject.Instantiate(prefab); node.go.transform.SetParent(node.transform); node.go.transform.localPosition = Vector3.zero; node.go.transform.localRotation = Quaternion.identity; Debug.Log(uid + " Added child"); } if (children != null) { if (children.Count < 2) { Debug.LogError(uid + " childNodes.Count < 2"); return; } } else { Debug.LogError(uid + " childNodes list == null"); return; } // Get a list containing two modulor values List <float> ms = new List <float>(); foreach (LeModule child in children) { switch (subdivisionAxis) { case axis.x: ms = Modulor.GetList(size.x, 2); break; case axis.y: ms = Modulor.GetList(size.y, 2); break; case axis.z: ms = Modulor.GetList(size.z, 2); break; } } // Check sizes to see if they are out of bounds foreach (var value in ms) { } // Revese the list randomly if (ExtRandom <bool> .Chance(1, 2)) { ms.Reverse(); } for (int i = 0; i < children.Count; i++) { switch (subdivisionAxis) { case axis.x: children[i].size.x = ms[i]; break; case axis.y: children[i].size.y = ms[i]; break; case axis.z: children[i].size.z = ms[i]; break; } } foreach (LeModule child in children) { var scale = child.go.transform.localScale; switch (subdivisionAxis) { case axis.x: scale.x = child.size.x; scale.y = size.y; scale.z = size.z; break; case axis.y: scale.x = size.x; scale.y = child.size.y; scale.z = size.z; break; case axis.z: scale.x = size.x; scale.y = size.y; scale.z = child.size.z; break; } child.go.transform.localScale = scale; } // Effect position // index 0 pos = index 1 scale / 2 // index 1 should be moved the scale of index 0 / 2 foreach (LeModule child in children) { var pos = child.transform.localPosition; int childIndex = children.IndexOf(child); if (childIndex == 0) { switch (subdivisionAxis) { case axis.x: pos.x = children[1].size.x / 2 * -1; pos.y = 0; pos.z = 0; break; case axis.y: pos.x = 0; pos.y = children[1].size.y / 2 * -1; pos.z = 0; break; case axis.z: pos.x = 0; pos.y = 0; pos.z = children[1].size.z / 2 * -1; break; } } else if (childIndex == 1) { switch (subdivisionAxis) { case axis.x: pos.x = children[0].size.x / 2; pos.y = 0; pos.z = 0; break; case axis.y: pos.x = 0; pos.y = children[0].size.y / 2; pos.z = 0; break; case axis.z: pos.x = 0; pos.y = 0; pos.z = children[0].size.z / 2; break; } } else { Debug.LogError(uid + "Index should not be greater than 1"); } child.transform.localPosition = pos; } // Set the size of the un effected axises to the values in the parent foreach (LeModule child in children) { switch (subdivisionAxis) { case axis.x: child.size.y = size.y; child.size.z = size.z; break; case axis.y: child.size.x = size.x; child.size.z = size.z; break; case axis.z: child.size.x = size.x; child.size.y = size.y; break; } } // Hide self foreach (LeModule item in children) { item.go.SetActive(true); } go.SetActive(false); }
void Update() { // todo: switch statement to set the current axis count if (divs < 0) { divs = 0; } if (divs > childNodes.Count) { while (divs > childNodes.Count) { var go = new GameObject(); go.transform.SetParent(this.transform); go.transform.localPosition = Vector3.zero; var node = go.AddComponent <Module>(); childNodes.Add(node); node.parentNode = this; var prefab = Resources.Load <GameObject>("Prefabs/Cube"); node.meshGo = GameObject.Instantiate(prefab); node.meshGo.transform.SetParent(node.transform); node.meshGo.transform.localPosition = Vector3.zero; // Debug.Log("Added child"); } } else if (divs < childNodes.Count) { while (divs < childNodes.Count) { var node = childNodes[childNodes.Count - 1]; GameObject.Destroy(node.gameObject); childNodes.RemoveAt(childNodes.Count - 1); // Debug.Log("Removed child"); } } // Set the size of the un effected axises to the values in the parent if (parentNode) { switch (parentNode.divAxis) { case axis.x: size.y = parentNode.size.y; size.z = parentNode.size.z; break; case axis.y: size.x = parentNode.size.x; size.z = parentNode.size.z; break; case axis.z: size.x = parentNode.size.x; size.y = parentNode.size.y; break; } } // Apply scale based child count if (childNodes != null) { if (childNodes.Count > 0) { List <float> ms = new List <float>(); foreach (Module item in childNodes) { switch (divAxis) { case axis.x: ms = Modulor.GetList(size.x, divs); break; case axis.y: ms = Modulor.GetList(size.y, divs); break; case axis.z: ms = Modulor.GetList(size.z, divs); break; } } for (int i = 0; i < childNodes.Count; i++) { switch (divAxis) { case axis.x: childNodes[i].size.x = ms[i]; break; case axis.y: childNodes[i].size.y = ms[i]; break; case axis.z: childNodes[i].size.z = ms[i]; break; } } foreach (Module item in childNodes) { var scale = item.meshGo.transform.localScale; switch (divAxis) { case axis.x: scale.x = item.size.x; scale.y = size.y; scale.z = size.z; // scale.x -= margin; break; case axis.y: scale.x = size.x; scale.y = item.size.y; scale.z = size.z; // scale.y -= margin; break; case axis.z: scale.x = size.x; scale.y = size.y; scale.z = item.size.z; // scale.z -= margin; break; } item.meshGo.transform.localScale = scale; } // Effect position // index 0 pos = index 1 scale / 2 // index 1 should be moved the scale of index 0 / 2 foreach (Module item in childNodes) { var pos = item.transform.localPosition; int index = childNodes.IndexOf(item); if (index == 0) { switch (divAxis) { case axis.x: pos.x = childNodes[1].size.x / 2 * -1; break; case axis.y: pos.y = childNodes[1].size.y / 2 * -1; break; case axis.z: pos.z = childNodes[1].size.z / 2 * -1; break; } } else if (index == 1) { switch (divAxis) { case axis.x: pos.x = childNodes[0].size.x / 2; break; case axis.y: pos.y = childNodes[0].size.y / 2; break; case axis.z: pos.z = childNodes[0].size.z / 2; break; } } else { Debug.LogError("Index should not be greater than 1"); } item.transform.localPosition = pos; } } } if (meshGo) { if (divs > 0) { visible = false; } } if (meshGo) { if (visible) { meshGo.SetActive(true); } else { meshGo.SetActive(false); } } }