Unity-Technologies / Unity.Mathematics

The C# math library used in Unity providing vector types and math functions with a shader like syntax
Other
1.38k stars 156 forks source link

Swizzle property z and w for int2/float2 and w for int3/float3 #224

Open Thaina opened 2 years ago

Thaina commented 2 years ago

Please add complete swizzle property to int2 float2 int3 float3 similar to int4 and float4 and define it as 0

So that we could simply upcast vec2 and vec3 to another dimension easily as downcast vec4 to lower dimension

Such as

var screenPoint = new float2(5,3);
var groundPoint = screenPoint.xzy; // (5,0,3)
var wallPoint = screenPoint.zyx; // (0,3,5)

Alternatively it might be xy0 x0y _0xy yx0 y0x _0yx and maybe xy1 x1y _1xy yx1 y1x _1yx and so on for specific default number

Cat-du-Fromage commented 2 years ago

+1, the number of time i wished there was a way to make point.x0z is beyond count!

vertxxyz commented 1 year ago

I was like, ah yes, lets use assembly definition references to add this...

public partial struct float2
{
    /// <summary>Swizzles the vector.</summary>
    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
    public float3 xy0
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        get => new(x, y, 0);
    }
}

But nope, as soon as it ends up in Bursted code... Burst error BC0102: Unexpected internal compiler error while processing function

At least new float3(float2, z) exists.

ltmx commented 11 months ago

Unfortunately, unity is apparently working on enabling users to extend native class, using the attribute [ExtensionsOfNativeClass], but is definitely not supported at the moment. In the meantime, You can do a pull request and implement it in this file : https://github.com/LTMX/Unity.mathx/blob/master/Runtime/mathx.floatx.gen.cs Have a nice day fellow !