예제 #1
0
    private Vector3 RecordBranchModel(BranchIndex index, Vector3 bottom)
    {
        /*
         * 计算形态数据
         * 根据形态数据即可绘制对应的GameObject
         */
        index.MorphologicalSim();

        if (cylinderObject.activeSelf)
        {
            cylinderObject.SetActive(false);
        }

        cylinderObject.transform.localRotation = Quaternion.Euler(Vector3.zero);
        cylinderObject.transform.position      = bottom + new Vector3(0, index.Length / 2f, 0) * SCALE;                                                            //该节间的中心位置
        cylinderObject.transform.localScale    = new Vector3(index.Radius / DEFAULT_RADIUS, index.Length / DEFAULT_HEIGHT, index.Radius / DEFAULT_RADIUS) * SCALE; //尺寸

        RotationGameObject(cylinderObject, bottom, index.Rotation);                                                                                                //旋转该圆柱体

        Vector3[] vertices = GameObjectOperation.GetVerticesInWorld(cylinderObject);
        Vector2[] uv       = GameObjectOperation.GetUV(cylinderObject);

        if (temp_Vertices.Count == 0)   //最底端的节间
        {
            for (int i = 0; i < 40; i++)
            {
                temp_Vertices.Add(vertices[i]);
                temp_UV.Add(uv[i]);
            }

            AddTriangles(0, 20);
        }
        else    //非最底端节间
        {
            for (int i = 0; i < 20; i++)
            {
                /*
                 * 添加顶点
                 * 由于底端的顶点与前驱节间的顶点相同
                 * 故只用添加后20个顶点即可
                 */
                temp_Vertices.Add(vertices[i + 20]);

                /*
                 * 添加UV坐标
                 * 需要颠倒纹理(与上一个节间的纹理相反)
                 * 确保连接处的纹理过渡不会突兀
                 */
                temp_UV.Add(temp_UV[index.Previous.TopVerticesIndex - 20 + i]);
            }

            AddTriangles(index.Previous.TopVerticesIndex, temp_Vertices.Count - 20);
        }

        //删除GameObject,防止重复
        //GameObject.Destroy(cylinderObject);

        /*
         * 补齐index的数据
         * 用于后续生成节间
         */
        index.BottomVerticesIndex = index.Previous.TopVerticesIndex == -1 ?
                                    0 : index.Previous.TopVerticesIndex;

        index.TopVerticesIndex = temp_Vertices.Count - 20;

        /*
         * 返回当前顶端的中心位置
         * 由于枝干旋转,故根据顶端的顶点位置计算
         */
        return(GetCenterPoint(index.TopVerticesIndex, temp_Vertices));
    }