示例#1
0
    /// <summary>
    /// converts an L-System in to a collection of instantiations of objects using a stack of Turtle3D objects
    /// Renderable characters are:
    /// F - Draw Stem at current scale
    /// F(x) - Draw stem of width (x)
    /// L - Draw Leaf at current scale
    /// </summary>
    /// <param name="lsystem">string containing a parameterized and/or bracketed L-System</param>

    void TreeBuilder(string lsystem)
    {
        turtles.Push(topTurtle);
        for (int i = 0; i < lsystem.Length; i++)
        {
            //
            float angle = 25f;
            // Process the commands, many of them just set values on the top turtle of the stack
            switch (lsystem[i])
            {
            case 'F':     // Move forward & draw
                GameObject segment = Instantiate(stem);
                // If this is parameterized, we draw the stem with a scale
                // factor in the non-Z directions: aka thickness of the stem
                if (lsystem[i + 1] == '(')
                {
                    string foo = lsystem.Substring(i + 2);
                    foo = foo.Substring(0, foo.IndexOf(')'));
                    float thickness = float.Parse(foo);
                    i += foo.Length;
                    DrawObject(segment, this.transform, new Vector3(thickness * topTurtle.scale.x, thickness * topTurtle.scale.y, topTurtle.scale.z));
                    topTurtle.Move();
                }
                else
                {
                    MoveDraw(segment, this.transform);
                }
                break;

            case 'L':
                DrawObject(Instantiate(leaf), this.transform);
                break;

            case 'f':     // Just move forward, no drawing
                topTurtle.Move();
                break;

            case '+':     // Rotate Right
                if (lsystem[i + 1] == '(')
                {
                    string foo = lsystem.Substring(i + 2);
                    foo   = foo.Substring(0, foo.IndexOf(')'));
                    angle = float.Parse(foo);
                    i    += foo.Length;
                }
                topTurtle.Turn(Quaternion.AngleAxis(angle, Vector3.up));
                break;

            case '-':     //Rotate Left
                if (lsystem[i + 1] == '(')
                {
                    string foo = lsystem.Substring(i + 2);
                    foo   = foo.Substring(0, foo.IndexOf(')'));
                    angle = float.Parse(foo);
                    i    += foo.Length;
                }
                topTurtle.Turn(Quaternion.AngleAxis(-angle, Vector3.up));
                break;

            case '&':     //Pitch down
                if (lsystem[i + 1] == '(')
                {
                    string foo = lsystem.Substring(i + 2);
                    foo   = foo.Substring(0, foo.IndexOf(')'));
                    angle = float.Parse(foo);
                    i    += foo.Length;
                }
                topTurtle.Turn(Quaternion.AngleAxis(angle, Vector3.right));
                break;

            case '^':     // Pitch up
                if (lsystem[i + 1] == '(')
                {
                    string foo = lsystem.Substring(i + 2);
                    foo   = foo.Substring(0, foo.IndexOf(')'));
                    angle = float.Parse(foo);
                    i    += foo.Length;
                }
                topTurtle.Turn(Quaternion.AngleAxis(-angle, Vector3.right));
                break;

            case '\\':     //Roll left
                if (lsystem[i + 1] == '(')
                {
                    string foo = lsystem.Substring(i + 2);
                    foo   = foo.Substring(0, foo.IndexOf(')'));
                    angle = float.Parse(foo);
                    i    += foo.Length;
                }
                topTurtle.Turn(Quaternion.AngleAxis(-137.5f, Vector3.forward));
                break;

            case '/':     // Roll right
                if (lsystem[i + 1] == '(')
                {
                    string foo = lsystem.Substring(i + 2);
                    foo   = foo.Substring(0, foo.IndexOf(')'));
                    angle = float.Parse(foo);
                    i    += foo.Length;
                }
                topTurtle.Turn(Quaternion.AngleAxis(137.5f, Vector3.forward));
                break;

            case '[':     //Push stack
                turtles.Push(new Turtle3D(topTurtle));
                break;

            case ']':     //Pop stack
                topTurtle = turtles.Pop();
                break;

            case '!':     // Shrink scale of turtle and all children
                topTurtle.scale *= .85f;
                break;

            case '\'':     // Color?
                break;
            }
        }
        topTurtle = turtles.Pop();
        Debug.Log(turtles.Count);
    }
示例#2
0
 // for if you want to copy somebody else
 public Turtle3D(Turtle3D copy)
 {
     this.position    = copy.position;
     this.orientation = copy.orientation;
     this.scale       = copy.scale;
 }