Пример #1
0
    public static Vector2[] BuildUVs(Texture2DContainer a_rTextureContainer, Vector2[] a_rRenderMeshUVs, Uni2DTextureAtlas a_rTextureAtlas)
    {
        // UV data
        Rect oUVRect          = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
        bool bIsUVRectFlipped = false;

        // No atlas
        if (a_rTextureAtlas == null)
        {
            oUVRect          = new Rect(0.0f, 0.0f, 1.0f, 1.0f);
            bIsUVRectFlipped = false;
        }
        else if (!string.IsNullOrEmpty(a_rTextureContainer.GUID) && a_rTextureAtlas.GetUVs(a_rTextureContainer.GUID, out oUVRect, out bIsUVRectFlipped) == false)               // Atlas
        {
            // Not found in atlas!!!
            Debug.LogError("Uni2D: Atlas \'" + a_rTextureAtlas.name + "\' does not contain the texture with GUID \'" + a_rTextureContainer.GUID + "\'."
                           + "\nCheck your animation clip and atlas settings.", a_rTextureAtlas);
        }

        Vector2[] oMeshUVs     = a_rRenderMeshUVs;
        int       iVertexCount = oMeshUVs.Length;

        // UV normalization (computes mesh UVs coords in atlas UVs space)
        Vector2[] oNormalizedMeshUVs = new Vector2[iVertexCount];
        Vector2   f2UVOffset;           // Position (== min coords) of sprite UV rect in atlas. (0;0) if not atlased
        Vector2   f2UVRange;            // UV range (== max coords - position) in atlas. (1;1) if not atlased

        // If UV rect is not flipped...
        if (bIsUVRectFlipped == false)
        {
            f2UVOffset = new Vector2(oUVRect.xMin, oUVRect.yMin);
            f2UVRange  = new Vector2(oUVRect.xMax, oUVRect.yMax) - f2UVOffset;

            for (int iUVIndex = 0; iUVIndex < iVertexCount; ++iUVIndex)
            {
                oNormalizedMeshUVs[iUVIndex] = f2UVOffset + Vector2.Scale(f2UVRange, oMeshUVs[iUVIndex]);
            }
        }
        else         // UV rect is flipped
        {
            f2UVOffset = new Vector2(oUVRect.xMax, oUVRect.yMin);
            f2UVRange  = new Vector2(oUVRect.xMin, oUVRect.yMax) - f2UVOffset;

            for (int iUVIndex = 0; iUVIndex < iVertexCount; ++iUVIndex)
            {
                Vector2 f2UV = oMeshUVs[iUVIndex];

                // Need to swap UV coords
                oNormalizedMeshUVs[iUVIndex] = f2UVOffset + new Vector2(f2UVRange.x * f2UV.y, f2UVRange.y * f2UV.x);
            }
        }

        return(oNormalizedMeshUVs);
    }
Пример #2
0
    public static Vector2[] BuildUVs( Texture2DContainer a_rTextureContainer, Vector2[ ] a_rRenderMeshUVs, Uni2DTextureAtlas a_rTextureAtlas )
    {
        // UV data
        Rect oUVRect = new Rect();
        bool bIsUVRectFlipped = false;

        // No atlas
        if( a_rTextureAtlas == null )
        {
            oUVRect = new Rect( 0.0f, 0.0f, 1.0f, 1.0f );
            bIsUVRectFlipped = false;
        }
        else if( !string.IsNullOrEmpty( a_rTextureContainer.GUID ) && a_rTextureAtlas.GetUVs( a_rTextureContainer.GUID, out oUVRect, out bIsUVRectFlipped ) == false )	// Atlas
        {
            // Not found in atlas!!!
            Debug.LogError( "Uni2D: Atlas \'" + a_rTextureAtlas.name + "\' does not contain the texture with GUID \'" + a_rTextureContainer.GUID + "\'."
                + "\nCheck your animation clip and atlas settings.", a_rTextureAtlas );
        }

        Vector2[ ] oMeshUVs = a_rRenderMeshUVs;
        int iVertexCount    = oMeshUVs.Length;

        // UV normalization (computes mesh UVs coords in atlas UVs space)
        Vector2[ ] oNormalizedMeshUVs = new Vector2[ iVertexCount ];
        Vector2 f2UVOffset;	// Position (== min coords) of sprite UV rect in atlas. (0;0) if not atlased
        Vector2 f2UVRange;	// UV range (== max coords - position) in atlas. (1;1) if not atlased

        // If UV rect is not flipped...
        if( bIsUVRectFlipped == false )
        {
            f2UVOffset = new Vector2( oUVRect.xMin, oUVRect.yMin );
            f2UVRange  = new Vector2( oUVRect.xMax, oUVRect.yMax ) - f2UVOffset;

            for( int iUVIndex = 0; iUVIndex < iVertexCount; ++iUVIndex )
            {
                oNormalizedMeshUVs[ iUVIndex ] = f2UVOffset + Vector2.Scale( f2UVRange, oMeshUVs[ iUVIndex ] );
            }
        }
        else // UV rect is flipped
        {
            f2UVOffset = new Vector2( oUVRect.xMax, oUVRect.yMin );
            f2UVRange  = new Vector2( oUVRect.xMin, oUVRect.yMax ) - f2UVOffset;

            for( int iUVIndex = 0; iUVIndex < iVertexCount; ++iUVIndex )
            {
                Vector2 f2UV = oMeshUVs[ iUVIndex ];

                // Need to swap UV coords
                oNormalizedMeshUVs[ iUVIndex ] = f2UVOffset + new Vector2( f2UVRange.x * f2UV.y, f2UVRange.y * f2UV.x );
            }
        }

        return oNormalizedMeshUVs;
    }