SKSprite.generateVerts C# (CSharp) Method

generateVerts() protected method

protected generateVerts ( Mesh &mesh ) : void
mesh Mesh
return void
    protected virtual void generateVerts( ref Mesh mesh )
    {
        // fetch required information and cache the uv values
        var spriteInfo = spriteSheet.textureInfoForImage( sourceImageName );
        var uvRect = spriteInfo.uvRect;
        var uvArrayForFullQuad = new Vector2[4];

        // handle filling up the UV values. flipped get reversed
        if( !_isFlipped )
        {
            uvArrayForFullQuad[0] = new Vector2( uvRect.xMin, uvRect.yMax );
            uvArrayForFullQuad[1] = new Vector2( uvRect.xMin, uvRect.yMin );
            uvArrayForFullQuad[2] = new Vector2( uvRect.xMax, uvRect.yMin );
            uvArrayForFullQuad[3] = new Vector2( uvRect.xMax, uvRect.yMax );
        }
        else
        {
            uvArrayForFullQuad[3] = new Vector2( uvRect.xMin, uvRect.yMax );
            uvArrayForFullQuad[2] = new Vector2( uvRect.xMin, uvRect.yMin );
            uvArrayForFullQuad[1] = new Vector2( uvRect.xMax, uvRect.yMin );
            uvArrayForFullQuad[0] = new Vector2( uvRect.xMax, uvRect.yMax );
        }

        // create a multiplier to adjust for different ortho sizes
        var orthoAdjustment = ( 2f * spriteSheet.cameraOrthoSize ) / spriteSheet.targetScreenHeight;
        var orthoAdjustedPixelPerfectHDSize = pixelPerfectHDSize * orthoAdjustment;

        // mesh storage lists
        var verts = new List<Vector3>();
        var uvs = new List<Vector2>();
        var tris = new List<int>();
        var colors = new List<Color>();

        // figure out the number of quads we are going to need
        var horizontalQuadCount = Mathf.CeilToInt( desiredSize.x / pixelPerfectHDSize.x );
        var verticalQuadCount = Mathf.CeilToInt( desiredSize.y / pixelPerfectHDSize.y );
        var triIndex = 0;
        var anchorOffset = offsetForAnchor() * orthoAdjustment;

        // do we have enough room for the full width and height? this will only matter for the last row/column
        var clipHorizontally = desiredSize.x / pixelPerfectHDSize.x != horizontalQuadCount;
        var clipVertically = desiredSize.y / pixelPerfectHDSize.y != verticalQuadCount;

        // we build the quads from bottom left to top right
        for( var x = 0; x < horizontalQuadCount; x++ )
        {
            for( var y = 0; y < verticalQuadCount; y++ )
            {
                var offset = new Vector2( x * orthoAdjustedPixelPerfectHDSize.x, y * orthoAdjustedPixelPerfectHDSize.y );
                var rect = new Rect( offset.x * scale.x, offset.y * scale.y, orthoAdjustedPixelPerfectHDSize.x * scale.x, orthoAdjustedPixelPerfectHDSize.y * scale.y );
                rect.center += anchorOffset;

                var didClipHorizontally = false;
                var didClipVertically = false;
                if( clipHorizontally && x == horizontalQuadCount - 1 )
                {
                    didClipHorizontally = true;

                    // get the actual width of the clipped quad
                    var quadWidth = desiredSize.x % orthoAdjustedPixelPerfectHDSize.x;
                    rect.width = quadWidth * scale.x;
                }

                if( clipVertically && y == verticalQuadCount - 1 )
                {
                    didClipVertically = true;

                    // get the actual height of the clipped quad
                    var quadHeight = desiredSize.y % orthoAdjustedPixelPerfectHDSize.y;
                    rect.height = quadHeight * scale.y;

                    // offset vertically to line up the quads
                    //rect.y += ( originalHeight - rect.height );
                }

                // add the quad to our vert list
                verts.Add( new Vector3( rect.xMin, rect.yMax, 0 ) );
                verts.Add( new Vector3( rect.xMin, rect.yMin, 0 ) );
                verts.Add( new Vector3( rect.xMax, rect.yMin, 0 ) );
                verts.Add( new Vector3( rect.xMax, rect.yMax, 0 ) );

                if( didClipHorizontally || didClipVertically )
                    uvs.AddRange( getUvsForClippedQuad( uvRect, rect.width, rect.height, orthoAdjustedPixelPerfectHDSize ) );
                else
                    uvs.AddRange( uvArrayForFullQuad );

                tris.Add( triIndex + 3 );
                tris.Add( triIndex + 1 );
                tris.Add( triIndex );
                tris.Add( triIndex + 2 );
                tris.Add( triIndex + 1 );
                tris.Add( triIndex + 3 );
                triIndex += 4;

                colors.Add( tintColor );
                colors.Add( tintColor );
                colors.Add( tintColor );
                colors.Add( tintColor );
            } // end for
        } // end for

        mesh.vertices = verts.ToArray();
        mesh.colors = colors.ToArray();
        mesh.triangles = tris.ToArray();
        mesh.uv = uvs.ToArray();
    }