first commit

This commit is contained in:
lethanhsonvsp
2025-11-17 15:36:52 +07:00
commit 6f2eafa33c
14093 changed files with 1253472 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
# 4×4 matrices
To create a 4×4 transformation matrix, use the constructors in [`float4x4`](xref:Unity.Mathematics.float4x4) to assign one value to all elements of the matrix, or individually set all 16 elements directly:
```c#
// Unity Mathematics example
void Build4x4UnityMathematics()
{
var c0 = new float4(1.0f, 0.0f, 0.0f, 0.0f);
var c1 = new float4(0.0f, 1.0f, 0.0f, 0.0f);
var c2 = new float4(0.0f, 0.0f, 1.0f, 0.0f);
var c3 = new float4(0.0f, 0.0f, 0.0f, 1.0f);
var m = new float4x4(c0, c1, c2, c3);
}
```
## Multiplying a 4×4 matrix
Unity Mathematics and `UnityEngine` define the `*` operator differently. The `*` operator for [`float4x4`](xref:Unity.Mathematics.float4x4) implements componentwise multiplication. If you multiply a `float4x4` of all 1s with 0.5 on the diagonal, you get back the half identity because the upper and lower triangles of the matrix are multiplied by the respective zero entries from `f4x4_HalfIdentity`:
```c#
// Unity Mathematics example
void OperatorMultiply4x4UnityMathematics()
{
float4x4 result = f4x4_Ones * f4x4_HalfIdentity;
// result:
// 0.5, 0.0, 0.0, 0.0,
// 0.0, 0.5, 0.0, 0.0,
// 0.0, 0.0, 0.5, 0.0,
// 0.0, 0.0, 0.0, 0.5
}
```
## Multiplying a 4×4 matrix and a 4D vector
Use the [`math.mul`](xref:Unity.Mathematics.math.mul*) method to multiply a 4×4 matrix and a 4D vector. If you supply a `float4x4` as the first parameter and a `float4` as the second, it performs a 4×4 matrix multiplication with a 4×1 column vector, which returns a 4×1 column vector as a `float4`.
`math.mul` can also multiply a 1×4 row vector by a 4×4 matrix to produce a 1×4 row vector by taking a `float4` as the first parameter and a `float4×4` as the second. Unity Mathematics stores the row vector in a `float4` and it isn't treated as a separate type.
```c#
// Unity Mathematics example
void Multiply4x4AndVector4UnityMathematics()
{
float4 result1 = math.mul(f4x4, f4); // 4x4 * 4x1 = 4x1
float4 result2 = math.mul(f4, f4x4); // 1x4 * 4x4 = 1x4
}
```

View File

@@ -0,0 +1,8 @@
* [Unity Mathematics](index.md)
* [Compatibility with Mathf](compatibility.md)
* [Getting started](getting-started.md)
* Common operations
* [4×4 matrices](4x4-matrices.md)
* [Vector multiplication](vector-multiplication.md)
* [Quaternion multiplication](quaternion-multiplication.md)
* [Random numbers](random-numbers.md)

View File

@@ -0,0 +1,17 @@
# Compatibility with `UnityEngine` mathematics
If you want to Burst-compile your project, it's best practice to use the Unity Mathematics package by default and only use `Mathf` when necessary. Unity Mathematics isn't implemented in the same way as the [`UnityEngine.Mathf`](https://docs.unity3d.com/ScriptReference/Mathf.html) struct in the core Unity engine. Because of this, if your application relies on the specific behaviors of `Mathf`, you'll have to reimplement them to get similar behavior in Unity Mathematics.
>[!IMPORTANT]
>For performance reasons, if your project uses the Mono compiler, you should continue to use the mathematical operations in `Mathf`, rather than Unity Mathematics.
You can use both `Mathf` and Unity Mathematics methods in your project, but it might impact on the performance of your application because the conversions between the `UnityEngine` and `Unity.Mathematics` types such as `Vector3` to `float3` and vice-versa are performance-intensive.
## Porting `UnityEngine` code to `Unity.Mathematics`
If you want to migrate code from `UnityEngine` to `Unity.Mathematics`, you need to make the following changes first:
* Update `UnityEngine` types to `Unity.Mathematics` types. For example, [`Vector4`](https://docs.unity3d.com/ScriptReference/Vector4.html) to [`float4`](xref:Unity.Mathematics.float4), and [`Quaternion`](https://docs.unity3d.com/ScriptReference/Quaternion.html) to [`quaternion`](xref:Unity.Mathematics.quaternion). These examples aren't exhaustive: see the [Scripting API reference](https://docs.unity3d.com/Packages/com.unity.mathematics@latest/index.html?subfolder=/api/index.html) for the full list of Unity Mathematics types available.
* Update any operators involved in matrices or vectors. For example, the `Matrix4x4` multiplication operator implements matrix multiplication, but the `float4x4` multiplication operator implements componentwise multiplication.
* Degrees to radians
* How your code generates random numbers. `Random` in `Unity.Mathematics` works differently to [`Random`](https://docs.unity3d.com/ScriptReference/Random.html) in `UnityEngine`. You can completely control random number generation with `Random` in `Unity.Mathematics`, and it's instanced, rather than static. It's also exclusive with its upper bound, which is important to bear in mind if you want to convert `UnityEngine` code which is sensitive to the bounds. For more information, see the documentation on [Random numbers](random-numbers.md).

View File

@@ -0,0 +1,13 @@
apiRules:
- exclude:
uidRegex: Tests$
type: Namespace
- exclude:
uidRegex: ^Unity\..*\.Tests\..*$
type: Namespace
- exclude:
uidRegex: Unity\.Mathematics\.Shaders$
type: Namespace
- exclude:
uidRegex: Unity\.IL2CPP\.CompilerServices$
type: Namespace

View File

@@ -0,0 +1,29 @@
# Getting started
To use Unity Mathematics, add `using Unity.Mathematics` to your code:
```C#
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);
...
}
```
## Naming convention
In C# `int` and `float` are built-in types. The Burst compiler extends this set of built-in types to also include vectors, matrices, and quaternions. These types are built-in because the Burst compiler already has implementations of these types, and so can use them to generate better code than for custom types.
To signify that these types are built-in their type names are in all lower case. The operators on these built-in types in [`Unity.Mathematics.math`](xref:Unity.Mathematics.math) are intrinsics and are 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.

View File

@@ -0,0 +1,27 @@
# Unity Mathematics
Unity Mathematics is a C# math library that provides vector types and math functions that have a shader-like
syntax, similar to [SIMD](https://en.wikipedia.org/wiki/Single_instruction,_multiple_data) or [HLSL](https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl). The [Burst compiler](https://docs.unity3d.com/Packages/com.unity.burst@latest) uses Unity Mathematics to compile C#/IL code into highly efficient native code.
It implements the following vector and matrix types:
* floatN, quaternion
* float3×3, float4×4
Plus elementary functions:
* min, max, fabs, etc.
* sin, cos, sqrt, normalize, dot, cross, etc.
## Installation
You can install the Mathematics package through Unity's Package Manager. For more information, see the Unity User Manual documentation on [Adding and removing packages](https://docs.unity3d.com/Manual/upm-ui-actions.html).
### Editor config
Unity Mathematics uses [editorconfig](http://editorconfig.org/) to keep files formatted for EOL and spaces.
Your IDE should have support for `editorconfig`. If it doesn't, you can get the extension for it here:
* [VS2015/VS2017 EditorConfig extension](https://marketplace.visualstudio.com/items?itemName=EditorConfigTeam.EditorConfig)
* [Visual Studio Code EditorConfig extension](https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig)
* [SublimeText EditorConfig extension](https://github.com/sindresorhus/editorconfig-sublime)

View File

@@ -0,0 +1,5 @@
{
"useMemberPages": true,
"hideGlobalNamespace": false,
"symbolDefines":""
}

View File

@@ -0,0 +1,17 @@
# Quaternion multiplication
To rotate a quaternion, use the [`AxisAngle`](xref:Unity.Mathematics.quaternion.AxisAngle*) method. You need to specify the axis of rotation and the angle of rotation, in that order. All are in radians rather than degrees. `math.mul` multiplies the quaternion, just as with [matrices](4x4-matrices.md) and vectors.
```c#
// Unity Mathematics example
void QuaternionMultiplicationUnityMathematics()
{
var axis = new float3(0.0f, 1.0f, 0.0f);
var q = quaternion.AxisAngle(axis,math.radians(45.0f));
var orientation = quaternion.Euler(
math.radians(45.0f),
math.radians(90.0f),
math.radians(180.0f));
var result = math.mul(q, orientation);
}
```

View File

@@ -0,0 +1,21 @@
## Random numbers
To generate random numbers, you must create and manage the random number generator state yourself with the [`Random`](xref:Unity.Mathematics.Random) struct. You can control the random number generator state explicitly, which is useful if you're using parallel code, or if you want to make sure that one source of random numbers is seeded differently than another source. You can also have as many `Random` instances as you like.
Once you set up the state, use [`NextFloat`](xref:Unity.Mathematics.Random.NextFloat) to get random floats. By default it returns random numbers between `[0, 1)`, exclusive:
```c#
// Unity Mathematics example
void RandomNumberUnityMathematics()
{
// Choose some non-zero seed and set up the random number generator state.
uint seed = 1;
Unity.Mathematics.Random rng = new Unity.Mathematics.Random(seed);
// [0, 1) exclusive
float randomFloat1 = rng.NextFloat();
// [-5, 5) exclusive
float randomFloat2 = rng.NextFloat(-5.0f, 5.0f);
}
```

View File

@@ -0,0 +1,16 @@
# Vector multiplication
To multiply vectors, use the `*`:
```c#
// Unity Mathematics example
void ComponentwiseVectorMultiplyUnityMathematics()
{
var v0 = new float4(2.0f, 4.0f, 6.0f, 8.0f);
var v1 = new float4(1.0f, -1.0f, 1.0f, -1.0f);
var result = v0 * v1;
// result == new float4(2.0f, -4.0f, 6.0f, -8.0f).
}
```
This is a common way of writing [SIMD](https://en.wikipedia.org/wiki/Single_instruction,_multiple_data) code, which applies a single instruction to multiple data elements. Other operators such as addition, subtraction, and division work in the same way.