use spring.transformIndexOffset

This commit is contained in:
ousttrue
2024-11-18 16:30:16 +09:00
parent 264a6bcf78
commit 84b64c0be2
4 changed files with 49 additions and 63 deletions

View File

@@ -60,10 +60,6 @@ namespace UniGLTF.SpringBoneJobs
Profiler.BeginSample("FastSpringBone.ReconstructBuffers.DisposeBuffers");
if (_combinedBuffer is FastSpringBoneCombinedBuffer combined)
{
Profiler.BeginSample("FastSpringBone.ReconstructBuffers.SaveToSourceBuffer");
combined.SaveToSourceBuffer();
Profiler.EndSample();
// TODO: Dispose せずに再利用?
combined.Dispose();
}

View File

@@ -97,7 +97,7 @@ namespace UniGLTF.SpringBoneJobs
springsCount += buffer.Springs.Length;
collidersCount += buffer.Colliders.Length;
logicsCount += buffer.Logics.Length;
transformsCount += buffer.BlittableTransforms.Length;
transformsCount += buffer.Transforms.Length;
}
Profiler.EndSample();
@@ -112,6 +112,23 @@ namespace UniGLTF.SpringBoneJobs
private JobHandle Batching(JobHandle handle)
{
// TransformAccessArrayの構築
Profiler.BeginSample("FastSpringBone.ReconstructBuffers.LoadTransformAccessArray");
var transforms = new Transform[_transforms.Length];
var transformAccessArrayOffset = 0;
foreach (var buffer in _batchedBuffers)
{
Array.Copy(buffer.Transforms, 0, transforms, transformAccessArrayOffset, buffer.Transforms.Length);
transformAccessArrayOffset += buffer.Transforms.Length;
}
_transformAccessArray = new TransformAccessArray(transforms);
Profiler.EndSample();
handle = new PullTransformJob
{
Transforms = Transforms
}.Schedule(TransformAccessArray, handle);
Profiler.BeginSample("FastSpringBone.ReconstructBuffers.ScheduleLoadBufferJobs");
var springsOffset = 0;
var collidersOffset = 0;
@@ -129,14 +146,6 @@ namespace UniGLTF.SpringBoneJobs
_jointMap.Add(buffer.Transforms[head], logicsOffset + j);
}
// バッファの読み込みをスケジュール
handle = new LoadTransformsJob
{
SrcTransforms = buffer.BlittableTransforms,
DestTransforms = new NativeSlice<BlittableTransform>(_transforms, transformOffset,
buffer.BlittableTransforms.Length)
}.Schedule(buffer.BlittableTransforms.Length, 1, handle);
handle = new LoadSpringsJob
{
ModelIndex = i,
@@ -165,26 +174,9 @@ namespace UniGLTF.SpringBoneJobs
springsOffset += buffer.Springs.Length;
collidersOffset += buffer.Colliders.Length;
logicsOffset += buffer.Logics.Length;
transformOffset += buffer.BlittableTransforms.Length;
transformOffset += buffer.Transforms.Length;
}
handle = InitCurrentTails(handle);
// TransformAccessArrayの構築と並行してJobを行うため、この時点で走らせておく
JobHandle.ScheduleBatchedJobs();
Profiler.EndSample();
// TransformAccessArrayの構築
Profiler.BeginSample("FastSpringBone.ReconstructBuffers.LoadTransformAccessArray");
var transforms = new Transform[_transforms.Length];
var transformAccessArrayOffset = 0;
foreach (var buffer in _batchedBuffers)
{
Array.Copy(buffer.Transforms, 0, transforms, transformAccessArrayOffset, buffer.Transforms.Length);
transformAccessArrayOffset += buffer.BlittableTransforms.Length;
}
_transformAccessArray = new TransformAccessArray(transforms);
Profiler.EndSample();
return handle;
@@ -328,29 +320,36 @@ namespace UniGLTF.SpringBoneJobs
#endif
private struct InitCurrentTailsJob : IJobParallelFor
{
[ReadOnly] public NativeArray<BlittableSpring> Springs;
[ReadOnly] public NativeArray<BlittableJointImmutable> Logics;
[ReadOnly] public NativeArray<BlittableTransform> Transforms;
[WriteOnly] public NativeSlice<Vector3> CurrentTails;
[WriteOnly] public NativeSlice<Vector3> PrevTails;
[WriteOnly] public NativeSlice<Vector3> NextTails;
[NativeDisableParallelForRestriction] public NativeSlice<Vector3> CurrentTails;
[NativeDisableParallelForRestriction] public NativeSlice<Vector3> PrevTails;
[NativeDisableParallelForRestriction] public NativeSlice<Vector3> NextTails;
public void Execute(int jointIndex)
public void Execute(int springIndex)
{
var tailIndex = Logics[jointIndex].tailTransformIndex;
if (tailIndex == -1)
var spring = Springs[springIndex];
for (int jointIndex = spring.logicSpan.startIndex; jointIndex < spring.logicSpan.EndIndex; ++jointIndex)
{
// tail 無い
var tail = Transforms[Logics[jointIndex].headTransformIndex];
CurrentTails[jointIndex] = tail.position;
PrevTails[jointIndex] = tail.position;
NextTails[jointIndex] = tail.position;
}
else
{
var tail = Transforms[tailIndex];
CurrentTails[jointIndex] = tail.position;
PrevTails[jointIndex] = tail.position;
NextTails[jointIndex] = tail.position;
var tailIndex = Logics[jointIndex].tailTransformIndex;
if (tailIndex == -1)
{
// tail 無い
var index = springIndex + Logics[jointIndex].headTransformIndex;
var tail = Transforms[index];
CurrentTails[jointIndex] = tail.position;
PrevTails[jointIndex] = tail.position;
NextTails[jointIndex] = tail.position;
}
else
{
var tail = Transforms[spring.transformIndexOffset + tailIndex];
CurrentTails[jointIndex] = tail.position;
PrevTails[jointIndex] = tail.position;
NextTails[jointIndex] = tail.position;
}
}
}
}
@@ -365,12 +364,14 @@ namespace UniGLTF.SpringBoneJobs
{
return new InitCurrentTailsJob
{
Springs = Springs,
Logics = Logics,
Transforms = Transforms,
CurrentTails = CurrentTails,
PrevTails = PrevTails,
NextTails = NextTails,
}.Schedule(Logics.Length, 1, handle);
}.Schedule(Springs.Length, 1, handle);
}
public void InitializeJointsLocalRotation(FastSpringBoneBuffer buffer)

View File

@@ -22,7 +22,6 @@ namespace UniGLTF.SpringBoneJobs.InputPorts
public NativeArray<BlittableJointMutable> Joints { get; }
public NativeArray<BlittableCollider> Colliders { get; }
public NativeArray<BlittableJointImmutable> Logics { get; }
public NativeArray<BlittableTransform> BlittableTransforms { get; }
public Transform[] Transforms { get; }
public bool IsDisposed { get; private set; }
@@ -103,16 +102,6 @@ namespace UniGLTF.SpringBoneJobs.InputPorts
Joints = new NativeArray<BlittableJointMutable>(blittableJoints.ToArray(), Allocator.Persistent);
Colliders = new NativeArray<BlittableCollider>(blittableColliders.ToArray(), Allocator.Persistent);
Logics = new NativeArray<BlittableJointImmutable>(blittableLogics.ToArray(), Allocator.Persistent);
BlittableTransforms = new NativeArray<BlittableTransform>(Transforms.Select(transform => new BlittableTransform
{
position = transform.position,
rotation = transform.rotation,
localPosition = transform.localPosition,
localRotation = transform.localRotation,
localScale = transform.localScale,
localToWorldMatrix = transform.localToWorldMatrix,
worldToLocalMatrix = transform.worldToLocalMatrix
}).ToArray(), Allocator.Persistent);
Profiler.EndSample();
}
@@ -157,7 +146,6 @@ namespace UniGLTF.SpringBoneJobs.InputPorts
IsDisposed = true;
Springs.Dispose();
Joints.Dispose();
BlittableTransforms.Dispose();
Colliders.Dispose();
Logics.Dispose();
}

View File

@@ -26,6 +26,7 @@ namespace UniVRM10
if (fastSpringBoneBuffer != null)
{
fastSpringBoneBuffer.Dispose();
fastSpringBoneBuffer = null;
}
Func<Transform, TransformState> GetOrAddDefaultTransformState = (Transform tf) =>
@@ -41,7 +42,7 @@ namespace UniVRM10
// create(Spring情報の再収集。設定変更の反映)
var springs = vrm.SpringBone.Springs.Select(spring => new FastSpringBoneSpring
{
{
center = spring.Center,
colliders = spring.ColliderGroups
.SelectMany(group => group.Colliders)