| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using Unity.Mathematics; |
| | 6 | |
|
| | 7 | | namespace GDX.Collections.Generic |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Array2D Based Extension Methods |
| | 11 | | /// </summary> |
| | 12 | | [VisualScriptingCompatible(2)] |
| | 13 | | public static class Array2DExtensions |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Generate an array scaled by bilinear interpolation. |
| | 17 | | /// </summary> |
| | 18 | | /// <remarks>Works with <see cref="float" /> values.</remarks> |
| | 19 | | /// <param name="inputArray">The existing <see cref="Array2D{T}" /> to scale.</param> |
| | 20 | | /// <param name="scaleFactor">The multiple to scale by.</param> |
| | 21 | | public static Array2D<float> Scale(ref this Array2D<float> inputArray, int scaleFactor = 2) |
| 1 | 22 | | { |
| 1 | 23 | | int originalRowsMinusOne = inputArray.RowCount - 1; |
| 1 | 24 | | int originalColsMinusOne = inputArray.ColumnCount - 1; |
| | 25 | |
|
| 1 | 26 | | int newRows = inputArray.RowCount * scaleFactor; |
| 1 | 27 | | int newCols = inputArray.ColumnCount * scaleFactor; |
| | 28 | |
|
| 1 | 29 | | Array2D<float> returnArray = new Array2D<float>(inputArray.RowCount * scaleFactor, |
| | 30 | | inputArray.ColumnCount * scaleFactor); |
| | 31 | |
|
| 10 | 32 | | for (int x = 0; x < newCols; x++) |
| 40 | 33 | | for (int y = 0; y < newRows; y++) |
| 16 | 34 | | { |
| 16 | 35 | | float gx = (float)x / newCols * originalColsMinusOne; |
| 16 | 36 | | float gy = (float)y / newCols * originalRowsMinusOne; |
| 16 | 37 | | int gxi = (int)gx; |
| 16 | 38 | | int gyi = (int)gy; |
| | 39 | |
|
| 16 | 40 | | float c00 = inputArray[gxi, gyi]; |
| 16 | 41 | | float c10 = inputArray[gxi + 1, gyi]; |
| 16 | 42 | | float c01 = inputArray[gxi, gyi + 1]; |
| 16 | 43 | | float c11 = inputArray[gxi + 1, gyi + 1]; |
| | 44 | |
|
| 16 | 45 | | float tx = gx - gxi; |
| 16 | 46 | | float ty = gy - gyi; |
| 16 | 47 | | returnArray[x, y] = math.lerp(math.lerp(c00, c10, tx), math.lerp(c01, c11, tx), ty); |
| 16 | 48 | | } |
| | 49 | |
|
| 1 | 50 | | return returnArray; |
| 1 | 51 | | } |
| | 52 | | } |
| | 53 | | } |