### Unity Mathematics Basic Vector Operations Source: https://github.com/unity-technologies/unity.mathematics/blob/master/src/Documentation~/getting-started.md Demonstrates how to use Unity Mathematics for basic vector operations in C#. It includes normalizing vectors and calculating the dot product between them. Ensure 'using Unity.Mathematics' is included in your namespace. ```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); ... } ``` -------------------------------- ### Generate Random Floats with Unity.Mathematics Source: https://github.com/unity-technologies/unity.mathematics/blob/master/src/Documentation~/random-numbers.md Demonstrates how to initialize a `Unity.Mathematics.Random` generator with a seed and generate random floating-point numbers. It shows how to get a random float between 0 (inclusive) and 1 (exclusive), and how to get a random float within a specified range. ```csharp 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); } ``` -------------------------------- ### Component-wise Multiplication of 4x4 Matrices in Unity Mathematics Source: https://github.com/unity-technologies/unity.mathematics/blob/master/src/Documentation~/4x4-matrices.md Illustrates component-wise multiplication of two `float4x4` matrices in Unity Mathematics using the `*` operator. The example shows the result of multiplying an identity matrix with a scaled identity matrix. ```C# 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 } ``` -------------------------------- ### C# Floating Point Precision: Constants in Local Variables Source: https://github.com/unity-technologies/unity.mathematics/blob/master/doc/readme.md Illustrates floating-point precision in C# when constants are stored in local variables. This example shows that the result of a square root calculation involving large numbers might consistently yield 'Infinity' across different build types, potentially due to extended precision calculations. ```C# double a = -2.6e160; double b = -1.2e160; double d = System.Math.Sqrt(a * a + b * b); System.Console.WriteLine("" + d); ``` -------------------------------- ### Random Number Generation in Unity.Mathematics Source: https://github.com/unity-technologies/unity.mathematics/blob/master/src/Documentation~/compatibility.md Explains the difference in random number generation between UnityEngine.Random and Unity.Mathematics.Random. Unity.Mathematics.Random is instanced, offers full control, and uses an exclusive upper bound. ```csharp /* * How your code generates random numbers. * Random in Unity.Mathematics works differently to Random 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. */ // Example of Unity.Mathematics Random: // Unity.Mathematics.Random random = new Unity.Mathematics.Random(12345); // Seed the random number generator // float randomNumber = random.NextFloat(0.0f, 1.0f); // Generates a float between 0.0 (inclusive) and 1.0 (exclusive) // int randomInt = random.NextInt(0, 10); // Generates an int between 0 (inclusive) and 10 (exclusive) ``` -------------------------------- ### C# Math Operations with Unity.Mathematics Source: https://github.com/unity-technologies/unity.mathematics/blob/master/readme.md Demonstrates basic vector operations like normalization and dot product using the Unity.Mathematics library. It utilizes `using static Unity.Mathematics.math` for direct access to math functions and defines vector types like `float3`. ```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); ... } ``` -------------------------------- ### Porting UnityEngine Types to Unity.Mathematics Source: https://github.com/unity-technologies/unity.mathematics/blob/master/src/Documentation~/compatibility.md Illustrates the conversion of common UnityEngine types like Vector4 and Quaternion to their Unity.Mathematics equivalents, float4 and quaternion, respectively. This is a key step when migrating code. ```csharp /* * Update UnityEngine types to Unity.Mathematics types. * For example, Vector4 to float4, and Quaternion to quaternion. */ // Example conversion: // UnityEngine.Vector4 unityEngineVector = new UnityEngine.Vector4(1.0f, 2.0f, 3.0f, 4.0f); // Unity.Mathematics.float4 unityMathVector = new Unity.Mathematics.float4(unityEngineVector.x, unityEngineVector.y, unityEngineVector.z, unityEngineVector.w); // UnityEngine.Quaternion unityEngineQuaternion = UnityEngine.Quaternion.identity; // Unity.Mathematics.quaternion unityMathQuaternion = Unity.Mathematics.quaternion.identity; ``` -------------------------------- ### Unity.Mathematics Code Generation Source: https://github.com/unity-technologies/unity.mathematics/blob/master/doc/readme.md The Unity.Mathematics library utilizes a C# program named Unity.Mathematics.CodeGen to generate code for various vector and matrix type variants. This includes constructors, conversion operators, arithmetic operators, swizzles, ToString methods, and Hash functions, ensuring regularity and reducing manual coding effort. The generated files are identifiable by their *.gen.cs extension. ```C# using Unity.Mathematics; // Example of generated code structure (conceptual) public struct float3x3 { // Constructors, operators, swizzles, etc. public static float3x3 identity { get; } public static float3x3 operator *(float3x3 a, float3x3 b) { /* ... */ } public float3 this[int index] { get; set; } } public struct quaternion { // Constructors, operators, swizzles, etc. public static quaternion identity { get; } public static quaternion operator *(quaternion a, quaternion b) { /* ... */ } } // ... other types and functions generated by Unity.Mathematics.CodeGen ``` -------------------------------- ### Create 4x4 Matrix in Unity Mathematics Source: https://github.com/unity-technologies/unity.mathematics/blob/master/src/Documentation~/4x4-matrices.md Demonstrates how to create a 4x4 transformation matrix (`float4x4`) in Unity Mathematics. It shows initializing column vectors and then constructing the matrix using these vectors. ```C# 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); } ``` -------------------------------- ### C# Floating Point Precision: Constants in Array Source: https://github.com/unity-technologies/unity.mathematics/blob/master/doc/readme.md Demonstrates floating-point precision differences in C# where constants are stored in an array. The output can vary between Debug and Release builds due to JIT optimizations, potentially resulting in 'Infinity' or a finite value for the square root calculation. ```C# var a = new double[2] = { -2.5e160, 1.2e160 }; double d = System.Math.Sqrt(a[0] * a[0] + a[1] * a[1]); System.Console.WriteLine("" + d); ``` -------------------------------- ### Operator Differences: Matrix Multiplication Source: https://github.com/unity-technologies/unity.mathematics/blob/master/src/Documentation~/compatibility.md Highlights a key difference in operator behavior between UnityEngine.Matrix4x4 and Unity.Mathematics.float4x4. While Matrix4x4 multiplication is standard matrix multiplication, float4x4 multiplication is component-wise. ```csharp /* * 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. */ // Example: // UnityEngine.Matrix4x4 matrixA = UnityEngine.Matrix4x4.identity; // UnityEngine.Matrix4x4 matrixB = UnityEngine.Matrix4x4.TRS(UnityEngine.Vector3.one, UnityEngine.Quaternion.identity, UnityEngine.Vector3.one); // UnityEngine.Matrix4x4 resultMatrix = matrixA * matrixB; // Standard matrix multiplication // Unity.Mathematics.float4x4 floatMatrixA = Unity.Mathematics.float4x4.identity; // Unity.Mathematics.float4x4 floatMatrixB = Unity.Mathematics.float4x4.identity; // Unity.Mathematics.float4x4 resultFloatMatrix = floatMatrixA * floatMatrixB; // Component-wise multiplication ``` -------------------------------- ### Quaternion Multiplication using Unity Mathematics Source: https://github.com/unity-technologies/unity.mathematics/blob/master/src/Documentation~/quaternion-multiplication.md This C# code snippet demonstrates quaternion multiplication in Unity's Mathematics library. It creates a quaternion from an axis and angle, defines another quaternion using Euler angles, and then multiplies them using `math.mul`. ```csharp 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); } ``` -------------------------------- ### Multiply 4x4 Matrix with 4D Vector in Unity Mathematics Source: https://github.com/unity-technologies/unity.mathematics/blob/master/src/Documentation~/4x4-matrices.md Shows how to multiply a 4x4 matrix (`float4x4`) with a 4D vector (`float4`) using the `math.mul` method in Unity Mathematics. It covers both matrix-column vector and row vector-matrix multiplication. ```C# void Multiply4x4AndVector4UnityMathematics() { float4 result1 = math.mul(f4x4, f4); // 4x4 * 4x1 = 4x1 float4 result2 = math.mul(f4, f4x4); // 1x4 * 4x4 = 1x4 } ``` -------------------------------- ### C# Vector Multiplication (Unity Mathematics) Source: https://github.com/unity-technologies/unity.mathematics/blob/master/src/Documentation~/vector-multiplication.md Performs component-wise multiplication of two float4 vectors using the '*' operator in Unity Mathematics. This operation leverages Single Instruction, Multiple Data (SIMD) principles. ```C# using Unity.Mathematics; // 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). } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.