Skip to content

Commit

Permalink
Vrm10FastSpringboneRuntimeStandalone
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Sep 20, 2024
1 parent aac5680 commit e53ff87
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public bool IsSpringBoneEnabled
set => m_fastSpringBoneBuffer.IsSpringBoneEnabled = value;
}

public float DeltaTime => throw new NotImplementedException();

/// <param name="initialTransform">VRMの初期姿勢(T-Pose)状態。instanceがT-Poseから変化していても大丈夫</param>
public void Initialize(Vrm10Instance instance)
{
Expand Down Expand Up @@ -143,5 +145,10 @@ public void RestoreInitialTransform()

// TODO:
}

public void Process()
{
// FastSpringBoneService が実行するので何もしない
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using System;
using System.Linq;
using UniGLTF;
using UniGLTF.Utils;
using UnityEngine;
using UniGLTF.SpringBoneJobs.Blittables;
using UniGLTF.SpringBoneJobs.InputPorts;
using UniGLTF.SpringBoneJobs;

namespace UniVRM10
{
/// <summary>
/// FastSpringbone(job) で動作します。
/// FastSpringBoneService(Singleton)を経由せずに直接実行します。
/// </summary>
public class Vrm10FastSpringboneRuntimeStandalone : IVrm10SpringBoneRuntime
{
private Vrm10Instance m_instance;
private FastSpringBoneSpring[] m_springs;
private Quaternion[] m_initialLocalRotations;
private FastSpringBoneBuffer m_fastSpringBoneBuffer;
public FastSpringBoneBufferCombiner m_bufferCombiner = new();
private FastSpringBoneScheduler m_fastSpringBoneScheduler;

public Vector3 ExternalForce
{
get => m_fastSpringBoneBuffer.ExternalForce;
set => m_fastSpringBoneBuffer.ExternalForce = value;
}
public bool IsSpringBoneEnabled
{
get => m_fastSpringBoneBuffer.IsSpringBoneEnabled;
set => m_fastSpringBoneBuffer.IsSpringBoneEnabled = value;
}

public float DeltaTime => Time.deltaTime;

public Vrm10FastSpringboneRuntimeStandalone()
{
m_fastSpringBoneScheduler = new(m_bufferCombiner);
}

/// <param name="initialTransform">VRMの初期姿勢(T-Pose)状態。instanceがT-Poseから変化していても大丈夫</param>
public void Initialize(Vrm10Instance instance)
{
m_instance = instance;

// NOTE: FastSpringBoneService は UnitTest などでは動作しない
if (Application.isPlaying)
{
ReconstructSpringBone();
}
}

public void Dispose()
{
m_bufferCombiner.Unregister(m_fastSpringBoneBuffer);
m_fastSpringBoneBuffer.Dispose();
m_bufferCombiner.Dispose();
}

/// <summary>
/// このVRMに紐づくSpringBone関連のバッファを再構築する
/// ランタイム実行時にSpringBoneに対して変更を行いたいときは、このメソッドを明示的に呼ぶ必要がある
/// </summary>
public void ReconstructSpringBone()
{
// release
if (m_fastSpringBoneBuffer != null)
{
m_bufferCombiner.Unregister(m_fastSpringBoneBuffer);
m_fastSpringBoneBuffer.Dispose();
}

// create(Spring情報の再収集。設定変更の反映)
m_springs = m_instance.SpringBone.Springs.Select(spring => new FastSpringBoneSpring
{
center = spring.Center,
colliders = spring.ColliderGroups
.SelectMany(group => group.Colliders)
.Select(collider => new FastSpringBoneCollider
{
Transform = collider.transform,
Collider = new BlittableCollider
{
offset = collider.Offset,
radius = collider.Radius,
tailOrNormal = collider.TailOrNormal,
colliderType = TranslateColliderType(collider.ColliderType)
}
}).ToArray(),
joints = spring.Joints
.Select(joint => new FastSpringBoneJoint
{
Transform = joint.transform,
Joint = new BlittableJointMutable
{
radius = joint.m_jointRadius,
dragForce = joint.m_dragForce,
gravityDir = joint.m_gravityDir,
gravityPower = joint.m_gravityPower,
stiffnessForce = joint.m_stiffnessForce
},
DefaultLocalRotation = GetOrAddDefaultTransformState(joint.transform).LocalRotation,
}).ToArray(),
}).ToArray();

// DOTS buffer 構築
m_fastSpringBoneBuffer = new FastSpringBoneBuffer(m_springs);
m_bufferCombiner.Register(m_fastSpringBoneBuffer);
// reset 用の初期状態の記録
m_initialLocalRotations = m_fastSpringBoneBuffer.Transforms.Select(x => x.localRotation).ToArray();
}

private TransformState GetOrAddDefaultTransformState(Transform tf)
{
if (m_instance.DefaultTransformStates.TryGetValue(tf, out var defaultTransformState))
{
return defaultTransformState;
}

Debug.LogWarning($"{tf.name} does not exist on load.");
return new TransformState(null);
}

private static BlittableColliderType TranslateColliderType(VRM10SpringBoneColliderTypes colliderType)
{
switch (colliderType)
{
case VRM10SpringBoneColliderTypes.Sphere:
return BlittableColliderType.Sphere;
case VRM10SpringBoneColliderTypes.Capsule:
return BlittableColliderType.Capsule;
case VRM10SpringBoneColliderTypes.Plane:
return BlittableColliderType.Plane;
case VRM10SpringBoneColliderTypes.SphereInside:
return BlittableColliderType.SphereInside;
case VRM10SpringBoneColliderTypes.CapsuleInside:
return BlittableColliderType.CapsuleInside;
default:
throw new ArgumentOutOfRangeException();
}
}

public void RestoreInitialTransform()
{
// Spring の joint に対応する transform の回転を初期状態
for (int i = 0; i < m_fastSpringBoneBuffer.Transforms.Length; ++i)
{
var transform = m_fastSpringBoneBuffer.Transforms[i];
transform.localRotation = m_initialLocalRotations[i];
}

// TODO:
}

public void Process()
{
m_fastSpringBoneScheduler.Schedule(DeltaTime).Complete();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/VRM10/Runtime/Components/Vrm10Runtime/Vrm10Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public void Process()
// 5. Apply Expression
// LookAt の角度制限などはこちらで処理されます。
Expression.Process(eyeDirection);

// 6. SpringBone
SpringBone.Process();
}
}
}
4 changes: 4 additions & 0 deletions Assets/VRM10/Runtime/IO/IVrm10SpringBoneRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ public interface IVrm10SpringBoneRuntime : IDisposable
public Vector3 ExternalForce { get; set; }

public bool IsSpringBoneEnabled { get; set; }

public float DeltaTime { get; }

public void Process();
}
}

0 comments on commit e53ff87

Please sign in to comment.