Class TextureCanvas
- Namespace
- Stride.CommunityToolkit.Rendering.Utilities
- Assembly
- Stride.CommunityToolkit.dll
Provides functionality for drawing and manipulating textures on a canvas. This class supports various operations such as drawing rectangles, drawing textures, applying filters, transforming colors, and resampling textures. It is designed to work with the Stride graphics framework and provides methods for loading and storing texture data.
public class TextureCanvas : IDisposable
- Inheritance
-
TextureCanvas
- Implements
Constructors
TextureCanvas(RenderContext, Size2?, PixelFormat)
Initializes a new instance of the TextureCanvas class.
public TextureCanvas(RenderContext renderContext, Size2? size, PixelFormat pixelFormat = PixelFormat.R8G8B8A8_UNorm)
Parameters
renderContextRenderContextsizeSize2?pixelFormatPixelFormat
Properties
ByteSize
Calculates the texture size in bytes
public int ByteSize { get; }
Property Value
PixelFormat
Gets or sets the current pixel format
public PixelFormat PixelFormat { get; set; }
Property Value
Remarks
Setting a different pixel format may cause a resample of the already drawn back buffer or call the setter before
you start drawing.
You may consider using Resample(Size2, PixelFormat, SamplingPattern) if you also want to change the size in a single operation.
Size
Gets or sets the current texture size
public Size2 Size { get; set; }
Property Value
Remarks
Setting a different size may cause a resample of the already drawn back buffer or call the setter before you start
drawing.
You may consider using Resample(Size2, PixelFormat, SamplingPattern) if you also want to change the pixel format in a single operation.
Methods
Apply(ImageEffect, params Texture?[]?)
Applies an image effect to the buffer texture
public void Apply(ImageEffect effect, params Texture?[]? inputs)
Parameters
effectImageEffectThe image effect to apply
inputsTexture[]The input textures
Remarks
Null, Empty arrays or array entries with null as a value will be replaced by the current drawing buffer
The output will always be replaced by the TextureCanvas as a single output.
BrightFilter(float, float, Color3?)
Applies a BrightFilter effect.
public void BrightFilter(float threshold = 0.2, float steepness = 1, Color3? colorMultiplier = null)
Parameters
thresholdfloatThe threshold relative to the white point
steepnessfloatthe smooth-step steepness for bright pass filtering.
colorMultiplierColor3?Modulates bright areas with the provided color. It affects the color of sub-sequent bloom, light-streak effects
Clear()
Clears the current drawing context and releases internal texture buffers
public void Clear()
CoCMapBlur(int)
Applies a CoCMapBlur effect.
public void CoCMapBlur(int radius = 4)
Parameters
radiusintThe radius.
Colorize(Color4)
Applies a color multiply effect.
public void Colorize(Color4 colorMultiplier)
Parameters
colorMultiplierColor4The color multiplier
Combine(Texture?[]?, float[]?, Color3[]?)
Applies a ColorCombiner effect.
public void Combine(Texture?[]? textures = null, float[]? factors = null, Color3[]? colorMultipliers = null)
Parameters
texturesTexture[]The textures combine (use null to use the current drawing buffer as a texture input)
factorsfloat[]The factors used to multiply the colors.
colorMultipliersColor3[]The color multiplier of each texture. Default is White
Remarks
Null, Empty arrays or array entries with null as a value will be replaced by the current drawing buffer
Dispose()
Releases all resources used by the TextureCanvas object.
public void Dispose()
DrawRect(Rectangle, Stretch, Anchor, SamplingPattern)
Draws a rectangle on the canvas.
public void DrawRect(Rectangle destinationRect, TextureCanvas.Stretch stretch = Stretch.Stretch, TextureCanvas.Anchor anchor = Anchor.TopLeft, SamplingPattern samplingPattern = SamplingPattern.Linear)
Parameters
destinationRectRectangleThe destination rectangle where the rectangle will be drawn.
stretchTextureCanvas.StretchThe stretch mode to apply to the rectangle.
anchorTextureCanvas.AnchorThe anchor point for the rectangle.
samplingPatternSamplingPatternThe sampling pattern to use for drawing.
DrawTexture(Texture, Rectangle, Rectangle, Color4?, Stretch, Anchor, SamplingPattern)
Draws a texture to the TextureCanvas
public void DrawTexture(Texture sourceTexture, Rectangle sourceRect, Rectangle destinationRect, Color4? colorMultiplier = null, TextureCanvas.Stretch stretch = Stretch.Stretch, TextureCanvas.Anchor anchor = Anchor.TopLeft, SamplingPattern samplingPattern = SamplingPattern.Linear)
Parameters
sourceTextureTextureThe source texture to draw
sourceRectRectangleThe sub rectangle of the source texture to draw
destinationRectRectangleThe sub rectangle of the target texture
colorMultiplierColor4?The color multiplier. Default is White
stretchTextureCanvas.StretchThe stretch mode
anchorTextureCanvas.AnchorThe anchor mode
samplingPatternSamplingPatternThe sampling pattern
DrawTexture(Texture, Rectangle, Color4?, Stretch, Anchor, SamplingPattern)
Draws a texture to the TextureCanvas
public void DrawTexture(Texture sourceTexture, Rectangle destinationRect, Color4? colorMultiplier = null, TextureCanvas.Stretch stretch = Stretch.Stretch, TextureCanvas.Anchor anchor = Anchor.TopLeft, SamplingPattern samplingPattern = SamplingPattern.Linear)
Parameters
sourceTextureTextureThe source texture to draw
destinationRectRectangleThe sub rectangle of the target texture in percentages
colorMultiplierColor4?The color multiplier. Default is White
stretchTextureCanvas.StretchThe stretch mode
anchorTextureCanvas.AnchorThe anchor mode
samplingPatternSamplingPatternThe sampling pattern
DrawTexture(Texture, RectangleF, RectangleF, Color4?, Stretch, Anchor, SamplingPattern)
Draws a texture to the TextureCanvas using rectangles with percentage values
public void DrawTexture(Texture sourceTexture, RectangleF relativeSourceRect, RectangleF relativeDestinationRect, Color4? colorMultiplier = null, TextureCanvas.Stretch stretch = Stretch.Stretch, TextureCanvas.Anchor anchor = Anchor.TopLeft, SamplingPattern samplingPattern = SamplingPattern.Linear)
Parameters
sourceTextureTextureThe source texture to draw
relativeSourceRectRectangleFThe sub rectangle of the source texture to draw in percentages
relativeDestinationRectRectangleFThe sub rectangle of the target texture in percentages
colorMultiplierColor4?The color multiplier. Default is White
stretchTextureCanvas.StretchThe stretch mode
anchorTextureCanvas.AnchorThe anchor mode
samplingPatternSamplingPatternThe sampling pattern
DrawTexture(Texture, RectangleF, Color4?, Stretch, Anchor, SamplingPattern)
Draws a texture to the TextureCanvas using a rectangle with percentage values
public void DrawTexture(Texture sourceTexture, RectangleF relativeDestinationRect, Color4? colorMultiplier = null, TextureCanvas.Stretch stretch = Stretch.Stretch, TextureCanvas.Anchor anchor = Anchor.TopLeft, SamplingPattern samplingPattern = SamplingPattern.Linear)
Parameters
sourceTextureTextureThe source texture to draw
relativeDestinationRectRectangleFThe sub rectangle of the target texture in percentages
colorMultiplierColor4?The color multiplier. Default is White
stretchTextureCanvas.StretchThe stretch mode
anchorTextureCanvas.AnchorThe anchor mode
samplingPatternSamplingPatternThe sampling pattern
DrawTexture(Texture, Color4?, Stretch, Anchor, SamplingPattern)
Draws a texture to the drawing context
public void DrawTexture(Texture sourceTexture, Color4? colorMultiplier = null, TextureCanvas.Stretch stretch = Stretch.Stretch, TextureCanvas.Anchor anchor = Anchor.TopLeft, SamplingPattern samplingPattern = SamplingPattern.Linear)
Parameters
sourceTextureTextureThe source texture to draw
colorMultiplierColor4?The color multiplier. Default is White
stretchTextureCanvas.StretchThe stretch mode
anchorTextureCanvas.AnchorThe anchor mode
samplingPatternSamplingPatternThe sampling pattern
GaussianBlur(int, float)
Applies a GaussianBlur effect.
public void GaussianBlur(int radius = 4, float sigmaRatio = 2)
Parameters
radiusintThe radius.
sigmaRatiofloatThe sigma ratio. The sigma ratio is used to calculate the sigma based on the radius: The actual formula is sigma = radius / SigmaRatio. The default value is 2.0f.
GetData()
Copies the current texture data to a byte array
public byte[] GetData()
Returns
- byte[]
GetData(byte[])
Copies the current texture data to a byte array
public void GetData(byte[] data)
Parameters
databyte[]The data array to store the data
Load(byte[])
Replaces the current TextureCanvas with a texture loaded from a byte[]
public void Load(byte[] data)
Parameters
databyte[]The byte[] containing file data
Remarks
This method also replaces the current size and pixel format. Use Resample(Size2, PixelFormat, SamplingPattern) to change it afterwards.
Load(Stream)
Replaces the current TextureCanvas with a texture loaded from a Stream
public void Load(Stream stream)
Parameters
Remarks
This method also replaces the current size and pixel format. Use Resample(Size2, PixelFormat, SamplingPattern) to change it afterwards.
Load(string)
Replaces the current TextureCanvas with a texture loaded from the local file system
public void Load(string path)
Parameters
pathstringThe file path
Remarks
This method also replaces the current size and pixel format. Use Resample(Size2, PixelFormat, SamplingPattern) to change it afterwards.
Recolorize(Color4?)
Applies a grey scale effect by copying the red channel to blue and green and optionally also recolors the resulting image.
public void Recolorize(Color4? colorMultiplier = null)
Parameters
Resample(Size2, PixelFormat, SamplingPattern)
Resamples the texture in a different size or pixel format
public void Resample(Size2 size, PixelFormat pixelFormat, SamplingPattern samplingPattern = SamplingPattern.Linear)
Parameters
sizeSize2The new size
pixelFormatPixelFormatThe new pixel format
samplingPatternSamplingPatternThe sampling pattern
SetData(byte[], int, int, PixelFormat)
Replaces the current TextureCanvas with a texture loaded from a byte[] containing pixel data
public void SetData(byte[] data, int width, int height, PixelFormat pixelFormat)
Parameters
databyte[]The byte[] containing pixel data
widthintThe width of the new texture
heightintThe height of the new texture
pixelFormatPixelFormatThe pixel format of the new texture
Remarks
This method also replaces the current size and pixel format. Use Resample(Size2, PixelFormat, SamplingPattern) to change it afterwards.
Store(Stream, ImageFileType)
Stores the current texture to a Stream in the selected file format
public void Store(Stream stream, ImageFileType fileType)
Parameters
streamStreamThe target stream
fileTypeImageFileTypeThe file type to write
Store(string, ImageFileType?)
Stores the current texture to a file in the local file system.
public void Store(string path, ImageFileType? fileType = null)
Parameters
pathstringThe target file path
fileTypeImageFileType?The file type to write or null for automatic inference based on the file extension
ToTexture(TextureFlags)
Copies the current texture data to a new texture
public Texture ToTexture(TextureFlags flags = TextureFlags.ShaderResource)
Parameters
flagsTextureFlags
Returns
Transform(IEnumerable<ColorTransform>?, IEnumerable<ColorTransform>?, IEnumerable<ColorTransform>?)
Applies a ColorTransformGroup effect.
public void Transform(IEnumerable<ColorTransform>? transforms = null, IEnumerable<ColorTransform>? preTransforms = null, IEnumerable<ColorTransform>? postTransforms = null)
Parameters
transformsIEnumerable<ColorTransform>The color transforms to apply
preTransformsIEnumerable<ColorTransform>The color pre-transforms to apply
postTransformsIEnumerable<ColorTransform>The color post-transforms to apply