void MoveTurtle() { foreach (char ch in sentence) { switch (ch) { case 'F': Vector3 startPos = turtle.CrtPosition; turtle.Translate(); Vector3 endPos = turtle.CrtPosition; //DrawBranchLine(startPos, endPos); DrawBranch(startPos, endPos); turtle.MultiplyWidth(scaleFactor); break; case '+': turtle.RotateTheta(GetVariationOfAngle(theta, variance)); turtle.MultiplyWidth(scaleFactor * 0.75f); break; case '-': turtle.RotateTheta(-GetVariationOfAngle(theta, variance)); turtle.MultiplyWidth(scaleFactor * 0.75f); break; case '&': turtle.RotatePhi(GetVariationOfAngle(phi, variance)); turtle.MultiplyWidth(scaleFactor * 0.75f); break; case '^': turtle.RotatePhi(-GetVariationOfAngle(phi, variance)); turtle.MultiplyWidth(scaleFactor * 0.75f); break; case '[': turtle.Push(); break; case ']': //DrawLeafLine(turtle.CrtPosition, turtle.CrtPosition + turtle.GetRotatedTranslation()); DrawLeaf(turtle.CrtPosition); turtle.Pull(); break; default: break; } } }
// Start is called before the first frame update void Start() { string work = axiom; for (int i = 0; i < depth; i++) { work = Expand(rules, work); } Turtle turtle = new Turtle(transform.position, transform.rotation * Quaternion.AngleAxis(90.0f, Vector3.left)); foreach (char c in work) { foreach (Action action in actions) { if (action.symbol == c) { if (action.pop) { turtle.Pop(); } if (action.push) { turtle.Push(); } if (action.rotX != 0.0f || action.rotY != 0.0f || action.rotZ != 0.0f) { turtle.Turn(new Vector3(action.rotX, action.rotY, action.rotZ)); } if (action.moveZ != 0.0f) { turtle.MoveForward(action.moveZ); if (action.leaf) { Instantiate(leafPrefab, turtle.GetPosition(), turtle.GetRotation()); } else { Instantiate(trunkPrefab, turtle.GetPosition(), turtle.GetRotation()); } } } } } }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); this.Services.AddService(typeof(SpriteBatch), spriteBatch); system = new TreeSystem(); turtle = new Turtle(this); turtle.Operations.Add('a', () => turtle.Draw(dist)); turtle.Operations.Add('b', () => turtle.Draw(dist)); turtle.Operations.Add('m', () => turtle.Move(dist)); turtle.Operations.Add('-', () => turtle.Turn(MathHelper.ToRadians(-angle))); turtle.Operations.Add('+', () => turtle.Turn(MathHelper.ToRadians(angle))); turtle.Operations.Add('[', () => { turtle.Push(); turtle.Turn(MathHelper.ToRadians(-angle)); }); turtle.Operations.Add(']', () => { turtle.Pop(); turtle.Turn(MathHelper.ToRadians(angle)); }); turtle.Drawings.Add(system.Get(level, 1)); base.LoadContent(); }
public void Interpret(out Mesh meshBranches, out Mesh meshLeaves) { Turtle turtle = new Turtle(Eval(initialWidth)); foreach (var elem in str){ switch (elem.symbol){ case LSElement.LSSymbol.LEAF: AddLeaf (turtle.Peek().M,elem.data [0], elem.data [1], turtle.GetDist()); break; case LSElement.LSSymbol.DRAW: float movDist = elem.data [0]; AddCone(turtle.Peek().M, movDist, turtle.GetWidth(), turtle.GetWidth() * elem.data[1], turtle.GetDist()); turtle.Move(movDist); break; case LSElement.LSSymbol.TURN: turtle.Turn(elem.data[0]); break; case LSElement.LSSymbol.ROLL: turtle.Roll(elem.data[0]); break; case LSElement.LSSymbol.PUSH_STATE: turtle.Push(); break; case LSElement.LSSymbol.POP_STATE: turtle.Pop(); break; case LSElement.LSSymbol.WIDTH: turtle.SetWidth(elem.data[0]); break; case LSElement.LSSymbol.GRAVITY: turtle.Gravity(elem.data[0]); break; } } float max = 0; foreach (var u in uvLeafs) { max = Mathf.Max (u.x, max); } for (int i = 0; i < uvs.Count; i++) { uvs [i] = new Vector2( uvs [i].x/max,0); } for (int i = 0; i < uvLeafs.Count; i++) { uvLeafs [i] = new Vector2( uvLeafs[i].x/max ,0); } meshBranches = new Mesh(); if (vertices.Count >= 65536) { Debug.LogError ("Tree - too many verts: "+vertices.Count); } else { vertCount = vertices.Count; meshBranches.vertices = vertices.ToArray (); meshBranches.triangles = indices.ToArray (); meshBranches.uv = uvs.ToArray (); PostprocessMesh (meshBranches); //Debug.Log ("vertices "+vertices.Count); } uvs.Clear(); vertices.Clear(); indices.Clear(); meshLeaves = new Mesh(); if (verticesLeaf.Count >= 65536) { Debug.LogError ("Tree leaves - too many verts: "+verticesLeaf.Count); } else { vertLeafCount = verticesLeaf.Count; meshLeaves.vertices = verticesLeaf.ToArray (); meshLeaves.triangles = indicesLeaf.ToArray (); meshLeaves.uv = uvLeafs.ToArray (); PostprocessMesh (meshLeaves); } uvLeafs.Clear(); verticesLeaf.Clear(); indicesLeaf.Clear(); }