public AtlasEntry( Texture2D a_rAtlasTexture, Material a_rAtlasMaterial, string[ ] a_rTextureGUIDs, PackedRect[ ] a_rUVs )
		{
			m_rAtlasTexture        = a_rAtlasTexture;
			m_rAtlasMaterial       = a_rAtlasMaterial;
			m_rAtlasedTextureGUIDs = a_rTextureGUIDs;
			m_rUVs                 = a_rUVs;
		}
Exemple #2
0
    static RectPack TryPackingRects(List <Rect> rects, Rect drawingArea)
    {
        // Try to pack all rects.
        List <Rect> emptySpaces = new List <Rect>();

        emptySpaces.Add(drawingArea);
        List <Rect> storedRects     = new List <Rect>();
        bool        couldntPackRect = false;

        for (int i = 0; i < rects.Count && !couldntPackRect; i++)
        {
            PackedRect packedRect = null;
            // Try to pack the rect in an empty space.
            for (int j = emptySpaces.Count - 1; j >= 0 && packedRect == null; j--)
            {
                packedRect = StoreRect(rects[i], emptySpaces[j]);
                if (packedRect != null)
                {
                    storedRects.Add(new Rect(packedRect.storedRect));
                    emptySpaces.RemoveAt(j);
                    emptySpaces.Add(packedRect.biggerEmptyRect);
                    emptySpaces.Add(packedRect.smallerEmptyRect);
                }
            }

            // If we can't pack this rect, stop.
            if (packedRect == null)
            {
                couldntPackRect = true;
            }
        }

        // Return the rects that we could pack.
        return(new RectPack(drawingArea, storedRects));
    }
Exemple #3
0
        private int Gather(float xMin, float xMax, int start = 0)
        {
            int count = sortedRectList.size;

            PackedRect[] packedRects = sortedRectList.array;
            for (int i = start; i < count; i++)
            {
                ref PackedRect rect = ref packedRects[i];
                if (rect.xMin > xMax)
                {
                    return(i);
                }
            }
    private AtlasEntry GenerateAtlasEntry(int a_iAtlasEntryIndex, Texture2D a_rAtlasTexture, Dictionary <int, PackedRect> a_rTextureIndexUVRectsDict, string a_oGeneratedDataPathLocal)
    {
        Texture2D rCurrentAtlasTexture;
        Material  rCurrentAtlasMaterial;

        float fWidth  = (float)a_rAtlasTexture.width;
        float fHeight = (float)a_rAtlasTexture.height;

        if (m_oAtlasEntries != null && m_oAtlasEntries.Length > a_iAtlasEntryIndex && m_oAtlasEntries[a_iAtlasEntryIndex] != null)
        {
            AtlasEntry rAtlasEntry = m_oAtlasEntries[a_iAtlasEntryIndex];
            rCurrentAtlasTexture  = rAtlasEntry.atlasTexture;
            rCurrentAtlasMaterial = rAtlasEntry.material;
        }
        else
        {
            rCurrentAtlasTexture  = null;
            rCurrentAtlasMaterial = null;
        }

        // rSavedAtlasTexture = instance of the saved/exported/serialized atlas texture at a_oGeneratedDataPathLocal
        Texture2D rSavedAtlasTexture  = this.ExportAndSaveAtlasTexture(a_iAtlasEntryIndex, rCurrentAtlasTexture, a_rAtlasTexture, a_oGeneratedDataPathLocal);
        Material  rSavedAtlasMaterial = this.GenerateAtlasMaterial(a_iAtlasEntryIndex, rCurrentAtlasMaterial, rSavedAtlasTexture, a_oGeneratedDataPathLocal);

        int iTextureCount = a_rTextureIndexUVRectsDict.Count;

        string[]     oTextureGUIDs  = new string[iTextureCount];
        PackedRect[] oPackedUVRects = new PackedRect[iTextureCount];

        int iTextureIndex = 0;

        foreach (KeyValuePair <int, PackedRect> rTextureIndexPackedUVRectPair in a_rTextureIndexUVRectsDict)
        {
            oTextureGUIDs[iTextureIndex] = m_rTextureContainers[rTextureIndexPackedUVRectPair.Key].GUID;

            PackedRect oNormalizedPackedRect = rTextureIndexPackedUVRectPair.Value;

            // Normalize UVs
            Rect rUVRect = oNormalizedPackedRect.rect;
            oNormalizedPackedRect.rect = new Rect(rUVRect.x / fWidth,
                                                  rUVRect.y / fHeight,
                                                  (rUVRect.width - m_iPadding) / fWidth,
                                                  (rUVRect.height - m_iPadding) / fHeight);

            oPackedUVRects[iTextureIndex] = oNormalizedPackedRect;

            ++iTextureIndex;
        }

        return(new AtlasEntry(rSavedAtlasTexture, rSavedAtlasMaterial, oTextureGUIDs, oPackedUVRects));
    }
    private Texture2D GenerateAtlasTexture(Dictionary <int, PackedRect> a_rTextureIndexPackedUVRectsDict, int a_iAtlasWidth, int a_iAtlasHeight)
    {
        Texture2D oAtlasTexture = new Texture2D(a_iAtlasWidth, a_iAtlasHeight, TextureFormat.ARGB32, false);

        // GetPixels32 returns a copy of the atlas pixels
        //Color32[ ] oAtlasColor32 = oAtlasTexture.GetPixels32( );

        // Create ourself the pixel array so we don't need to fill it: array is zero initialized
        Color32[] oAtlasColor32 = new Color32[a_iAtlasWidth * a_iAtlasHeight];

        /*
         * //Fill with black zero alpha
         * Color32 f4ClearColor = new Color32( 0, 0, 0, 0 );
         * for( int iColorIndex = 0, iColorCount = oAtlasColor32.Length; iColorIndex < iColorCount; ++iColorIndex )
         * {
         *      oAtlasColor32[ iColorIndex ] = f4ClearColor;
         * }
         */

        // Copy the atlased textures pixels into the atlas texture
        foreach (KeyValuePair <int, PackedRect> rIndexedPackedRect in a_rTextureIndexPackedUVRectsDict)
        {
            // The texture to copy
            Texture2D rPackedTexture = m_rTextureContainers[rIndexedPackedRect.Key];

            PackedRect rPackedRect = rIndexedPackedRect.Value;

            // The UV rect
            Rect rUVRect = rPackedRect.rect;

            //Debug.Log( rPackedTexture.name + ": " + rPackedRect.rect.x + " / " + rPackedRect.rect.y + " / " + rPackedTexture.width + "px ; " + rPackedTexture.height + "px / flipped: " + rPackedRect.isFlipped );

            // Copy texture into the atlas
            SetPixelBlock32(rPackedTexture.GetPixels32( ), oAtlasColor32,
                            (int)rUVRect.x, (int)rUVRect.y,
                            (int)rUVRect.width, (int)rUVRect.height,
                            a_iAtlasWidth, a_iAtlasHeight,
                            rPackedRect.isFlipped);
        }

        // Set the new atlas pixels
        oAtlasTexture.SetPixels32(oAtlasColor32);
        oAtlasTexture.Apply( );
        oAtlasColor32 = null;

        return(oAtlasTexture);
    }
	private AtlasEntry GenerateAtlasEntry( int a_iAtlasEntryIndex, Texture2D a_rAtlasTexture, Dictionary<int,PackedRect> a_rTextureIndexUVRectsDict, string a_oGeneratedDataPathLocal )
	{
		Texture2D rCurrentAtlasTexture;
		Material rCurrentAtlasMaterial;

		float fWidth = (float) a_rAtlasTexture.width;
		float fHeight = (float) a_rAtlasTexture.height;

		if( m_oAtlasEntries != null && m_oAtlasEntries.Length > a_iAtlasEntryIndex && m_oAtlasEntries[ a_iAtlasEntryIndex ] != null )
		{
			AtlasEntry rAtlasEntry = m_oAtlasEntries[ a_iAtlasEntryIndex ];
			rCurrentAtlasTexture   = rAtlasEntry.atlasTexture;
			rCurrentAtlasMaterial  = rAtlasEntry.material;
		}
		else
		{
			rCurrentAtlasTexture  = null;
			rCurrentAtlasMaterial = null;
		}

		// rSavedAtlasTexture = instance of the saved/exported/serialized atlas texture at a_oGeneratedDataPathLocal 
		Texture2D rSavedAtlasTexture = this.ExportAndSaveAtlasTexture( a_iAtlasEntryIndex, rCurrentAtlasTexture, a_rAtlasTexture, a_oGeneratedDataPathLocal );
		Material rSavedAtlasMaterial = this.GenerateAtlasMaterial( a_iAtlasEntryIndex, rCurrentAtlasMaterial, rSavedAtlasTexture, a_oGeneratedDataPathLocal );

		int iTextureCount             = a_rTextureIndexUVRectsDict.Count;
		string[ ] oTextureGUIDs       = new string[ iTextureCount ];
		PackedRect[ ] oPackedUVRects  = new PackedRect[ iTextureCount ];

		int iTextureIndex = 0;
		foreach( KeyValuePair<int,PackedRect> rTextureIndexPackedUVRectPair in a_rTextureIndexUVRectsDict )
		{
			oTextureGUIDs[ iTextureIndex ] = m_rTextureContainers[ rTextureIndexPackedUVRectPair.Key ].GUID;

			PackedRect oNormalizedPackedRect = rTextureIndexPackedUVRectPair.Value;

			// Normalize UVs
			Rect rUVRect = oNormalizedPackedRect.rect;
			oNormalizedPackedRect.rect = new Rect( rUVRect.x / fWidth,
					rUVRect.y / fHeight,
					rUVRect.width / fWidth,
					rUVRect.height / fHeight );

			oPackedUVRects[ iTextureIndex ] = oNormalizedPackedRect;

			++iTextureIndex;
		}

		return new AtlasEntry( rSavedAtlasTexture, rSavedAtlasMaterial, oTextureGUIDs, oPackedUVRects );
	}