Created
May 30, 2024 22:10
-
-
Save Sythelux/e60c346e6e83017a0b7a0aaa263d9a9c to your computer and use it in GitHub Desktop.
Godot 4 C# Array3D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using Godot; | |
using Godot.Collections; | |
public class Array3D<[MustBeVariant] T> | |
{ | |
public readonly Array<T> Array; | |
public readonly int Width; | |
public readonly int Height; | |
public readonly int Depth; | |
public readonly int Length; // Width * Height * Depth | |
public T this[uint index] | |
{ | |
get => Array[(int)index]; | |
set => Array[(int)index] = value; | |
} | |
public T this[int index] | |
{ | |
get => Array[index]; | |
set => Array[index] = value; | |
} | |
public T this[uint z, uint y, uint x] | |
{ | |
get => Array[(int)(z * Width * Height + y * Width + x)]; | |
set => Array[(int)(z * Width * Height + y * Width + x)] = value; | |
} | |
public T this[int z, int y, int x] | |
{ | |
get => Array[z * Width * Height + y * Width + x]; | |
set => Array[z * Width * Height + y * Width + x] = value; | |
} | |
public Array3D(int depth, int height, int width) | |
{ | |
Width = width; | |
Height = height; | |
Depth = depth; | |
Length = Width * Height * Depth; | |
Array = new Array<T>(new T[Length]); | |
} | |
public Array3D(T[,,] source) | |
{ | |
Array = new Array<T>(source.Cast<T>()); | |
Depth = source.GetLength(0); | |
Height = source.GetLength(1); | |
Width = source.GetLength(2); | |
Length = Width * Height * Depth; | |
} | |
public Array3D(int depth, int height, int width, out Array<T> outputNativeArray) | |
{ | |
Depth = depth; | |
Height = height; | |
Width = width; | |
Length = Width * Height * Depth; | |
Array = new Array<T>(new T[Length]); | |
outputNativeArray = Array; //TODO check, but this should be a reference | |
} | |
public void Clear() | |
{ | |
Array.Clear(); | |
// UnsafeUtility.MemClear(Array, Length * UnsafeUtility.SizeOf<T>()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment