Пример #1
0
    private void Draw()
    {
        for (int k = 0; k < currentPath.Length; k++)
        {
            switch (currentPath[k])
            {
            // 'F' Means a straight line-- continue on. In this particular implementation, it uses a fancy algorith to
            case 'F':
                initalPosition = transform.position;
                // By default, elements are NOT leaves
                bool isLeaf = false;

                GameObject currentElement;
                // I have no idea how this works, but it determines if the next element to draw should be a leaf or a branch.
                if (currentPath[k + 1] % currentPath.Length == 'X' || currentPath[k + 3] % currentPath.Length == 'F' && currentPath[k + 4] % currentPath.Length == 'X')
                {
                    currentElement = Instantiate(leaf);
                    isLeaf         = true;
                }
                else
                {
                    currentElement = Instantiate(branch);
                }

                // Make the newest element drawn inherit from the tree prefab (???)
                currentElement.transform.SetParent(tree.transform);

                TreeElement currentTreeElement = currentElement.GetComponent <TreeElement>();

                if (isLeaf)
                {
                    transform.Translate(Vector3.up * 2f * Random.Range(minLeafLength, maxLeafLength));
                    currentTreeElement.GetComponent <LineRenderer>().startWidth = 0;
                    currentTreeElement.GetComponent <LineRenderer>().endWidth   = leafWidth;
                    leafCount++;
                }
                else
                {
                    transform.Translate(Vector3.up * 2f * Random.Range(minBranchLength, maxBranchLength));
                    currentTreeElement.lineRenderer.startWidth = branchWidth;
                    currentTreeElement.lineRenderer.endWidth   = branchWidth;
                    branchCount++;
                }
                currentTreeElement.lineRenderer.SetPosition(0, initalPosition);
                currentTreeElement.lineRenderer.SetPosition(1, transform.position);
                currentTreeElement.lineRenderer.sharedMaterial = currentTreeElement.material;
                break;

            case 'X':
                break;

            case '+':
                transform.Rotate(Vector3.forward * angle * (1f + variance / 10f * randomRotations[k % randomRotations.Length]));
                break;

            case '-':
                transform.Rotate(Vector3.back * angle * (1f + variance / 10f * randomRotations[k % randomRotations.Length]));
                break;

            case '*':
                transform.Rotate(Vector3.up * 120f * (1f + variance / 10f * randomRotations[k % randomRotations.Length]));
                break;

            case '/':
                transform.Rotate(Vector3.down * 120f * (1f + variance / 10f * randomRotations[k % randomRotations.Length]));
                break;

            case '[':
                SavedTransform savedTransformPush = new SavedTransform();
                savedTransformPush.position = transform.position;
                savedTransformPush.rotation = transform.rotation;
                savedTransforms.Push(savedTransformPush);
                break;

            case ']':
                SavedTransform savedTransformPop = savedTransforms.Pop();

                transform.position = savedTransformPop.position;
                transform.rotation = savedTransformPop.rotation;
                break;
            }
        }
        Debug.Log("leafCount : " + leafCount + " , branchCount : " + branchCount);
    }