Пример #1
0
    public Mesh BuildLeaves()
    {
        PGTreeBase        p     = transform.parent.GetComponent("PGTreeBase") as PGTreeBase;
        PGTreeTrunkSimple trunk = transform.parent.GetComponentInChildren <PGTreeTrunkSimple>();

        MeshBuilder meshBuilder = new MeshBuilder();

        for (int i = 0; i < trunk.Leaves.Count - 1; i++)
        {
            for (int j = 0; j < p.m_leaves_per_branch; j++)
            {
                Vector3    offset      = trunk.Leaves[i].position;
                Vector3    localOffset = new Vector3(0.0f, Mathf.Cos(p.m_stem_start_angle * Mathf.Deg2Rad), Mathf.Sin(p.m_stem_start_angle * Mathf.Deg2Rad)) * p.m_stem_length;
                Quaternion rotation    = trunk.Leaves[i].rotation * Quaternion.Euler(0.0f, (360 * (float)j) / (float)p.m_leaves_per_branch, 0.0f);
                BuildLeaf(meshBuilder, offset, localOffset, rotation, p.m_leaf_length, p.m_leaf_width, p.m_leaf_length_rand, p.m_leaf_width_rand, p.m_leaf_length_segments, p.m_leaf_width_segments,
                          p.m_leaf_bend, p.m_leaf_bend_rand, p.m_leaf_curl, p.m_leaf_curl_rand, p.m_leaf_twist, p.m_leaf_twist_rand, true, p.m_leaf_variation);
                //BuildLeaf(meshBuilder,offset,localOffset,rotation,p.m_leaf_length,p.m_leaf_width,p.m_leaf_length_rand,p.m_leaf_width_rand,p.m_leaf_length_segments,p.m_leaf_width_segments,
                //	p.m_leaf_bend,p.m_leaf_bend_rand,p.m_leaf_curl,p.m_leaf_curl_rand,p.m_leaf_twist,p.m_leaf_twist_rand,true,p.m_leaf_variation);
            }
        }

        Mesh mesh = meshBuilder.CreateMesh();

        mesh.RecalculateNormals();
        return(mesh);
    }
Пример #2
0
    public void Init(int seed)
    {
        Random.InitState(seed);

        treeSpecies = gameObject.GetComponent <TreeSpecies>();
        // Load variables from species creator
        if (treeSpecies == null)
        {
            Debug.LogError("ERROR: No tree species attached!");
        }
        GetVariablesFromSpecies();
        treeSpecies.GenerateMaterials();

        //LODManager lodManager = new LODManager();

        //trunkObject = new GameObject("Trunk");
        //trunkObject.transform.parent = transform;
        trunkObject = gameObject.EnsureChildGameObject("Trunk");
        trunkObject.transform.localPosition = Vector3.zero;                                // Remove offset from parenting
        PGTreeTrunkSimple trunkScript = trunkObject.EnsureComponent <PGTreeTrunkSimple>(); //trunkObject.AddComponent<PGTreeTrunkSimple>();

        trunkObject.EnsureComponent <MeshRenderer>();                                      //trunkObject.AddComponent<MeshRenderer>();
        trunkObject.GetComponent <Renderer>().material = treeSpecies.m_trunk_mat;

        trunkScript.CreateObject(false);

        // Add collider
        collider        = trunkObject.EnsureComponent <CapsuleCollider>();
        collider.radius = (m_start_radius + m_end_radius) / 2.0f;
        collider.height = m_height;
        collider.center = new Vector3(0.0f, m_height / 2.0f - collider.radius, 0.0f);

        if (m_hasLeaves)
        {
            leafObject = gameObject.EnsureChildGameObject("Leaves");
            //leafObject.transform.parent = transform;
            leafObject.transform.localPosition = Vector3.zero;             // Remove offset from parenting
            PGTreeLeaf leafScript = leafObject.EnsureComponent <PGTreeLeaf>();
            leafObject.EnsureComponent <MeshRenderer>();
            leafObject.GetComponent <Renderer>().material = treeSpecies.m_leaf_mat;

            leafScript.CreateObject(false);
        }

        // Add fruit
        if (m_has_fruit)
        {
            int   fruitNum        = Random.Range(treeSpecies.m_fruit_num_min, treeSpecies.m_fruit_num_max + 1);
            int   leafPositionNum = trunkScript.Leaves.Count;
            int[] usedIndices     = new int[fruitNum];

            for (int i = 0; i < fruitNum && i < leafPositionNum; i++)
            {
                GameObject newFruit           = GameObject.Instantiate(treeSpecies.m_fruit) as GameObject;
                int        fruitPositionIndex = Random.Range(0, leafPositionNum - 1);
                bool       spareIndex         = false;
                int        loopSafetyCount    = 0;
                while (spareIndex && loopSafetyCount < leafPositionNum)
                {
                    fruitPositionIndex = Random.Range(0, leafPositionNum - 1);
                    // Check if position index already has a fruit
                    bool matchFound = false;
                    for (int j = 0; j < fruitNum; j++)
                    {
                        if (fruitPositionIndex == usedIndices[j])
                        {
                            matchFound = true;
                        }
                    }
                    loopSafetyCount++;
                    // If no match is found, no fruit is at that index
                    spareIndex = !matchFound;
                }
                usedIndices[i]                   = fruitPositionIndex;
                newFruit.transform.parent        = transform;
                newFruit.transform.localPosition = trunkScript.Leaves[fruitPositionIndex].position;
                newFruit.transform.rotation      = trunkScript.Leaves[fruitPositionIndex].rotation * Quaternion.Euler(180.0f, 0.0f, 0.0f);
            }
        }
    }