Esempio n. 1
0
    //USED TO UPDATE THE BRANCHES EVERY FRAME
    public void UpdateRecursions()
    {
        float absolutTime = time * maxDuration;

        for (int i = 0; i < actualBranchesNumber; i++)
        {
            //DISTANCE FROM ROOT / LENGTH
            BranchesInfo branch = branches [i];


            //NOT GROWN
            if (branch.birthDate > absolutTime)
            {
                branch.branch.time = 0f;
            }
            else
            {
                if (absolutTime - branch.birthDate < branch.growthDuration || !branch.grownOnce)
                {
                    branch.branch.time = Mathf.Min((absolutTime - branch.birthDate) / branch.growthDuration, 1f);

                    float radius = Mathf.Lerp(
                        initialShapeOverLength.Evaluate(branch.lengthRatio),
                        finalShapeOverLength.Evaluate(branch.lengthRatio),
                        shapeOverTime.Evaluate(time));


                    branch.branch.transform.position = branch.finalPosition - positionsAndNormals.nor [branch.normalIndex] * radius;
                    branch.grownOnce = true;
                    branch.branch.UpdatePlant();
                }
            }
        }
    }
Esempio n. 2
0
    //USED TO FIRST GENERATE THE BRANCHES ON THE PLANT
    public void GenerateRecursions()
    {
        actualBranchesNumber = 0;

        Vector3 randomVector = new Vector3(0.132354f, 1.98654f, -1.5646f).normalized;

        //CREATE THE PARENT OF ALL LEAVES
        if (branchesParent != null)
        {
            DestroyImmediate(branchesParent.gameObject);
        }

        branchesParent        = new GameObject("branchesParent").transform;
        branchesParent.parent = transform;
        branchesParent.transform.localPosition = Vector3.zero;

        branches = new BranchesInfo[nbOfBranches];

        //FINISHED POINTS OF THE MODEL
        GeneratePoints(1f, points);

        float angle = Random.value * 2 * Mathf.PI;


        for (int i = 0; i < nbOfBranches; i++)
        {
            float lengthRatio = (float)i / (float)nbOfBranches;

            lengthRatio = branchesDistributionOverLength.Evaluate(lengthRatio);
            if (lengthRatio == 1f)
            {
                lengthRatio = (nbOfSegments - 1f) / (float)nbOfSegments;
            }

            float proba = branchesDistribution.Evaluate(lengthRatio);


            if (Random.value < proba)
            {
                //POSITION
                int segment = (int)(lengthRatio * nbOfSegments);

                float lerpValue = (lengthRatio - (segment / (float)nbOfSegments)) * nbOfSegments;


                //float value = Random.value;
                float value = 0f;
                //POINT AND OPPOSED POINT
                int pointIndex1 = nbOfSides * segment + (int)(value * nbOfSides);
                int pointIndex2 = nbOfSides * segment + (int)((value + 0.5f > 1f ? value - 0.5f : value + 0.5f) * nbOfSides);



                // INTERPOLATION BETWEEN SEGMENTS


                Vector3 position = Vector3.zero;
                if (branchesOnlyOnSections)
                {
                    position = positionsAndNormals.pos [segment];
                }
                else
                {
                    position = Vector3.Lerp(positionsAndNormals.pos [segment], positionsAndNormals.pos [segment + 1], lerpValue);
                }


                float actualTrunkRadius = finalShapeOverLength.Evaluate(lengthRatio) * initialRadius;

                //INITIAL RADIUS
                float initialBrancheRadiusBranch = actualTrunkRadius * 0.8f;

                if (offsetTangents)
                {
                    Vector3 normal = positionsAndNormals.nor [segment];
                    position += normal * actualTrunkRadius;
                }
                Vector3 tangentDirection = (points [pointIndex1 + nbOfSides] - points [pointIndex1]).normalized;
                //INITIAL DIRECTION
                Vector3 u = (points[pointIndex1] - points[pointIndex2]).normalized;
                Vector3 v = Vector3.Cross(tangentDirection, u).normalized;

                angle += brancheAngleDelta;
                Vector3 direction = u * Mathf.Cos(angle) + v * Mathf.Sin(angle);

                //????????????????
                //TANGEANT DIRECTION


                //ORIENTATION TOWARD THE SUN ?????????????????
                direction = Vector3.Lerp(direction, tangentDirection, branchesTangencityOverLength.Evaluate(lengthRatio));

                direction.Normalize();

                float growthDuration = branchGrowthDuration.RandomValue();

                float birthDate = Mathf.Lerp(maxDuration - growthDuration, ValueAt(trunkTimeOverTime, lengthRatio) * maxDuration, branchBirthDateDistribution.Evaluate(Random.value));


                //BRANCHE CREATION
                Plant branche = Instantiate(branchPrefab, position, Quaternion.identity).GetComponent <Plant> ();


                branche.transform.parent   = branchesParent;
                branche.transform.position = position;
                branche.name = "Branche_" + actualBranchesNumber.ToString();

                //PARAMETERS SETTINGS

                branche.TrunkGrowthDuration = branchGrowthDuration;

                //branche.leafGrowthDuration = leafGrowthDuration;

                branche.isBranch         = true;
                branche.InitialDirection = direction;
                if (!branche.brancheIndependentRadius)
                {
                    branche.initialRadius = initialBrancheRadiusBranch * branchInitialRadiusMultiplier.RandomValue();
                }
                else
                {
                    branche.initialRadius *= branchInitialRadiusMultiplier.RandomValue();
                }

                branche.initialNormal = tangentDirection;

                //branche.initialSegmentLength = initialSegmentLength;
                //branche.finalSegmentLength = finalSegmentLength;

                //branche.nbOfSides = Mathf.Max (3, nbOfSides - 1);
                if (!branche.brancheIndependentLength)
                {
                    float nbOfS = branchLengthOverTrunkLength.Evaluate(lengthRatio) * nbOfSegments * brancheLengthRatio.RandomValue() + 1;
                    branche.nbOfSegments = (int)nbOfS;
                }
                else
                {
                    float mult = brancheLengthRatio.RandomValue();
                    branche.finalSegmentLength   *= mult;
                    branche.initialSegmentLength *= mult;
                }

                //BRANCH CREATION



                branche.time = 0f;
                branche.InitializePlant();

                BranchesInfo b = new BranchesInfo(branche, birthDate, segment, position, lengthRatio, growthDuration);

                branches [actualBranchesNumber] = b;


                //INCREMENTS
                actualBranchesNumber++;
            }
        }
    }