Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Unity.Mathematics available on github

Discussion in 'Entity Component System and C# Job system' started by xoofx, Apr 10, 2018.

  1. xoofx

    xoofx

    Unity Technologies

    Joined:
    Nov 5, 2016
    Posts:
    79
    Hello,

    We are making the `Unity.Mathematics` library available at https://github.com/Unity-Technologies/Unity.Mathematics

    This is the library used by all our ECS demos. This library was also specially crafted to be compatible with the burst compiler

    You can't send yet a PR to this project, but we are working on providing a Contributor License Agreement (CLA) with a sign-over functionality as our UCL License doesn't cover this yet.

    Enjoy!
     
  2. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    906
    nice!

    Would be cool see the lib grow to include some 3D math utilities such as math.ShortestDistanceToPlane, math.IntersecionTwoPlanes, math.ClosestPointOnSegment, etc, etc... at some point. I guess this is the kind of thing the community could handle
     
    Last edited: Apr 10, 2018
  3. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    361
    Really cool to see that so many more project are put on Github!

    I really dont understand why its made with a shader-like syntax though. I think its alienating the common Unity developer, who is used to C# naming conventions. To me it looks strange to read the examples so far, its not quite normal C# syntax. It does look like a C#/HLSL hybrid, and in my book that is not a good thing at all.

    Sure its kind of neat you can use the `using math;` syntax, but its not what C# Unity developers are used to, and they are the overwhelming majority of your users. It feels very odd to me that 99% of the codebase adheres to one standard, and this special 1% does it differently. Graphics developers will feel out of place because its still C#, C# developers will feel out of place because its kind-of-shadery. Is the current Mathf library not adopted, and hard to use because of its syntax?

    It also adds that one extra step to converting your code from the old system to the new, it will become harder to diff the old and new code. I understand that this new library might never fully replace Mathf, but lets at least make them a bit similar.

    I can already see so many beginners asking why they have to write `dot` in one place and `Mathf.Dot` in some other.
     
    Last edited: May 11, 2018
  4. Rennan24

    Rennan24

    Joined:
    Jul 13, 2014
    Posts:
    27
    Congratulations to the people at Unity! Hopefully you guys are able to sort out the legal stuff for this new repository and the C# source code repo so that people can start posting PRs :p

    All jokes aside, the main thing that I'm concerned about is when this new math library reaches maturity and is fully integrated with Unity, is Unity going to drop the old math stuff? or are they going to live along side each other and people are going to have to consciously pick between the two libraries? (Vector3 vs float3, Matrix4x4 vs float4x4) Because if that's the case then it would make things pretty confusing for everyone. And as people have pointed out the syntax is more like a C#/HLSL hybrid and C# developers might get irked by that. Either syntax Unity decides to pick, it won't stop me from using the new math library and utilizing that sweet new Burst compiler :D
     
    Enrico-Monese likes this.
  5. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    1,786
    I think the HLSL syntax is the lesser of two evils. A different convention on the C# side when not working with the gpu is a minor annoyance especially if it's clearly encapsulated to just that library. But when writing code that does work with the gpu, having the api on both sides match is a bigger deal. It's not just the capitalization it's the types also.
     
  6. Tuni

    Tuni

    Joined:
    May 24, 2013
    Posts:
    74
    I don't like the new syntax. You have now 3 different kinds of naming conventions across your engine:

    1. The old Unity style
    2. The new Unity style using the standard C# conventions
    3. The math library

    I don't think this is a good idea for new Unity users who are learning programming with Unity.

    It is a problem for your other users too. I know the Mathf library, but not much about shader programming. So I have to adopt to a new style because you think that graphic developers will be the gameplay programmers in the future?
     
  7. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    338
    I think Unity is slowly going to more professional route. Actually what I see the new math library is still the same thing at high level view. Maybe u you are not get used to the changes to capital letter API to small letter API and something like float2, float3. You still can use the old math library if u want but u will not able to gain best possible optimization. I see somewhere that mention new Unity library will have namespace using Unity.Something. Maybe Unity should create more detailed documentation to explain all these.
     
  8. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    997
    But this is supplemental. You can use mathf, or if your a graphics guy use this.

    EDIT: I am mostly a c# developer, having used c# since 2010ish religiously, yet since writing shaders I do find it easier to think of data as what they are (3 pieces of floating point data, 4 pieces, etc), so having a choice is nice :)
     
    Last edited: Apr 11, 2018
    optimise likes this.
  9. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    361
    A job system that handles fish movement is not working with the GPU, and still uses this syntax.

    I understand that its supplemental, something next to the Mathf library. Wouldnt it be nice if the syntax was the same though, so its easier to switch between one and the other?
     
    johnnyt and Claytonious like this.
  10. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    906
    there are plans to make Burst compile C# directly to GPGPU/shader language though ( link )

    Personally I am very happy with this library. It may not follow C# conventions, but to me it feels very clean, intuitive, and devoid of any bloat
     
    GameDevCouple_I and RaL like this.
  11. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    997

    I dont understand, maybe I am just being obtuse, but this is how I see it:

    This is going to do the same thing as mathf. So why would you make them match? That completely reverses the point of having an extra math library.

    People who do C# syntax math have no problem using mathf. No change or extra library is needed for them. This is also not for them but for others, hence the different standards.

    People who do GPU style math will need to acclimatize to mathf.

    Now C# can use mathf, HLSL/GLSL can use this new library, and everything is written as you would expect if you use the one made for your syntax style. I really dont see any point in making a second library that is the same as the first.
     
  12. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    906
    Mathf had to be replaced because it was too heavy and not SIMD-optimizable. Ideally, it should get deprecated as soon as possible
     
    RaL likes this.
  13. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    361
    Dont get me wrong, i'm super happy with the library!

    I understand that it might cross compile, but that is even more of an argument to follow C# standards in my opinion. If you want non-gpu developers to use this, dont make it look like gpu code!
     
  14. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    997
    Right but isnt this a reason for it to use the style of syntax chosen?

    I am still not understanding why you wouldnt want syntax to match the way its done everywhere else. Anywhere you are doing parralel math operations on (potentially) big sets of data, for visual stuff like games and digital media, its usually float3, float4 etc etc. (from my experience)

    For me, mathf was always the odd one out as it was just plain odd compared to math in other engines or frameworks.

    Anyway, I guess its just a matter of preference. I suppose people could define it to be the way they want using:

    using Vector3 as float3 or something similar in their scripts (not a great solution for those who dont like the syntax, but still a way around it for now)


    EDIT:

    I feel you, I suppose if I felt the same way I would be finding it weird too. I am sure there will be some intermediate solution that pleases both parties at some point in the future, or at least hope so!
     
    TJHeuvel-net likes this.
  15. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    361
    You're right, its preference. And i agree with you, it shouldnt be the odd one out.

    But 90% of our Unity codebase is C#, and 10% is shaders. Surely within a Unity context following the C# conventions cannot be the odd one out. Especially when writing C#! Adding a transpiler to make it easier for C# developers to do GPU code should be easiest for C# developers if you ask me.

    Anyway, good things. Super cool to see this on github, and that there are plans to allow us to contribute as well. The very fact we can give feedback like this is a great improvement!
     
    Last edited: Apr 11, 2018
    johnnyt and GameDevCouple_I like this.
  16. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    997
    I have a feeling (which could be completely wrong) that is partly because with 2018.1, SRP, shadergraph, etc etc we are finally moving to a world where rendering and rendering related things are under our control. So before because we weren't interacting with this stuff as much (or even could interact with it), it didn't matter, but now that we are, having a more graphics oriented and SIMD compliant library is -potentially- beneficial for any users who are starting out fresh moving forward.

    I do agree that it is confusing right now, my hopes are that during 2018.1 and 2 cycle, this all gets cleared up as not only here but everywhere as there are currently many places where there are like 2-3 ways of doing the same thing, with no real documentation on which is better or why that choice was made.

    I also am super happy for all the stuff on GitHub, it makes it so much easier to keep track of development and reduces my need to ask about progress here on the forums, so keep that up unity! :)
     
    Rennan24 and TJHeuvel-net like this.
  17. ceebeee

    ceebeee

    Joined:
    Mar 7, 2017
    Posts:
    229
    Hi does this work in Unity 2017? or does it require Unity 2018? the repository doesn't really make that clear.
     
  18. Cynicat

    Cynicat

    Joined:
    Jun 12, 2013
    Posts:
    218
    I personally really like the new math convention. Matching HLSL offers a lot of benefits, for one its a very slick syntax, stuff like length instead of magnitude, which is more intuitive for beginners. plus stuff like float3, int3, half3, float4x4 is such a slick convention, i love it, especially for renaming. also there are tons of good math examples in HLSL, which can now be copy-pasted without a problem. It also matches more high performance math libraries out there. So i for one really like the new convention, it should also make it easier for people to pick up shaders in future.
     
  19. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    997
    Yeah I mean ultimately, if your fully new, then it doesnt matter what conventions are because its all new to you and your learning it for the first time. This will only really be a problem for those that have gotten very used to and comfortable with c# mathf conventions, and never tried to learn any graphics programming or shader programming or maths within other languages.

    So really I actually think it will be easier for new users to adopt this than older users.
     
    Cynicat likes this.
  20. ceebeee

    ceebeee

    Joined:
    Mar 7, 2017
    Posts:
    229
    Can someone answer my question please? Does this work in Unity 2017 or not?
     
  21. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    361
    Why dont you just try it out?
     
    GameDevCouple_I likes this.
  22. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    997
    Yeah, I dont understand why you wouldnt just try it, its a tiny git repository itll take minutes to figure it out, less time than it took you to post here.
     
  23. ceebeee

    ceebeee

    Joined:
    Mar 7, 2017
    Posts:
    229
    Mainly because i'd have to build some test project to exercise the library. Sorry that I thought someone here would know if it was tied to some feature of 2018 or not, before I wasted a bunch of time on it. I do have projects to take care of.
     
    Last edited: Apr 13, 2018
  24. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    408
    I just did a quick demo project using 2017.3. Only works with .NET 4.6. Not all of the types are supported in the editor (namely the bool* and half* families).

    Several of the Basic functions checked out as working, but I didn't give it a thorough shakedown or move the test suite over and run it.
     
    ceebeee likes this.
  25. Tudor-Nita

    Tudor-Nita

    Joined:
    Dec 10, 2009
    Posts:
    332
    In a more and more parallel world, it only makes sense to have a common syntax. It does not matter if you do shaders or not since most code will run on all available calculation cores in the short to medium future.

    Here's where the cg/ hlsl notation comes in. It is mature, it is well supported and documented and more than that, it's one of a handful of math libraries that were actually created with a parallel workload in mind.

    I for one appreciate the new math library. The minor effort to get used to it will probably pay itself off soon enough.
     
    Cynicat, recursive, Rennan24 and 2 others like this.
  26. ceebeee

    ceebeee

    Joined:
    Mar 7, 2017
    Posts:
    229
    Also I saw this post on twitter:



    So without the burst compiler (2018) it's not going to be faster and therefore not really worth using (unless you just like the syntax I guess)
     
  27. Cynicat

    Cynicat

    Joined:
    Jun 12, 2013
    Posts:
    218
    Oh! quick bonus tip for peeps! Unity now has shader swizzles! These allow you to shuffle the components of a vector and convert between vectors really, really easily!

    Code (CSharp):
    1. // you have a vector
    2. float4 x = new float4(1f, 2f, 4f, 8f);
    3. float3 y = x.xyz; // 1f, 2f, 4f
    4. float3 z = x.yyy; // 2f, 2f, 2f
    5. float3 w = x.zwx; // 4f, 8f, 1f
    It's hidden from intellisense because there are thousands of combinations, but they are there! Seriously, swizzles are awesome as hell, thanks to whoever implemented this!
     
    TZ-, hippocoder, RaL and 2 others like this.
  28. Rennan24

    Rennan24

    Joined:
    Jul 13, 2014
    Posts:
    27
    How did you hide yours from intellisense? o_O I use Rider for my editor and when It shows intellisense, I'm flooded with 340 possibilities for the swizzling and its kind of annoying at times lol
     
  29. Cynicat

    Cynicat

    Joined:
    Jun 12, 2013
    Posts:
    218
    Sounds like rider doesn't obey the [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] attribute. I use visual studio code and it works fine, sorry =<
     
    twobob likes this.
  30. Rennan24

    Rennan24

    Joined:
    Jul 13, 2014
    Posts:
    27
    Welp, I better go complain to Rider then lolol, Thanks for the heads up! :D
     
    Cynicat likes this.
  31. H-Fly

    H-Fly

    Joined:
    Sep 8, 2013
    Posts:
    4
    How can I convert float3[] to Vector3[].
     
  32. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    2,013
    Why are mathematics methods in lower case? mul, min, abs
    Stay with c# convention, guys.
     
    johnnyt likes this.
  33. xoofx

    xoofx

    Unity Technologies

    Joined:
    Nov 5, 2016
    Posts:
    79
    The reason was to provide a library that is closer to shaders syntax so that graphics programmers are more familiar with it (and can port more easily code to it).

    This library is indeed departing from standard C# convention, but it was a trade-off to make the API more friendly to people not familiar/"friendly" with C#
     
  34. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    10
    In other word: make the API "friendly" for a small target group of shader programmers, who are not "friendly" with C#, and trouble the large target group of C# programmers, who are not "friendly" with shaders programming.
     
    johnnyt, Seb-1814, gnurk and 11 others like this.
  35. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    2,013
    1. Shader writers can change a case when copy pasting.
    2. this is hard to read because it's in c# space
    3. it looks schizophrenic
    4. contradicts recent party line.
    5. Like @runner78 says: wrong logic
     
    johnnyt, Prodigga, VergilUa and 3 others like this.
  36. Tuni

    Tuni

    Joined:
    May 24, 2013
    Posts:
    74
    I don't understand it either. Why should graphics programmers start writing gameplay logic?
     
    VergilUa, Claytonious and RaL like this.
  37. Peter77

    Peter77

    Joined:
    Jun 12, 2013
    Posts:
    2,753
    I would prefer if you rename the namespace from "Mathematics" to just "Math". It's easier and faster to write. If I talk to someone, I say math rather than mathematics too.
     
    f1ac and starikcetin like this.
  38. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    630
    It’s likely gfx programmmes who wrote the new math library and they stuck to the convention they are used to.

    Anyway, impossible to make everyone happy - no big deal
     
  39. supron

    supron

    Joined:
    Aug 24, 2013
    Posts:
    22
    I understand the reasoning but I think that this is wrong. I also think that we should talk about it on this thread, exchange arguments and rethink this decision as whole community.

    1. OK - we can share code between shader and game code. But is that really frequent case? I'm programmer for 13 years and I don't remember such case in my entire career. Maybe very small functions like triangle-ray collision test or some kind of noise, but this is extremely rare case, usually done once or copy-pasted from another projects. There are plenty of MIT licensed implementations in HLSL and C#, and I bet that most of programmers do not hand write them. Even if we can use the same code in both languages, it's still in two separated files. We can't include *.cs file in shader anyway. Even if we could, there are still differences. In C# functions are usually in different namespaces and static classes, so code will differ anyway.

    2. You are talking about graphics programmers. I think that people mostly start with game logic, and then shift to graphics programming. Shader code is way harder to understand than C# scripts, and requires more technical knowledge. Graphics programming will be less significant with incoming Shader Graph updates. I think that in the future, even compute shaders should be written in C# and then compiled to GPU. ECS and Job system is great opportunity to go in this direction. Jobs are similar to GPU processing. You take piece of read-only data and then execute parallel code. It's the same pattern we see in compute shaders. New math library convention, targets small group of programmers, and this group will be even smaller in next few years.

    3. Only small part of the whole game code is GPU-like, but most of it uses math functions/types. We have vectors everywhere. In transforms, movement, physics, UI. Whole logic is usually math heavy, so almost all of our code will have mixed convention. We trade the whole code readability, not just one library.

    ECS is still in early development, so changing convention now is not a big deal.
     
  40. SubPixelPerfect

    SubPixelPerfect

    Joined:
    Oct 14, 2015
    Posts:
    159
    are you serious, codestyle isn't a problem at all, modern IDE going to do this thing for you automatically, better focus on software engineering and on library API

    i think this topic is so popular because real problems are bother only most involved professionals, and codestyle is a thing anyone can talk about no mater what is a qualification

    p.s. nothing personal
    p.p.s. i'm not going to give a single f**k about codestyle
     
    z00n, sngdan and teutonicus like this.
  41. supron

    supron

    Joined:
    Aug 24, 2013
    Posts:
    22
    I agree, but this is another argument to not depart from C# standard. If codestyle isn't problem at all, then why reinventing the wheel? Just stay stick to the official standard.

    But this problem was here from the beginning, and I'm not talking about this Math library. I'm talking about whole unity API. People were complaining about these small differences like camel casing in properties all the time. Unity is constantly improving its API, just look at ECS. It has very clean API and keeps standard. Soon math library is going to be the only one exception. I'm not going to dive into "why standardization is important", but we see that trend in the whole industry. Versioning, changelogs, naming conventions - everything is standardized to make our life easier. There's no need to break that without strong reasoning behind.

    That was my last post about code style. And yes, I have OCD :(
     
    johnnyt, FROS7 and starikcetin like this.
  42. starikcetin

    starikcetin

    Joined:
    Dec 7, 2017
    Posts:
    172
    I also believe you should stick with C# conventions.
    We are using C# after all. People who are not familiar with the conventions will get familiar with it eventually.
     
    Last edited: May 5, 2018
    johnnyt and FROS7 like this.
  43. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,357
    There was UnityScript with the reason to provide a language that was closer to JavaScript syntax to attract programmers that were more familiar with it. This also provided confusion. I've seen beginners trying to copy-paste pure JavaScript into Unity.

    On the other hand, I'd like to write float, float2, float3, int, int2, int3 and wouldn't like neither float, Float2, Float3, int, Int2, Int3 nor Single, Single2, Single3, Int32, Int2, Int3.
     
    starikcetin and Vanamerax like this.
  44. pavelkouril

    pavelkouril

    Joined:
    Jul 22, 2016
    Posts:
    94
    Yeah, personally, I don't mind the type names (float2, etc.), since it looks/feels like the float instead of Float from normal C# we use everyday.

    What I have an issue with are the names of the functions and classes, like "noise"/"tan"/"sin" etc.
     
    johnnyt, Seb-1814, VergilUa and 7 others like this.
  45. Q-Ted

    Q-Ted

    Joined:
    Dec 16, 2016
    Posts:
    8
    Stuff like string, int, float, etc are all just typedefs though and all link back to System classes. (System.String, System.Int32, etc)

    If that could be added you can satisfy most people. (aka, being able to use float2 and Unity.Mathematics.Float2)
    Functions should probably use the C# standard. We are working in C#, not shader languages. Trying to mask it so it looks like something it is not is just silly and will confuse people when you look at small snippets of code.
     
    johnnyt, Qbit86 and FROS7 like this.
  46. supron

    supron

    Joined:
    Aug 24, 2013
    Posts:
    22
    Exactly. I was talking mostly about functions. Actually I like these "native like" type names. What I don't like is "math.clamp" which feels really weird.
     
    johnnyt, FROS7 and M_R like this.
  47. interpol_kun

    interpol_kun

    Joined:
    Jul 28, 2016
    Posts:
    58
    I don't like the new convention too. It looks messy, let's get consistent naming across all the engine parts. And it seems that Unity Team arguments are illogical at all.

    We all here use Unity, so we are already familiar with C#. What is the point of to "make the API more friendly to people not familiar/"friendly" with C#"? Who are these people? People, who came from UE4 or other engines? Well, they will learn C# to actually write game code, so they WILL HAVE TO get friendly with C#.

    Moreover, you are creating Shader Graph and Node Based VFX Editor, so the more people will stop writing shader code and will be writing mostly in C#.

    I can't get it. I think we should really make a public discussion about that problem and find a consensus.

    I suggest using the C# naming convention. Types will be like native C# types (lowercase int, float, float2 and others) and functions will be uppercase (Sin, Cos). We should really discuss it, but it seems that you have decided it already and don't want to change it at all.

    I hope you will listen to your users.
     
  48. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    1,786
    There are reasons why most languages capitalize classes even if the convention is lower case for everything else.

    Good libraries that support multiple languages generally have idiomatic versions for both.

    I think they screwed up on both counts here myself.
     
    johnnyt likes this.
  49. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    361
    Lets say i have an existing codebase which i want to port to this new ECS style. Does your IDE automatically recognize that now it should use lowercase everywhere and add a static using statement on top? I dont think so. Especially when migrating code this is going to be a pain, if they keep the same-ish standards it would only be a different Using statement.
     
    interpol_kun likes this.
  50. andywatts

    andywatts

    Joined:
    Sep 19, 2015
    Posts:
    36
    Would like int3,etc to have == and !=