示例#1
0
    // Use this for initialization
    void Start()
    {
        MeshFilter filter = (MeshFilter)this.GetComponent <MeshFilter> ();

        if (filter == null)
        {
            filter = gameObject.AddComponent <MeshFilter> ();
        }
        filter.mesh = CylinderGenerator.generateCylinder(1f, 0.004f, 0.003f, 5);
        gameObject.AddComponent <MeshRenderer> ().material = material;

        int   numberOfNodes = 35;
        float rotationAngle = 75;
        float leafWidth     = 0.2f;

        for (int i = 0; i < numberOfNodes; i++)
        {
            Leaf newLeaf = new GameObject("BranchLeaf").AddComponent <Leaf> ().Initialize(fernBranchMaterial, leafWidth, 0.6f, null);
            newLeaf.transform.parent        = this.transform;
            newLeaf.transform.localScale    = Vector3.one * ((0.3f / (((float)i / (float)numberOfNodes) + 0.3f)) - 0.16f); //Mathf.Pow(1.13f, i/numberOfNodes);
            newLeaf.transform.localPosition = new Vector3(-(leafWidth / 2f) * newLeaf.transform.localScale.x, Mathf.Log10((((float)i / (float)numberOfNodes) + 0.1f) * 10f), 0f);
            newLeaf.transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
            //newLeaf.transform.RotateAround (newLeaf.transform.position + new Vector3((leafWidth/2f) * newLeaf.transform.localScale.x, 0f, 0f), this.transform.rotation.eulerAngles, -rotationAngle + ((i % 2) * (rotationAngle * 2)));
            rotateAroundLocal(newLeaf.transform, newLeaf.transform.position + new Vector3((leafWidth / 2f) * newLeaf.transform.localScale.x, 0f, 0f), Vector3.forward, -rotationAngle + ((i % 2) * (rotationAngle * 2)));

            /*newLeaf.transform.localRotation = Quaternion.Euler(0, 0, );
             *
             * //Correct for rotation anchor point.
             * float angle = Mathf.Deg2Rad * (newLeaf.transform.localRotation.eulerAngles.z);
             * float hypo = .1f;
             * newLeaf.transform.localPosition += new Vector3(Mathf.Sin(angle) * hypo, Mathf.Cos(angle) * hypo);*/
        }
    }
示例#2
0
    //Build the actual shape of the branch.
    public void build()
    {
        if (parentBranch != null)
        {
            this.manager          = parentBranch.manager;
            this.material         = parentBranch.material;
            this.transform.parent = parentBranch.transform;

            this.branchDepth = parentBranch.branchDepth + 1;


            //These constants control the appearance of the tree.
            updateLength();
            this.width = parentBranch.width * 0.5f;

            //Move the current branch so that it is stacked right above its parent.
            if (directBranch)
            {
                transform.localPosition = Vector3.up * parentBranch.length;
            }
            else
            {
                transform.localPosition = Vector3.up * Random.Range(parentBranch.length - ((parentBranch.length) * (0.8f - this.branchDepth / 10)), parentBranch.length);
            }


            //when we rotate we have to transform again so that the branch keeps its bottom connected to its parent's top.
            //float hypo = this.length/2f;
            //float opposite = Mathf.Sin(Mathf.Deg2Rad * transform.localRotation.eulerAngles.z) * hypo;
            //float adjacent = Mathf.Cos (Mathf.Deg2Rad * transform.localRotation.eulerAngles.y) * hypo;
            //float oppositeX = Mathf.Sin (Mathf.Deg2Rad * transform.localRotation.eulerAngles.x) * 2f *adjacent;
            //transform.localPosition = transform.localPosition + new Vector3 (0f, adjacent, 0f);
        }

        //Check for mesh filter and make one if none exists.
        MeshFilter filter = (MeshFilter)this.GetComponent <MeshFilter> ();

        if (filter == null)
        {
            filter = gameObject.AddComponent <MeshFilter> ();
        }

        //Create a new cylinder mesh.
        filter.mesh = mesh = new Mesh();
        int numberOfRingVerts = 6 - this.branchDepth;

        if (numberOfRingVerts < 2)
        {
            numberOfRingVerts = 2;
        }

        filter.mesh = mesh = CylinderGenerator.generateCylinder(this.length, this.width, this.width * 0.5f, numberOfRingVerts);
        //transform.localRotation = Quaternion.Euler(0f, 0f, weightedRand() * 30f);
    }
示例#3
0
    public void growChildren()
    {
        branchAge++;
        float originalLength = this.length;

        this.width = this.width * this.manager.widthFactor;
        if (parentBranch)
        {
            this.width = this.parentBranch.width * this.manager.childWidthFactor;
        }

        if (branchAge < 3)
        {
            this.length = this.ultimateLength / 4 + (this.ultimateLength / 4) * branchAge;
        }

        //Check for mesh filter and make one if none exists.
        MeshFilter filter = (MeshFilter)this.GetComponent <MeshFilter> ();

        if (filter == null)
        {
            filter = gameObject.AddComponent <MeshFilter> ();
        }

        //Create a new cylinder mesh to represent a larger version of the branch.
        filter.mesh = mesh = CylinderGenerator.generateCylinder(this.length, this.width, this.width * 0.65f, 6);

        if (childBranches.Count == 0)
        {
            branch();
        }
        else
        {
            for (int i = 0; i < childBranches.Count; i++)
            {
                //preserves the child branch's position on the parent branch.
                childBranches [i].transform.localPosition = Vector3.up * (childBranches [i].transform.localPosition.y * this.length) / originalLength;
                childBranches [i].growChildren();
            }
        }
    }