1
This commit is contained in:
@@ -3,28 +3,30 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BITKit;
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Jobs;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Jobs;
|
||||
using UnityEngine.Pool;
|
||||
|
||||
namespace BITFALL.Rig{
|
||||
public class TickOverrideTranformService : MonoBehaviour
|
||||
{
|
||||
public struct SetTransformJob : IJobParallelForTransform
|
||||
//[BurstCompile]
|
||||
public struct CopyTransformJob : IJobParallelForTransform
|
||||
{
|
||||
// Jobs declare all data that will be accessed in the job
|
||||
// By declaring it as read only, multiple jobs are allowed to access the data in parallel
|
||||
[Unity.Collections.ReadOnly]
|
||||
public NativeArray<Vector3> positions;
|
||||
public NativeArray<float3> positions;
|
||||
[Unity.Collections.ReadOnly]
|
||||
public NativeArray<Quaternion> rotations;
|
||||
public NativeArray<quaternion> rotations;
|
||||
|
||||
|
||||
// The code actually running on the job
|
||||
public void Execute(int index, TransformAccess transform)
|
||||
{
|
||||
transform.position = positions[index];
|
||||
transform.rotation = rotations[index];
|
||||
transform.SetPositionAndRotation(positions[index],rotations[index]);
|
||||
}
|
||||
}
|
||||
public static void Register(int id,TickOverrideTransform tickOverrideTransform)
|
||||
@@ -40,12 +42,16 @@ namespace BITFALL.Rig{
|
||||
private static readonly Dictionary<int, TickOverrideTransform> Dictionary = new();
|
||||
private static bool IsDirty;
|
||||
|
||||
private static TickOverrideTransform[] Array;
|
||||
|
||||
private static Transform[] Sources;
|
||||
private static Transform[] Targets;
|
||||
|
||||
[SerializeReference, SubclassSelector] private ITicker ticker;
|
||||
|
||||
|
||||
private TransformAccessArray m_AccessArray;
|
||||
private bool _isCompleted;
|
||||
private NativeArray<quaternion> _rotations;
|
||||
private NativeArray<float3> _positions;
|
||||
private JobHandle _jobHandle;
|
||||
private InitializationState _initializationState;
|
||||
private void OnEnable()
|
||||
{
|
||||
ticker.Add(Tick);
|
||||
@@ -56,33 +62,59 @@ namespace BITFALL.Rig{
|
||||
}
|
||||
private void Tick(float obj)
|
||||
{
|
||||
if (!_isCompleted) return;
|
||||
switch (_initializationState)
|
||||
{
|
||||
case InitializationState.Initializing when _jobHandle.IsCompleted:
|
||||
_jobHandle.Complete();
|
||||
_rotations.Dispose();
|
||||
_positions.Dispose();
|
||||
_initializationState = InitializationState.Initialized;
|
||||
break;
|
||||
case InitializationState.None:
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsDirty)
|
||||
{
|
||||
Array = Dictionary.Values.ToArray();
|
||||
var newLength = Dictionary.Count;
|
||||
Sources = new Transform[newLength];
|
||||
Targets = new Transform[newLength];
|
||||
var index = 0;
|
||||
foreach (var x in Dictionary.Values)
|
||||
{
|
||||
Sources[index] = x.Source;
|
||||
Targets[index] = x.Target;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
m_AccessArray = new TransformAccessArray(Array.Length);
|
||||
var length = Sources.Length;
|
||||
|
||||
var rotations = new NativeArray<Quaternion>(Array.Length, Allocator.TempJob);
|
||||
var positions = new NativeArray<Vector3>(Array.Length, Allocator.TempJob);
|
||||
if(length is 0) return;
|
||||
|
||||
m_AccessArray = new TransformAccessArray(length);
|
||||
m_AccessArray.SetTransforms(Sources);
|
||||
|
||||
var index = 0;
|
||||
foreach (var x in Array)
|
||||
_positions = new NativeArray<float3>(length, Allocator.Persistent);
|
||||
_rotations = new NativeArray<quaternion>(length, Allocator.Persistent);
|
||||
|
||||
var i = 0;
|
||||
foreach (var x in Targets)
|
||||
{
|
||||
positions[index] = x.Source.position;
|
||||
rotations[index] = x.Source.rotation;
|
||||
index++;
|
||||
_positions[i] = x.position;
|
||||
_rotations[i] = x.rotation;
|
||||
i++;
|
||||
}
|
||||
|
||||
var job = new SetTransformJob()
|
||||
var _job = new CopyTransformJob()
|
||||
{
|
||||
positions = positions,
|
||||
rotations = rotations
|
||||
positions = _positions,
|
||||
rotations = _rotations
|
||||
};
|
||||
job.Schedule(m_AccessArray);
|
||||
|
||||
_isCompleted = true;
|
||||
_jobHandle = _job.Schedule(m_AccessArray);
|
||||
|
||||
_initializationState = InitializationState.Initializing;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user