Files
Studio/Assets/Scripts/XED/Common/MatrixConverter.cs
2025-02-19 17:24:26 +09:00

112 lines
3.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Burst;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Collections;
[BurstCompile]
public struct TransformToMatrixJob : IJobParallelFor
{
[ReadOnly] public NativeArray<float3> positions;
[ReadOnly] public NativeArray<quaternion> rotations;
[ReadOnly] public NativeArray<float3> scales;
public NativeArray<float4x4> matrices;
public void Execute(int index)
{
matrices[index] = float4x4.TRS(positions[index], rotations[index], scales[index]);
}
}
public class MatrixConverter
{
public Matrix4x4[] ConvertToMatrixArray(Transform[] transformArray)
{
int count = transformArray.Length;
NativeArray<float3> positions = new NativeArray<float3>(count, Allocator.TempJob);
NativeArray<quaternion> rotations = new NativeArray<quaternion>(count, Allocator.TempJob);
NativeArray<float3> scales = new NativeArray<float3>(count, Allocator.TempJob);
NativeArray<float4x4> matrices = new NativeArray<float4x4>(count, Allocator.TempJob);
for (int i = 0; i < count; i++)
{
positions[i] = transformArray[i].position;
rotations[i] = transformArray[i].rotation;
scales[i] = transformArray[i].localScale;
}
TransformToMatrixJob job = new TransformToMatrixJob
{
positions = positions,
rotations = rotations,
scales = scales,
matrices = matrices
};
JobHandle handle = job.Schedule(count, 256);
handle.Complete();
Matrix4x4[] result = new Matrix4x4[count];
for (int i = 0; i < count; i++)
{
result[i] = matrices[i];
}
positions.Dispose();
rotations.Dispose();
scales.Dispose();
matrices.Dispose();
return result;
}
public Matrix4x4[] ConvertToMatrixArray(Transform meshTransform, Transform[] transformArray)
{
int count = transformArray.Length;
NativeArray<float3> positions = new NativeArray<float3>(count, Allocator.TempJob);
NativeArray<quaternion> rotations = new NativeArray<quaternion>(count, Allocator.TempJob);
NativeArray<float3> scales = new NativeArray<float3>(count, Allocator.TempJob);
NativeArray<float4x4> matrices = new NativeArray<float4x4>(count, Allocator.TempJob);
for (int i = 0; i < count; i++)
{
positions[i] = transformArray[i].position;
rotations[i] = transformArray[i].rotation;
scales[i] = transformArray[i].lossyScale;
}
TransformToMatrixJob job = new TransformToMatrixJob
{
positions = positions,
rotations = rotations,
scales = scales,
matrices = matrices
};
JobHandle handle = job.Schedule(count, 256);
positions.Dispose(handle);
rotations.Dispose(handle);
scales.Dispose(handle);
matrices.Dispose(handle);
handle.Complete();
Matrix4x4 meshMatrix = Matrix4x4.TRS(meshTransform.position, meshTransform.rotation, meshTransform.lossyScale);
Matrix4x4[] result = new Matrix4x4[count];
for (int i = 0; i < count; i++)
{
result[i] = matrices[i];
result[i] = result[i] * meshMatrix;
}
return result;
}
public Matrix4x4[] ConvertToMatrixArray(Matrix4x4 meshTransformMatrix, Matrix4x4[] transformMatrices)
{
int count = transformMatrices.Length;
Matrix4x4[] result = new Matrix4x4[count];
for (int i = 0; i < count; i++)
{
result[i] = transformMatrices[i] * meshTransformMatrix;
}
return result;
}
}