コード例 #1
0
    void CombineMeshes(MeshFilter[] meshFilters, int Count, string Name_, int Type)
    {
        GameObject NewMesh = new GameObject();

        NewMesh.name = name + "_" + Name_ + "_" + "_Mesh_" + Count;
        NewMesh.AddComponent <MeshFilter>();
        NewMesh.AddComponent <MeshRenderer>();
        NewMesh.GetComponent <MeshRenderer>().material = Material;

        CombineInstance[] combine = new CombineInstance[meshFilters.Length];
        for (int i = 0; i < meshFilters.Length; i++)
        {
            combine[i].mesh      = meshFilters[i].sharedMesh;
            combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
        }
        NewMesh.GetComponent <MeshFilter>().mesh = new Mesh();
        NewMesh.GetComponent <MeshFilter>().mesh.CombineMeshes(combine);
        if (Type == 0)
        {
            FullGeneratedModel       = DynamicArray.AddGameObjectToArray(FullGeneratedModel, NewMesh);
            NewMesh.transform.parent = GeneratedFullMeshParent.transform;
        }
        if (Type == 1)
        {
            Branches = DynamicArray.AddGameObjectToArray(Branches, NewMesh);
            NewMesh.transform.parent = GeneratedBranchParent.transform;
        }
    }
コード例 #2
0
 void AddColliders()
 {
     for (int i = 0; i < ColliderPos_.Length; i++)
     {
         GameObject Col = new GameObject();
         Col.name = "Col_" + i;
         Col.AddComponent <BoxCollider>();
         Col.transform.position   = ColliderPos_[i];
         Col.transform.rotation   = ColliderRot_[i];
         Col.transform.localScale = ColliderScale_[i];
         //Col.GetComponent<BoxCollider>().isTrigger = true;
         Col.GetComponent <BoxCollider>().size   = new Vector3(0.2f, 1, 0.2f);
         Col.GetComponent <BoxCollider>().center = new Vector3(0, 0.5f, 0);
         ColliderObj_         = DynamicArray.AddGameObjectToArray(ColliderObj_, Col);
         Col.transform.parent = Branches[BranchIds[i]].transform;
     }
     //transform.position += new Vector3(0, 0.25f, 0);
 }
コード例 #3
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>();
    }