Utilities

Utilities Class

The Utilities class provides various helper methods for converting Texture2D objects to different formats, such as Sprite, RenderTexture, and Cubemap. These utilities are essential for manipulating and using textures in Unity projects.

Overview

The Utilities class includes methods for converting textures to sprites, render textures, and cubemaps. These conversions are often needed in game development to handle and display images in different ways. This is especially helpful for converting Diffusion model responses into usable game assets.

Class Definition

using UnityEngine;

namespace SkillfulAI.API
{
    public static class Utilities
    {
        /// <summary>
        /// Converts a Texture2D to a Sprite.
        /// </summary>
        /// <param name="texture">The Texture2D to convert.</param>
        /// <returns>A Sprite created from the Texture2D.</returns>
        public static Sprite Texture2DToSprite(Texture2D texture)
        {
            return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
        }

        /// <summary>
        /// Converts a Texture2D to a RenderTexture.
        /// </summary>
        /// <param name="texture">The Texture2D to convert.</param>
        /// <returns>A RenderTexture created from the Texture2D.</returns>
        public static RenderTexture Texture2DToRenderTexture(Texture2D texture)
        {
            RenderTexture renderTexture = new RenderTexture(texture.width, texture.height, 0);
            Graphics.Blit(texture, renderTexture);
            return renderTexture;
        }

        /// <summary>
        /// Converts a Texture2D to a Cubemap.
        /// Note: This is a basic conversion and assumes the texture is a cross layout of the cubemap faces.
        /// </summary>
        /// <param name="texture">The Texture2D to convert.</param>
        /// <param name="cubemapSize">The size of the cubemap faces.</param>
        /// <returns>A Cubemap created from the Texture2D.</returns>
        public static Cubemap Texture2DToCubemap(Texture2D texture, int cubemapSize)
        {
            Cubemap cubemap = new Cubemap(cubemapSize, texture.format, false);
            Color[] colors = texture.GetPixels();

            int faceWidth = texture.width / 4;
            int faceHeight = texture.height / 3;

            // Define the face coordinates (in a cross layout)
            Rect[] faceRects = {
                new Rect(faceWidth * 2, faceHeight * 1, faceWidth, faceHeight), // Positive X
                new Rect(faceWidth * 0, faceHeight * 1, faceWidth, faceHeight), // Negative X
                new Rect(faceWidth * 1, faceHeight * 2, faceWidth, faceHeight), // Positive Y
                new Rect(faceWidth * 1, faceHeight * 0, faceWidth, faceHeight), // Negative Y
                new Rect(faceWidth * 1, faceHeight * 1, faceWidth, faceHeight), // Positive Z
                new Rect(faceWidth * 3, faceHeight * 1, faceWidth, faceHeight)  // Negative Z
            };

            // Assign pixels to cubemap faces
            for (int i = 0; i < 6; i++)
            {
                Rect faceRect = faceRects[i];
                Color[] faceColors = new Color[cubemapSize * cubemapSize];
                for (int y = 0; y < cubemapSize; y++)
                {
                    for (int x = 0; x < cubemapSize; x++)
                    {
                        faceColors[y * cubemapSize + x] = texture.GetPixel(
                            (int)(faceRect.x + (x * faceRect.width / cubemapSize)),
                            (int)(faceRect.y + (y * faceRect.height / cubemapSize))
                        );
                    }
                }
                cubemap.SetPixels(faceColors, (CubemapFace)i);
            }

            cubemap.Apply();
            return cubemap;
        }
    }
}

Detailed Explanation

Texture2DToSprite Method

Description: Converts a Texture2D to a Sprite.

Code:

public static Sprite Texture2DToSprite(Texture2D texture)
{
    return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}

Parameters:

  • texture: The Texture2D that will be converted to a Sprite.

Returns:

  • A Sprite created from the Texture2D.

Functionality:

  • Creates a Sprite from the entire Texture2D, using its dimensions and a pivot point at the center.

Texture2DToRenderTexture Method

Description: Converts a Texture2D to a RenderTexture.

Code:

public static RenderTexture Texture2DToRenderTexture(Texture2D texture)
{
    RenderTexture renderTexture = new RenderTexture(texture.width, texture.height, 0);
    Graphics.Blit(texture, renderTexture);
    return renderTexture;
}

Parameters:

  • texture: The Texture2D to be converted.

Returns:

  • A RenderTexture created from the Texture2D.

Functionality:

  • Creates a RenderTexture of the same size as the Texture2D and uses Graphics.Blit to copy the texture data to the render texture.

Texture2DToCubemap Method

Description: Converts a Texture2D to a Cubemap. Assumes the Texture2D is laid out in a cross format with cubemap faces.

Code:

public static Cubemap Texture2DToCubemap(Texture2D texture, int cubemapSize)
{
    Cubemap cubemap = new Cubemap(cubemapSize, texture.format, false);
    Color[] colors = texture.GetPixels();

    int faceWidth = texture.width / 4;
    int faceHeight = texture.height / 3;

    // Define the face coordinates (in a cross layout)
    Rect[] faceRects = {
        new Rect(faceWidth * 2, faceHeight * 1, faceWidth, faceHeight), // Positive X
        new Rect(faceWidth * 0, faceHeight * 1, faceWidth, faceHeight), // Negative X
        new Rect(faceWidth * 1, faceHeight * 2, faceWidth, faceHeight), // Positive Y
        new Rect(faceWidth * 1, faceHeight * 0, faceWidth, faceHeight), // Negative Y
        new Rect(faceWidth * 1, faceHeight * 1, faceWidth, faceHeight), // Positive Z
        new Rect(faceWidth * 3, faceHeight * 1, faceWidth, faceHeight)  // Negative Z
    };

    // Assign pixels to cubemap faces
    for (int i = 0; i < 6; i++)
    {
        Rect faceRect = faceRects[i];
        Color[] faceColors = new Color[cubemapSize * cubemapSize];
        for (int y = 0; y < cubemapSize; y++)
        {
            for (int x = 0; x < cubemapSize; x++)
            {
                faceColors[y * cubemapSize + x] = texture.GetPixel(
                    (int)(faceRect.x + (x * faceRect.width / cubemapSize)),
                    (int)(faceRect.y + (y * faceRect.height / cubemapSize))
                );
            }
        }
        cubemap.SetPixels(faceColors, (CubemapFace)i);
    }

    cubemap.Apply();
    return cubemap;
}

Parameters:

  • texture: The Texture2D to be converted.

  • cubemapSize: The size of each face of the Cubemap.

Returns:

  • A Cubemap created from the Texture2D.

Functionality:

  • Converts a Texture2D with a cross layout of cubemap faces into a Cubemap.

  • Defines the coordinates for each face, extracts pixel data, and assigns it to the corresponding cubemap faces.

Usage Example

  1. Convert Texture2D to Sprite:

    Sprite sprite = Utilities.Texture2DToSprite(myTexture2D);
  2. Convert Texture2D to RenderTexture:

    RenderTexture renderTexture = Utilities.Texture2DToRenderTexture(myTexture2D);
  3. Convert Texture2D to Cubemap:

    Cubemap cubemap = Utilities.Texture2DToCubemap(myTexture2D, 256);

Conclusion

The Utilities class provides essential methods for converting textures into different formats, which can be crucial for rendering and displaying textures in various ways within Unity. These methods facilitate working with sprites, render textures, and cubemaps, enhancing flexibility and functionality in your Unity projects.

Last updated