Пример #1
0
    void Start()
    {
        IcosphereGenerator icosphereGen = GetComponent <IcosphereGenerator>();
        GeneralMethods     met          = GetComponent <GeneralMethods>();
        Mesh  mesh;
        float r = Random.Range(0.0F, 1.0F);

        if (r < smallRockChance)
        {
            // small rock
            mesh          = icosphereGen.CreateIcosphere(false);
            mesh.vertices = met.Randomize(mesh.vertices, maxRandomDistance);
            mesh.vertices = met.ApplyTransformation(mesh.vertices, met.Scale(smallRockScale, smallRockScale, smallRockScale));
        }
        else
        {
            // big rock
            mesh          = icosphereGen.CreateIcosphere(true);
            mesh.vertices = met.Randomize(mesh.vertices, maxRandomDistance);
            mesh.vertices = met.ApplyTransformation(mesh.vertices, met.Scale(bigRockScale, bigRockScale, bigRockScale));
        }

        for (int i = 0; i < 3; i++)
        {
            r = Random.Range(0.0F, 1.0F);
            if (r < randomScaleChance)
            {
                float     scale = Random.Range(1.0F, maxRandomScale);
                Matrix4x4 mat;
                switch (i)
                {
                case 1:
                    mat = met.Scale(scale, 1.0F, 1.0F);
                    break;

                case 2:
                    mat = met.Scale(1.0F, scale, 1.0F);
                    break;

                default:
                    mat = met.Scale(1.0F, 1.0F, scale);
                    break;
                }
                mesh.vertices = met.ApplyTransformation(mesh.vertices, mat);
            }
        }

        mesh = met.ConvertToFlat(mesh);
        GetComponent <MeshFilter>().mesh         = mesh;
        GetComponent <Renderer>().material.color = rockColor;
        GetComponent <Renderer>().material.SetFloat("_Glossiness", 0.0F);
    }
Пример #2
0
    void Start()
    {
        IcosphereGenerator icosphereGen = GetComponent <IcosphereGenerator>();
        GeneralMethods     met          = GetComponent <GeneralMethods>();
        Mesh mesh = icosphereGen.CreateIcosphere(true);

        mesh.vertices = met.Randomize(mesh.vertices, maxRandomDistance);
        float r = Random.Range(0.0F, 1.0F);
        float yScale;

        if (r < shortBushChance)
        {
            // round bush
            yScale = Random.Range(0.8F, 1.0F);
        }
        else
        {
            // tall bush
            yScale = Random.Range(1.8F, 2.2F);
        }
        mesh.vertices = met.ApplyTransformation(mesh.vertices, met.Translate(0.0F, 0.5F * yScale * bushScale, 0.0F) * met.Scale(bushScale, bushScale * yScale, bushScale));

        mesh = met.ConvertToFlat(mesh);
        GetComponent <MeshFilter>().mesh         = mesh;
        GetComponent <Renderer>().material.color = bushColor;
        GetComponent <Renderer>().material.SetFloat("_Glossiness", 0.0F);
    }
Пример #3
0
    private Mesh GenerateTree(int n)
    {
        // Generates a self-similar tree mesh of n iterations
        // invariant: first 8 vertices of the returned mesh must form the bottom octagon (for connecting meshes)
        Mesh whole = new Mesh();

        whole.subMeshCount = 2;

        Vector3[] bottom = Octagon();
        bottom = met.ApplyTransformation(bottom, met.Scale(baseRadius, 0, baseRadius));

        if (n <= 1)   // last branch
        // branch
        {
            Vector3[] branchVertices = new Vector3[9];
            bottom.CopyTo(branchVertices, 0);
            branchVertices[8] = new Vector3(0, baseLength, 0);
            int[] branchTriangles = new int[] {
                8, 0, 1,
                8, 1, 2,
                8, 2, 3,
                8, 3, 4,
                8, 4, 5,
                8, 5, 6,
                8, 6, 7,
                8, 7, 0
            };
            // leaves
            Mesh leaves;
            if (hasLeaves)
            {
                leaves          = icosphereGen.CreateIcosphere(true);
                leaves.vertices = met.Randomize(leaves.vertices, leavesRandomFactor);
                float leavesScale = Random.Range(leavesScaleFactor * 0.8F, leavesScaleFactor * 1.3F);
                leaves.vertices = met.ApplyTransformation(leaves.vertices, met.Translate(0.0F, baseLength, 0.0F) * met.Scale(leavesScale, leavesScale, leavesScale));
            }
            else
            {
                leaves = new Mesh();
            }
            // combine
            Vector3[] wholeVertices = new Vector3[branchVertices.Length + leaves.vertices.Length];
            branchVertices.CopyTo(wholeVertices, 0);
            leaves.vertices.CopyTo(wholeVertices, branchVertices.Length);
            whole.vertices = wholeVertices;
            whole.SetTriangles(branchTriangles, 0, false);
            whole.SetTriangles(ShiftTriangles(leaves.triangles, branchVertices.Length), 1, false);
            return(whole);
        }
        // is not last branch
        float r = (float)Random.Range(0.0F, 1.0F);

        if (r < pNoBranch)   // a single branch continues, only direction might change
        {
            Mesh      onlyBranch   = GenerateTree(n - 1);
            float     direction    = Random.Range(0, 360);
            float     angle        = Random.Range(upperBranchMinAngle, upperBranchMaxAngle);
            Matrix4x4 branchMatrix = GenerateBranchMatrix(direction, angle);
            onlyBranch.vertices = met.ApplyTransformation(onlyBranch.vertices, branchMatrix);

            Vector3[] top          = GetTop();
            Vector3[] baseVertices = new Vector3[16];
            bottom.CopyTo(baseVertices, 0);
            top.CopyTo(baseVertices, 8);
            Mesh baseBranch = new Mesh(); // base branch without its subbranches
            baseBranch.subMeshCount = 2;
            baseBranch.vertices     = baseVertices;
            baseBranch.SetTriangles(ConnectOctagons(0, 8), 0, false);

            whole = CombineMeshes(baseBranch, onlyBranch);
            int[] trunkTriangles      = new int[whole.GetTriangles(0).Length + 48];
            int[] topToFirstTraingles = ConnectOctagons(8, 16);
            whole.GetTriangles(0).CopyTo(trunkTriangles, 0);
            topToFirstTraingles.CopyTo(trunkTriangles, whole.GetTriangles(0).Length);
            whole.SetTriangles(trunkTriangles, 0);
        }
        else   // branches to two subbrances
               // first
        {
            Mesh  firstBranch    = GenerateTree(n - 1);
            float directionFirst = Random.Range(0, 360);
            float angleFirst;
            if (r < pNoBranch + pEqualBranch)
            {
                angleFirst = Random.Range(lowerBranchMinAngle, lowerBranchMaxAngle);
            }
            else
            {
                angleFirst = Random.Range(upperBranchMinAngle, upperBranchMaxAngle);
            }
            Matrix4x4 firstBranchMatrix = GenerateBranchMatrix(directionFirst, angleFirst);
            firstBranch.vertices = met.ApplyTransformation(firstBranch.vertices, firstBranchMatrix);
            // second branch
            Mesh secondBranch;
            if (r < pNoBranch + pEqualBranch)   // branches to two (n - 1) subbranches
            {
                secondBranch = GenerateTree(n - 1);
            }
            else   // branches to one (n - 1) and one (n - 2) subbranches
            {
                secondBranch = GenerateTree(n - 2);
            }
            float     directionSecond    = Random.Range(directionFirst + 180 - (directionRange / 2), directionFirst + 180 + (directionRange / 2));
            float     angleSecond        = Random.Range(lowerBranchMinAngle, lowerBranchMaxAngle);
            Matrix4x4 secondBranchMatrix = GenerateBranchMatrix(directionSecond, angleSecond);
            secondBranch.vertices = met.ApplyTransformation(secondBranch.vertices, secondBranchMatrix);
            // whole
            Vector3[] top          = GetTop();
            Vector3[] baseVertices = new Vector3[16];
            bottom.CopyTo(baseVertices, 0);
            top.CopyTo(baseVertices, 8);
            Mesh baseBranch = new Mesh(); // base branch without its subbranches
            baseBranch.subMeshCount = 2;
            baseBranch.vertices     = baseVertices;
            baseBranch.SetTriangles(ConnectOctagons(0, 8), 0, false);
            baseBranch.SetTriangles(new int[0], 1, false);

            whole = CombineMeshes(CombineMeshes(baseBranch, firstBranch), secondBranch);
            int   trianglesSum         = whole.GetTriangles(0).Length + whole.GetTriangles(1).Length;
            int[] trunkTriangles       = new int[trianglesSum + 96];
            int[] topToFirstTriangles  = ConnectOctagons(8, 16);
            int[] topToSecondTriangles = ConnectOctagons(8, 16 + firstBranch.vertices.Length);
            whole.GetTriangles(0).CopyTo(trunkTriangles, 0);
            topToFirstTriangles.CopyTo(trunkTriangles, whole.GetTriangles(0).Length);
            topToSecondTriangles.CopyTo(trunkTriangles, whole.GetTriangles(0).Length + topToFirstTriangles.Length);
            whole.SetTriangles(trunkTriangles, 0, false);
        }
        return(whole);
    }