예제 #1
0
        public void GenerateMeshData(TreeFunction trunk, float simplifyLeafs, float radialResolution)
        {
            Stack <Queue <TreePoint> > treePoints = new Stack <Queue <TreePoint> >();

            foreach (Node stem in stems)
            {
                Stack <Queue <TreePoint> > newPoints = stem.ToSplines();
                while (newPoints.Count > 0)
                {
                    treePoints.Push(newPoints.Pop());
                }
            }
            Splines splines = new Splines(treePoints);

            splines.GenerateMeshData(7 * radialResolution, 3, trunk.TrootShape, trunk.TradiusMultiplier, trunk.TrootRadius, trunk.TrootHeight, trunk.TrootResolution
                                     , trunk.TflareNumber, trunk.TdisplacementStrength, trunk.TdisplacementSize, trunk.TspinAmount);


            Queue <int> leafTriangles = new Queue <int>();

            GenerateLeafData(splines.verts, splines.normals, splines.uvs, leafTriangles, splines.verts.Count, splines.colors, simplifyLeafs);
            verts              = splines.verts.ToArray();
            normals            = splines.normals.ToArray();
            uvs                = splines.uvs.ToArray();
            triangles          = splines.triangles.ToArray();
            colors             = splines.colors.ToArray();
            this.leafTriangles = leafTriangles.ToArray();
        }
예제 #2
0
        void UpdateBranch(bool normalShader = false)
        {
            if (branchObject == null)
            {
                CreateBranchObject(normalShader);
            }

            CreateLeafMesh();
            MTree        branch = new MTree(branchObject.transform);
            TreeFunction trunkF = new TreeFunction(0, FunctionType.Trunk, null);

            float resolution = 20;

            Random.InitState(seed);

            branch.AddTrunk(Vector3.up, Vector3.forward, stemLength, AnimationCurve.Linear(0, 1, 1, .3f), radius, resolution, randomness / 3, 0, AnimationCurve.Linear(0, 1, 0, 0), 0, .01f, 1, 0);
            branch.AddBranches(0, length, AnimationCurve.Linear(0, 1, 1, 1), resolution, branchNumber, splitProba, AnimationCurve.Linear(0, 1, 1, 1),
                               angle, randomness, AnimationCurve.Linear(0, 1, 1, .4f), .9f, 0, 1, 0f, 2, .1f, 1f, 0.00001f);
            branch.AddLeafs(leafCovering, leafNumber, new Mesh[] { leafMesh }, leafSize, false, 0, 0, 1, leafAngle);

            Mesh mesh = CreateBranchMesh(branch, trunkF);

            branchObject.GetComponent <MeshFilter>().mesh = mesh;

            if (cameraObject == null)
            {
                CreateCameraObject();
            }
            RenderCamera();

            DestoyObjects();
        }
예제 #3
0
    public void RemoveFunction(int index)
    {
        #if UNITY_EDITOR
        Undo.RecordObject(this, "Removed function");
        #endif
        Mtree.TreeFunction functionToRemove = treeFunctions[index];
        TreeFunction       newParent        = functionToRemove.parent;


        foreach (TreeFunction tf in treeFunctions)
        {
            if (tf.parent != null && tf.parent.id == functionToRemove.id)
            {
                tf.parent = newParent;
            }
        }

        treeFunctions.RemoveAt(index);

        if (selectedFunctionIndex >= index)
        {
            selectedFunctionIndex--;
        }

        UpdateTreeFunctions();
    }
예제 #4
0
        void UpdateBranch(bool normalShader = false)
        {
            switch (texSize)
            {
            case EnumTextureSize._512:
                textureSize = 512;
                break;

            case EnumTextureSize._1024:
                textureSize = 1024;
                break;

            case EnumTextureSize._2048:
                textureSize = 2048;
                break;

            case EnumTextureSize._4096:
                textureSize = 4096;
                break;
            }

            if (branchObject == null)
            {
                CreateBranchObject(normalShader);
            }
            if (!deadLeafsRendering)
            {
                CreateLeafMesh();
            }
            MTree        branch = new MTree(branchObject.transform);
            TreeFunction trunkF = new TreeFunction(0, FunctionType.Trunk, null);

            float resolution = 20;

            Random.InitState(seed);


            branch.AddTrunk(Vector3.up, Vector3.forward, stemLength, AnimationCurve.Linear(0, 1, 1, .3f), radius, resolution, randomness / 3, 0, AnimationCurve.Linear(0, 1, 0, 0), 0, .01f, 1, 0);
            branch.TwigSplit(branchNumber, angle * 90f, .9f, 0f);
            branch.Grow(length, AnimationCurve.Linear(0, 1, 1, 1), resolution, splitProba, AnimationCurve.Linear(0, 1, 1, 1), angle, 2, 1, 1, randomness, AnimationCurve.Linear(0, 1, 1, .4f), .9f, 0f, 0f, 1f, 0.00001f);
            if (!deadLeafsRendering)
            {
                branch.AddLeafs(leafCovering, leafNumber, new Mesh[] { leafMesh }, leafSize, false, 0, 0, 1, leafAngle, false, 0, 0, 0, 0);
            }
            Mesh mesh = CreateBranchMesh(branch, trunkF);

            branchObject.GetComponent <MeshFilter>().mesh = mesh;

            if (cameraObject == null)
            {
                CreateCameraObject();
            }
            RenderCamera();

            DestoyObjects();
        }
예제 #5
0
        Mesh CreateBranchMesh(MTree branch, TreeFunction trunkFunction)
        {
            Mesh mesh = new Mesh();

            branch.GenerateMeshData(trunkFunction, 0, 1);
            mesh.vertices = branch.verts;
            mesh.normals  = branch.normals;
            mesh.uv       = branch.uvs;
            Color[] colors = branch.colors;
            mesh.triangles = branch.triangles;
            if (branch.leafTriangles.Length > 0)
            {
                mesh.subMeshCount = 2;
                mesh.SetTriangles(branch.leafTriangles, 1);
            }
            mesh.colors = colors;
            return(mesh);
        }
예제 #6
0
        public TreeFunction(int id, FunctionType type, TreeFunction parent)
        {
            UpdateStyle();

            this.id     = id;
            this.type   = type;
            this.parent = parent;
            parentId    = parent == null ? -1 : parent.id;
            name        = type.ToString();
            position    = parent == null ? 0 : parent.position + positionOffset;
            seed        = Random.Range(0, 1000);
            rect        = new Rect();
            deleteRect  = new Rect();
            if (type == FunctionType.Grow)
            {
                Keyframe[] keys = new Keyframe[2] {
                    new Keyframe(0f, 1f, 0f, 0f), new Keyframe(1f, 0f, -.5f, -1f)
                };
                Gradius = new AnimationCurve(keys);
            }

            if (type == FunctionType.Branch && parent != null && parent.type != FunctionType.Trunk)
            {
                Blength     = 3;
                Bstart      = .2f;
                Bangle      = .5f;
                Bnumber     = 55;
                BmaxSplits  = 2;
                Bresolution = 1;
            }
            if (type == FunctionType.Trunk)
            {
                Keyframe[] keys = new Keyframe[2] {
                    new Keyframe(0f, 1f, 0f, 0f), new Keyframe(1f, 0f, -1f, -1f)
                };
                Tradius = new AnimationCurve(keys);
                Keyframe[] rootKeys = new Keyframe[2] {
                    new Keyframe(0f, 1f, -2f, -2f), new Keyframe(1f, 0f, 0f, 0f)
                };
                TrootShape = new AnimationCurve(rootKeys);
            }
        }