MoonWorks-docs/content/Graphics/Resources/Texture.md

67 lines
2.9 KiB
Markdown
Raw Permalink Normal View History

2021-01-26 02:22:59 +00:00
---
title: "Texture"
date: 2021-01-25T14:59:49-08:00
weight: 2
---
Textures are containers for pixel data. They always have a width, height, and color format. The most simple and common kind of texture is just a 2D texture. But textures can also have depth (3D textures), multiple layers (cubemaps), multiple resolution levels (mipmap levels), or a multisample count. Describing the use cases for all of these is beyond the scope of this tutorial, but they should come up as you learn how to execute certain rendering techniques.
To create a new texture resource, you can set up a `TextureCreateInfo` struct:
```cs
var textureCreateInfo = new TextureCreateInfo
{
Width = 128,
Height = 128,
Depth = 1,
2021-01-27 03:11:37 +00:00
Format = TextureFormat.R8G8B8A8,
2021-01-26 02:22:59 +00:00
IsCube = false,
LevelCount = 1,
SampleCount = SampleCount.One,
2021-01-26 04:59:27 +00:00
UsageFlags = TextureUsageFlags.Sampler
2021-01-26 02:22:59 +00:00
};
var myTexture = new Texture(GraphicsDevice, textureCreateInfo);
```
This sets up 128x128 2D Texture that uses 32 bits per pixel: 8 bits each for red, green, blue, and alpha.
We also indicate that we intend to use the texture for sampling.
2022-01-13 20:32:42 +00:00
To put pixels in the texture, we use the `SetTextureData` method on a command buffer.
2021-01-26 02:22:59 +00:00
```cs
var pixels = new Color[width * height];
2022-01-13 20:32:42 +00:00
// put data in the pixel array here!
myCommandBuffer.SetTextureData(myTexture, pixels);
2021-01-26 02:22:59 +00:00
```
2022-01-13 23:04:18 +00:00
We'll get to command buffers later, just know that you must upload data via command buffer so that you can safely use multithreading. This can speed up load times greatly.
2022-01-13 20:32:42 +00:00
2021-01-26 02:22:59 +00:00
Of course, having to fill in all this data every time is kind of cumbersome, so we have some shortcut methods for common texture usages.
```cs
2021-01-27 03:11:37 +00:00
var my2DTexture = Texture.CreateTexture2D(GraphicsDevice, 128, 128, TextureFormat.R8G8B8A8, TextureUsageFlags.Sampler);
var my3DTexture = Texture.CreateTexture3D(GraphicsDevice, 128, 128, TextureFormat.R8G8B8A8, TextureUsageFlags.Sampler);
var myCubeTexture = Texture.CreateTextureCube(GraphicsDevice, 128, TextureFormat.R8G8B8A8, TextureUsageFlags.Sampler);
2021-01-26 02:22:59 +00:00
```
There is also a built-in PNG loader that creates a 2D R8G8B8A8 texture and fills it with pixel data.
```cs
2022-01-13 20:32:42 +00:00
var myPNGTexture = Texture.LoadPNG(GraphicsDevice, myCommandBuffer, "coldsteel_the_hedgehog.png");
2021-01-26 02:22:59 +00:00
```
2021-01-27 03:11:37 +00:00
One last concept we should discuss before moving on is texture slicing.
Texture slices are a very commonly used concept in MoonWorks. You don't have to do texture operations on an entire texture - you can instead use a subsection of the texture for whatever your operation is.
```cs
2022-03-04 18:34:23 +00:00
var myTexture = Texture.LoadPNG(GraphicsDevice, myCommandBuffer, "myTexture.png");
2021-01-27 03:11:37 +00:00
var mySliceRect = new Rect(0, 0, 16, 16);
var myTextureSlice = new TextureSlice(myTexture, mySliceRect);
```
This code produces a texture slice which is a 16x16 square in the top left corner of the image. You can create slices of 3D textures and cube textures as well! We'll talk more about where to use texture slices later.