From 5bda6740ff077b6d49c340597b46be82eb16e7dd Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 19 Nov 2024 03:53:34 +0900 Subject: [PATCH] =?UTF-8?q?buffer.BackupCurrentTails=20=E3=82=92=20buffer?= =?UTF-8?q?=20=E5=A2=97=E6=B8=9B=E3=81=AE=E7=9B=B4=E5=89=8D=E3=81=AB?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FastSpringBoneBufferCombiner.cs | 70 +++++++++++-------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/Assets/UniGLTF/Runtime/SpringBoneJobs/FastSpringBoneBufferCombiner.cs b/Assets/UniGLTF/Runtime/SpringBoneJobs/FastSpringBoneBufferCombiner.cs index 813d8ae90..a43d5b439 100644 --- a/Assets/UniGLTF/Runtime/SpringBoneJobs/FastSpringBoneBufferCombiner.cs +++ b/Assets/UniGLTF/Runtime/SpringBoneJobs/FastSpringBoneBufferCombiner.cs @@ -5,6 +5,7 @@ using UnityEngine.Profiling; using UniGLTF.SpringBoneJobs.InputPorts; using UnityEngine; using Unity.Collections; +using System.Linq; namespace UniGLTF.SpringBoneJobs { @@ -21,37 +22,17 @@ namespace UniGLTF.SpringBoneJobs private FastSpringBoneCombinedBuffer _combinedBuffer; public FastSpringBoneCombinedBuffer Combined => _combinedBuffer; private readonly LinkedList _buffers = new LinkedList(); - private bool _isDirty; + private Queue<(bool isAdd, FastSpringBoneBuffer buffer)> _request = new(); public bool HasBuffer => _buffers.Count > 0 && _combinedBuffer != null; public void Register(FastSpringBoneBuffer buffer) { - _buffers.AddLast(buffer); - _isDirty = true; + _request.Enqueue((true, buffer)); } - public void Unregister(FastSpringBoneBuffer remove) + public void Unregister(FastSpringBoneBuffer buffer) { - // index が変わる前に シミュレーションの状態を保存する。 - // 状態の保存場所が BlittableJoint から CurrentTails に移動しているのでここでやる。 - if (_combinedBuffer is FastSpringBoneCombinedBuffer combined) - { - var logicsIndex = 0; - foreach (var buffer in _buffers) - { - if (buffer == remove) - { - // 削除するので skip - // joint の位置が変わる可能性があるので状態を保存せずに速度を0にする方がよい - continue; - } - buffer.BackupCurrentTails(combined.CurrentTails, logicsIndex); - logicsIndex += buffer.Logics.Length; - } - } - - _buffers.Remove(remove); - _isDirty = true; + _request.Enqueue((false, buffer)); } /// @@ -59,14 +40,45 @@ namespace UniGLTF.SpringBoneJobs /// public JobHandle ReconstructIfDirty(JobHandle handle) { - if (_isDirty) + if (_request.Count == 0) { - var result = ReconstructBuffers(handle); - _isDirty = false; - return result; + return handle; } - return handle; + if (_combinedBuffer is FastSpringBoneCombinedBuffer combined) + { + // index が変わる前に シミュレーションの状態を保存する。 + // 状態の保存場所が BlittableJoint から CurrentTails に移動しているのでここでやる。 + var logicsIndex = 0; + foreach (var buffer in _buffers) + { + if (_request.Any(x => !x.isAdd && x.buffer == buffer)) + { + // 削除するので skip + continue; + } + buffer.BackupCurrentTails(combined.CurrentTails, logicsIndex); + logicsIndex += buffer.Logics.Length; + } + } + + // buffer 増減 + while (_request.Count > 0) + { + var (isAdd, buffer) = _request.Dequeue(); + if (isAdd) + { + // 速度 0 にする + _buffers.AddLast(buffer); + } + else + { + _buffers.Remove(buffer); + } + } + + // 再構築 + return ReconstructBuffers(handle); } ///