Esempio n. 1
0
        //頂点
        void InitVerts(DicingTextures atlas)
        {
            if (atlas == null)
            {
                return;
            }
            //頂点データ
            this.verts = new List <QuadVerts>();

            //アトラス内のセルのサイズ
            int atlasCellSize = atlas.CellSize;
            //パディング済みのセルサイズ(テクスチャの方のセルサイズ)
            int paddingCellSize = atlasCellSize - atlas.Padding * 2;

            //テクスチャ内のセルの数
            int cellCountX = Mathf.CeilToInt(1.0f * Width / paddingCellSize);
            int cellCountY = Mathf.CeilToInt(1.0f * Height / paddingCellSize);

            //アトラス画像について
            int atlasWidth      = atlas.GetTexture(this.AtlasName).width;
            int atlasHeight     = atlas.GetTexture(this.AtlasName).height;
            int atlasCellCountX = Mathf.CeilToInt(1.0f * atlasWidth / atlasCellSize);
//			int atlasCellCountY = Mathf.CeilToInt(1.0f * atlasHeight / atlasCellSize);

            //セルのインデックス
            int index = 0;

            for (int cellY = 0; cellY < cellCountY; ++cellY)
            {
                float y0 = cellY * paddingCellSize;
                //本来のテクスチャの大きさ以上にはしない
                float y1 = Mathf.Min(y0 + paddingCellSize, this.Height);
                for (int cellX = 0; cellX < cellCountX; ++cellX)
                {
                    DicingTextureData.QuadVerts quadVerts = new DicingTextureData.QuadVerts();
                    float x0 = cellX * paddingCellSize;
                    //本来のテクスチャの大きさ以上にはしない
                    float x1 = Mathf.Min(x0 + paddingCellSize, this.Width);
                    quadVerts.v = new Vector4(x0, y0, x1, y1);
                    int cellIndex = cellIndexList[index];
                    quadVerts.isAllTransparent = (cellIndex == transparentIndex);

                    float ux = (cellIndex % atlasCellCountX) * atlasCellSize;
                    float uy = (cellIndex / atlasCellCountX) * atlasCellSize;
                    //パディングを考慮してUV値を設定しておく
                    float uvX = 1.0f * (ux + atlas.Padding) / atlasWidth;
                    float uvY = 1.0f * (uy + atlas.Padding) / atlasHeight;
                    float uvW = 1.0f * (x1 - x0) / atlasWidth;
                    float uvH = 1.0f * (y1 - y0) / atlasHeight;
                    quadVerts.uvRect = new Rect(uvX, uvY, uvW, uvH);
                    this.verts.Add(quadVerts);
                    index++;
                }
            }
        }
Esempio n. 2
0
        //描画頂点データに対してForeachして、funcitionをコールバックとして呼ぶ
        //UVが(0,0,1,1)の範囲であるのが必須
        //指定のUV座標に対してのもの
        void ForeachVertexListSub(Rect uvRect, bool skipTransParentCell, DicingTextures textures, Action <Rect, Rect> function)
        {
            Texture2D texture  = textures.GetTexture(this.AtlasName);
            float     textureW = texture.width;
            float     textureH = texture.height;
            //頂点データを取得
            List <DicingTextureData.QuadVerts> verts = GetVerts(textures);

            //UV指定を考慮して、描画ピクセルの矩形を取得
            Rect pixcelRect = new Rect(uvRect.x * this.Width, uvRect.y * this.Height, uvRect.width * this.Width, uvRect.height * this.Height);

            for (int i = 0; i < verts.Count; ++i)
            {
                var vert = verts[i];
                //透明ならスキップ
                if (skipTransParentCell && vert.isAllTransparent)
                {
                    continue;
                }

                //上下左右の座標
                float left   = vert.v.x;
                float right  = vert.v.z;
                float top    = vert.v.y;
                float bottom = vert.v.w;

                Rect uv = vert.uvRect;
                if (left > pixcelRect.xMax || top > pixcelRect.yMax || right < pixcelRect.x || bottom < pixcelRect.y)
                {
                    //全体が切り取り矩形の外
                    continue;
                }
                else
                {
                    //一部が切り取り矩形の外なら、頂点とUV値を調整して矩形内に収める
                    if (left < pixcelRect.x)
                    {
                        uv.xMin += (pixcelRect.x - left) / textureW;
                        left     = pixcelRect.x;
                    }
                    if (right > pixcelRect.xMax)
                    {
                        uv.xMax += (pixcelRect.xMax - right) / textureW;
                        right    = pixcelRect.xMax;
                    }

                    if (top < pixcelRect.y)
                    {
                        uv.yMin += (pixcelRect.y - top) / textureH;
                        top      = pixcelRect.y;
                    }
                    if (bottom > pixcelRect.yMax)
                    {
                        uv.yMax += (pixcelRect.yMax - bottom) / textureH;
                        bottom   = pixcelRect.yMax;
                    }
                }

                function(new Rect(left, top, right - left, bottom - top), uv);
            }
        }