Ejemplo n.º 1
0
    void Generate()
    {
        int xCount = Mathf.RoundToInt(CurXScale * 5);
        int zCount = Mathf.RoundToInt(CurZScale * 5);

        float xVal_ = 0;
        float zVal_ = 0;

        RaycastHit Hit_;
        Vector3    TempPos_ = transform.position;
        float      P        = 0;

        float Height_ = 0;

        //GameObject TempObj = new GameObject(); // Removed due to never being used - Nathan
        for (int x = 0, i = 0; x < xCount; x++)
        {
            for (int z = 0; z < zCount; z++)
            {
                xVal_ = x * 0.2f;
                zVal_ = z * 0.2f;
                P     = Mathf.PerlinNoise(xVal_ + transform.position.x, zVal_ + transform.position.z);
                if (P > Spread)
                {
                    float XR = Random.Range(-XRandomAngle, XRandomAngle);
                    float YR = Random.Range(-YRandomAngle, YRandomAngle);
                    float ZR = Random.Range(-ZRandomAngle, ZRandomAngle);

                    Quaternion Rot_T    = Quaternion.Euler(XR, YR, ZR);
                    Quaternion Rot_TMax = Quaternion.Euler(XR * BendFactor, YR * BendFactor, ZR * BendFactor);
                    Quaternion RC       = Quaternion.identity;

                    TempPos_ = transform.position + new Vector3(xVal_, CurYScale / 2, zVal_);
                    Height_  = P + Height;
                    Height_  = Mathf.Lerp(0.24f, Height, Height_);

                    if (Physics.Linecast(TempPos_, TempPos_ + new Vector3(0, -CurYScale / 2, 0), out Hit_))
                    {
                        if (Hit_.collider)
                        {
                            TempPos_  = Hit_.point;
                            TempPos_ += new Vector3(Random.Range(-0.1f, 0.1f), Random.Range(-0.1f, 0.1f), Random.Range(-0.1f, 0.1f));

                            int          Segments_      = Random.Range(MinHeightSegments, MaxHeightSegments);
                            GameObject[] TempObj_       = new GameObject[Segments_];
                            MeshFilter[] BranchObjects_ = new MeshFilter[0];
                            for (int ii = 0; ii < Segments_; ii++)
                            {
                                float RT_ = ((100 / Segments_) * ii) * 0.01f;
                                RC = Quaternion.Slerp(Rot_T, Rot_TMax, RT_);
                                if (ii > 0)
                                {
                                    TempPos_ = TempObj_[ii - 1].transform.position + (TempObj_[ii - 1].transform.up * TempObj_[ii - 1].transform.localScale.y);
                                }
                                GameObject Model = Instantiate(MainModels[Random.Range(0, MainModels.Length)], TempPos_, RC);
                                if (Random.Range(0, 5) < 2 && SecondaryModels.Length != 0)
                                {
                                    Quaternion SecondaryRotation = Quaternion.Euler(RC.x * XR, RC.y * YR, RC.z * ZR);

                                    GameObject SecondaryModel = Instantiate(SecondaryModels[Random.Range(0, SecondaryModels.Length)], TempPos_, SecondaryRotation);
                                    SecondaryModel.transform.Rotate(XR, 0, 0);
                                    SecondaryModel.transform.parent = transform;
                                    GeneratedModels = DynamicArray.AddGameObjectToArray(GeneratedModels, SecondaryModel);
                                    MeshFilter[] Temp_MS = SecondaryModel.GetComponentsInChildren <MeshFilter>();
                                    BranchObjects_ = DynamicArray.AddMeshFilterArrayToArray(BranchObjects_, Temp_MS);
                                }
                                TempObj_[ii] = Model;
                                Model.transform.localScale = new Vector3(1, Height_ / Segments_, 1);
                                Model.transform.parent     = transform;
                                GeneratedModels            = DynamicArray.AddGameObjectToArray(GeneratedModels, Model);
                                ColliderPos_   = DynamicArray.AddVector3ToArray(ColliderPos_, TempPos_);
                                ColliderScale_ = DynamicArray.AddVector3ToArray(ColliderScale_, Model.transform.localScale);
                                ColliderRot_   = DynamicArray.AddQuaternionToArray(ColliderRot_, RC);
                                BranchIds      = DynamicArray.AddIntToArray(BranchIds, i);

                                MeshFilter[] Temp_MM = Model.GetComponentsInChildren <MeshFilter>();
                                BranchObjects_ = DynamicArray.AddMeshFilterArrayToArray(BranchObjects_, Temp_MM);
                            }
                            if (GenerateIndiviualBranchMeshes)
                            {
                                GenerateBranch(BranchObjects_, i++);
                            }
                        }
                    }
                }
            }
        }
        if (GenerateConsolidatedMesh)
        {
            GenerateMesh();
        }
        DestroyGeneratedModels_();
        OtherBotanySystems = FindObjectsOfType <MarkBotanySystem_2>();
    }