Example #1
0
    // Making points along each branch
    public void MakePoints()
    {
        Vector3 currPos = startPosition;

        tree.currentTotalPoints += numPoints;

        // place the points along the branch
        for (int i = 0; i < numPoints; i++)
        {
            float valInBranch = ((float)i / ((float)numPoints - 1));

            float widthMultiplier = 1;
            if (iterationLevel == 0)
            {
                widthMultiplier = tree.trunkCurve.Evaluate(valInBranch);
            }
            else
            {
                widthMultiplier = tree.branchCurve.Evaluate(valInBranch);
            }


            if (i != 0)
            {
                float currNoiseSize  = currVal(tree.noiseSize, tree.noiseSizeReducer);
                float currNoisePower = currVal(tree.noisePower, tree.noisePowerReducer);


                float currUp = currVal(tree.upDesire, tree.upDesireReducer);

                if (i != 1)
                {
                    Vector3 dir = points[i - 1].position - points[i - 2].position;
                    currPos += dir.normalized * length * ((float)1 / ((float)numPoints - 1));
                }
                else
                {
                    currPos += length * direction * ((float)1 / ((float)numPoints - 1));
                }

                currPos += currUp * Vector3.up * .003f;

                Vector3 noiseDir = Perlin.CurlNoise(currPos * currNoiseSize + tree.noiseOffset);

                currPos += noiseDir * currNoisePower * .04f;
            }

            float fWidth = baseWidth * widthMultiplier;

            BranchPoint p = new BranchPoint(currPos, valInBranch, timeCreated + valInBranch, fWidth);
            points.Add(p);
            // TODO ADD NOISE
        }

        // Gets Tangents for each of the points for sake of
        // cubic beziers
        for (int i = 0; i < numPoints; i++)
        {
            BranchPoint p = points[i];

            if (i == 0)
            {
                p.normal = (points[1].position - p.position);
            }
            else if (i == points.Count - 1)
            {
                p.normal = (p.position - points[points.Count - 2].position);
            }
            else
            {
                p.normal = -(points[i - 1].position - points[i + 1].position);
            }


            if (i == 0)
            {
                p.tangent  = (Vector3.Cross(p.normal.normalized, Vector3.left)).normalized;
                p.binormal = (Vector3.Cross(p.normal, p.tangent)).normalized;
            }
            else
            {
                p.tangent  = -(Vector3.Cross(p.normal.normalized, points[i - 1].binormal)).normalized;
                p.binormal = (Vector3.Cross(p.normal, p.tangent)).normalized;
            }

            points[i] = p;
        }
    }