Convert Figma logo to code with AI

Unity-Technologies logoUnity.Mathematics

The C# math library used in Unity providing vector types and math functions with a shader like syntax

1,375
157
1,375
56

Top Related Projects

DirectXMath is an all inline SIMD C++ linear algebra library for use in games and graphics apps

9,089

OpenGL Mathematics (GLM)

12,417

Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.

88,735

Godot Engine – Multi-platform 2D and 3D game engine

Quick Overview

Unity.Mathematics is a low-level, high-performance math library for Unity, designed to work with the Burst compiler. It provides a comprehensive set of mathematical functions and data types optimized for use in game development and real-time applications, with a focus on SIMD-friendly code.

Pros

  • High performance, especially when used with the Burst compiler
  • Designed specifically for Unity, integrating well with the engine's ecosystem
  • Extensive set of mathematical functions and data types for game development
  • Supports both scalar and vector operations efficiently

Cons

  • Learning curve for developers not familiar with low-level math libraries
  • May require code refactoring to fully utilize its benefits in existing projects
  • Limited documentation compared to some other math libraries
  • Primarily focused on Unity development, potentially limiting its use in other contexts

Code Examples

  1. Vector operations:
using Unity.Mathematics;

float3 a = new float3(1, 2, 3);
float3 b = new float3(4, 5, 6);
float3 result = a + b; // result is (5, 7, 9)
  1. Matrix multiplication:
using Unity.Mathematics;

float4x4 matrix1 = float4x4.identity;
float4x4 matrix2 = float4x4.RotateY(math.radians(45));
float4x4 result = math.mul(matrix1, matrix2);
  1. Quaternion operations:
using Unity.Mathematics;

quaternion q1 = quaternion.Euler(math.radians(30), 0, 0);
quaternion q2 = quaternion.Euler(0, math.radians(45), 0);
quaternion combined = math.mul(q1, q2);

Getting Started

To use Unity.Mathematics in your Unity project:

  1. Ensure you have Unity 2018.3 or later installed.
  2. Install the Mathematics package via the Package Manager:
    • Open Window > Package Manager
    • Search for "Mathematics"
    • Click "Install"
  3. Add the following using statement to your C# scripts:
    using Unity.Mathematics;
    
  4. Start using the library's types and functions in your code:
    float3 position = new float3(1, 2, 3);
    quaternion rotation = quaternion.Euler(0, math.radians(90), 0);
    

Competitor Comparisons

DirectXMath is an all inline SIMD C++ linear algebra library for use in games and graphics apps

Pros of DirectXMath

  • Optimized for DirectX and Windows platforms
  • Extensive documentation and Microsoft support
  • Wider range of mathematical functions and utilities

Cons of DirectXMath

  • Limited cross-platform compatibility
  • C++ only, not designed for other languages
  • Steeper learning curve for beginners

Code Comparison

Unity.Mathematics:

float3 position = new float3(1f, 2f, 3f);
float3 direction = normalize(position);
float distance = length(position);

DirectXMath:

XMVECTOR position = XMVectorSet(1.0f, 2.0f, 3.0f, 0.0f);
XMVECTOR direction = XMVector3Normalize(position);
float distance = XMVectorGetX(XMVector3Length(position));

Key Differences

  • Unity.Mathematics is designed for Unity engine and C#, while DirectXMath is tailored for DirectX and C++
  • Unity.Mathematics offers a more intuitive syntax for game developers familiar with Unity
  • DirectXMath provides more low-level control and optimization for Windows-based graphics programming

Both libraries serve similar purposes but cater to different ecosystems and programming paradigms. The choice between them often depends on the target platform, development environment, and specific project requirements.

9,089

OpenGL Mathematics (GLM)

Pros of GLM

  • Mature and widely adopted in the C++ graphics programming community
  • Extensive feature set, including quaternions, matrices, and vector types
  • Header-only library, making it easy to integrate into existing projects

Cons of GLM

  • Designed primarily for C++, which may not be ideal for Unity developers
  • Larger codebase and potentially higher learning curve
  • May have performance overhead due to its generalized approach

Code Comparison

GLM:

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f, 0.0f, 0.0f));
glm::vec4 result = model * glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);

Unity.Mathematics:

using Unity.Mathematics;

float4x4 model = float4x4.Translate(new float3(1f, 0f, 0f));
float4 result = math.mul(model, new float4(1f, 1f, 1f, 1f));

Both libraries provide similar functionality for common mathematical operations used in graphics programming. GLM is more suited for C++ developers, while Unity.Mathematics is tailored for Unity and C# developers, offering better integration with the Unity ecosystem and potentially better performance for Unity-specific use cases.

12,417

Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.

Pros of bullet3

  • Comprehensive 3D physics simulation library with a wide range of features
  • Highly optimized for performance, suitable for real-time applications
  • Cross-platform support and integration with various game engines

Cons of bullet3

  • Steeper learning curve due to its complexity and extensive feature set
  • May be overkill for projects that only require basic math operations
  • Larger codebase and potential overhead for simpler applications

Code Comparison

bullet3:

btVector3 position(0, 10, 0);
btQuaternion rotation;
btRigidBody* body = createRigidBody(1.0, position, rotation);
world->addRigidBody(body);

Unity.Mathematics:

float3 position = new float3(0, 10, 0);
quaternion rotation = quaternion.identity;
float3 velocity = float3.zero;

Summary

While bullet3 excels in comprehensive physics simulation, Unity.Mathematics focuses on efficient math operations for Unity development. bullet3 offers more advanced features but may be complex for simple projects, whereas Unity.Mathematics provides a streamlined math library tailored for Unity's ecosystem.

88,735

Godot Engine – Multi-platform 2D and 3D game engine

Pros of Godot

  • Open-source and completely free, with a more permissive license
  • Full game engine with integrated editor, not just a math library
  • Supports multiple programming languages (GDScript, C#, C++)

Cons of Godot

  • Less optimized for high-performance math operations
  • Smaller community and ecosystem compared to Unity
  • Steeper learning curve for beginners due to its unique approach

Code Comparison

Unity.Mathematics:

float3 position = new float3(1f, 2f, 3f);
float3 velocity = new float3(0.1f, 0.2f, 0.3f);
position += velocity * deltaTime;

Godot:

var position = Vector3(1, 2, 3)
var velocity = Vector3(0.1, 0.2, 0.3)
position += velocity * delta

Summary

Godot is a full-featured game engine, while Unity.Mathematics is a specialized math library for Unity. Godot offers more flexibility and freedom but may have performance trade-offs for intensive math operations. Unity.Mathematics provides optimized math functions but is limited to the Unity ecosystem. The choice between them depends on project requirements and developer preferences.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Unity.Mathematics

A C# math library providing vector types and math functions with a shader like syntax. Used by the Burst compiler to compile C#/IL to highly efficient native code.

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.

In addition to this, the Burst compiler is able to recognize these types and provide the optimized SIMD type for the running CPU on all supported platforms (x64, ARMv7a...etc.)

NOTICE: The API is a work in progress and we may introduce breaking changes (API and underlying behavior)

Usage

You can use this library in your Unity game by using the Package Manager and referencing the package com.unity.mathematics. See the forum Welcome page for more details.

using static Unity.Mathematics.math;
namespace MyNamespace
{
    using Unity.Mathematics;
    
    ...
    var v1 = float3(1,2,3);
    var v2 = float3(4,5,6);
    v1 = normalize(v1);
    v2 = normalize(v2);
    var v3 = dot(v1, v2);
    ...
}

Building

Open the src\Unity.Mathematics.sln under Visual Studio 2015 or MonoDevelop and compile in Debug\Release.

Contributing

We don't yet accept PR on this repository. See the FAQ below.

The project is using editorconfig to keep files correctly formatted for EOL and spaces.

We assume that your IDE has support for editorconfig, you can download the following extensions if your IDE is listed:

Frequently Asked Question

Why developing another Math library instead of using existing Unity Vector3...etc.?

After years of feedback and experience with the previous API, we believe that providing an API that is closer to the way graphics developers have been using math libraries should better help its adoption and the ease of its usage. HLSL / GLSL math library is a very well designed, well understood math library leading to greater consistency.

Why not using System.Numerics.Vectors?

Mainly for the reason mentioned above, System.Numerics.Vectors is in many ways similar to our previous Vector library (more object oriented than graphics programming oriented). Also the fact that our Burst compiler is able to recognize a lot more patterns for SIMD types and math intrinsics makes it easier to work with a dedicated API that reflects this ability.

Naming convention

In C# int and float are considered builtin types. Burst extends this set of bultin types to also include vectors, matrices and quaternions. These types are bultin in the sense that Burst knows about them and is be able to generate better code using these types than what would be possible with equivalent code using custom types.

To signify that these types are bultin their type names are in all lower case. The operators on these bultin types found in Unity.Mathematics.math are considered intrinsics and are thus always in lower case.

There are no plans to extend the set of intrinsic types beyond the current set of vectors (typeN), matrices (typeNxN) and quaternions (quaternion).

This convention has the added benefit of making the library highly compatible with shader code and makes porting or sharing code between the two almost frictionless.

Why can't we send a PR yet?

We are working on providing a Contributor License Agreement (CLA) with a sign-over functionality and our UCL License doesn't cover this yet.

Licensing

Unity Companion License (“License”) Software Copyright © 2019 Unity Technologies ApS

For licensing details see LICENSE.md