Example #1
0
    /// <summary>
    /// 获取新生对象
    /// </summary>
    /// <returns></returns>
    private GameObject GetNewGameObject(PairedIndex <OrganIndex> pairedOrganIndex)
    {
        GameObject cloneObject = GameObject.Instantiate(pairedOrganIndex.CurIndex.Belong);

        /*
         * 因枝干顶点已经调整完毕
         * 故可用后一关键帧中枝干所指的顶点索引确定前一关键帧顶点的位置
         */
        Vector3 position = TreeModel.GetBranchTopCenter(m_PreGameObjects[0], pairedOrganIndex.CurIndex.From);

        foreach (GameObject _subObject in GameObjectOperation.GetGameObjects(cloneObject))
        {
            if (!GameObjectValidate.HavaVertices(_subObject))
            {
                continue;
            }

            Vector3[] vertices = GameObjectOperation.GetVertices(_subObject);

            //该对象所有顶点均为该位置
            CopyVertices(_subObject.transform.InverseTransformPoint(position), vertices);
            GameObjectOperation.UpdateMeshInfo(_subObject, vertices);
        }

        //设置阈值为0
        GameObjectOperation.SetThreshold(cloneObject, 0);

        return(cloneObject);
    }
Example #2
0
    /// <summary>
    /// 获取顶点
    /// </summary>
    public static Vector3[] GetVertices(GameObject _Object)
    {
        if (!GameObjectValidate.HavaVertices(_Object))
        {
            return(null);
        }

        return(_Object.GetComponent <MeshFilter>().mesh.vertices);
    }
Example #3
0
    /// <summary>
    /// 获取三角面片索引
    /// </summary>
    public static int[] GetTriangleIndexes(GameObject _Object)
    {
        if (!GameObjectValidate.HavaVertices(_Object))
        {
            return(null);
        }

        return(_Object.GetComponent <MeshFilter>().mesh.triangles);
    }
Example #4
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 #5
0
 public static void ClearTexture(GameObject _object)
 {
     if (!GameObjectValidate.HavaVertices(_object))
     {
         foreach (Transform child in _object.transform)
         {
             ClearTexture(child.gameObject);
         }
     }
     else
     {
         _object.GetComponent <MeshRenderer>().material.mainTexture = null;
     }
 }
Example #6
0
    /// <summary>
    /// 获取世界坐标系下的顶点坐标
    /// </summary>
    public static Vector3[] GetVerticesInWorld(GameObject _Object)
    {
        if (!GameObjectValidate.HavaVertices(_Object))
        {
            return(null);                                                       //无顶点
        }
        Matrix4x4 matrix = _Object.transform.localToWorldMatrix;                //转换矩阵

        Vector3[] Vertices = _Object.GetComponent <MeshFilter>().mesh.vertices; //局部坐标系下的顶点坐标
        for (int i = 0; i < Vertices.Length; i++)
        {
            Vertices[i] = matrix.MultiplyPoint3x4(Vertices[i]); //转换到世界坐标系中
        }

        return(Vertices);
    }
Example #7
0
    public static void SetTexture(GameObject _Object, Texture tex)
    {
        if (tex == null)
        {
            return;
        }

        if (!GameObjectValidate.HavaVertices(_Object))
        {
            foreach (Transform child in _Object.transform)
            {
                SetTexture(child.gameObject, tex);
            }
        }
        else
        {
            _Object.GetComponent <MeshRenderer>().material.mainTexture = tex;
        }
    }
Example #8
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 #9
0
    public static List <UnityEngine.Mesh> GetMeshes(GameObject _object)
    {
        if (_object == null)
        {
            return(null);
        }

        List <UnityEngine.Mesh> meshes = new List <UnityEngine.Mesh>();

        if (GameObjectValidate.HavaVertices(_object))
        {
            meshes.Add(_object.GetComponent <MeshFilter>().mesh);
        }

        foreach (Transform child in _object.transform)
        {
            meshes.AddRange(GetMeshes(child.gameObject));
        }

        return(meshes);
    }
Example #10
0
    /// <summary>
    /// 获取世界坐标系下的部分坐标
    /// </summary>
    public static Vector3[] GetVerticesInWorld(GameObject _Object, int Start, int Length)
    {
        if (!GameObjectValidate.HavaVertices(_Object))
        {
            return(null);                                                       //无顶点
        }
        Matrix4x4 matrix = _Object.transform.localToWorldMatrix;                //转换矩阵

        Vector3[] Vertices = _Object.GetComponent <MeshFilter>().mesh.vertices; //局部坐标系下的顶点坐标

        if (Start >= Vertices.Length || Start + Length > Vertices.Length || Length <= 0)
        {
            return(null);                                                                               //起始的位置超出边界或终点位置超出边界或长度过小
        }
        Vector3[] result = new Vector3[Length];

        for (int i = Start; i < Start + Length; i++)
        {
            result[i - Start] = matrix.MultiplyPoint3x4(Vertices[i]); //转换到世界坐标系
        }

        return(result);
    }
Example #11
0
    /// <summary>
    /// 更新该帧内的对象
    /// </summary>
    private void UpdateGameObject(GameObject preGameObject, GameObject curGameObject, List <Vector3> veticesFragment, WormholeFragment wormholeFragment)
    {
        if (m_AnimationIndex < m_AnimationMaxIndex)
        {
            /*
             * 若该片段为NULL或该片段的个数为0
             * 则说明该对象无顶点
             * 不继续进行计算
             */
            if (!GameObjectValidate.HavaVertices(preGameObject) || veticesFragment == null)
            {
                return;                                                                             //09-05
            }
            //09-05
            if (veticesFragment.Count != 0)
            {
                Vector3[] vertices = GameObjectOperation.GetVertices(preGameObject);

                /*
                 * 根据片段中记录的顶点偏移数据
                 * 计算对象在该帧移动后的位置
                 * 其中该片段记录的是世界坐标系下的数据
                 * 因此要转换成对象所在的局部坐标系下
                 */
                for (int i = 0; i < vertices.Length; i++)
                {
                    vertices[i] += veticesFragment[i];
                }

                /*
                 * 更新该对象
                 * 防止渲染出现异常
                 */
                GameObjectOperation.UpdateMeshInfo(preGameObject, vertices);
            }

            /*
             * 虫洞逐帧变化模拟
             * 当阈值增量为0,说明不变化
             * 当阈值增量不为0
             * 开启虫洞模拟 (_SimWormhole的值为1),并赋予相应的阈值
             */
            if (wormholeFragment.ThresholdDetla == 0)
            {
                return;
            }

            Material material = GameObjectOperation.GetMaterial(preGameObject);
            if (material == null || material.shader.name != "Custom/Leaves")
            {
                return;
            }

            material.SetFloat("_SimWormhole", 1);
            material.SetColor("_Threshold",
                              CellularTexture.DEC2Color(wormholeFragment.InitThreshold + (int)(wormholeFragment.ThresholdDetla * m_AnimationIndex))
                              );
        }
        else //最后一帧
        {
            /*
             * 无需计算
             * 只需设置前一关键帧的对象不可见
             * 后一帧对象可见即可
             */
            preGameObject.SetActive(false);
            curGameObject.SetActive(true);

            GameObjectOperation.ClearMaterials(preGameObject);
            GameObjectOperation.ClearMeshes(preGameObject);

            GameObject.Destroy(preGameObject);
        }
    }