public void Execute(string input, ref CGAContext context) { string primitive = ParsingHelper.QSString(input); string target = ParsingHelper.GetTarget(input); List <GameObject> targetRoots = null; if (!string.IsNullOrEmpty(target) && context.namedObjects.ContainsKey(target)) { targetRoots = context.namedObjects[target]; } string[] destinations = ParsingHelper.GetDest(input); foreach (InstantiateOption option in options) { if (option.name == primitive) { if (targetRoots == null) { GameObject ig = InstantiateCorrect(option.primitive, option); ig.transform.SetParent(context.root); ig.transform.localPosition = context.current.position; if (option.primitive == CGAPrimitive.Cube) { ig.transform.localPosition += new Vector3(0, context.current.scale.y / 2, 0); } else { ig.transform.localPosition += new Vector3(0, context.current.scale.y, 0); } ig.transform.localRotation = context.current.rotation; ig.transform.localScale = context.current.scale; if (destinations.Length > 0) { if (context.namedObjects.ContainsKey(destinations[0])) { context.namedObjects[destinations[0]].Add(ig); } else { List <GameObject> gol = new List <GameObject>(); gol.Add(ig); context.namedObjects.Add(destinations[0], gol); } ig.name = destinations[0]; } ig.GetComponent <Renderer>().material = context.material; } else { foreach (GameObject tr in targetRoots) { GameObject ig = InstantiateCorrect(option.primitive, option); ig.transform.SetParent(tr.transform); ig.transform.localPosition = Vector3.zero; ig.transform.localRotation = Quaternion.identity; ig.transform.localScale = Vector3.one; if (destinations.Length > 0) { if (context.namedObjects.ContainsKey(destinations[0])) { context.namedObjects[destinations[0]].Add(ig); } else { List <GameObject> gol = new List <GameObject>(); gol.Add(ig); context.namedObjects.Add(destinations[0], gol); } ig.name = destinations[0]; } if (option.primitive == CGAPrimitive.Roof) { ig.GetComponentInChildren <Renderer>().material = context.roofMat; } else if (option.primitive == CGAPrimitive.Window) { ig.GetComponentInChildren <Renderer>().material = context.windMat; } else { ig.GetComponent <Renderer>().material = context.material; } } } } } }
public void Execute(string input, ref CGAContext context) { string ax = ParsingHelper.QSString(input); SplitAxis sa = (SplitAxis)Enum.Parse(typeof(SplitAxis), ax);; string target = ParsingHelper.GetTarget(input); string[] destinations = ParsingHelper.GetDest(input); CSF csfVals = ParsingHelper.CSFValues(input); List <GameObject> t = null; if (!string.IsNullOrEmpty(target) && context.namedObjects.ContainsKey(target)) { t = context.namedObjects[target]; List <GameObject> newNamedObjectContent = new List <GameObject>(); Vector3 a = new Vector3(1, 0, 0); foreach (GameObject g in t) { GameObject newg = GameObject.Instantiate(g); newg.name = g.name; GameObject.Destroy(newg.GetComponent <MeshRenderer>()); GameObject.Destroy(newg.GetComponent <MeshFilter>()); newNamedObjectContent.Add(newg); newg.transform.SetParent(g.transform.parent); newg.transform.localScale = g.transform.localScale; newg.transform.localPosition = g.transform.localPosition; newg.transform.localRotation = g.transform.localRotation; Transform[] trans = g.GetComponentsInChildren <Transform>(); foreach (Transform tra in trans) { if (tra.gameObject != g) { GameObject.Destroy(tra.gameObject); } } GameObject tar = g; Vector3 start = g.transform.position + g.transform.lossyScale / 2; int s = csfVals.orderedCSF.Count; float size = 0; switch (sa) { case SplitAxis.X: a = -g.transform.right; size = g.transform.lossyScale.x; break; case SplitAxis.Y: a = -g.transform.up; size = g.transform.lossyScale.y; break; case SplitAxis.Z: a = -g.transform.forward; size = g.transform.lossyScale.z; break; } List <float> sizes = new List <float>(); float sizeOfDynamic = size; float perc = 0; foreach (float v in csfVals.fixedValues) { sizeOfDynamic -= (size * v); perc += v; } if (perc > 1) { Debug.LogWarning("You messed up something with percentages..."); } int relativeCount = 0; foreach (int v in csfVals.relativeValues) { relativeCount += v; } float acumulatedPosition = 0; List <GameObject> splits = new List <GameObject>(); for (int i = 1; i < s; i++) { if (csfVals.orderedCSF[i - 1].isRelative) { acumulatedPosition += (csfVals.relativeValues[csfVals.orderedCSF[i - 1].index] / (relativeCount * 1.0f)) * sizeOfDynamic; } else { acumulatedPosition += csfVals.fixedValues[csfVals.orderedCSF[i - 1].index] * size; } GameObject[] splited = MeshCut.Cut(tar, start + a * acumulatedPosition, a, tar.GetComponent <Renderer>().material); //((size/s) * i) tar = splited[1]; if (destinations != null && destinations.Length > i) { string name = destinations[i - 1]; splited[0].name = name; if (context.namedObjects.ContainsKey(name)) { context.namedObjects[name].Add(splited[0]); } else { context.namedObjects.Add(name, new List <GameObject>() { splited[0] }); } } splited[0].transform.SetParent(newg.transform); splited[0].transform.localScale = Vector3.one; splited[1].transform.SetParent(newg.transform); splited[1].transform.localScale = Vector3.one; if (destinations != null && destinations.Length > i && i == s - 1) { string name = destinations[i]; splited[1].name = name; if (context.namedObjects.ContainsKey(name)) { context.namedObjects[name].Add(splited[1]); } else { context.namedObjects.Add(name, new List <GameObject>() { splited[1] }); } } if (i != s - 1) { splits.Add(splited[0]); } else { splits.Add(splited[0]); splits.Add(splited[1]); } } foreach (GameObject sgo in splits) { MeshCut.UpdatePivot(sgo); } //Rebuild dict! context.namedObjects[target] = newNamedObjectContent; } } else { Debug.LogWarning("Cannot split without any target"); return; } }