122 lines
2.9 KiB
C#
122 lines
2.9 KiB
C#
using System;
|
|
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
|
|
{
|
|
//[BurstCompile]
|
|
public struct CopyTransformJob : IJobParallelForTransform
|
|
{
|
|
[Unity.Collections.ReadOnly]
|
|
public NativeArray<float3> positions;
|
|
[Unity.Collections.ReadOnly]
|
|
public NativeArray<quaternion> rotations;
|
|
|
|
|
|
// The code actually running on the job
|
|
public void Execute(int index, TransformAccess transform)
|
|
{
|
|
transform.SetPositionAndRotation(positions[index],rotations[index]);
|
|
}
|
|
}
|
|
public static void Register(int id,TickOverrideTransform tickOverrideTransform)
|
|
{
|
|
Dictionary.Add(id,tickOverrideTransform);
|
|
IsDirty = true;
|
|
}
|
|
public static void UnRegister(int id)
|
|
{
|
|
Dictionary.Remove(id);
|
|
IsDirty = true;
|
|
}
|
|
private static readonly Dictionary<int, TickOverrideTransform> Dictionary = new();
|
|
private static bool IsDirty;
|
|
|
|
private static Transform[] Sources;
|
|
private static Transform[] Targets;
|
|
|
|
[SerializeReference, SubclassSelector] private ITicker ticker;
|
|
|
|
private TransformAccessArray m_AccessArray;
|
|
private NativeArray<quaternion> _rotations;
|
|
private NativeArray<float3> _positions;
|
|
private JobHandle _jobHandle;
|
|
private InitializationState _initializationState;
|
|
private void OnEnable()
|
|
{
|
|
ticker.Add(Tick);
|
|
}
|
|
private void OnDisable()
|
|
{
|
|
ticker.Remove(Tick);
|
|
}
|
|
private void Tick(float obj)
|
|
{
|
|
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)
|
|
{
|
|
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++;
|
|
}
|
|
}
|
|
|
|
var length = Sources.Length;
|
|
|
|
if(length is 0) return;
|
|
|
|
m_AccessArray = new TransformAccessArray(length);
|
|
m_AccessArray.SetTransforms(Sources);
|
|
|
|
_positions = new NativeArray<float3>(length, Allocator.Persistent);
|
|
_rotations = new NativeArray<quaternion>(length, Allocator.Persistent);
|
|
|
|
var i = 0;
|
|
foreach (var x in Targets)
|
|
{
|
|
_positions[i] = x.position;
|
|
_rotations[i] = x.rotation;
|
|
i++;
|
|
}
|
|
var _job = new CopyTransformJob()
|
|
{
|
|
positions = _positions,
|
|
rotations = _rotations
|
|
};
|
|
_jobHandle = _job.Schedule(m_AccessArray);
|
|
|
|
_initializationState = InitializationState.Initializing;
|
|
}
|
|
}
|
|
|
|
}
|