Class TextureLoader
Loads image files into textures the way Game Studio's content pipeline would have prepared them, by what the texture is for: a colour as sRGB and premultiplied, data as linear, a normal map linear with unit normals in every mip, each with a full mipmap chain. Each file is loaded once per role and kept until the loader is disposed.
public sealed class TextureLoader : IDisposable
- Inheritance
-
TextureLoader
- Implements
Remarks
Texture.Load(device, stream) with its defaults does none of this: it loads every file as
linear, straight alpha, one mip. A colour texture comes out pale, a transparent edge gets a halo, a
tiled floor shimmers, and nothing warns. The pipeline does the rest at import, which is why the same
PNG looks right as an asset and wrong from code.
What the loader does not do is block compression: the texture stays eight bits per channel,
four to eight times the memory of the BC formats an asset would use. For that, or for mipmaps built
offline, ship a .dds: a path ending in .dds is loaded as it is, only its colour space
chosen by the role.
Constructors
TextureLoader(GraphicsDevice, string?)
Loads image files into textures the way Game Studio's content pipeline would have prepared them, by what the texture is for: a colour as sRGB and premultiplied, data as linear, a normal map linear with unit normals in every mip, each with a full mipmap chain. Each file is loaded once per role and kept until the loader is disposed.
public TextureLoader(GraphicsDevice device, string? root = null)
Parameters
deviceGraphicsDeviceThe device the textures are created on.
rootstringThe folder relative paths are resolved against; the executable's folder by default.
Remarks
Texture.Load(device, stream) with its defaults does none of this: it loads every file as
linear, straight alpha, one mip. A colour texture comes out pale, a transparent edge gets a halo, a
tiled floor shimmers, and nothing warns. The pipeline does the rest at import, which is why the same
PNG looks right as an asset and wrong from code.
What the loader does not do is block compression: the texture stays eight bits per channel,
four to eight times the memory of the BC formats an asset would use. For that, or for mipmaps built
offline, ship a .dds: a path ending in .dds is loaded as it is, only its colour space
chosen by the role.
Properties
Root
The folder relative paths are resolved against.
public string Root { get; }
Property Value
Methods
Color(string)
A colour texture: an albedo, an emissive, a sprite. sRGB, premultiplied, mipmapped.
public Texture Color(string path)
Parameters
Returns
Data(string)
A data texture: glossiness, metalness, occlusion, a mask, a height map. Linear, mipmapped.
public Texture Data(string path)
Parameters
Returns
Dispose()
Disposes every texture the loader has loaded; they must no longer be drawn.
public void Dispose()
FromImage(GraphicsDevice, Image, TextureLoadOptions)
Makes a texture from a decoded image, prepared for its role: for pixels edited after decoding and before upload. Not kept: the caller owns it.
public static Texture FromImage(GraphicsDevice device, Image image, TextureLoadOptions options)
Parameters
deviceGraphicsDeviceThe device to create it on.
imageImageA 2D image of eight bits per channel, RGBA or BGRA, as
Image.Loaddecodes a PNG or JPEG.optionsTextureLoadOptionsThe role and the steps to take.
Returns
Exceptions
- NotSupportedException
The image is not a single 2D image of eight-bit RGBA or BGRA.
Load(GraphicsDevice, Stream, TextureLoadOptions)
Loads a texture from a stream - an embedded resource, a download - prepared for its role. Not kept: the caller owns it.
public static Texture Load(GraphicsDevice device, Stream stream, TextureLoadOptions options)
Parameters
deviceGraphicsDeviceThe device to create it on.
streamStreamA PNG, JPEG, BMP, GIF or TIFF.
optionsTextureLoadOptionsThe role and the steps to take.
Returns
Load(string, TextureLoadOptions)
A texture with every option spelled out. Loaded once per path and options, kept until the loader is disposed.
public Texture Load(string path, TextureLoadOptions options)
Parameters
pathstringThe file, absolute or relative to Root.
optionsTextureLoadOptionsThe role and the steps to take.
Returns
NormalMap(string, bool)
A tangent-space normal map. Linear, mipmapped with unit normals.
public Texture NormalMap(string path, bool invertY = false)
Parameters
pathstringThe file, absolute or relative to Root.
invertYboolWhether to invert the green channel: on for a map whose green points up (Blender, Unity, glTF), off for the engine's own green-down convention.