1
This commit is contained in:
141
Src/Unity/Scripts/Rig/TickOverrideTranformService.cs
Normal file
141
Src/Unity/Scripts/Rig/TickOverrideTranformService.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
11
Src/Unity/Scripts/Rig/TickOverrideTranformService.cs.meta
Normal file
11
Src/Unity/Scripts/Rig/TickOverrideTranformService.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5cda5807f7f44b140aa1de720894faab
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
34
Src/Unity/Scripts/Rig/TickOverrideTransform.cs
Normal file
34
Src/Unity/Scripts/Rig/TickOverrideTransform.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITFALL.Rig
|
||||
{
|
||||
public class TickOverrideTransform : MonoBehaviour
|
||||
{
|
||||
public Transform Source;
|
||||
public Transform Target;
|
||||
|
||||
private int Id;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
Id = GetInstanceID();
|
||||
TickOverrideTranformService.Register(Id,this);
|
||||
}
|
||||
private void OnDisable()
|
||||
{
|
||||
TickOverrideTranformService.UnRegister(Id);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void OnValidate()
|
||||
{
|
||||
if(!Source)Source = transform;
|
||||
UnityEditor.EditorUtility.SetDirty(this);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
11
Src/Unity/Scripts/Rig/TickOverrideTransform.cs.meta
Normal file
11
Src/Unity/Scripts/Rig/TickOverrideTransform.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 14f045e623587104d8c1360ce31ea92f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user