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

[FR] math.all + math.any for multi dimensional boolean #225

Open Thaina opened 2 years ago

Thaina commented 2 years ago
        public static bool2 all(bool2x2 m) { return new bool2(all(m.c0),all(m.c1)); }
        public static bool2 all(bool3x2 m) { return new bool2(all(m.c0),all(m.c1)); }
        public static bool2 all(bool4x2 m) { return new bool2(all(m.c0),all(m.c1)); }

        public static bool2 any(bool2x2 m) { return new bool2(any(m.c0),any(m.c1)); }
        public static bool2 any(bool3x2 m) { return new bool2(any(m.c0),any(m.c1)); }
        public static bool2 any(bool4x2 m) { return new bool2(any(m.c0),any(m.c1)); }

        public static bool3 all(bool2x3 m) { return new bool3(all(m.c0),all(m.c1),all(m.c2)); }
        public static bool3 all(bool3x3 m) { return new bool3(all(m.c0),all(m.c1),all(m.c2)); }
        public static bool3 all(bool4x3 m) { return new bool3(all(m.c0),all(m.c1),all(m.c2)); }

        public static bool3 any(bool2x3 m) { return new bool3(any(m.c0),any(m.c1),any(m.c2)); }
        public static bool3 any(bool3x3 m) { return new bool3(any(m.c0),any(m.c1),any(m.c2)); }
        public static bool3 any(bool4x3 m) { return new bool3(any(m.c0),any(m.c1),any(m.c2)); }

        public static bool4 all(bool2x4 m) { return new bool4(all(m.c0),all(m.c1),all(m.c2),all(m.c3)); }
        public static bool4 all(bool3x4 m) { return new bool4(all(m.c0),all(m.c1),all(m.c2),all(m.c3)); }
        public static bool4 all(bool4x4 m) { return new bool4(all(m.c0),all(m.c1),all(m.c2),all(m.c3)); }

        public static bool4 any(bool2x4 m) { return new bool4(any(m.c0),any(m.c1),any(m.c2),any(m.c3)); }
        public static bool4 any(bool3x4 m) { return new bool4(any(m.c0),any(m.c1),any(m.c2),any(m.c3)); }
        public static bool4 any(bool4x4 m) { return new bool4(any(m.c0),any(m.c1),any(m.c2),any(m.c3)); }

Sample use case

int[] vertexTriangles; // index buffer of mesh
var rightEdge = new int3(0,1,4); // index of vertex on the right of uv rectangle
var topEdge = new int3(1,3,4); // index of vertex on the top of uv rectangle
var rightTop = new int3x2(new int3(0,1,4),new int3(1,3,4));

foreach(int index in vertexTriangles)
{
    bool3x2 checkRightTop = index % 6 == rightTop;
    bool2 isRightTop = math.any(checkRightTop); // check that vertexIndex is which corner of UV space
    // isRightTop.x is left or right
    // isRightTop.y is bottom or top
    var v = math.select(rightTopValue,leftBottomValue,isRightTop); // select leftTop/rightTop/leftBottom/rightBottom
}
ltmx commented 11 months ago

Implemented in Unity.mathx : https://github.com/LTMX/Unity.mathx Enjoy !