Class TextureUtils
Provides a series of utility methods for handling Unity UnityEngine.Texture objects, including creation, manipulation, channel extraction, and conversion of textures.
Inherited Members
Namespace: TriLibCore.Utils
Assembly: TriLibCore.dll
Syntax
public static class TextureUtils
Methods
ApplyTexture2D(TextureLoadingContext, bool)
Uploads the data of a UnityEngine.Texture2D to the GPU based on the specified textureLoadingContext.
Declaration
public static void ApplyTexture2D(TextureLoadingContext textureLoadingContext, bool procedural)
Parameters
| Type | Name | Description |
|---|---|---|
| TextureLoadingContext | textureLoadingContext | The TextureLoadingContext containing the Unity texture (if any) and additional options for texture processing. |
| bool | procedural | A value indicating whether the texture should be treated as procedurally generated.
If |
ApplyTransparency(TextureLoadingContext, Texture, Texture)
Applies the alpha/transparency data from a transparencyTexture onto the color data of a diffuseTexture,
producing a new texture in the textureLoadingContext.
Declaration
public static void ApplyTransparency(TextureLoadingContext textureLoadingContext, Texture diffuseTexture, Texture transparencyTexture)
Parameters
| Type | Name | Description |
|---|---|---|
| TextureLoadingContext | textureLoadingContext | The TextureLoadingContext used to create and store the resulting transparent texture. |
| Texture | diffuseTexture | The diffuse (color) texture to which transparency will be applied. |
| Texture | transparencyTexture | The texture containing transparency or mask data. |
BuildMetallicTexture(TextureLoadingContext, Texture, Texture, Texture, Texture, Color, Color, float, float?, float?, bool, bool, int, int)
Constructs a metallic texture from the specified inputs (diffuse, metallic, specular, glossiness textures, etc.)
and stores the result within the textureLoadingContext.
Declaration
public static void BuildMetallicTexture(TextureLoadingContext textureLoadingContext, Texture diffuseTexture, Texture metallicTexture, Texture specularTexture, Texture glossinessTexture, Color defaultDiffuse, Color defaultSpecular, float shininessExponent, float? defaultRoughness, float? defaultMetallic, bool usingRoughness = false, bool mixTextureChannelsWithColors = false, int metallicComponentIndex = 0, int glossinessComponentIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| TextureLoadingContext | textureLoadingContext | The TextureLoadingContext in which the resulting texture is created and stored. |
| Texture | diffuseTexture | The diffuse (color) texture. |
| Texture | metallicTexture | The texture containing metallic data. |
| Texture | specularTexture | The texture containing specular data. |
| Texture | glossinessTexture | The texture containing glossiness or roughness data. |
| Color | defaultDiffuse | The default diffuse color, if the diffuse texture is absent or partially defined. |
| Color | defaultSpecular | The default specular color, if the specular texture is absent or partially defined. |
| float | shininessExponent | The shininess or glossiness exponent for materials. |
| float? | defaultRoughness | An optional default roughness value used if no glossiness or roughness texture is present. |
| float? | defaultMetallic | An optional default metallic value used if no metallic texture is present. |
| bool | usingRoughness | true if the |
| bool | mixTextureChannelsWithColors | true if the color values (diffuse, specular) should be multiplied by the textures' channels; false to use the textures' channels directly. |
| int | metallicComponentIndex | The channel index in the metallic texture to sample metallic data from. |
| int | glossinessComponentIndex | The channel index in the glossiness/roughness texture to sample glossiness/roughness data from. |
CopyTextureCPU(RenderTexture, Texture2D, bool, bool)
Copies pixel data from a UnityEngine.RenderTexture to a UnityEngine.Texture2D on the CPU side.
Declaration
public static void CopyTextureCPU(RenderTexture from, Texture2D to, bool updateMipMaps, bool makeNoLongerReadable)
Parameters
| Type | Name | Description |
|---|---|---|
| RenderTexture | from | The source UnityEngine.RenderTexture. |
| Texture2D | to | The destination UnityEngine.Texture2D. |
| bool | updateMipMaps | If |
| bool | makeNoLongerReadable | If |
CreateTexture2D(TextureLoadingContext)
Creates a new UnityEngine.Texture2D based on the data in textureLoadingContext,
if a Unity texture has not already been created or loaded.
Declaration
public static void CreateTexture2D(TextureLoadingContext textureLoadingContext)
Parameters
| Type | Name | Description |
|---|---|---|
| TextureLoadingContext | textureLoadingContext | The TextureLoadingContext containing metadata (e.g., width, height) for the new texture. |
ExtractChannelData(int, TextureLoadingContext, string)
Extracts a specific color channel (e.g., R, G, B, A) from the original Untiy texture.
and updates Unity texture in the textureLoadingContext with the extracted channel data.
Declaration
public static void ExtractChannelData(int channelIndex, TextureLoadingContext textureLoadingContext, string suffix = "")
Parameters
| Type | Name | Description |
|---|---|---|
| int | channelIndex | The index of the channel to extract (0 for Red, 1 for Green, 2 for Blue, 3 for Alpha). |
| TextureLoadingContext | textureLoadingContext | The TextureLoadingContext that contains the source texture and receives the new texture. |
| string | suffix | A string suffix added to the resulting texture's name for clarity (e.g., "_redChannel"). |
FixNPOTTexture(TextureLoadingContext)
Rescales a non-power-of-two (NPOT) texture to the nearest power-of-two resolution if necessary, applying any desired mipmap generation or compression.
Declaration
public static void FixNPOTTexture(TextureLoadingContext textureLoadingContext)
Parameters
| Type | Name | Description |
|---|---|---|
| TextureLoadingContext | textureLoadingContext | The TextureLoadingContext containing the texture to be rescaled. |
FixNormalMap(TextureLoadingContext)
Reorders the channels in a normal map texture if needed, modifying the normal map in place.
Declaration
public static void FixNormalMap(TextureLoadingContext textureLoadingContext)
Parameters
| Type | Name | Description |
|---|---|---|
| TextureLoadingContext | textureLoadingContext | The TextureLoadingContext containing a normal map texture, if applicable. |
GetNextPOT(int)
Calculates the next power-of-two value for a given integer, at minimum 1.
Declaration
public static int GetNextPOT(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value | The value to convert to the next power-of-two. |
Returns
| Type | Description |
|---|---|
| int | The next power-of-two value. |
IsValidTextureFileType(string)
Checks whether a given filename has a known image file extension.
Declaration
public static bool IsValidTextureFileType(string filename)
Parameters
| Type | Name | Description |
|---|---|---|
| string | filename | The filename to check. |
Returns
| Type | Description |
|---|---|
| bool | true if the filename extension matches a standard image type; otherwise false. |
LoadTexture2D(TextureLoadingContext)
Loads a texture from the specified Stream using Unity's built-in loader LoadImage.
Declaration
public static void LoadTexture2D(TextureLoadingContext textureLoadingContext)
Parameters
| Type | Name | Description |
|---|---|---|
| TextureLoadingContext | textureLoadingContext | The TextureLoadingContext containing the raw data stream and other texture parameters. |
LoadTexture2DNonAlloc(TextureLoadingContext)
Loads a texture by directly setting pixel data, bypassing Unity's LoadImage method.
Declaration
public static void LoadTexture2DNonAlloc(TextureLoadingContext textureLoadingContext)
Parameters
| Type | Name | Description |
|---|---|---|
| TextureLoadingContext | textureLoadingContext | The TextureLoadingContext holding raw image data in Data, as well as the texture metadata (width, height, etc.). |
SpecularDiffuseToAlbedo(TextureLoadingContext, Texture, Texture, Vector4, Vector4, float, bool, bool)
Converts a specular workflow texture to an albedo texture by mixing the diffuse and specular channels as needed.
Declaration
public static void SpecularDiffuseToAlbedo(TextureLoadingContext textureLoadingContext, Texture diffuseTexture, Texture specularTexture, Vector4 diffuseColor, Vector4 specularColor, float glossiness, bool outputBaseColor, bool reassign = false)
Parameters
| Type | Name | Description |
|---|---|---|
| TextureLoadingContext | textureLoadingContext | The TextureLoadingContext that will store the newly generated albedo texture. |
| Texture | diffuseTexture | The original diffuse texture. |
| Texture | specularTexture | The specular texture to mix with the diffuse. |
| Vector4 | diffuseColor | The diffuse color if the diffuse texture is missing or partially defined. |
| Vector4 | specularColor | The specular color if the specular texture is missing or partially defined. |
| float | glossiness | The glossiness level to apply in the mixing. |
| bool | outputBaseColor | true to generate a base color texture; false to generate a metallic-smoothness texture. |
| bool | reassign | Not used. |