Ejemplo n.º 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);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// 调整前后枝干的顶点
    /// </summary>
    private void AdjustBranch()
    {
        if (m_PairedBranchIndexes.Count == 1 && m_PairedBranchIndexes[0].PreIndex == null)
        {
            AdjustFreshBranch();
            return;
        }

        /*
         * 获取前后两个关键帧的枝干对象
         * 用于后续获取顶点以及更改顶点
         */
        GameObject preBranch = m_PairedBranchIndexes[0].PreIndex.Belong;
        GameObject curBranch = m_PairedBranchIndexes[0].CurIndex.Belong;

        Vector3[] preVertices = GameObjectOperation.GetVertices(preBranch);
        Vector3[] vertices    = new Vector3[GameObjectOperation.GetVertices(curBranch).Length]; //该顶点数组用于存放修改后的前关键帧顶点

        /*
         * 遍历已经匹配好的Index
         * 根据PairedIndex中记录的前后Index的数据
         * 对前后关键帧中枝干的数据进行修改
         */
        foreach (PairedIndex <BranchIndex> pairedBranchIndex in m_PairedBranchIndexes)
        {
            BranchIndex preBranchIndex = pairedBranchIndex.PreIndex;
            BranchIndex curBranchIndex = pairedBranchIndex.CurIndex;

            if (curBranchIndex.IsFirstBranch() && preBranchIndex.IsFirstBranch())   //均为第一个枝干
            {
                CopyVertices(preVertices, preBranchIndex.BottomVerticesIndex, vertices, curBranchIndex.BottomVerticesIndex, 40);
            }
            else if (preBranchIndex != null) //若存在匹配的第一个关键帧的枝干索引,则将第一个关键帧枝干索引指向的顶点插入到第二个关键帧枝干索引指向的顶点位置中
            {
                CopyVertices(preVertices, preBranchIndex.TopVerticesIndex, vertices, curBranchIndex.TopVerticesIndex);
            }
            else//若第二个关键帧枝干不存在匹配的第一个关键帧枝干索引,则遍历其前驱,直至有存在的第一个关键帧枝干索引,并将该枝干索引指向的顶点插入到第二个关键帧枝干索引指向的顶点位置中
            {
                CopyVertices(vertices, curBranchIndex.Previous.TopVerticesIndex, vertices, curBranchIndex.TopVerticesIndex);
            }
        }

        //更新对象
        GameObjectOperation.UpdateMeshInfo(preBranch, vertices, GameObjectOperation.GetUV(curBranch), GameObjectOperation.GetTriangleIndexes(curBranch));

        AddPreObject(preBranch);
        AddCurObject(curBranch);
    }
Ejemplo n.º 3
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]);
        }
    }
Ejemplo n.º 4
0
    private void AdjustFreshBranch()
    {
        GameObject curBranch = m_PairedBranchIndexes[0].CurIndex.Belong;
        GameObject preBranch = GameObject.Instantiate(curBranch);

        /*
         * 摧毁所有的子对象
         * 防止重复
         */
        GameObjectOperation.DestroyAllChildren(preBranch);

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

        //CopyVertices(curBranch.transform.InverseTransformPoint(Vector3.zero), vertices);
        CopyVertices(TreeModel.GetBranchBottomCenter(curBranch, m_PairedBranchIndexes[0].CurIndex), vertices);

        GameObjectOperation.UpdateMeshInfo(preBranch, vertices);


        AddPreObject(preBranch);
        AddCurObject(curBranch);
    }
Ejemplo n.º 5
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);
        }
    }
Ejemplo n.º 6
0
    /// <summary>
    /// 根据当前模型的数据写叶片长度和最大宽度
    /// 后续则根据实际需要读取该文件,减少重复计算
    /// </summary>
    private void WriteLeafLengthAndWidth()
    {
        if (m_MeshModelInstance == null)
        {
            throw new UnityException("Error Mesh Path");
        }

        //实例化,否则无法获取顶点和UV坐标
        GameObject meshModel = (GameObject)GameObject.Instantiate(m_MeshModelInstance);

        /*
         * 获取顶点坐标和UV坐标
         * 用于后续计算长度和宽度
         */
        Vector3[] vertices = GameObjectOperation.GetVertices(meshModel.transform.GetChild(0).gameObject);
        Vector2[] uv       = GameObjectOperation.GetUV(meshModel.transform.GetChild(0).gameObject);

        /*
         * 获取未放大和缩小的模型中
         * 叶片的长度、宽度和叶片面积
         * 用于后续与实际叶片长度和宽度或叶片面积做比较,确定模型的缩放比例
         */
        LeafLength      = GetLeafLength(vertices, uv); //m
        LeafWidth       = GetLeafWidth(vertices, uv);  //m
        UniformLeafArea = ComputeLeafArea(meshModel);  //㎡

        /*
         * 清除GameObject
         * 防止模型显示
         */
        GameObject.Destroy(meshModel);

        /*
         * 记录叶片长度和宽度
         * 用于写入文件中
         */
        string str = "";

        str += "LeafLength " + LeafLength + " m\n";
        str += "LeafWidth " + LeafWidth + " m\n";
        str += "MeshArea " + UniformMeshArea + " m^2\n";
        str += "LeafArea " + UniformLeafArea + " m^2\n";

        str += "VisualPixelCount " + VisualPixelCount + "\n";
        str += "PixelCount " + PixelCount + "\n";
        str += "VisibilityRatio " + VisibilityRatio + "\n";

        /*
         * 将数据写入文件
         * 方便后续读取
         */
        //string filePath = System.Environment.CurrentDirectory + "\\Assets\\Resources\\" + m_strMeshPath.Remove(m_strMeshPath.LastIndexOf('.')) + ".tmp";
        string directoryPath = System.Environment.CurrentDirectory + "\\Data";

        if (!System.IO.Directory.Exists(directoryPath))
        {
            Directory.CreateDirectory(directoryPath);
        }

        string       filePath = directoryPath + "\\" + Path.GetFileNameWithoutExtension(m_strMeshPath) + ".data";
        FileStream   stream   = new FileStream(filePath, FileMode.Create);
        StreamWriter writer   = new StreamWriter(stream);

        writer.WriteLine(str);  //写入

        //完成写入,内存释放
        writer.Close();
        stream.Close();
    }