示例#1
0
        readonly Queue <Matrix4x4> mtxQueue = new Queue <Matrix4x4>();       // queue of pending branches to draw

        // Draws a line, moves forward, and then queues up new postions to draw from per new branch
        void BranchFrom(Matrix4x4 mtx)
        {
            if (currentLineCount++ >= lineCount)
            {
                return;                 // stop recursion if we hit our limit
            }
            Draw.Matrix = mtx;
            float   lineLength = Mathf.Lerp(branchLengthMin, branchLengthMax, Random.value); // random branch length
            Vector3 offset     = new Vector3(0, lineLength, 0);                              // offset along the local Y axis

            Draw.Line(Vector3.zero, offset);
            Draw.Translate(offset);               // moves the drawing matrix in its local space

            // create a random number of branches from the current position
            int branchCount = Random.Range(branchesMin, branchesMax + 1);

            for (int i = 0; i < branchCount; i++)
            {
                using (Draw.MatrixScope) {                                                                           // saves the current matrix state, and restores it at the end of this scope
                    float angDeviation = Mathf.Lerp(-maxAngDeviation, maxAngDeviation, ShapesMath.RandomGaussian()); // random angular deviation
                    if (use3D)
                    {
                        Draw.Rotate(angDeviation, ShapesMath.GetRandomPerpendicularVector(Vector3.up));                             // rotates the current drawing matrix on a random axis
                    }
                    else
                    {
                        Draw.Rotate(angDeviation);                       // rotates the current drawing matrix on the Z axis
                    }
                    mtxQueue.Enqueue(Draw.Matrix);                       // save the drawing matrix to draw a branch with later
                }
            }

            while (mtxQueue.Count > 0)              // process all positions in the queue
            {
                BranchFrom(mtxQueue.Dequeue());     // draw new branches at the positions saved in the queue
            }
        }