mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-08 20:08:53 -05:00
Add fastSpringBone10
This commit is contained in:
8
Assets/VRM10/Runtime/FastSpringBone.meta
Normal file
8
Assets/VRM10/Runtime/FastSpringBone.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 197e77a1310790d4786b7d9adca918b6
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/VRM10/Runtime/FastSpringBone/Blittables.meta
Normal file
8
Assets/VRM10/Runtime/FastSpringBone/Blittables.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e9d242d5eb124c341a1685fa016f73b7
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,20 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace VRM.FastSpringBones.Blittables
|
||||
{
|
||||
/// <summary>
|
||||
/// VRMSpringBoneのSphereColliderをBlittableにしたもの
|
||||
/// 位置情報は親であるColliderGroupが持つ
|
||||
/// </summary>
|
||||
public readonly struct BlittableCollider
|
||||
{
|
||||
public Vector3 Offset { get; }
|
||||
public float Radius { get; }
|
||||
|
||||
public BlittableCollider(Vector3 offset, float radius)
|
||||
{
|
||||
Offset = offset;
|
||||
Radius = radius;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 67352bbe9afc01940af2be44aa1416fc
|
||||
timeCreated: 1550209189
|
||||
@@ -0,0 +1,20 @@
|
||||
using Unity.Collections;
|
||||
using Unity.Collections.LowLevel.Unsafe;
|
||||
|
||||
namespace VRM.FastSpringBones.Blittables
|
||||
{
|
||||
/// <summary>
|
||||
/// VRMSpringBoneのColliderGroupをBlittableにしたもの
|
||||
/// </summary>
|
||||
public readonly struct BlittableColliderGroup
|
||||
{
|
||||
public BlittableColliders Colliders { get; }
|
||||
public unsafe BlittableTransform* Transform { get; }
|
||||
|
||||
public unsafe BlittableColliderGroup(NativeArray<BlittableCollider> colliders, BlittableTransform* transform)
|
||||
{
|
||||
Colliders = new BlittableColliders((BlittableCollider*)colliders.GetUnsafePtr(), colliders.Length);
|
||||
Transform = transform;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 478b0f68277d5a346a9e909e398451aa
|
||||
timeCreated: 1550223602
|
||||
@@ -0,0 +1,34 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace VRM.FastSpringBones.Blittables
|
||||
{
|
||||
/// <summary>
|
||||
/// BlittableColliderGroupのポインタの配列
|
||||
/// </summary>
|
||||
public readonly unsafe struct BlittableColliderGroups
|
||||
{
|
||||
private readonly BlittableColliderGroup* _data;
|
||||
public int Length { get; }
|
||||
|
||||
public BlittableColliderGroup this[int i] => _data[i];
|
||||
|
||||
public void DrawGizmos()
|
||||
{
|
||||
for (var i = 0; i < Length; i++)
|
||||
{
|
||||
var group = this[i];
|
||||
var colliders = group.Colliders;
|
||||
for (var j = 0; j < colliders.Count; ++j)
|
||||
{
|
||||
Gizmos.DrawWireSphere(group.Transform->WorldPosition, colliders[j].Radius);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BlittableColliderGroups(BlittableColliderGroup* data, int length)
|
||||
{
|
||||
_data = data;
|
||||
Length = length;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d8ecae557f477ff4fa60ef840e6b6167
|
||||
timeCreated: 1550563011
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace VRM.FastSpringBones.Blittables
|
||||
{
|
||||
/// <summary>
|
||||
/// BlittableColliderのポインタの配列
|
||||
/// </summary>
|
||||
public unsafe struct BlittableColliders
|
||||
{
|
||||
private readonly BlittableCollider* _colliders;
|
||||
public int Count { get; }
|
||||
|
||||
public BlittableCollider this[int i] => _colliders[i];
|
||||
|
||||
public BlittableColliders(BlittableCollider* colliders, int count)
|
||||
{
|
||||
_colliders = colliders;
|
||||
Count = count;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 14be485b03404da4a9a5ba19211f9c1b
|
||||
timeCreated: 1550562383
|
||||
149
Assets/VRM10/Runtime/FastSpringBone/Blittables/BlittablePoint.cs
Normal file
149
Assets/VRM10/Runtime/FastSpringBone/Blittables/BlittablePoint.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace VRM.FastSpringBones.Blittables
|
||||
{
|
||||
/// <summary>
|
||||
/// VRMSpringBoneLogicをBlittableにしたもの
|
||||
/// ベルレ積分など、コアな計算を行う
|
||||
/// </summary>
|
||||
public unsafe struct BlittablePoint
|
||||
{
|
||||
private readonly float _length;
|
||||
private readonly Quaternion _localRotation;
|
||||
private readonly Vector3 _boneAxis;
|
||||
private readonly float _radius;
|
||||
private Vector3 _prevPosition;
|
||||
|
||||
private readonly BlittableColliderGroups* _blittableColliderGroups;
|
||||
private readonly BlittableTransform* _center;
|
||||
|
||||
public Vector3 CurrentPosition { get; private set; }
|
||||
|
||||
private readonly BlittableTransform* _transform;
|
||||
|
||||
public BlittablePoint(
|
||||
Transform transform,
|
||||
float radius,
|
||||
BlittableTransform* center,
|
||||
BlittableColliderGroups* blittableColliderGroups,
|
||||
BlittableTransform* blittableTransform)
|
||||
{
|
||||
Vector3 localPosition;
|
||||
if (transform.childCount == 0)
|
||||
{
|
||||
var delta = transform.position - transform.parent.position;
|
||||
var childPosition = transform.position + delta.normalized * 0.07f;
|
||||
localPosition = transform.worldToLocalMatrix.MultiplyPoint(childPosition);
|
||||
}
|
||||
else
|
||||
{
|
||||
var firstChild = transform.GetChild(0);
|
||||
var scale = firstChild.lossyScale;
|
||||
localPosition = firstChild.localPosition;
|
||||
localPosition.x *= scale.x;
|
||||
localPosition.y *= scale.y;
|
||||
localPosition.z *= scale.z;
|
||||
}
|
||||
|
||||
var worldChildPosition = (Vector3)transform.TransformPoint(localPosition);
|
||||
_prevPosition = CurrentPosition = center != null ? center->LocalToWorld.inverse.MultiplyPoint3x4(worldChildPosition) : worldChildPosition;
|
||||
|
||||
_localRotation = transform.localRotation;
|
||||
_boneAxis = localPosition.normalized;
|
||||
_length = localPosition.magnitude;
|
||||
_radius = radius;
|
||||
_blittableColliderGroups = blittableColliderGroups;
|
||||
_transform = blittableTransform;
|
||||
_center = center;
|
||||
}
|
||||
|
||||
public void Update(float stiffnessForce, float dragForce, Vector3 external)
|
||||
{
|
||||
// 親のRotationが変わっている可能性があるので更新する
|
||||
_transform->UpdateLocalToWorldMatrix();
|
||||
|
||||
Vector3 currentPosition;
|
||||
Vector3 prevPosition;
|
||||
|
||||
if (_center == null)
|
||||
{
|
||||
currentPosition = CurrentPosition;
|
||||
prevPosition = _prevPosition;
|
||||
}
|
||||
else
|
||||
{
|
||||
var centerLocalToWorld = _center->LocalToWorld;
|
||||
currentPosition = centerLocalToWorld.MultiplyPoint3x4(CurrentPosition);
|
||||
prevPosition = centerLocalToWorld.MultiplyPoint3x4(_prevPosition);
|
||||
}
|
||||
|
||||
// verlet積分で次の位置を計算
|
||||
var nextPosition = currentPosition
|
||||
+ (currentPosition - prevPosition) * (1.0f - dragForce) // 前フレームの移動を継続する(減衰もあるよ)
|
||||
+ _transform->ParentWorldRotation * _localRotation * _boneAxis * stiffnessForce // 親の回転による子ボーンの移動目標
|
||||
+ external; // 外力による移動量
|
||||
|
||||
// 長さをboneLengthに強制
|
||||
var position = _transform->WorldPosition;
|
||||
nextPosition = position + (nextPosition - position).normalized * _length;
|
||||
|
||||
nextPosition = Collision(nextPosition, position);
|
||||
|
||||
if (_center == null)
|
||||
{
|
||||
_prevPosition = currentPosition;
|
||||
CurrentPosition = nextPosition;
|
||||
}
|
||||
else
|
||||
{
|
||||
var centerWorldToLocal = _center->LocalToWorld.inverse;
|
||||
_prevPosition = centerWorldToLocal.MultiplyPoint3x4(currentPosition);
|
||||
CurrentPosition = centerWorldToLocal.MultiplyPoint3x4(nextPosition);
|
||||
}
|
||||
|
||||
//回転を適用
|
||||
_transform->SetWorldRotation(ApplyRotation(nextPosition));
|
||||
}
|
||||
|
||||
private Vector3 Collision(Vector3 nextPosition, Vector3 position)
|
||||
{
|
||||
for (var i = 0; i < _blittableColliderGroups->Length; ++i)
|
||||
{
|
||||
var colliderGroup = (*_blittableColliderGroups)[i];
|
||||
|
||||
for (var j = 0; j < colliderGroup.Colliders.Count; ++j)
|
||||
{
|
||||
var collider = colliderGroup.Colliders[j];
|
||||
var colliderPosition = colliderGroup.Transform->TransformPoint(collider.Offset);
|
||||
var r = _radius + collider.Radius;
|
||||
|
||||
if (!((nextPosition - colliderPosition).sqrMagnitude <= (r * r))) continue;
|
||||
|
||||
// ヒット。Colliderの半径方向に押し出す
|
||||
var normal = (nextPosition - colliderPosition).normalized;
|
||||
var posFromCollider = colliderPosition + normal * (_radius + collider.Radius);
|
||||
|
||||
// 長さをboneLengthに強制
|
||||
nextPosition = position + (posFromCollider - position).normalized * _length;
|
||||
}
|
||||
}
|
||||
return nextPosition;
|
||||
}
|
||||
|
||||
private static Quaternion FromToRotation(Vector3 from, Vector3 to)
|
||||
=> Quaternion.AxisAngle(
|
||||
angle: Mathf.Acos(Mathf.Clamp(Vector3.Dot(from.normalized, to.normalized), -1f, 1f)),
|
||||
axis: Vector3.Cross(from, to).normalized
|
||||
);
|
||||
|
||||
private Quaternion ApplyRotation(Vector3 nextTail)
|
||||
{
|
||||
var rotation = _transform->ParentWorldRotation * _localRotation;
|
||||
return
|
||||
FromToRotation(
|
||||
rotation * _boneAxis,
|
||||
nextTail - _transform->WorldPosition) *
|
||||
rotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8299788b0fbfe574397e8940e718f50f
|
||||
timeCreated: 1549430605
|
||||
@@ -0,0 +1,23 @@
|
||||
namespace VRM.FastSpringBones.Blittables
|
||||
{
|
||||
/// <summary>
|
||||
/// BlittablePointのポインタの配列
|
||||
/// </summary>
|
||||
public unsafe struct BlittablePoints
|
||||
{
|
||||
private readonly BlittablePoint* _points;
|
||||
public int Count { get; }
|
||||
|
||||
public BlittablePoint this[int i]
|
||||
{
|
||||
get => _points[i];
|
||||
set => _points[i] = value;
|
||||
}
|
||||
|
||||
public BlittablePoints(BlittablePoint* points, int count)
|
||||
{
|
||||
_points = points;
|
||||
Count = count;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 55f68ce7085cb7640a50cb133741361b
|
||||
timeCreated: 1550721598
|
||||
@@ -0,0 +1,64 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace VRM.FastSpringBones.Blittables
|
||||
{
|
||||
/// <summary>
|
||||
/// 1本の毛束を示すBlittable型
|
||||
/// </summary>
|
||||
public unsafe readonly struct BlittableRootBone
|
||||
{
|
||||
private readonly float _gravityPower;
|
||||
private readonly Vector3 _gravityDir;
|
||||
private readonly float _dragForce;
|
||||
private readonly float _stiffnessForce;
|
||||
private readonly BlittablePoints* _blittablePoints;
|
||||
|
||||
public void DrawGizmos()
|
||||
{
|
||||
for (var i = 0; i < _blittablePoints->Count; i++)
|
||||
{
|
||||
var point = (*_blittablePoints)[i];
|
||||
Gizmos.DrawWireSphere(point.CurrentPosition, 0.05f);
|
||||
}
|
||||
for (var i = 0; i < _blittablePoints->Count - 1; i++)
|
||||
{
|
||||
var point1 = (*_blittablePoints)[i];
|
||||
var point2 = (*_blittablePoints)[i + 1];
|
||||
Gizmos.DrawLine(point1.CurrentPosition, point2.CurrentPosition);
|
||||
}
|
||||
}
|
||||
|
||||
public BlittableRootBone(
|
||||
float gravityPower,
|
||||
Vector3 gravityDir,
|
||||
float dragForce,
|
||||
float stiffnessForce,
|
||||
BlittablePoints* blittablePoints)
|
||||
{
|
||||
_gravityPower = gravityPower;
|
||||
_gravityDir = gravityDir;
|
||||
_dragForce = dragForce;
|
||||
_stiffnessForce = stiffnessForce;
|
||||
_blittablePoints = blittablePoints;
|
||||
}
|
||||
|
||||
public void Update(float deltaTime)
|
||||
{
|
||||
var stiffness = _stiffnessForce * deltaTime;
|
||||
var external = _gravityDir * (_gravityPower * deltaTime);
|
||||
for (var i = 0; i < _blittablePoints->Count; i++)
|
||||
{
|
||||
var point = (*_blittablePoints)[i];
|
||||
|
||||
// Pointを更新
|
||||
point.Update(
|
||||
stiffness,
|
||||
_dragForce,
|
||||
external
|
||||
);
|
||||
|
||||
(*_blittablePoints)[i] = point;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 30738bbbf18309448a80f7944ec2aef3
|
||||
timeCreated: 1549952425
|
||||
@@ -0,0 +1,72 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.Jobs;
|
||||
|
||||
namespace VRM.FastSpringBones.Blittables
|
||||
{
|
||||
/// <summary>
|
||||
/// Transformの必要な機能だけを絞り、Blittableに対応させたクラス
|
||||
/// </summary>
|
||||
public unsafe struct BlittableTransform
|
||||
{
|
||||
private readonly BlittableTransform* _parent;
|
||||
private Quaternion _worldRotation;
|
||||
private Vector3 _localPosition;
|
||||
private Vector3 _localScale;
|
||||
private Quaternion _localRotation;
|
||||
private Matrix4x4 _localToWorld;
|
||||
|
||||
public Vector3 WorldPosition { get; private set; }
|
||||
|
||||
public void SetWorldRotation(Quaternion rotation)
|
||||
{
|
||||
var parentWorldRotation = ParentWorldRotation;
|
||||
_localRotation = Quaternion.Inverse(parentWorldRotation) * rotation;
|
||||
UpdateLocalToWorldMatrix();
|
||||
}
|
||||
|
||||
public Matrix4x4 LocalToWorld => _localToWorld;
|
||||
|
||||
private Matrix4x4 LocalTransform => Matrix4x4.TRS(_localPosition, _localRotation, _localScale);
|
||||
|
||||
public Quaternion ParentWorldRotation => _parent != null ? _parent->_worldRotation : Quaternion.identity;
|
||||
|
||||
public BlittableTransform(BlittableTransform* parent, Transform transform)
|
||||
{
|
||||
_parent = parent;
|
||||
|
||||
WorldPosition = transform.position;
|
||||
_worldRotation = transform.rotation;
|
||||
_localPosition = transform.localPosition;
|
||||
_localRotation = transform.localRotation;
|
||||
_localScale = transform.localScale;
|
||||
|
||||
_localToWorld = transform.localToWorldMatrix;
|
||||
}
|
||||
|
||||
public void PullFrom(TransformAccess transform)
|
||||
{
|
||||
WorldPosition = transform.position;
|
||||
_worldRotation = transform.rotation;
|
||||
_localPosition = transform.localPosition;
|
||||
_localRotation = transform.localRotation;
|
||||
_localScale = transform.localScale;
|
||||
|
||||
_localToWorld = transform.localToWorldMatrix;
|
||||
}
|
||||
|
||||
public void PushTo(TransformAccess transform)
|
||||
{
|
||||
transform.localPosition = _localPosition;
|
||||
transform.localRotation = _localRotation;
|
||||
}
|
||||
|
||||
public Vector3 TransformPoint(Vector3 offset) => _localToWorld.MultiplyPoint3x4(offset);
|
||||
|
||||
public void UpdateLocalToWorldMatrix()
|
||||
{
|
||||
_localToWorld = _parent == null ? LocalTransform : _parent->_localToWorld * LocalTransform;
|
||||
WorldPosition = _localToWorld.MultiplyPoint3x4(Vector3.zero);
|
||||
_worldRotation = _localToWorld.rotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 95fac2f4af221054f99dbe868d26fc4b
|
||||
timeCreated: 1550544819
|
||||
8
Assets/VRM10/Runtime/FastSpringBone/Components.meta
Normal file
8
Assets/VRM10/Runtime/FastSpringBone/Components.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57b934d4e245cef49acc06524c3e1f7d
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,31 @@
|
||||
using UnityEngine;
|
||||
using VRM.FastSpringBones.Blittables;
|
||||
using VRM.FastSpringBones.NativeWrappers;
|
||||
using VRM.FastSpringBones.Registries;
|
||||
|
||||
namespace VRM.FastSpringBones.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// VRMSpringBoneColliderGroupに対応したクラス
|
||||
/// バッファの作成も行う
|
||||
/// </summary>
|
||||
public sealed unsafe class FastSpringBoneColliderGroup : MonoBehaviour
|
||||
{
|
||||
private NativeTransform _nativeTransform;
|
||||
private NativeColliderGroup _nativeColliderGroup;
|
||||
|
||||
public BlittableColliderGroup* ColliderGroupPtr => _nativeColliderGroup.GetUnsafePtr();
|
||||
|
||||
public void Initialize(TransformRegistry transformRegistry, BlittableCollider[] colliders)
|
||||
{
|
||||
_nativeTransform = new NativeTransform(transformRegistry, TransformSynchronizationType.PullOnly, transform);
|
||||
_nativeColliderGroup = new NativeColliderGroup(colliders, _nativeTransform);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_nativeTransform?.Dispose();
|
||||
_nativeColliderGroup?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b5fe6672f5b7b1049bb9e1bed44ee015
|
||||
timeCreated: 1550213674
|
||||
@@ -0,0 +1,100 @@
|
||||
using Unity.Jobs;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Profiling;
|
||||
using VRM.FastSpringBones.Registries;
|
||||
using VRM.FastSpringBones.Schedulers;
|
||||
|
||||
namespace VRM.FastSpringBones.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// Jobを連続して発火させるComponent
|
||||
/// シーンに1つだけあればいい
|
||||
/// </summary>
|
||||
[DefaultExecutionOrder(11000)]
|
||||
public sealed class FastSpringBoneScheduler : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private bool showGizmos;
|
||||
|
||||
private CustomSampler _updateSampler;
|
||||
|
||||
private PullTransformJobScheduler _pullTransformJobScheduler;
|
||||
private PushTransformJobScheduler _pushTransformJobScheduler;
|
||||
private UpdateSpringBoneJobScheduler _updateSpringBoneJobScheduler;
|
||||
|
||||
private RootBoneRegistry _rootBoneRegistry;
|
||||
private ColliderGroupRegistry _colliderGroupRegistry;
|
||||
|
||||
private JobHandle _prevJobHandle;
|
||||
|
||||
public bool ShowGizmos { get => showGizmos; set => showGizmos = value; }
|
||||
|
||||
public void Initialize(
|
||||
RootBoneRegistry rootBoneRegistry,
|
||||
TransformRegistry transformRegistry,
|
||||
ColliderGroupRegistry colliderGroupRegistry)
|
||||
{
|
||||
_rootBoneRegistry = rootBoneRegistry;
|
||||
_colliderGroupRegistry = colliderGroupRegistry;
|
||||
|
||||
_updateSampler = CustomSampler.Create("FastSpringBone(Update)");
|
||||
|
||||
_pullTransformJobScheduler = new PullTransformJobScheduler(transformRegistry);
|
||||
_pushTransformJobScheduler = new PushTransformJobScheduler(transformRegistry);
|
||||
_updateSpringBoneJobScheduler = new UpdateSpringBoneJobScheduler(_rootBoneRegistry);
|
||||
|
||||
_rootBoneRegistry.SubscribeOnValueChanged(OnRootBoneChanged);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_rootBoneRegistry.UnSubscribeOnValueChanged(OnRootBoneChanged);
|
||||
_prevJobHandle.Complete();
|
||||
|
||||
_pullTransformJobScheduler.Dispose();
|
||||
_pushTransformJobScheduler.Dispose();
|
||||
_updateSpringBoneJobScheduler.Dispose();
|
||||
}
|
||||
|
||||
private void OnRootBoneChanged()
|
||||
{
|
||||
_prevJobHandle.Complete();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
if (!ShowGizmos) return;
|
||||
|
||||
_prevJobHandle.Complete();
|
||||
|
||||
Gizmos.color = Color.blue;
|
||||
foreach (var rootBoneWrapper in _rootBoneRegistry.Items)
|
||||
{
|
||||
rootBoneWrapper.Value.DrawGizmos();
|
||||
}
|
||||
|
||||
Gizmos.color = Color.yellow;
|
||||
foreach (var colliderGroup in _colliderGroupRegistry.Items)
|
||||
{
|
||||
colliderGroup.DrawGizmos();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
_updateSampler.Begin();
|
||||
|
||||
_prevJobHandle.Complete();
|
||||
|
||||
var tempJobHandle = default(JobHandle);
|
||||
tempJobHandle = _pullTransformJobScheduler.Schedule(tempJobHandle);
|
||||
tempJobHandle = _updateSpringBoneJobScheduler.Schedule(tempJobHandle);
|
||||
tempJobHandle = _pushTransformJobScheduler.Schedule(tempJobHandle);
|
||||
|
||||
_prevJobHandle = tempJobHandle;
|
||||
|
||||
_updateSampler.End();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3affa859639024f439b13d4cd4a9b0f6
|
||||
timeCreated: 1549521562
|
||||
@@ -0,0 +1,124 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using VRM.FastSpringBones.Blittables;
|
||||
using VRM.FastSpringBones.NativeWrappers;
|
||||
using VRM.FastSpringBones.Registries;
|
||||
|
||||
namespace VRM.FastSpringBones.Components
|
||||
{
|
||||
/// <summary>
|
||||
/// SpringBoneの1本の毛束の処理を担当するクラス
|
||||
/// </summary>
|
||||
public sealed class FastSpringRootBone : IDisposable
|
||||
{
|
||||
private readonly TransformRegistry _transformRegistry;
|
||||
private readonly RootBoneRegistry _rootBoneRegistry;
|
||||
private readonly ColliderGroupRegistry _colliderGroupRegistry;
|
||||
private readonly Transform _transform;
|
||||
|
||||
private float _radius;
|
||||
private NativeTransform _center;
|
||||
|
||||
private IReadOnlyDictionary<Transform, int> _transformIndexMap;
|
||||
private NativeColliderGroups _nativeColliderGroups;
|
||||
|
||||
private NativePoints _nativePoints;
|
||||
private NativePointer<BlittableRootBone> _rootBoneWrapper;
|
||||
|
||||
private readonly IList<NativeTransform> _transformWrappers = new List<NativeTransform>();
|
||||
private readonly IList<NativePointer<BlittablePoint>> _points = new List<NativePointer<BlittablePoint>>();
|
||||
|
||||
public FastSpringRootBone(
|
||||
TransformRegistry transformRegistry,
|
||||
Transform transform,
|
||||
RootBoneRegistry rootBoneRegistry,
|
||||
ColliderGroupRegistry colliderGroupRegistry
|
||||
)
|
||||
{
|
||||
_transformRegistry = transformRegistry;
|
||||
_transform = transform;
|
||||
_rootBoneRegistry = rootBoneRegistry;
|
||||
_colliderGroupRegistry = colliderGroupRegistry;
|
||||
}
|
||||
|
||||
public IReadOnlyList<FastSpringBoneColliderGroup> ColliderGroups
|
||||
{
|
||||
get => _nativeColliderGroups.ColliderGroups;
|
||||
set => _nativeColliderGroups.ColliderGroups = value;
|
||||
}
|
||||
|
||||
public unsafe void Initialize(
|
||||
float gravityPower,
|
||||
Vector3 gravityDir,
|
||||
float dragForce,
|
||||
float stiffnessForce,
|
||||
IReadOnlyList<FastSpringBoneColliderGroup> colliderGroups,
|
||||
float radius,
|
||||
Transform center)
|
||||
{
|
||||
_radius = radius;
|
||||
if (center != null)
|
||||
{
|
||||
_center = new NativeTransform(_transformRegistry, TransformSynchronizationType.PullOnly, center);
|
||||
}
|
||||
|
||||
_nativeColliderGroups = new NativeColliderGroups(colliderGroups);
|
||||
|
||||
NativeTransform parent = null;
|
||||
if (_transform.parent)
|
||||
{
|
||||
parent = new NativeTransform(_transformRegistry, TransformSynchronizationType.PullOnly, _transform.parent);
|
||||
}
|
||||
SetupRecursive(_transform, parent);
|
||||
|
||||
_nativePoints = new NativePoints(_points);
|
||||
|
||||
_rootBoneWrapper = new NativePointer<BlittableRootBone>(new BlittableRootBone(gravityPower, gravityDir, dragForce, stiffnessForce, _nativePoints.GetUnsafePtr()));
|
||||
_rootBoneRegistry.Register(_rootBoneWrapper);
|
||||
_colliderGroupRegistry.Register(_nativeColliderGroups);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_colliderGroupRegistry.Unregister(_nativeColliderGroups);
|
||||
_rootBoneRegistry.Unregister(_rootBoneWrapper);
|
||||
|
||||
foreach (var transformWrapper in _transformWrappers)
|
||||
{
|
||||
transformWrapper.Dispose();
|
||||
}
|
||||
|
||||
foreach (var point in _points)
|
||||
{
|
||||
point.Dispose();
|
||||
}
|
||||
|
||||
_center?.Dispose();
|
||||
_nativeColliderGroups?.Dispose();
|
||||
_nativePoints.Dispose();
|
||||
_rootBoneWrapper.Dispose();
|
||||
}
|
||||
|
||||
private unsafe void SetupRecursive(Transform trs, NativeTransform parent = null)
|
||||
{
|
||||
var transformWrapper = new NativeTransform(_transformRegistry, TransformSynchronizationType.PushOnly, trs, parent);
|
||||
_transformWrappers.Add(transformWrapper);
|
||||
|
||||
var point = new NativePointer<BlittablePoint>(
|
||||
new BlittablePoint(
|
||||
trs,
|
||||
_radius,
|
||||
_center != null ? _center.GetUnsafePtr() : null,
|
||||
_nativeColliderGroups.GetUnsafePtr(),
|
||||
transformWrapper.GetUnsafePtr())
|
||||
);
|
||||
_points.Add(point);
|
||||
|
||||
for (var i = 0; i < trs.childCount; ++i)
|
||||
{
|
||||
SetupRecursive(trs.GetChild(i), transformWrapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 03e3a3dad32f6e64a9fd0327d74c8305
|
||||
timeCreated: 1550036996
|
||||
21
Assets/VRM10/Runtime/FastSpringBone/FastSpringBone10.asmdef
Normal file
21
Assets/VRM10/Runtime/FastSpringBone/FastSpringBone10.asmdef
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "FastSpringBone10",
|
||||
"references": [
|
||||
"GUID:2665a8d13d1b3f18800f46e256720795"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": true,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": false,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [
|
||||
{
|
||||
"name": "com.unity.burst",
|
||||
"expression": "0.0.1",
|
||||
"define": "ENABLE_SPRINGBONE_BURST"
|
||||
}
|
||||
],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f2ca1407928ebdc4bbe7765cc278be44
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/VRM10/Runtime/FastSpringBone/NativeWrappers.meta
Normal file
8
Assets/VRM10/Runtime/FastSpringBone/NativeWrappers.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b6633497ce721ce4c81d514187ef4407
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using Unity.Collections;
|
||||
using VRM.FastSpringBones.Blittables;
|
||||
|
||||
namespace VRM.FastSpringBones.NativeWrappers
|
||||
{
|
||||
/// <summary>
|
||||
/// BlittableColliderGroupのライフサイクルを管理するWrapper
|
||||
/// </summary>
|
||||
public sealed unsafe class NativeColliderGroup : IDisposable
|
||||
{
|
||||
private readonly NativePointer<BlittableColliderGroup> _nativePointer;
|
||||
|
||||
private NativeArray<BlittableCollider> Colliders { get; }
|
||||
|
||||
public BlittableColliderGroup* GetUnsafePtr() => _nativePointer.GetUnsafePtr();
|
||||
|
||||
public NativeColliderGroup(BlittableCollider[] colliders, NativeTransform nativeTransform)
|
||||
{
|
||||
Colliders = new NativeArray<BlittableCollider>(colliders, Allocator.Persistent);
|
||||
_nativePointer = new NativePointer<BlittableColliderGroup>(new BlittableColliderGroup(Colliders, nativeTransform.GetUnsafePtr()));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Colliders.IsCreated) Colliders.Dispose();
|
||||
|
||||
_nativePointer.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a8d248be1ab9abe46abd06caa4758d6b
|
||||
timeCreated: 1550640666
|
||||
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Collections;
|
||||
using Unity.Collections.LowLevel.Unsafe;
|
||||
using VRM.FastSpringBones.Blittables;
|
||||
using VRM.FastSpringBones.Components;
|
||||
|
||||
namespace VRM.FastSpringBones.NativeWrappers
|
||||
{
|
||||
/// <summary>
|
||||
/// BlittableColliderGroupsのライフサイクルを管理するWrapper
|
||||
/// </summary>
|
||||
public sealed unsafe class NativeColliderGroups : IDisposable
|
||||
{
|
||||
private readonly NativePointer<BlittableColliderGroups> _nativePointer = new NativePointer<BlittableColliderGroups>();
|
||||
private NativeArray<BlittableColliderGroup> _colliderGroupArray;
|
||||
private IReadOnlyList<FastSpringBoneColliderGroup> _colliderGroups;
|
||||
|
||||
//Disposeされた後にUpdateColliderGroupsが呼ばれるのを防ぐためのフラグ
|
||||
private bool _isDisposed;
|
||||
|
||||
public BlittableColliderGroups* GetUnsafePtr() => _nativePointer.GetUnsafePtr();
|
||||
public void DrawGizmos() => _nativePointer.Value.DrawGizmos();
|
||||
|
||||
public IReadOnlyList<FastSpringBoneColliderGroup> ColliderGroups
|
||||
{
|
||||
get => _colliderGroups;
|
||||
set
|
||||
{
|
||||
_colliderGroups = value;
|
||||
UpdateColliderGroups();
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateColliderGroups()
|
||||
{
|
||||
if (_isDisposed) return;
|
||||
if (_colliderGroupArray.IsCreated)
|
||||
{
|
||||
_colliderGroupArray.Dispose();
|
||||
}
|
||||
CreateColliderGroupArray(_colliderGroups);
|
||||
UpdateData();
|
||||
}
|
||||
|
||||
public NativeColliderGroups(IReadOnlyList<FastSpringBoneColliderGroup> colliderGroups)
|
||||
{
|
||||
_colliderGroups = colliderGroups;
|
||||
UpdateColliderGroups();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_colliderGroupArray.IsCreated)
|
||||
{
|
||||
_colliderGroupArray.Dispose();
|
||||
_isDisposed = true;
|
||||
}
|
||||
_nativePointer.Dispose();
|
||||
}
|
||||
|
||||
private void CreateColliderGroupArray(IReadOnlyList<FastSpringBoneColliderGroup> colliderGroups)
|
||||
{
|
||||
_colliderGroupArray = new NativeArray<BlittableColliderGroup>(colliderGroups.Count, Allocator.Persistent);
|
||||
for (var i = 0; i < _colliderGroupArray.Length; ++i)
|
||||
{
|
||||
_colliderGroupArray[i] = *colliderGroups[i].ColliderGroupPtr;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateData()
|
||||
{
|
||||
_nativePointer.Value = new BlittableColliderGroups(
|
||||
(BlittableColliderGroup*)_colliderGroupArray.GetUnsafePtr(),
|
||||
_colliderGroupArray.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73c3eb940b180fc48a3e7cdacc621543
|
||||
timeCreated: 1550645184
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using Unity.Collections;
|
||||
using Unity.Collections.LowLevel.Unsafe;
|
||||
|
||||
namespace VRM.FastSpringBones.NativeWrappers
|
||||
{
|
||||
public sealed class NativePointer<T> : IDisposable where T : unmanaged
|
||||
{
|
||||
private readonly unsafe T* _unsafePtr;
|
||||
|
||||
public unsafe T* GetUnsafePtr() => _unsafePtr;
|
||||
|
||||
public unsafe T Value
|
||||
{
|
||||
get => *_unsafePtr;
|
||||
set => *_unsafePtr = value;
|
||||
}
|
||||
|
||||
public unsafe NativePointer()
|
||||
{
|
||||
_unsafePtr = (T*)UnsafeUtility.Malloc(sizeof(T), 16, Allocator.Persistent);
|
||||
}
|
||||
|
||||
public unsafe NativePointer(T value)
|
||||
{
|
||||
_unsafePtr = (T*)UnsafeUtility.Malloc(sizeof(T), 16, Allocator.Persistent);
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public unsafe void Dispose()
|
||||
{
|
||||
UnsafeUtility.Free(_unsafePtr, Allocator.Persistent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7c4d1058273489b478b053eb13702d6e
|
||||
timeCreated: 1595316733
|
||||
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Collections;
|
||||
using Unity.Collections.LowLevel.Unsafe;
|
||||
using VRM.FastSpringBones.Blittables;
|
||||
|
||||
namespace VRM.FastSpringBones.NativeWrappers
|
||||
{
|
||||
/// <summary>
|
||||
/// BlittablePointsGroupのライフサイクルを管理するWrapper
|
||||
/// </summary>
|
||||
public sealed unsafe class NativePoints : IDisposable
|
||||
{
|
||||
private readonly NativePointer<BlittablePoints> _nativePointer;
|
||||
private NativeArray<BlittablePoint> _buffer;
|
||||
|
||||
public BlittablePoints* GetUnsafePtr() => _nativePointer.GetUnsafePtr();
|
||||
|
||||
public NativePoints(IList<NativePointer<BlittablePoint>> points)
|
||||
{
|
||||
_buffer = new NativeArray<BlittablePoint>(points.Count, Allocator.Persistent);
|
||||
for (var i = 0; i < _buffer.Length; ++i)
|
||||
{
|
||||
_buffer[i] = points[i].Value;
|
||||
}
|
||||
|
||||
_nativePointer = new NativePointer<BlittablePoints>(new BlittablePoints((BlittablePoint*) _buffer.GetUnsafePtr(), _buffer.Length));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_buffer.IsCreated)
|
||||
{
|
||||
_buffer.Dispose();
|
||||
}
|
||||
_nativePointer.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 91cb239111add8342b25267432c53894
|
||||
timeCreated: 1550723674
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using VRM.FastSpringBones.Blittables;
|
||||
using VRM.FastSpringBones.Registries;
|
||||
|
||||
namespace VRM.FastSpringBones.NativeWrappers
|
||||
{
|
||||
/// <summary>
|
||||
/// BlittableTransformのライフサイクルを管理するWrapper
|
||||
/// </summary>
|
||||
public sealed class NativeTransform : IDisposable
|
||||
{
|
||||
private readonly NativePointer<BlittableTransform> _nativePointer;
|
||||
public Transform Transform { get; }
|
||||
|
||||
private readonly TransformRegistry _transformRegistry;
|
||||
|
||||
public unsafe BlittableTransform* GetUnsafePtr() => _nativePointer.GetUnsafePtr();
|
||||
public BlittableTransform Value => _nativePointer.Value;
|
||||
|
||||
public unsafe NativeTransform(
|
||||
TransformRegistry transformRegistry,
|
||||
TransformSynchronizationType transformSynchronizationType,
|
||||
Transform transform,
|
||||
NativeTransform parent = null
|
||||
)
|
||||
{
|
||||
_nativePointer = new NativePointer<BlittableTransform>(new BlittableTransform(parent != null ? parent.GetUnsafePtr() : null, transform));
|
||||
|
||||
Transform = transform;
|
||||
|
||||
_transformRegistry = transformRegistry;
|
||||
_transformRegistry.Register(this, transformSynchronizationType);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_transformRegistry.Unregister(this);
|
||||
|
||||
_nativePointer.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 31fdb7a504d498242a3e459e3a63bf2d
|
||||
timeCreated: 1550545201
|
||||
8
Assets/VRM10/Runtime/FastSpringBone/Registries.meta
Normal file
8
Assets/VRM10/Runtime/FastSpringBone/Registries.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a590127469e104049996cf9b123db183
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
using VRM.FastSpringBones.NativeWrappers;
|
||||
|
||||
namespace VRM.FastSpringBones.Registries
|
||||
{
|
||||
public sealed class ColliderGroupRegistry : Registry<NativeColliderGroups>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9f5847b2d8bb16242aac76bda14b0202
|
||||
timeCreated: 1594963749
|
||||
28
Assets/VRM10/Runtime/FastSpringBone/Registries/Registry.cs
Normal file
28
Assets/VRM10/Runtime/FastSpringBone/Registries/Registry.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace VRM.FastSpringBones.Registries
|
||||
{
|
||||
public class Registry<T>
|
||||
{
|
||||
private readonly List<T> _items = new List<T>();
|
||||
private Action _onValueChanged;
|
||||
|
||||
public IReadOnlyList<T> Items => _items;
|
||||
|
||||
public void Register(T value)
|
||||
{
|
||||
_items.Add(value);
|
||||
_onValueChanged?.Invoke();
|
||||
}
|
||||
|
||||
public void Unregister(T value)
|
||||
{
|
||||
_items.Remove(value);
|
||||
_onValueChanged?.Invoke();
|
||||
}
|
||||
|
||||
public void SubscribeOnValueChanged(Action action) => _onValueChanged += action;
|
||||
public void UnSubscribeOnValueChanged(Action action) => _onValueChanged -= action;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9d127e4a4d5d05a46929eb3d3692d720
|
||||
timeCreated: 1595407071
|
||||
@@ -0,0 +1,12 @@
|
||||
using VRM.FastSpringBones.Blittables;
|
||||
using VRM.FastSpringBones.NativeWrappers;
|
||||
|
||||
namespace VRM.FastSpringBones.Registries
|
||||
{
|
||||
/// <summary>
|
||||
/// 今生きているRootBoneの一覧を返すクラス
|
||||
/// </summary>
|
||||
public sealed class RootBoneRegistry : Registry<NativePointer<BlittableRootBone>>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34da1f63315656e479315841b7112d7c
|
||||
timeCreated: 1550734399
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using VRM.FastSpringBones.NativeWrappers;
|
||||
|
||||
namespace VRM.FastSpringBones.Registries
|
||||
{
|
||||
/// <summary>
|
||||
/// 今生きているTransformの一覧を返すクラス
|
||||
/// </summary>
|
||||
public sealed class TransformRegistry
|
||||
{
|
||||
private readonly List<NativeTransform> _transforms = new List<NativeTransform>();
|
||||
public IReadOnlyList<NativeTransform> Transforms => _transforms;
|
||||
|
||||
private readonly List<NativeTransform> _pullTargets = new List<NativeTransform>();
|
||||
public IReadOnlyList<NativeTransform> PullTargets => _pullTargets;
|
||||
|
||||
private readonly List<NativeTransform> _pushTargets = new List<NativeTransform>();
|
||||
public IReadOnlyList<NativeTransform> PushTargets => _pushTargets;
|
||||
|
||||
private Action _onValueChanged;
|
||||
|
||||
public void SubscribeOnValueChanged(Action action) => _onValueChanged += action;
|
||||
|
||||
public void UnSubscribeOnValueChanged(Action action) => _onValueChanged -= action;
|
||||
|
||||
public void Register(NativeTransform nativeTransform, TransformSynchronizationType synchronizationType)
|
||||
{
|
||||
_transforms.Add(nativeTransform);
|
||||
switch (synchronizationType)
|
||||
{
|
||||
case TransformSynchronizationType.PullOnly:
|
||||
_pullTargets.Add(nativeTransform);
|
||||
break;
|
||||
case TransformSynchronizationType.PushOnly:
|
||||
_pushTargets.Add(nativeTransform);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(synchronizationType), synchronizationType, null);
|
||||
}
|
||||
_onValueChanged?.Invoke();
|
||||
}
|
||||
|
||||
public void Unregister(NativeTransform nativeTransform)
|
||||
{
|
||||
_transforms.Remove(nativeTransform);
|
||||
|
||||
if (_pullTargets.Contains(nativeTransform))
|
||||
{
|
||||
_pullTargets.Remove(nativeTransform);
|
||||
}
|
||||
|
||||
if (_pushTargets.Contains(nativeTransform))
|
||||
{
|
||||
_pushTargets.Remove(nativeTransform);
|
||||
}
|
||||
_onValueChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0603b4348a649df4fa966eeb3cd1011a
|
||||
timeCreated: 1550666913
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace VRM.FastSpringBones.Registries
|
||||
{
|
||||
public enum TransformSynchronizationType
|
||||
{
|
||||
PullOnly,
|
||||
PushOnly,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 03978423768dd5542875359a745a0d6d
|
||||
timeCreated: 1551169872
|
||||
8
Assets/VRM10/Runtime/FastSpringBone/Schedulers.meta
Normal file
8
Assets/VRM10/Runtime/FastSpringBone/Schedulers.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 160bc0dc84d022a48862d46add47a26c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#if ENABLE_SPRINGBONE_BURST
|
||||
using Unity.Burst;
|
||||
#endif
|
||||
using Unity.Collections;
|
||||
using Unity.Collections.LowLevel.Unsafe;
|
||||
using Unity.Jobs;
|
||||
using UnityEngine.Jobs;
|
||||
using UnityEngine.Profiling;
|
||||
using VRM.FastSpringBones.Blittables;
|
||||
using VRM.FastSpringBones.NativeWrappers;
|
||||
using VRM.FastSpringBones.Registries;
|
||||
|
||||
namespace VRM.FastSpringBones.Schedulers
|
||||
{
|
||||
/// <summary>
|
||||
/// GameObjectの世界からBlittableな世界へTransformを複製する処理を行うクラス
|
||||
/// </summary>
|
||||
public sealed unsafe class PullTransformJobScheduler : IDisposable
|
||||
{
|
||||
private BlittableTransform** _transformPointers;
|
||||
private TransformAccessArray _transformAccessArray;
|
||||
|
||||
private readonly CustomSampler _sampler = CustomSampler.Create("Schedule CopyFromTransformJob");
|
||||
private readonly TransformRegistry _transformRegistry;
|
||||
|
||||
private bool _dirty = true;
|
||||
|
||||
private IReadOnlyList<NativeTransform> Targets => _transformRegistry.PullTargets;
|
||||
|
||||
public PullTransformJobScheduler(TransformRegistry transformRegistry)
|
||||
{
|
||||
_transformRegistry = transformRegistry;
|
||||
|
||||
_transformRegistry.SubscribeOnValueChanged(OnTransformChanged);
|
||||
}
|
||||
|
||||
private void OnTransformChanged()
|
||||
{
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
public JobHandle Schedule(JobHandle dependOn = default(JobHandle))
|
||||
{
|
||||
if (Targets.Count == 0)
|
||||
{
|
||||
return dependOn;
|
||||
}
|
||||
|
||||
_sampler.Begin();
|
||||
|
||||
// リストが変更されていたらバッファを再構築
|
||||
if (_dirty)
|
||||
{
|
||||
ReconstructBuffers();
|
||||
|
||||
_dirty = false;
|
||||
}
|
||||
|
||||
// ジョブを発火
|
||||
var job = new Job { TransformPointers = _transformPointers };
|
||||
var jobHandle = job.Schedule(_transformAccessArray, dependOn);
|
||||
|
||||
_sampler.End();
|
||||
|
||||
return jobHandle;
|
||||
}
|
||||
|
||||
private void ReconstructBuffers()
|
||||
{
|
||||
ReleaseBuffers();
|
||||
|
||||
var transforms = Targets;
|
||||
_transformPointers = (BlittableTransform**)UnsafeUtility.Malloc(
|
||||
sizeof(BlittableTransform*) * transforms.Count,
|
||||
16,
|
||||
Allocator.Persistent
|
||||
);
|
||||
|
||||
_transformAccessArray = new TransformAccessArray(transforms.Count);
|
||||
|
||||
for (var i = 0; i < transforms.Count; i++)
|
||||
{
|
||||
_transformPointers[i] = transforms[i].GetUnsafePtr();
|
||||
_transformAccessArray.Add(transforms[i].Transform);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ReleaseBuffers();
|
||||
_transformRegistry.UnSubscribeOnValueChanged(OnTransformChanged);
|
||||
}
|
||||
|
||||
private void ReleaseBuffers()
|
||||
{
|
||||
if (_transformAccessArray.isCreated) _transformAccessArray.Dispose();
|
||||
if (_transformPointers != null)
|
||||
{
|
||||
UnsafeUtility.Free(_transformPointers, Allocator.Persistent);
|
||||
_transformPointers = null;
|
||||
}
|
||||
}
|
||||
|
||||
#if ENABLE_SPRINGBONE_BURST
|
||||
[BurstCompile]
|
||||
#endif
|
||||
private struct Job : IJobParallelForTransform
|
||||
{
|
||||
[NativeDisableUnsafePtrRestriction] public BlittableTransform** TransformPointers;
|
||||
|
||||
public void Execute(int index, TransformAccess transform)
|
||||
{
|
||||
TransformPointers[index]->PullFrom(transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4ac8728e24e9f5240aa45867878dcc10
|
||||
timeCreated: 1550547066
|
||||
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#if ENABLE_SPRINGBONE_BURST
|
||||
using Unity.Burst;
|
||||
#endif
|
||||
using Unity.Collections;
|
||||
using Unity.Collections.LowLevel.Unsafe;
|
||||
using Unity.Jobs;
|
||||
using UnityEngine.Jobs;
|
||||
using UnityEngine.Profiling;
|
||||
using VRM.FastSpringBones.Blittables;
|
||||
using VRM.FastSpringBones.NativeWrappers;
|
||||
using VRM.FastSpringBones.Registries;
|
||||
|
||||
namespace VRM.FastSpringBones.Schedulers
|
||||
{
|
||||
/// <summary>
|
||||
/// Blittableな世界からGameObjectの世界へTransformを送り込む処理を行うクラス
|
||||
/// </summary>
|
||||
public sealed unsafe class PushTransformJobScheduler : IDisposable
|
||||
{
|
||||
private BlittableTransform** _transformPointers;
|
||||
private TransformAccessArray _transformAccessArray;
|
||||
|
||||
private readonly CustomSampler _sampler = CustomSampler.Create("Schedule CopyFromTransformJob");
|
||||
private readonly TransformRegistry _transformRegistry;
|
||||
|
||||
private bool _dirty = true;
|
||||
|
||||
private IReadOnlyList<NativeTransform> Targets => _transformRegistry.PushTargets;
|
||||
|
||||
public PushTransformJobScheduler(TransformRegistry transformRegistry)
|
||||
{
|
||||
_transformRegistry = transformRegistry;
|
||||
|
||||
_transformRegistry.SubscribeOnValueChanged(OnTransformChanged);
|
||||
}
|
||||
|
||||
private void OnTransformChanged()
|
||||
{
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
public JobHandle Schedule(JobHandle dependOn = default(JobHandle))
|
||||
{
|
||||
if (Targets.Count == 0)
|
||||
{
|
||||
return dependOn;
|
||||
}
|
||||
|
||||
_sampler.Begin();
|
||||
|
||||
// リストが変更されていたらバッファを再構築
|
||||
if (_dirty)
|
||||
{
|
||||
ReconstructBuffers();
|
||||
|
||||
_dirty = false;
|
||||
}
|
||||
|
||||
// Jobを発火
|
||||
var job = new Job { TransformPointers = _transformPointers };
|
||||
var jobHandle = job.Schedule(_transformAccessArray, dependOn);
|
||||
|
||||
_sampler.End();
|
||||
|
||||
return jobHandle;
|
||||
}
|
||||
|
||||
private void ReconstructBuffers()
|
||||
{
|
||||
ReleaseBuffers();
|
||||
|
||||
var transforms = Targets;
|
||||
_transformPointers = (BlittableTransform**)UnsafeUtility.Malloc(
|
||||
sizeof(BlittableTransform*) * transforms.Count,
|
||||
16,
|
||||
Allocator.Persistent
|
||||
);
|
||||
|
||||
_transformAccessArray = new TransformAccessArray(transforms.Count);
|
||||
|
||||
for (var i = 0; i < transforms.Count; i++)
|
||||
{
|
||||
_transformPointers[i] = transforms[i].GetUnsafePtr();
|
||||
_transformAccessArray.Add(transforms[i].Transform);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ReleaseBuffers();
|
||||
_transformRegistry.UnSubscribeOnValueChanged(OnTransformChanged);
|
||||
}
|
||||
|
||||
private void ReleaseBuffers()
|
||||
{
|
||||
if (_transformAccessArray.isCreated) _transformAccessArray.Dispose();
|
||||
if (_transformPointers != null)
|
||||
{
|
||||
UnsafeUtility.Free(_transformPointers, Allocator.Persistent);
|
||||
_transformPointers = null;
|
||||
}
|
||||
}
|
||||
|
||||
#if ENABLE_SPRINGBONE_BURST
|
||||
[BurstCompile]
|
||||
#endif
|
||||
private struct Job : IJobParallelForTransform
|
||||
{
|
||||
[NativeDisableUnsafePtrRestriction] public BlittableTransform** TransformPointers;
|
||||
|
||||
public void Execute(int index, TransformAccess transform)
|
||||
{
|
||||
TransformPointers[index]->PushTo(transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 91e1bc2d3c93f1b4db3e08534237e6a0
|
||||
timeCreated: 1550717407
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#if ENABLE_SPRINGBONE_BURST
|
||||
using Unity.Burst;
|
||||
#endif
|
||||
using Unity.Collections;
|
||||
using Unity.Collections.LowLevel.Unsafe;
|
||||
using Unity.Jobs;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Profiling;
|
||||
using VRM.FastSpringBones.Blittables;
|
||||
using VRM.FastSpringBones.NativeWrappers;
|
||||
using VRM.FastSpringBones.Registries;
|
||||
|
||||
namespace VRM.FastSpringBones.Schedulers
|
||||
{
|
||||
/// <summary>
|
||||
/// SpringBoneを更新する処理を行うクラス
|
||||
/// </summary>
|
||||
public sealed unsafe class UpdateSpringBoneJobScheduler : IDisposable
|
||||
{
|
||||
private BlittableRootBone** _rootBonePointers;
|
||||
|
||||
private readonly RootBoneRegistry _rootBoneRegistry;
|
||||
private readonly CustomSampler _sampler = CustomSampler.Create("Schedule CopyFromTransformJob");
|
||||
|
||||
private bool _dirty = true;
|
||||
|
||||
private IReadOnlyList<NativePointer<BlittableRootBone>> Targets => _rootBoneRegistry.Items;
|
||||
|
||||
public UpdateSpringBoneJobScheduler(RootBoneRegistry rootBoneRegistry)
|
||||
{
|
||||
_rootBoneRegistry = rootBoneRegistry;
|
||||
|
||||
_rootBoneRegistry.SubscribeOnValueChanged(OnRootBoneChanged);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
ReleaseBuffer();
|
||||
_rootBoneRegistry.UnSubscribeOnValueChanged(OnRootBoneChanged);
|
||||
}
|
||||
|
||||
private void OnRootBoneChanged()
|
||||
{
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
public JobHandle Schedule(JobHandle dependOn = default(JobHandle))
|
||||
{
|
||||
if (Targets.Count == 0)
|
||||
{
|
||||
return dependOn;
|
||||
}
|
||||
|
||||
_sampler.Begin();
|
||||
|
||||
// リストが変更されていたらバッファを再構築
|
||||
if (_dirty)
|
||||
{
|
||||
ReconstructBuffers();
|
||||
|
||||
_dirty = false;
|
||||
}
|
||||
|
||||
// Jobを発火
|
||||
var job = new Job { RootBonePointers = _rootBonePointers, DeltaTime = Time.deltaTime };
|
||||
var jobHandle = job.Schedule(Targets.Count, 0, dependOn);
|
||||
|
||||
_sampler.End();
|
||||
|
||||
return jobHandle;
|
||||
}
|
||||
|
||||
private void ReconstructBuffers()
|
||||
{
|
||||
ReleaseBuffer();
|
||||
|
||||
_rootBonePointers = (BlittableRootBone**)UnsafeUtility.Malloc(
|
||||
sizeof(BlittableTransform*) * Targets.Count,
|
||||
16,
|
||||
Allocator.Persistent
|
||||
);
|
||||
|
||||
for (var i = 0; i < Targets.Count; i++)
|
||||
{
|
||||
_rootBonePointers[i] = Targets[i].GetUnsafePtr();
|
||||
}
|
||||
}
|
||||
|
||||
private void ReleaseBuffer()
|
||||
{
|
||||
if (_rootBonePointers == null) return;
|
||||
UnsafeUtility.Free(_rootBonePointers, Allocator.Persistent);
|
||||
_rootBonePointers = null;
|
||||
}
|
||||
|
||||
#if ENABLE_SPRINGBONE_BURST
|
||||
[BurstCompile]
|
||||
#endif
|
||||
private struct Job : IJobParallelFor
|
||||
{
|
||||
[NativeDisableUnsafePtrRestriction] public BlittableRootBone** RootBonePointers;
|
||||
public float DeltaTime;
|
||||
|
||||
public void Execute(int index)
|
||||
{
|
||||
// 各点を更新する
|
||||
RootBonePointers[index]->Update(DeltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 39839e9c65fc7a54abacaadf2d519fbf
|
||||
timeCreated: 1549516763
|
||||
Reference in New Issue
Block a user