Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Mathematics vs. Vector3

Discussion in 'Scripting Dev Blitz Day 2023 - Q&A' started by Baste, Feb 22, 2023.

  1. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,204
    There's this thing that's perplexed me for a while, and I don't feel like I ever got a proper answer.

    Unity is now in this very weird position where there's two different ways, in the same engine, to express a Vector3 - the old Vector3, and the new float3.

    What Unity.Mathematics has been sold as is this high performance maths library that's got special burst support etc. etc. The thing I wonder is why? Why wasn't that performance work done for the old, existing apis? It seems to me that instead of doing the work to make all of Unity that already exists potentially faster, you made copies of the API with the same structs, the same memory layout, the same operations, and then made that faster.

    I assume that there's good reasons, but I haven't actually seen them spelled out. What made it not possible to add special support for Vector3 to burst? Is it possible to at one point in the future unify the two maths APIs?
     
  2. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,016
  3. peturdarri

    peturdarri

    Joined:
    Mar 19, 2016
    Posts:
    23
    The documentation for the package says this:
    The main goal of this library is to provide a friendly Math API familiar to SIMD and graphic/shaders developers, using the well known float4, float3 types...etc. with all intrinsics functions provided by a static class math that can be imported easily into your C# program with using static Unity.Mathematics.math.

    With that in mind, it makes sense why Unity would make Burst recognize and optimize those types instead of the built in ones. What you don't want is to have a mix of Vector4s and float4s in your job, adding unnecessary conversions. So they had to pick one or the other.
     
  4. xoofx

    xoofx

    Unity Technologies

    Joined:
    Nov 5, 2016
    Posts:
    412
    As @peturdarri linked, the initial goal of the Unity.Mathematics was to provide a foundation library for Burst optimized code, with a closer semantic to HLSL, so that for example, you could compute 4 cos with an api like math.cos(float4). The library was also completely decoupled from the Unity Engine (unlike the Vector3 API that cannot run outside of the Unity engine) so that it was more prepared for running outside of Unity. Burst from the beginning was developed with this goal, to keep the compatibility with Mono, but also with CoreCLR.

    But I agree that it has caused an unfortunate fragmentation of the API, and as @bdovaz is pointing out, once we will migrate to CoreCLR, it will make things a bit worse.

    That being said, our goal with the .NET Modernization and the migration to CoreCLR is to rely more and more on standard .NET instead of custom solutions. It will simplify interop and you will be able to use/integrate more optimized .NET libraries to develop your game.

    So, to respond to your question:

    Ideally, I would love, but I won't lie, in practice, I fear that these APIs will be around for quite some time. :)
    • The Vector3 API is the legacy API and probably here to stay.
    • The Unity.Mathematics (and Burst Intrinsics) are used for DOTS/Burst optimized code.
    • The .NET API (System.Numerics and System.Runtime.Intrinsics) are going to be relevant whenever you need to interop with regular .NET libraries.
    At least, one thing that we can help and simplify, is to make sure that all these APIs provide implicit operators so that you should be able to pass them around without struggling with any conversions.
     
    Nad_B, bdovaz, TeodorVecerdi and 4 others like this.
  5. BSimonSweet

    BSimonSweet

    Joined:
    Aug 17, 2022
    Posts:
    51
    Would it make sense to make a breaking change in the engine and just use Unity.Mathematics for everything ? And have a script updater to change user code if needed.
     
  6. xoofx

    xoofx

    Unity Technologies

    Joined:
    Nov 5, 2016
    Posts:
    412
    It could make sense indeed, I will relay this suggestion to the team responsible for Unity.Mathematics.
     
    tspk91, Nad_B, TeodorVecerdi and 3 others like this.