Search Unity

Feedback Vector3Int

Discussion in 'Scripting' started by neginfinity, Apr 18, 2022.

  1. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    This struct exists, but it could use few improvements.

    First, There's no Vector3Int.Cross(). It is possible to run into a situation where you need a cross-product performed on integers.

    Implementation is trivial:
    Code (csharp):
    1.  
    2.     ///Vec3i ==> Vector3Int
    3.     public static Vec3i cross(Vec3i a, Vec3i b){
    4.         //yzx zxy zxy yzx
    5.         return vec3i(
    6.             a.y * b.z - a.z * b.y,
    7.             a.z * b.x - a.x * b.z,
    8.             a.x * b.y - a.y * b.x
    9.         );
    10.     }
    11.  
    Second, for some interesting reason its x/y/z fields are implemented as properties and not as public fields, like Vector3 does it. In a situation where a lot of integer Vector3Int math is involved this actually appears to generate function calls on every field access which can eat away milliseconds. It is unclear whether this is optimized away at a later point or not.
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,993
    Well, the properties are marked with the AggressiveInlining attribute. So depending on the backend those method calls would be removed. Though even if they are not removed, unless you literally have billions of read / write operations taking place you wouldn't really notice it.

    About any "missing" function, it's trivial to add whatever you need as extension method. There will always be a method, property that is missing which may be useful in some situation. The dot product may also be useful in some cases.