TreeNode getNode(LSystemGraph structureGraph) { TreeNode node = new TreeNode(); node.startIndex = structureGraph.id; node.startPosOld = structureGraph.position; node.originalOrientation = structureGraph.orientation; node.startLSNode = structureGraph; while (structureGraph != null && structureGraph.neighbour.Count == 1) { structureGraph = structureGraph.neighbour[0] as LSystemGraph; } node.endIndex = structureGraph.id; node.endPosOld = structureGraph.position; if (structureGraph.neighbour.Count == 2) { node.firstChild = getNode(structureGraph.neighbour[0] as LSystemGraph); node.secondChild = getNode(structureGraph.neighbour[1] as LSystemGraph); } return(node); }
LSystemGraph updateStructureGraph(GraphNode node, LSystemGraph parent) { node.visited = true; node.position = new Vector3(deformedPositions [node.id].x, deformedPositions [node.id].y); foreach (GraphNode neighbour in node.neighbours) { LSystemGraph nNode = new LSystemGraph(); nNode.id = node.id; nNode.symbol = "R"; Vector3 gNodePos = new Vector3(deformedPositions [node.id].x, deformedPositions [node.id].y); Vector3 gNeigPos = new Vector3(deformedPositions [neighbour.id].x, deformedPositions [neighbour.id].y); nNode.position = gNodePos; Vector3 nodeChildVec = (gNeigPos - gNodePos); nNode.orientation = Quaternion.FromToRotation(Vector3.up, nodeChildVec.normalized); nNode.length = nodeChildVec.magnitude / 4f; parent.neighbour.Add(nNode); if (!neighbour.visited) { updateStructureGraph(neighbour, nNode); } } return(parent); }
Graph _recStraightBranches(LSystemGraph parent, LSystemGraph root) { LSystemGraph node = new LSystemGraph(); node.id = root.id; node.symbol = root.symbol; node.length = root.length; node.isBranchingNode = root.isBranchingNode; node.parent = parent; if (!node.isBranchingNode) { node.angle = 0; } else { node.angle = root.angle; } node.orientation = parent.orientation * Quaternion.AngleAxis(node.angle, Vector3.forward); node.position = parent.position + parent.orientation * (4.0f * parent.length * Vector3.up); foreach (LSystemGraph child in root.neighbour) { node.neighbour.Add(_recStraightBranches(node, child)); } return(node); }
LSystemGraph copyStructure(LSystemGraph parent, LSystemGraph node) { if (node == null) { return(null); } LSystemGraph resNode = new LSystemGraph(); resNode.id = node.id; resNode.symbol = node.symbol; resNode.parent = parent; resNode.angle = node.angle; resNode.isBranchingNode = node.isBranchingNode; resNode.length = node.length; resNode.orientation = node.orientation; resNode.position = node.position; resNode.size = node.size; foreach (LSystemGraph child in node.neighbour) { resNode.neighbour.Add(copyStructure(resNode, child)); } return(resNode); }
private Vector3 elementEndPos(LSystemGraph node) { Vector3 position = node.position; Quaternion orientation = node.orientation; float length = node.length; float leafScale = node.symbol == "L" ? 3f : 1f; return(position + orientation * (4.0f * length * leafScale * Vector3.up)); }
void updateStructure(TreeNode node, LSystemGraph root) { if (node == null) { return; } snapStructureToFinalTreeNode(node, root); updateStructure(node.firstChild, root); updateStructure(node.secondChild, root); }
void fillHullInformation(TreeNode node, LSystemGraph root) { if (node == null) { return; } preFillHullInformation(node, root); fillHullInformation(node.firstChild, root); fillHullInformation(node.secondChild, root); }
void rotateBranch(LSystemGraph node, int endIndex) { Interpreter current = new Interpreter(node.orientation, node.position); Stack <Interpreter> stack = new Stack <Interpreter> (); stack.Push(current); bool branching = false; bool wasLast = false; current.position = current.position + current.direction * node.spacePos; if (node.neighbour.Count == 0) { return; } node = node.neighbour[0] as LSystemGraph; while (!wasLast) { switch (node.symbol) { case "m": if (!branching) { current.direction = current.direction * Quaternion.Euler(node.spaceAngle); } node.position = current.position; node.orientation = current.direction; current.position = current.position + current.direction * node.spacePos; branching = false; break; default: break; } if (node.id == endIndex) { wasLast = true; } else { node = node.neighbour[0] as LSystemGraph; } } }
void _translateBranch(LSystemGraph node, int endIndex, Vector3 toOldPosition) { if (node.id > endIndex) { return; } node.position += toOldPosition; foreach (LSystemGraph child in node.neighbour) { _translateBranch(child, endIndex, toOldPosition); } }
void _collectDistances(LSystemGraph root, LSystemGraph node) { if (node == null) { return; } distances.Add(Vector3.Distance(root.position, node.position)); foreach (LSystemGraph child in node.neighbour) { _collectDistances(root, child); } }
void _recRotateBranch(LSystemGraph parent, LSystemGraph node, int endIndex) { if (node.id > endIndex) { return; } node.orientation = parent.orientation * Quaternion.AngleAxis(node.angle, Vector3.forward); node.position = parent.position + parent.orientation * (4.0f * parent.length * Vector3.up); foreach (LSystemGraph child in node.neighbour) { _recRotateBranch(node, child, endIndex); } }
int _countParts(LSystemGraph structNode) { if (structNode == null) { return(0); } int sum = 1; foreach (LSystemGraph child in structNode.neighbour) { sum += _countParts(child); } return(sum); }
float _countSizes(LSystemGraph structNode) { if (structNode == null) { return(0); } float sum = structNode.size.magnitude; foreach (LSystemGraph child in structNode.neighbour) { sum += _countSizes(child); } return(sum); }
void _collectAngles(LSystemGraph node) { if (node == null) { return; } foreach (LSystemGraph child in node.neighbour) { if (!child.isBranchingNode) { angles.Add(Quaternion.Angle(node.orientation, child.orientation)); } _collectAngles(child); } }
void preFillHullInformation(TreeNode node, LSystemGraph root) { node.hullStart = node.startPosNew; node.hullEnd = node.endPosNew; Vector3 hullDirection = (node.hullEnd - node.hullStart).normalized; node.hullLeft = node.hullEnd + new Vector3(-hullDirection.y, hullDirection.x, hullDirection.z); node.hullRight = node.hullEnd + new Vector3(hullDirection.y, -hullDirection.x, hullDirection.z); float max = -99; float min = 99; for (int i = node.startIndex; i <= node.endIndex; i++) { Vector3 pos = elementEndPos(getGraphNode(i, root)); pos.z = 0; float distance = DistanceToLine(new Ray(node.hullStart, node.hullEnd - node.hullStart), pos); float angle = Vector3.Angle(node.hullEnd - node.hullStart, pos - node.hullStart); float distanceTreshold = (pos - node.hullStart).magnitude * Mathf.Cos(angle * Mathf.Deg2Rad); distance *= distanceTreshold < 32 ? 0f : 1; if (distance > 0) { if (distance > max) { max = distance; node.hullLeft = pos; } } else if (distance < 0) { if (distance < min) { min = distance; node.hullRight = pos; } } } float hullDiameter = (node.hullEnd - node.hullStart).magnitude; node.hullLeft = node.hullStart + hullDiameter * (node.hullLeft - node.hullStart).normalized; node.hullRight = node.hullStart + hullDiameter * (node.hullRight - node.hullStart).normalized; }
void translateBranch(LSystemGraph node, int endIndex, Vector3 toOldPosition) { bool wasLast = false; while (!wasLast) { node.position += toOldPosition; if (node.id == endIndex) { wasLast = true; } else { node = node.neighbour[0] as LSystemGraph; } } }
void _fillValues(LSystemGraph node) { if (node == null) { return; } elementVol += node.size.x * node.size.y * node.size.z; if (node.position.x > boxMax.x) { boxMax.x = node.position.x; } if (node.position.y > boxMax.y) { boxMax.y = node.position.y; } if (node.position.z > boxMax.z) { boxMax.z = node.position.z; } if (node.position.x < boxMin.x) { boxMin.x = node.position.x; } if (node.position.y < boxMin.y) { boxMin.y = node.position.y; } if (node.position.z < boxMin.z) { boxMin.z = node.position.z; } foreach (LSystemGraph child in node.neighbour) { _fillValues(child); } }
LSystemGraph getGraphNode(int index, LSystemGraph root) { if (root.id != index) { foreach (LSystemGraph child in root.neighbour) { LSystemGraph lsg = getGraphNode(index, child); if (lsg != null) { return(lsg); } } } else { return(root); } return(null); }
void snapStructureToFinalTreeNode(TreeNode node, LSystemGraph root) { Vector3 oldDir = node.endPosOld - node.startPosOld; Vector3 newDir = node.endPosNew - node.startPosNew; Quaternion toNewOrientation = Quaternion.FromToRotation(oldDir, newDir); Vector3 toOldPosition = node.startPosNew; LSystemGraph lSystemNode = getGraphNode(node.startIndex, root); lSystemNode.position = new Vector3(0, 0, 0); lSystemNode.orientation = toNewOrientation * node.originalOrientation; foreach (LSystemGraph child in lSystemNode.neighbour) { _recRotateBranch(lSystemNode, child, node.endIndex); } _translateBranch(lSystemNode, node.endIndex, toOldPosition); }
Graph straightBranches(Graph root) { LSystemGraph tmpRoot = root as LSystemGraph; LSystemGraph result = new LSystemGraph(); result.id = tmpRoot.id; result.symbol = tmpRoot.symbol; result.isBranchingNode = tmpRoot.isBranchingNode; result.length = tmpRoot.length; result.angle = tmpRoot.angle; result.position = tmpRoot.position; result.orientation = tmpRoot.orientation; foreach (LSystemGraph child in tmpRoot.neighbour) { result.neighbour.Add(_recStraightBranches(result, child)); } return(result); }
void fillHullInformation(TreeNode node, LSystemGraph root) { if (node == null) { return; } preFillHullInformation(node, root); fillHullInformation(node.firstChild, root); fillHullInformation(node.secondChild, root); // continue only if you have kids if (node.firstChild == null || node.secondChild == null) { return; } Vector3 firstChildRightDir = (node.firstChild.hullRight - node.firstChild.hullStart).normalized; Vector3 secondChildLeftDir = (node.secondChild.hullLeft - node.secondChild.hullStart).normalized; Vector3 nodeDir = (node.hullEnd - node.hullStart).normalized; float fcnAngle = Vector3.Angle(firstChildRightDir, nodeDir); float scnAngle = Vector3.Angle(secondChildLeftDir, nodeDir); if (fcnAngle > 5) { Quaternion quat = Quaternion.FromToRotation(firstChildRightDir, nodeDir); rotateTree(node.firstChild, quat, Vector3.zero); } if (scnAngle > 5) { Quaternion quat = Quaternion.FromToRotation(secondChildLeftDir, nodeDir); rotateTree(node.secondChild, quat, Vector3.zero); } computeNewHull(node); }
private void _recFillSizeValues(List <GameObject> goList, LSystemGraph node, ref float minXSize, ref float maxXSize) { if (goList [node.id].GetComponent <Renderer>() != null) { Vector3 endPos = elementEndPos(node); if (endPos.x < minXSize) { minXSize = endPos.x; } if (endPos.x > maxXSize) { maxXSize = endPos.x; } } foreach (LSystemGraph child in node.neighbour) { _recFillSizeValues(goList, child, ref minXSize, ref maxXSize); } }
LSystemGraph copyStructure(LSystemGraph parent, LSystemGraph node) { if (node == null) { return(null); } LSystemGraph resNode = new LSystemGraph(); resNode.id = node.id; resNode.symbol = node.symbol; resNode.parent = parent; resNode.position = node.position; resNode.orientation = node.orientation; resNode.spacePos = node.spacePos; resNode.spaceAngle = node.spaceAngle; foreach (LSystemGraph child in node.neighbour) { resNode.neighbour.Add(copyStructure(resNode, child)); } return(resNode); }
public static Structure LoadIvy(int id, int dimension, string type, XmlNode structureXml) { Structure structure = new Structure(); RepresentationMesh representation = new RepresentationMesh(dimension); structure.name = "Ivy_" + id; representation.gameObject = new GameObject(structure.name); int priority = 1; XmlNode structureParametersXml = structureXml.FirstChild; foreach (XmlNode parameterXml in structureParametersXml) { structure.parameters[parameterXml.Attributes ["name"].Value] = new StructureParameter(System.Convert.ToSingle(parameterXml.Attributes ["value"].Value), priority); priority++; } // structure state XmlNode stringNode = structureXml.LastChild.FirstChild; string stringValue = stringNode.InnerText.Trim(); string[] stringPairs = stringValue.Split(new char[] { '[', ']' }, System.StringSplitOptions.RemoveEmptyEntries); List <KeyValuePair <string, string> > symbolsStrings = new List <KeyValuePair <string, string> >(); int index = 0; while (index < stringPairs.Length) { if (stringPairs[index].Length == 1) { if (stringPairs[index] == "E") { symbolsStrings.Add(new KeyValuePair <string, string>("E", "")); index++; continue; } else { symbolsStrings.Add(new KeyValuePair <string, string>(stringPairs[index], stringPairs[index + 1])); index += 2; continue; } } else { index++; } } int symbolId = 0; bool isBranching = false; Interpreter current = new Interpreter(Quaternion.identity, representation.gameObject.transform.position); Stack <Interpreter> stack = new Stack <Interpreter> (); stack.Push(current); LSystemGraph node = new LSystemGraph(); node.neighbour = new List <Graph> (); Stack <LSystemGraph> nodeStack = new Stack <LSystemGraph>(); representation.structure = node; nodeStack.Push(node); foreach (KeyValuePair <string, string> symbolStrings in symbolsStrings) { Dictionary <string, float> structureParameters = new Dictionary <string, float> (); string[] parameters = symbolStrings.Value.Split(new char[] { ',' }, System.StringSplitOptions.RemoveEmptyEntries); foreach (string parameterStr in parameters) { string[] keyValue = parameterStr.Split(new char[] { '=' }, System.StringSplitOptions.RemoveEmptyEntries); if (keyValue.Length != 2) { continue; } structureParameters[keyValue[0].Trim()] = System.Convert.ToSingle(keyValue[1]); } switch (symbolStrings.Key) { case "S": { current.direction = current.direction * Quaternion.AngleAxis(structureParameters["angle"], Vector3.forward); LSystemGraph tempNode = new LSystemGraph(); tempNode.id = symbolId; tempNode.symbol = symbolStrings.Key; tempNode.position = current.position; tempNode.orientation = current.direction; tempNode.angle = structureParameters["angle"]; tempNode.length = structureParameters["length"]; tempNode.isBranchingNode = isBranching; tempNode.parent = node; node.neighbour.Add(tempNode); node = tempNode; current.position = current.position + current.direction * (4.0f * structureParameters["length"] * Vector3.up); isBranching = false; symbolId++; break; } case "B": { stack.Push(current); current = new Interpreter(current); nodeStack.Push(node); isBranching = true; break; } case "L": { Interpreter tempInter = new Interpreter(current.direction * Quaternion.AngleAxis(structureParameters["angle"], Vector3.forward), current.position); LSystemGraph tempNode = new LSystemGraph(); tempNode.id = symbolId; tempNode.symbol = symbolStrings.Key; tempNode.position = tempInter.position; tempNode.orientation = tempInter.direction; tempNode.angle = structureParameters["angle"]; tempNode.length = structureParameters["length"]; tempNode.isBranchingNode = true; tempNode.parent = node; node.neighbour.Add(tempNode); symbolId++; break; } case "T": { LSystemGraph tempNode = new LSystemGraph(); tempNode.id = symbolId; tempNode.symbol = symbolStrings.Key; tempNode.position = current.position; tempNode.orientation = current.direction; tempNode.angle = structureParameters["angle"]; tempNode.parent = node; node.neighbour.Add(tempNode); current = stack.Pop(); node = nodeStack.Pop(); symbolId++; break; } case "E": current = stack.Pop(); node = nodeStack.Pop(); break; default: break; } } representation.structure = representation.structure.neighbour [0]; putIvyMesh(representation.structure, representation.gameObject, representation.goList); representation.gameObject.SetActive(false); structure.representation = representation; Vector3 minPosSize = new Vector3(); Vector3 maxPosSize = new Vector3(); for (int i = 0; i < representation.gameObject.transform.childCount; i++) { GameObject childGO = representation.gameObject.transform.GetChild(i).gameObject; Bounds childBounds = childGO.GetComponent <Renderer> ().bounds; if (childBounds.max.x > maxPosSize.x) { maxPosSize.x = childBounds.max.x; } else if (childBounds.min.x < minPosSize.x) { minPosSize.x = childBounds.min.x; } if (childBounds.max.y > maxPosSize.y) { maxPosSize.y = childBounds.max.y; } else if (childBounds.min.y < minPosSize.y) { minPosSize.y = childBounds.min.y; } if (childBounds.max.z > maxPosSize.z) { maxPosSize.z = childBounds.max.z; } else if (childBounds.min.z < minPosSize.z) { minPosSize.z = childBounds.min.z; } } structure.representation.size = maxPosSize - minPosSize; return(structure); }
LSystemGraph copyStructure(LSystemGraph parent, LSystemGraph node, GameObject go) { if (node == null) { return(null); } LSystemGraph resNode = new LSystemGraph(); resNode.id = node.id; resNode.symbol = node.symbol; resNode.parent = parent; resNode.angle = node.angle; GameObject resObject = go.transform.GetChild(node.id).gameObject; Mesh mesh = Mesh.Instantiate(resObject.GetComponent <MeshFilter>().sharedMesh) as Mesh; Vector3[] vertices = mesh.vertices; Vector3 boxMin = new Vector3(999f, 999f, 999f); Vector3 boxMax = new Vector3(-999f, -999f, -999f); Vector3 resPosition = new Vector3(); int j = 0; while (j < vertices.Length) { Vector3 vertexPos = resObject.transform.rotation * Vector3.Scale(vertices[j], resObject.transform.localScale) + resObject.transform.position; vertexPos.x *= scaleX; vertexPos.y *= scaleY; if (vertexPos.x > boxMax.x) { boxMax.x = vertexPos.x; } if (vertexPos.y > boxMax.y) { boxMax.y = vertexPos.y; } if (vertexPos.z > boxMax.z) { boxMax.z = vertexPos.z; } if (vertexPos.x < boxMin.x) { boxMin.x = vertexPos.x; } if (vertexPos.y < boxMin.y) { boxMin.y = vertexPos.y; } if (vertexPos.z < boxMin.z) { boxMin.z = vertexPos.z; } resPosition += vertexPos; j++; } resNode.position = resPosition / vertices.Length; resNode.size = boxMax - boxMin; foreach (LSystemGraph child in node.neighbour) { resNode.neighbour.Add(copyStructure(resNode, child, go)); } return(resNode); }
void createMesh(Graph node, GameObject parent, List <GameObject> goList) { LSystemGraph lNode = node as LSystemGraph; if (lNode == null) { return; } GameObject prefab = null; float length = 1f; float width = 1f; switch (lNode.symbol) { case "S": { prefab = Resources.Load("2DSegment") as GameObject; length = lNode.length / 2f; break; } case "T": { prefab = Resources.Load("2DTip") as GameObject; break; } case "L": { prefab = Resources.Load("2DLeaf") as GameObject; length = lNode.length; width = lNode.length; break; } default: { GameObject gameObject = new GameObject(); gameObject.transform.localPosition = lNode.position; gameObject.transform.rotation = lNode.orientation; gameObject.transform.localScale = new Vector3(width, length, gameObject.transform.localScale.z); gameObject.name = lNode.symbol; gameObject.transform.parent = parent.transform; goList.Add(gameObject); foreach (Graph gNode in node.neighbour) { createMesh(gNode, parent, goList); } return; } } GameObject go = GameObject.Instantiate(prefab) as GameObject; go.transform.localPosition = lNode.position; go.transform.rotation = lNode.orientation; go.transform.localScale = new Vector3(width, length, go.transform.localScale.z); go.name = lNode.symbol; go.transform.parent = parent.transform; Bounds goBound = go.GetComponent <Renderer> ().bounds; if (maxYSize < goBound.max.y) { maxYSize = goBound.max.y; } if (minYSize > goBound.min.y) { minYSize = goBound.min.y; } goList.Add(go); foreach (Graph gNode in node.neighbour) { createMesh(gNode, parent, goList); } }
void createMesh(Graph node, GameObject parent, List <GameObject> goList /*, Material nMat*/) { LSystemGraph lNode = node as LSystemGraph; if (lNode == null) { return; } GameObject prefab = null; switch (lNode.symbol) { case "m": { prefab = Resources.Load("PARP/adp") as GameObject; break; } default: { GameObject gameObject = new GameObject(); gameObject.transform.localPosition = lNode.position; gameObject.transform.rotation = lNode.orientation; gameObject.name = lNode.symbol; gameObject.transform.parent = parent.transform; lNode.size = new Vector3(); goList.Add(gameObject); foreach (Graph gNode in node.neighbour) { createMesh(gNode, parent, goList /*, nMat*/); } return; } } GameObject go = GameObject.Instantiate(prefab) as GameObject; go.transform.localPosition = lNode.position; go.transform.rotation = lNode.orientation; go.name = lNode.symbol; go.transform.parent = parent.transform; Mesh mesh = Mesh.Instantiate(go.GetComponent <MeshFilter>().sharedMesh) as Mesh; Vector3[] vertices = mesh.vertices; Vector3 boxMin = new Vector3(999f, 999f, 999f); Vector3 boxMax = new Vector3(-999f, -999f, -999f); int j = 0; while (j < vertices.Length) { Vector3 vertexPos = go.transform.rotation * Vector3.Scale(vertices[j], go.transform.localScale) + go.transform.position; if (vertexPos.x > boxMax.x) { boxMax.x = vertexPos.x; } if (vertexPos.y > boxMax.y) { boxMax.y = vertexPos.y; } if (vertexPos.z > boxMax.z) { boxMax.z = vertexPos.z; } if (vertexPos.x < boxMin.x) { boxMin.x = vertexPos.x; } if (vertexPos.y < boxMin.y) { boxMin.y = vertexPos.y; } if (vertexPos.z < boxMin.z) { boxMin.z = vertexPos.z; } if (vertexPos.z < minZ) { minZ = vertexPos.z; } if (vertexPos.z > maxZ) { maxZ = vertexPos.z; } j++; } lNode.size = boxMax - boxMin; /* * go.GetComponent<MeshRenderer> ().material = nMat; */ goList.Add(go); foreach (Graph gNode in node.neighbour) { createMesh(gNode, parent, goList /*, nMat*/); } }
void createMesh(LSystemGraph node, GameObject parent, List <GameObject> goList) { if (node == null) { return; } GameObject prefab = null; float length = 1f; float width = 1f; switch (node.symbol) { case "R": { prefab = Resources.Load("2DSegment") as GameObject; length = node.length / 2f; break; } default: { GameObject gameObject = new GameObject(); gameObject.transform.localPosition = node.position; gameObject.transform.rotation = node.orientation; gameObject.transform.localScale = new Vector3(width, length, parent.transform.localScale.z); gameObject.name = node.symbol; gameObject.transform.parent = parent.transform; node.size = new Vector3(); goList.Add(gameObject); foreach (LSystemGraph gNode in node.neighbour) { createMesh(gNode, parent, goList); } return; } } GameObject go = GameObject.Instantiate(prefab) as GameObject; go.transform.localPosition = node.position; go.transform.rotation = node.orientation; go.transform.localScale = new Vector3(width, length, go.transform.localScale.z); go.name = node.symbol; go.transform.parent = parent.transform; Mesh mesh = Mesh.Instantiate(go.GetComponent <MeshFilter>().sharedMesh) as Mesh; Vector3[] vertices = mesh.vertices; Vector3 boxMin = new Vector3(999f, 999f, 999f); Vector3 boxMax = new Vector3(-999f, -999f, -999f); int j = 0; while (j < vertices.Length) { Vector3 vertexPos = go.transform.rotation * Vector3.Scale(vertices[j], go.transform.localScale) + go.transform.position; if (vertexPos.x > boxMax.x) { boxMax.x = vertexPos.x; } if (vertexPos.y > boxMax.y) { boxMax.y = vertexPos.y; } if (vertexPos.z > boxMax.z) { boxMax.z = vertexPos.z; } if (vertexPos.x < boxMin.x) { boxMin.x = vertexPos.x; } if (vertexPos.y < boxMin.y) { boxMin.y = vertexPos.y; } if (vertexPos.z < boxMin.z) { boxMin.z = vertexPos.z; } j++; } node.size = boxMax - boxMin; goList.Add(go); foreach (LSystemGraph gNode in node.neighbour) { createMesh(gNode, parent, goList); } }
SpatialElement _getSymNode(LSystemGraph structNode, int level) { if (structNode == null) { return(null); } SpatialElement node = new SpatialElement(); node.level = level; node.stemCount = 0; node.allCount = 0; node.stemSize = 0; node.leftBranches = new List <SpatialElement> (); node.rightBranches = new List <SpatialElement> (); while (structNode.neighbour.Count != 1 && structNode.neighbour.Count != 0) { if (structNode.neighbour.Count != 2) { node.stemCount += structNode.neighbour.Count; foreach (LSystemGraph neighbour in structNode.neighbour) { node.stemSize += neighbour.size.magnitude; } structNode = structNode.neighbour[2] as LSystemGraph; } else { if (level > 0) { if ((structNode.neighbour[0] as LSystemGraph).angle < 0) { node.rightBranches.Add(_getSymNode(structNode.neighbour[0] as LSystemGraph, level - 1)); } else { node.leftBranches.Add(_getSymNode(structNode.neighbour[0] as LSystemGraph, level - 1)); } } else { node.stemCount += _countParts(structNode.neighbour[0] as LSystemGraph); node.stemSize += _countSizes(structNode.neighbour[0] as LSystemGraph); } structNode = structNode.neighbour[1] as LSystemGraph; } } node.stemCount += structNode.neighbour.Count; node.stemSize += structNode.size.magnitude; if (structNode.neighbour.Count == 0) { node.stemCount++; } int leftSum = 0; for (int i = 0; i < node.leftBranches.Count; i++) { leftSum += node.leftBranches[i].allCount; } int rightSum = 0; for (int i = 0; i < node.rightBranches.Count; i++) { rightSum += node.rightBranches[i].allCount; } node.allCount = node.stemCount + leftSum + rightSum; float leftSizeSum = 0; for (int i = 0; i < node.leftBranches.Count; i++) { leftSizeSum += node.leftBranches[i].allSize; } float rightSizeSum = 0; for (int i = 0; i < node.rightBranches.Count; i++) { rightSizeSum += node.rightBranches[i].allSize; } node.allSize = node.stemSize + leftSizeSum + rightSizeSum; return(node); }
void updateSize(GameObject gameObject, LSystemGraph node) { if (node == null) { return; } GameObject go = gameObject.transform.GetChild(node.id).gameObject; Mesh mesh = Mesh.Instantiate(go.GetComponent <MeshFilter>().sharedMesh) as Mesh; Vector3[] vertices = mesh.vertices; Vector3 boxMin = new Vector3(999f, 999f, 999f); Vector3 boxMax = new Vector3(-999f, -999f, -999f); int j = 0; while (j < vertices.Length) { Vector3 vertexPos = go.transform.rotation * Vector3.Scale(vertices[j], go.transform.localScale) + go.transform.position; if (vertexPos.x > boxMax.x) { boxMax.x = vertexPos.x; } if (vertexPos.y > boxMax.y) { boxMax.y = vertexPos.y; } if (vertexPos.z > boxMax.z) { boxMax.z = vertexPos.z; } if (vertexPos.x < boxMin.x) { boxMin.x = vertexPos.x; } if (vertexPos.y < boxMin.y) { boxMin.y = vertexPos.y; } if (vertexPos.z < boxMin.z) { boxMin.z = vertexPos.z; } j++; } node.size = boxMax - boxMin; foreach (LSystemGraph child in node.neighbour) { updateSize(gameObject, child); } }