Ejemplo n.º 1
0
        public CNode Insert(Texture2D texture)
        {
            CNode retNode = null;

            if (texture != null)
            {
                int texWidth  = texture.width;
                int texHeight = texture.height;

                if (!IsLeaf)   //不是叶子结点了
                {
                    if (leftChild != null)
                    {
                        retNode = leftChild.Insert(texture);
                    }
                    if (retNode == null &&
                        rightChild != null)
                    {
                        retNode = rightChild.Insert(texture);
                    }
                }
                else
                {
                    bool bNeedFliped = false;
                    if (IsFilled)
                    {
                        retNode = null;
                    }
                    else if (CanFillIn(texWidth, texHeight, out bNeedFliped))
                    {
                        bool bPerfectFillIn = bNeedFliped ?
                                              CanPerfectFillIn(texHeight, texWidth) :
                                              CanPerfectFillIn(texWidth, texHeight);

                        //如果完美匹配
                        if (bPerfectFillIn)
                        {
                            retNode = this;
                            Fill(ref retNode, texture, bNeedFliped);
                        }
                        else
                        {
                            int fillWidth  = (bNeedFliped ? texHeight : texWidth);
                            int fillHeight = (bNeedFliped ? texWidth : texHeight);

                            SplitNode(fillWidth, fillHeight, out leftChild, out rightChild);

                            if (leftChild != null)
                            {
                                retNode = leftChild.Insert(texture);
                            }
                        }
                    }
                }
            }

            return(retNode);
        }
Ejemplo n.º 2
0
        public static void Pack(Texture2D[] splitTexs, int width, int height, out PackTextureAttrSet outPackTextureAttrSet)
        {
            outPackTextureAttrSet = null;
            if (splitTexs != null)
            {
                SortTexture(ref splitTexs);

                CNode rootNode = GenerateRootNode(splitTexs, width, height);
                if (rootNode != null)
                {
                    //插一插
                    for (int i = 0; i < splitTexs.Length; ++i)
                    {
                        if (rootNode.Insert(splitTexs[i]) == null)
                        {
                            string assetPath = AssetDatabase.GetAssetPath(splitTexs[i]);
                            Debug.LogWarning(string.Format("no suitable area:{0}", assetPath));
                        }
                    }
                    //遍历一下节点树生成图集
                    GeneratePackTexture(rootNode, out outPackTextureAttrSet);
                }
            }
        }