Example #1
0
    private Bounds ViewportBounds()
    {
        /*
         * 获取当前植物的所有对象
         * 逐个遍历
         * 将对象的顶点坐标(世界坐标系下)转换成视窗坐标(viewport 以左下角为原点(0, 0), 右上角为(1, 1)的坐标系)
         * 根据视窗坐标最大最小值生成包围盒
         * 用于确定当前对象在视窗中的位置,便于相机调整其自身位置
         * 使视点合适
         */
        List <GameObject> objects = new List <GameObject>();

        foreach (TreeModel treeModel in LScene.GetInstance().TreeModels)
        {
            objects.AddRange(GameObjectOperation.GetGameObjects(treeModel.TreeModelInstance));
        }

        Vector3 max = Vector3.zero, min = Vector3.zero;

        /*
         * 判断是否初始化 max 和 min
         */
        bool flag = true;

        foreach (GameObject _object in objects)
        {
            //无顶点
            if (!GameObjectValidate.HavaVertices(_object))
            {
                continue;
            }

            Vector3[] vertices = GameObjectOperation.GetVerticesInWorld(_object);

            foreach (Vector3 vertex in vertices)
            {
                Vector3 viewport = mainCamera.WorldToViewportPoint(vertex);

                if (flag)
                {
                    max = viewport; min = viewport; flag = false; continue;
                }
                else
                {
                    max = Vector3.Max(max, viewport);
                    min = Vector3.Min(min, viewport);
                }
            }
        }

        Bounds bounds = new Bounds((max + min) / 2.0f, max - min);

        return(bounds);
    }
Example #2
0
    private void ComputeAnimationFragment()
    {
        /*
         * 初始化动画片段
         * 防止数据重复
         */
        InitialAnimationFragment();
        InitialWormholeFragment();

        /*
         * 调整前后两关键帧顶点
         * 保证前后两关键帧顶点个数相同
         * 方便后续计算顶点的移动
         */
        AdjustTree();

        for (int i = 0; i < m_PreGameObjects.Count; i++)
        {
            /*
             * 检验两个对象的Tag
             * 保证两个对象的Tag一致
             */
            GameObjectValidate.ValidateTagOfObject(m_PreGameObjects[i], m_CurGameObjects[i]);

            /*
             * 判断该组对象是否有顶点
             * 因调整后,前后两对象顶点个数相同
             * 故判断前一个对象是否有顶点即可
             */
            if (!GameObjectValidate.HavaVertices(m_PreGameObjects[i]))
            {
                m_AnimationFragment.Add(new List <Vector3>());
                m_WormholeFragment.Add(new WormholeFragment());
                continue;
            }

            //ComputeAnimationFragment(GameObjectOperation.GetVerticesInWorld(m_PreGameObjects[i]), GameObjectOperation.GetVerticesInWorld(m_CurGameObjects[i]));
            ComputeAnimationFragment(m_PreGameObjects[i], GameObjectOperation.GetVertices(m_PreGameObjects[i]), GameObjectOperation.GetVerticesInWorld(m_CurGameObjects[i]));
            ComputeWormholeFragment(m_PreGameObjects[i], m_CurGameObjects[i]);
        }
    }
Example #3
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));
    }