-
Notifications
You must be signed in to change notification settings - Fork 20
/
TextureCanvas.cs
735 lines (661 loc) · 28.9 KB
/
TextureCanvas.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
using Stride.Graphics;
using Stride.Rendering;
using Stride.Rendering.Images;
namespace Stride.CommunityToolkit.Rendering.Utilities;
/// <summary>
/// A helper class for simple image processing
/// </summary>
public class TextureCanvas : IDisposable
{
private PixelFormat _pixelFormat;
private Texture? _primaryBuffer;
private Texture? _secondaryBuffer;
private Size2 _size;
private RenderContext RenderContext { get; }
private RenderDrawContext RenderDrawContext => RenderContext.GetThreadContext();
private GraphicsContext GraphicsContext => RenderDrawContext.GraphicsContext;
private GraphicsDevice GraphicsDevice => RenderContext.GraphicsDevice;
private CommandList CommandList => GraphicsContext.CommandList;
public TextureCanvas(RenderContext renderContext, Size2? size, PixelFormat pixelFormat = PixelFormat.R8G8B8A8_UNorm)
{
RenderContext = renderContext;
_size = size ?? new Size2(1024, 1024);
_pixelFormat = pixelFormat;
}
/// <summary>
/// Gets or sets the current pixel format
/// </summary>
/// <remarks>
/// Setting a different pixel format may cause a resample of the already drawn back buffer or call the setter before
/// you start drawing.<br />
/// You may consider using <see cref="Resample" /> if you also want to change the size in a single operation.
/// </remarks>
public PixelFormat PixelFormat
{
get => _pixelFormat;
set
{
_pixelFormat = value;
Resample(_size, value);
}
}
/// <summary>
/// Gets or sets the current texture size
/// </summary>
/// <remarks>
/// Setting a different size may cause a resample of the already drawn back buffer or call the setter before you start
/// drawing.<br />
/// You may consider using <see cref="Resample" /> if you also want to change the pixel format in a single operation.
/// </remarks>
public Size2 Size
{
get => _size;
set
{
_size = value;
Resample(value, _pixelFormat);
}
}
/// <summary>
/// Calculates the texture size in bytes
/// </summary>
public int ByteSize => EnsurePrimary().CalculatePixelDataCount<byte>();
public void Dispose()
{
Clear();
GC.SuppressFinalize(this);
}
public void DrawRect(
Rectangle destinationRect,
Stretch stretch = Stretch.Stretch,
Anchor anchor = Anchor.TopLeft,
SamplingPattern samplingPattern = SamplingPattern.Linear
)
{
CommandList.DrawQuad();
}
/// <summary>
/// Draws a texture to the drawing context
/// </summary>
/// <param name="sourceTexture">The source texture to draw</param>
/// <param name="colorMultiplier">The color multiplier. Default is <see cref="Stride.Core.Mathematics.Color.White"/></param>
/// <param name="stretch">The stretch mode</param>
/// <param name="anchor">The anchor mode</param>
/// <param name="samplingPattern">The sampling pattern</param>
public void DrawTexture(
Texture sourceTexture,
Color4? colorMultiplier = null,
Stretch stretch = Stretch.Stretch,
Anchor anchor = Anchor.TopLeft,
SamplingPattern samplingPattern = SamplingPattern.Linear
)
{
var sourceRect = new Rectangle(0, 0, sourceTexture.Width, sourceTexture.Height);
var destinationRect = new Rectangle(0, 0, _size.Width, _size.Height);
DrawTexture(sourceTexture, sourceRect, destinationRect, colorMultiplier, stretch, anchor, samplingPattern);
}
/// <summary>
/// Draws a texture to the <see cref="TextureCanvas" />
/// </summary>
/// <param name="sourceTexture">The source texture to draw</param>
/// <param name="destinationRect">The sub rectangle of the target texture in percentages</param>
/// <param name="colorMultiplier">The color multiplier. Default is <see cref="Stride.Core.Mathematics.Color.White"/></param>
/// <param name="stretch">The stretch mode</param>
/// <param name="anchor">The anchor mode</param>
/// <param name="samplingPattern">The sampling pattern</param>
public void DrawTexture(
Texture sourceTexture,
Rectangle destinationRect,
Color4? colorMultiplier = null,
Stretch stretch = Stretch.Stretch,
Anchor anchor = Anchor.TopLeft,
SamplingPattern samplingPattern = SamplingPattern.Linear
)
{
var sourceRect = new Rectangle(0, 0, sourceTexture.Width, sourceTexture.Height);
DrawTexture(sourceTexture, sourceRect, destinationRect, colorMultiplier, stretch, anchor, samplingPattern);
}
/// <summary>
/// Draws a texture to the <see cref="TextureCanvas" /> using a rectangle with percentage values
/// </summary>
/// <param name="sourceTexture">The source texture to draw</param>
/// <param name="relativeDestinationRect">The sub rectangle of the target texture in percentages</param>
/// <param name="colorMultiplier">The color multiplier. Default is <see cref="Stride.Core.Mathematics.Color.White"/></param>
/// <param name="stretch">The stretch mode</param>
/// <param name="anchor">The anchor mode</param>
/// <param name="samplingPattern">The sampling pattern</param>
public void DrawTexture(
Texture sourceTexture,
RectangleF relativeDestinationRect,
Color4? colorMultiplier = null,
Stretch stretch = Stretch.Stretch,
Anchor anchor = Anchor.TopLeft,
SamplingPattern samplingPattern = SamplingPattern.Linear
)
{
var sourceRect = new Rectangle(0, 0, sourceTexture.Width, sourceTexture.Height);
DrawTexture(sourceTexture, sourceRect, ToAbsoluteSize(_size, relativeDestinationRect), colorMultiplier, stretch, anchor, samplingPattern);
}
/// <summary>
/// Draws a texture to the <see cref="TextureCanvas" />
/// </summary>
/// <param name="sourceTexture">The source texture to draw</param>
/// <param name="sourceRect">The sub rectangle of the source texture to draw</param>
/// <param name="destinationRect">The sub rectangle of the target texture</param>
/// <param name="colorMultiplier">The color multiplier. Default is <see cref="Stride.Core.Mathematics.Color.White"/></param>
/// <param name="stretch">The stretch mode</param>
/// <param name="anchor">The anchor mode</param>
/// <param name="samplingPattern">The sampling pattern</param>
public void DrawTexture(Texture sourceTexture, Rectangle sourceRect, Rectangle destinationRect, Color4? colorMultiplier = null,
Stretch stretch = Stretch.Stretch, Anchor anchor = Anchor.TopLeft,
SamplingPattern samplingPattern = SamplingPattern.Linear)
{
var bufferTexture = EnsurePrimary();
var (viewport, scissor) = GetViewportAndScissor(sourceTexture, sourceRect, destinationRect, stretch, anchor);
using var scaler = new ImageScaler(samplingPattern);
scaler.RasterizerState = new RasterizerStateDescription(CullMode.Back) { ScissorTestEnable = true };
scaler.Color = colorMultiplier.GetValueOrDefault(Color4.White);
scaler.SetViewport(viewport);
scaler.SetScissorRectangle(scissor);
scaler.SetInput(sourceTexture);
scaler.SetOutput(bufferTexture);
scaler.Draw(RenderDrawContext);
}
/// <summary>
/// Draws a texture to the <see cref="TextureCanvas" /> using rectangles with percentage values
/// </summary>
/// <param name="sourceTexture">The source texture to draw</param>
/// <param name="relativeSourceRect">The sub rectangle of the source texture to draw in percentages</param>
/// <param name="relativeDestinationRect">The sub rectangle of the target texture in percentages</param>
/// <param name="colorMultiplier">The color multiplier. Default is <see cref="Stride.Core.Mathematics.Color.White"/></param>
/// <param name="stretch">The stretch mode</param>
/// <param name="anchor">The anchor mode</param>
/// <param name="samplingPattern">The sampling pattern</param>
public void DrawTexture(
Texture sourceTexture,
RectangleF relativeSourceRect,
RectangleF relativeDestinationRect,
Color4? colorMultiplier = null,
Stretch stretch = Stretch.Stretch,
Anchor anchor = Anchor.TopLeft,
SamplingPattern samplingPattern = SamplingPattern.Linear
)
{
var sourceRect = ToAbsoluteSize(sourceTexture.Size, relativeSourceRect);
var destinationRect = ToAbsoluteSize(_size, relativeSourceRect);
DrawTexture(sourceTexture, sourceRect, destinationRect, colorMultiplier, stretch, anchor, samplingPattern);
}
/// <summary>
/// Replaces the current <see cref="TextureCanvas" /> with a texture loaded from the local file system
/// </summary>
/// <param name="path">The file path</param>
/// <remarks>
/// This method also replaces the current size and pixel format. Use <see cref="Resample" /> to change it
/// afterwards.
/// </remarks>
public void Load(string path)
{
using var fileStream = File.OpenRead(path);
Load(fileStream);
}
/// <summary>
/// Replaces the current <see cref="TextureCanvas" /> with a texture loaded from a <see cref="T:byte[]" />
/// </summary>
/// <param name="data">The <see cref="T:byte[]" /> containing file data</param>
/// <remarks>
/// This method also replaces the current size and pixel format. Use <see cref="Resample" /> to change it
/// afterwards.
/// </remarks>
public void Load(byte[] data)
{
using var stream = new MemoryStream(data);
stream.Seek(0, SeekOrigin.Begin);
Load(stream);
}
/// <summary>
/// Replaces the current <see cref="TextureCanvas" /> with a texture loaded from a <see cref="Stream" />
/// </summary>
/// <param name="stream">The <see cref="Stream" /> containing file data</param>
/// <remarks>
/// This method also replaces the current size and pixel format. Use <see cref="Resample" /> to change it
/// afterwards.
/// </remarks>
public void Load(Stream stream)
{
_primaryBuffer?.Dispose();
_primaryBuffer = Texture.Load(GraphicsDevice, stream, TextureFlags.ShaderResource | TextureFlags.RenderTarget);
_size = new Size2(_primaryBuffer.Size.Width, _primaryBuffer.Size.Height);
_pixelFormat = _primaryBuffer.Format;
}
/// <summary>
/// Stores the current texture to a file in the local file system.
/// </summary>
/// <param name="path">The target file path</param>
/// <param name="fileType">The file type to write or null for automatic inference based on the file extension</param>
public void Store(string path, ImageFileType? fileType = null)
{
using var stream = File.Open(path, FileMode.Create);
fileType ??= Path.GetExtension(path) switch
{
".stride" => ImageFileType.Stride,
".dds" => ImageFileType.Dds,
".png" => ImageFileType.Png,
".gif" => ImageFileType.Gif,
".jpg" or ".jpeg" => ImageFileType.Jpg,
".bmp" => ImageFileType.Bmp,
".tiff" => ImageFileType.Tiff,
".wmp" => ImageFileType.Wmp,
".tga" => ImageFileType.Tga,
_ => throw new NotSupportedException()
};
Store(stream, fileType.Value);
}
/// <summary>
/// Stores the current texture to a <see cref="Stream" /> in the selected file format
/// </summary>
/// <param name="stream">The target stream</param>
/// <param name="fileType">The file type to write</param>
public void Store(Stream stream, ImageFileType fileType)
{
var buffer = EnsurePrimary();
buffer.Save(CommandList, stream, fileType);
}
/// <summary>
/// Replaces the current <see cref="TextureCanvas" /> with a texture loaded from a <see cref="T:byte[]" /> containing
/// pixel data
/// </summary>
/// <param name="data">The <see cref="T:byte[]" /> containing pixel data</param>
/// <param name="width">The width of the new texture</param>
/// <param name="height">The height of the new texture</param>
/// <param name="pixelFormat">The pixel format of the new texture</param>
/// <remarks>
/// This method also replaces the current size and pixel format. Use <see cref="Resample" /> to change it
/// afterwards.
/// </remarks>
public void SetData(byte[] data, int width, int height, PixelFormat pixelFormat)
{
_primaryBuffer?.Dispose();
_primaryBuffer = Texture.New2D(GraphicsDevice, width, height, pixelFormat, data,
TextureFlags.ShaderResource | TextureFlags.RenderTarget);
_size = new Size2(_primaryBuffer.Size.Width, _primaryBuffer.Size.Height);
_pixelFormat = _primaryBuffer.Format;
}
/// <summary>
/// Copies the current texture data to a byte array
/// </summary>
/// <param name="data">The data array to store the data</param>
public void GetData(byte[] data)
{
var buffer = EnsurePrimary();
buffer.GetData(CommandList, data);
}
/// <summary>
/// Copies the current texture data to a byte array
/// </summary>
public byte[] GetData()
{
var buffer = EnsurePrimary();
return buffer.GetData<byte>(CommandList);
}
/// <summary>
/// Copies the current texture data to a new texture
/// </summary>
public Texture ToTexture(TextureFlags flags = TextureFlags.ShaderResource)
{
return Texture.New2D(GraphicsDevice, _size.Width, _size.Height, PixelFormat, GetData(), flags);
}
/// <summary>
/// Applies a <see cref="Stride.Rendering.Images.BrightFilter" /> effect.
/// </summary>
/// <param name="threshold">The threshold relative to the white point</param>
/// <param name="steepness">the smooth-step steepness for bright pass filtering.</param>
/// <param name="colorMultiplier">
/// Modulates bright areas with the provided color. It affects the color of sub-sequent bloom,
/// light-streak effects
/// </param>
public void BrightFilter(float threshold = 0.2f, float steepness = 1.0f, Color3? colorMultiplier = null)
{
using var effect = new BrightFilter();
effect.Steepness = steepness;
effect.Threshold = threshold;
effect.Color = colorMultiplier.GetValueOrDefault(new Color3(1f));
Apply(effect);
}
/// <summary>
/// Applies a <see cref="Stride.Rendering.Images.GaussianBlur" /> effect.
/// </summary>
/// <param name="radius">The radius.</param>
/// <param name="sigmaRatio">
/// The 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.
/// </param>
public void GaussianBlur(int radius = 4, float sigmaRatio = 2.0f)
{
using var effect = new GaussianBlur();
effect.Radius = radius;
effect.SigmaRatio = sigmaRatio;
Apply(effect);
}
/// <summary>
/// Applies a <see cref="Stride.Rendering.Images.CoCMapBlur" /> effect.
/// </summary>
/// <param name="radius">The radius.</param>
public void CoCMapBlur(int radius = 4)
{
using var effect = new CoCMapBlur();
effect.Radius = radius;
Apply(effect);
}
/// <summary>
/// Applies a <see cref="Stride.Rendering.Images.ColorTransformGroup" /> effect.
/// </summary>
/// <param name="transforms">The color transforms to apply</param>
/// <param name="preTransforms">The color pre-transforms to apply</param>
/// <param name="postTransforms">The color post-transforms to apply</param>
public void Transform(
IEnumerable<ColorTransform>? transforms = null,
IEnumerable<ColorTransform>? preTransforms = null,
IEnumerable<ColorTransform>? postTransforms = null
)
{
if (transforms is null && preTransforms is null && postTransforms is null) return;
using var effect = new ColorTransformGroup();
if (transforms is not null)
foreach (var transform in transforms)
effect.Transforms.Add(transform);
if (preTransforms is not null)
foreach (var transform in preTransforms)
effect.PreTransforms.Add(transform);
if (postTransforms is not null)
foreach (var transform in postTransforms)
effect.PostTransforms.Add(transform);
Apply(effect);
}
/// <summary>
/// Applies a <see cref="Stride.Rendering.Images.ColorCombiner" /> effect.
/// </summary>
/// <param name="textures">The textures combine (use null to use the current drawing buffer as a texture input)</param>
/// <param name="factors">The factors used to multiply the colors.</param>
/// <param name="colorMultipliers">The color multiplier of each texture. Default is <see cref="Stride.Core.Mathematics.Color.White"/></param>
/// <remarks>Null, Empty arrays or array entries with null as a value will be replaced by the current drawing buffer</remarks>
public void Combine(Texture?[]? textures = null, float[]? factors = null, Color3[]? colorMultipliers = null)
{
using var effect = new ColorCombiner();
if (factors is { Length: > 0 }) Array.Copy(factors, 0, effect.Factors, 0, factors.Length);
if (colorMultipliers is { Length: > 0 }) Array.Copy(colorMultipliers, 0, effect.ModulateRGB, 0, colorMultipliers.Length);
Apply(effect, textures);
}
/// <summary>
/// Applies a color multiply effect.
/// </summary>
/// <param name="colorMultiplier">The color multiplier</param>
public void Colorize(Color4 colorMultiplier)
{
using var effect = new ImageScaler();
effect.Color = colorMultiplier;
Apply(effect);
}
/// <summary>
/// Applies a grey scale effect by copying the red channel to blue and green and optionally also recolors the resulting image.
/// </summary>
/// <param name="colorMultiplier">The color multiplier. Default is <see cref="Stride.Core.Mathematics.Color.White"/></param>
public void Recolorize(Color4? colorMultiplier = null)
{
using var effect = new ImageScaler();
effect.IsOnlyChannelRed = true;
effect.Color = colorMultiplier.GetValueOrDefault(Color4.White);
Apply(effect);
}
/// <summary>
/// Applies an image effect to the buffer texture
/// </summary>
/// <param name="effect">The image effect to apply</param>
/// <param name="inputs">The input textures</param>
/// <remarks>
/// Null, Empty arrays or array entries with null as a value will be replaced by the current drawing buffer<br />
/// The output will always be replaced by the <see cref="TextureCanvas" /> as a single output.
/// </remarks>
public void Apply(ImageEffect effect, params Texture?[]? inputs)
{
// Get or create buffers
var primaryBuffer = EnsurePrimary();
var secondaryBuffer = EnsureSecondary();
// Apply the effect
if (inputs is not { Length: > 0 })
effect.SetInput(primaryBuffer);
else
for (var i = 0; i < inputs.Length; i++)
effect.SetInput(i, inputs[i] ?? primaryBuffer);
effect.SetOutput(secondaryBuffer);
effect.Draw(RenderDrawContext);
effect.Reset();
// Switch primary and secondary buffer
_primaryBuffer = secondaryBuffer;
_secondaryBuffer = primaryBuffer;
}
/// <summary>
/// Resamples the texture in a different size or pixel format
/// </summary>
/// <param name="size">The new size</param>
/// <param name="pixelFormat">The new pixel format</param>
/// <param name="samplingPattern">The sampling pattern</param>
public void Resample(Size2 size, PixelFormat pixelFormat, SamplingPattern samplingPattern = SamplingPattern.Linear)
{
// Nothing changed so we can safely skip this step
if (_primaryBuffer is null || (size == _size && pixelFormat == _pixelFormat)) return;
// Create a new render target with the new settings and dispose the previous buffer at the end
_secondaryBuffer?.Dispose();
_secondaryBuffer = CreateRenderTarget(RenderContext, size, pixelFormat);
try
{
// We use the image scaler effect to resample the image to the new size and format
using var scaler = new ImageScaler(SamplingPattern.Linear);
scaler.SetInput(_primaryBuffer);
scaler.SetOutput(_secondaryBuffer);
// Finally draw the current buffer to the new render texture
scaler.Draw(RenderDrawContext);
}
finally
{
_primaryBuffer?.Dispose();
_primaryBuffer = _secondaryBuffer;
_secondaryBuffer = null;
}
}
/// <summary>
/// Clears the current drawing context and releases internal texture buffers
/// </summary>
public void Clear()
{
_primaryBuffer?.Dispose();
_primaryBuffer = null;
_secondaryBuffer?.Dispose();
_secondaryBuffer = null;
}
/// <summary>
/// Ensures the primary buffer is allocated
/// </summary>
private Texture EnsurePrimary() => _primaryBuffer ??= CreateRenderTarget(RenderContext, _size, _pixelFormat);
/// <summary>
/// Ensures the secondary buffer is allocated (for effects)
/// </summary>
private Texture EnsureSecondary() => _secondaryBuffer ??= CreateRenderTarget(RenderContext, _size, _pixelFormat);
/// <summary>
/// Creates a new render texture based on size and pixel format
/// </summary>
private static Texture CreateRenderTarget(RenderContext renderContext, Size2 size, PixelFormat pixelFormat) =>
Texture.New2D(renderContext.GraphicsDevice, size.Width, size.Height, pixelFormat,
TextureFlags.ShaderResource | TextureFlags.RenderTarget);
/// <summary>
/// Calculates the target viewport and scissor for drawing textures
/// </summary>
private static (Viewport viewport, Rectangle scissor) GetViewportAndScissor(Texture sourceTexture,
Rectangle sourceRect, Rectangle destinationRect, Stretch stretch, Anchor anchor)
{
float widthScale, heightScale;
switch (stretch)
{
case Stretch.None:
widthScale = 1;
heightScale = 1;
break;
case Stretch.Stretch:
widthScale = destinationRect.Width / (float)sourceRect.Width;
heightScale = destinationRect.Height / (float)sourceRect.Height;
break;
case Stretch.Contain:
widthScale = destinationRect.Width / (float)sourceRect.Width;
heightScale = destinationRect.Height / (float)sourceRect.Height;
widthScale = heightScale = Math.Min(widthScale, heightScale);
break;
case Stretch.Cover:
widthScale = destinationRect.Width / (float)sourceRect.Width;
heightScale = destinationRect.Height / (float)sourceRect.Height;
widthScale = heightScale = Math.Max(widthScale, heightScale);
break;
default:
throw new ArgumentOutOfRangeException(nameof(stretch), stretch, null);
}
var finalWidth = (int)(sourceRect.Width * widthScale);
var finalHeight = (int)(sourceRect.Height * heightScale);
int alignmentX;
switch (anchor)
{
case Anchor.Left:
case Anchor.TopLeft:
case Anchor.BottomLeft:
alignmentX = 0;
break;
case Anchor.Top:
case Anchor.Center:
case Anchor.Bottom:
alignmentX = (destinationRect.Width - finalWidth) / 2;
break;
case Anchor.TopRight:
case Anchor.Right:
case Anchor.BottomRight:
alignmentX = destinationRect.Width - finalWidth;
break;
default:
throw new ArgumentOutOfRangeException(nameof(anchor), anchor, null);
}
int alignmentY;
switch (anchor)
{
case Anchor.TopLeft:
case Anchor.Top:
case Anchor.TopRight:
alignmentY = 0;
break;
case Anchor.Left:
case Anchor.Center:
case Anchor.Right:
alignmentY = (destinationRect.Height - finalHeight) / 2;
break;
case Anchor.BottomLeft:
case Anchor.Bottom:
case Anchor.BottomRight:
alignmentY = destinationRect.Height - finalHeight;
break;
default:
throw new ArgumentOutOfRangeException(nameof(anchor), anchor, null);
}
var finalX = destinationRect.X + alignmentX;
var finalY = destinationRect.Y + alignmentY;
var scissorX = Math.Max(finalX, destinationRect.X);
var scissorY = Math.Max(finalY, destinationRect.Y);
var scissorWidth = Math.Min(finalWidth, destinationRect.Width);
var scissorHeight = Math.Min(finalHeight, destinationRect.Height);
var viewportX = finalX - sourceRect.X * widthScale;
var viewportY = finalY - sourceRect.Y * heightScale;
var viewportWidth = (int)(sourceTexture.Width * widthScale);
var viewportHeight = (int)(sourceTexture.Height * heightScale);
var viewport = new Viewport(viewportX, viewportY, viewportWidth, viewportHeight);
var scissor = new Rectangle(scissorX, scissorY, scissorWidth, scissorHeight);
return (viewport, scissor);
}
private static Rectangle ToAbsoluteSize(Size3 size, RectangleF sourceRect)
{
return new Rectangle(
(int)(size.Width * sourceRect.X),
(int)(size.Width * sourceRect.Y),
(int)(size.Width * sourceRect.Width),
(int)(size.Width * sourceRect.Height)
);
}
private static Rectangle ToAbsoluteSize(Size2 size, RectangleF sourceRect)
{
return new Rectangle(
(int)(size.Width * sourceRect.X),
(int)(size.Width * sourceRect.Y),
(int)(size.Width * sourceRect.Width),
(int)(size.Width * sourceRect.Height)
);
}
/// <summary>
/// Stretch modes when textures to a <see cref="TextureCanvas" />
/// </summary>
public enum Stretch
{
/// <summary>
/// The texture preserves its original size. Overflowing content is cropped.
/// </summary>
None,
/// <summary>
/// The texture is resized to fill the destination dimensions. The aspect ratio is not preserved.
/// </summary>
Stretch,
/// <summary>
/// The texture is resized to fit in the destination dimensions while it preserves its native aspect ratio.
/// </summary>
Contain,
/// <summary>
/// The texture is resized to fill the destination dimensions while it preserves its native aspect ratio. If the aspect
/// ratio of the destination rectangle differs from the source, the source texture is clipped to fit in the destination
/// dimensions.
/// </summary>
Cover
}
/// <summary>
/// Anchor positions when drawing to a <see cref="TextureCanvas" />
/// </summary>
public enum Anchor
{
/// <summary>
/// Adjust the position so the top-left corner of the source and target rect are aligned.
/// </summary>
TopLeft,
/// <summary>
/// Adjust the position so the top-edge center of the source and target rect are aligned.
/// </summary>
Top,
/// <summary>
/// Adjust the position so the top-right corner of the source and target rect are aligned.
/// </summary>
TopRight,
/// <summary>
/// Adjust the position so the left-edge center of the source and target rect are aligned.
/// </summary>
Left,
/// <summary>
/// Adjust the position so the center of the source and target rect are aligned.
/// </summary>
Center,
/// <summary>
/// Adjust the position so the right-edge center of the source and target rect are aligned.
/// </summary>
Right,
/// <summary>
/// Adjust the position so the bottom-left corner of the source and target rect are aligned.
/// </summary>
BottomLeft,
/// <summary>
/// Adjust the position so the bottom-edge center of the source and target rect are aligned.
/// </summary>
Bottom,
/// <summary>
/// Adjust the position so the bottom-right corner of the source and target rect are aligned.
/// </summary>
BottomRight
}
}