142 lines
3.5 KiB
C#
142 lines
3.5 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 BITKit.UX.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 OnDestroy()
|
|
// {
|
|
// if (_initializationState is not InitializationState.Initializing) return;
|
|
// _jobHandle.Complete();
|
|
// _rotations.Dispose();
|
|
// _positions.Dispose();
|
|
// }
|
|
|
|
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];
|
|
|
|
//_positions = new NativeArray<float3>(newLength, Allocator.Persistent);
|
|
//_rotations = new NativeArray<quaternion>(newLength, Allocator.Persistent);
|
|
|
|
var index = 0;
|
|
foreach (var x in Dictionary.Values)
|
|
{
|
|
Sources[index] = x.Source;
|
|
Targets[index] = x.Target;
|
|
index++;
|
|
}
|
|
}
|
|
|
|
var length =Sources?.Length ?? 0;
|
|
|
|
if(length is 0) return;
|
|
|
|
// m_AccessArray = new TransformAccessArray(length);
|
|
// m_AccessArray.SetTransforms(Sources);
|
|
|
|
|
|
for (var j = 0; j < length; j++)
|
|
{
|
|
Sources[j].SetPositionAndRotation(Targets[j].position,Targets[j].rotation);
|
|
}
|
|
// foreach (var x in Targets)
|
|
// {
|
|
// _positions[i] = x.position;
|
|
// _rotations[i] = x.rotation;
|
|
// i++;
|
|
// }
|
|
//
|
|
// foreach (var x in Sources)
|
|
// {
|
|
// x.position = _positions[i];
|
|
// x.rotation = _rotations[i];
|
|
// }
|
|
// var _job = new CopyTransformJob()
|
|
// {
|
|
// positions = _positions,
|
|
// rotations = _rotations
|
|
// };
|
|
// _jobHandle = _job.Schedule(m_AccessArray);
|
|
//
|
|
// _initializationState = InitializationState.Initializing;
|
|
}
|
|
}
|
|
|
|
}
|