From 4c6f5ebb480404f33b0c7b64b59d4e13a882b0c3 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 2 Dec 2024 14:48:36 +0900 Subject: [PATCH] ParticleSettings --- .../ClothSample/ClothViewer/ClothGuess.cs | 4 +- .../ClothWarp/Editor/ClothWarpRootEditor.cs | 15 +++-- .../Runtime/Components/ClothWarpRoot.cs | 29 +++------- .../Components/ClothWarpRuntimeProvider.cs | 15 +++-- .../Runtime/Jobs/ClothWarpJobRuntime.cs | 6 +- .../ClothWarp/Runtime/Jobs/Particle.cs | 55 ++++++++++++++++--- .../Runtime/Jobs/WarpCollisionJob.cs | 4 +- .../ClothWarp/Runtime/ParticleList.cs | 2 +- 8 files changed, 78 insertions(+), 52 deletions(-) diff --git a/Assets/VRM10_Samples/ClothSample/ClothViewer/ClothGuess.cs b/Assets/VRM10_Samples/ClothSample/ClothViewer/ClothGuess.cs index da73be448..13e0deab6 100644 --- a/Assets/VRM10_Samples/ClothSample/ClothViewer/ClothGuess.cs +++ b/Assets/VRM10_Samples/ClothSample/ClothViewer/ClothGuess.cs @@ -138,7 +138,7 @@ namespace UniVRM10.Cloth.Viewer var warp = childchild.gameObject.AddComponent(); // Name = name, // CollisionMask = mask, - warp.BaseSettings.radius = 0.02f; + warp.BaseSettings.Radius = 0.02f; // Connection = type transforms.Add(warp); break; @@ -179,7 +179,7 @@ namespace UniVRM10.Cloth.Viewer if (warp != null) { // CollisionMask = mask, - warp.BaseSettings.radius = 0.02f; + warp.BaseSettings.Radius = 0.02f; // Connection = type transforms.Add(warp); } diff --git a/Assets/VRM10_Samples/ClothSample/ClothWarp/Editor/ClothWarpRootEditor.cs b/Assets/VRM10_Samples/ClothSample/ClothWarp/Editor/ClothWarpRootEditor.cs index 40a14a548..f18658809 100644 --- a/Assets/VRM10_Samples/ClothSample/ClothWarp/Editor/ClothWarpRootEditor.cs +++ b/Assets/VRM10_Samples/ClothSample/ClothWarp/Editor/ClothWarpRootEditor.cs @@ -95,11 +95,10 @@ namespace UniVRM10.ClothWarp.Components m_treeview = new MultiColumnTreeView(); BindColumn("Transform", 120, () => new ObjectField(), (_) => false, "Transform"); BindColumn("Mode", 40, () => new EnumField(), (_) => true, "Mode"); - BindColumn("stiffnessForce", 40, () => new FloatField(), isCustom, "Settings.stiffnessForce"); - BindColumn("gravityPower", 40, () => new FloatField(), isCustom, "Settings.gravityPower"); - BindColumn("gravityDir", 120, () => new Vector3Field(), isCustom, "Settings.gravityDir"); - BindColumn("dragForce", 40, () => new FloatField(), isCustom, "Settings.dragForce"); - BindColumn("radius", 40, () => new FloatField(), isCustom, "Settings.radius"); + BindColumn("Stiffness", 40, () => new FloatField(), isCustom, "Settings.Stiffness"); + BindColumn("Gravity", 120, () => new Vector3Field(), isCustom, "Settings.Gravity"); + BindColumn("Deceleration", 40, () => new FloatField(), isCustom, "Settings.Deceleration"); + BindColumn("Radius", 40, () => new FloatField(), isCustom, "Settings.Radius"); m_treeview.autoExpand = true; m_treeview.SetRootItems(m_target.m_rootitems); @@ -117,10 +116,10 @@ namespace UniVRM10.ClothWarp.Components { if (Application.isPlaying) { - m_vrm.Runtime.SpringBone.SetJointLevel(m_target.transform, m_target.BaseSettings); + m_vrm.Runtime.SpringBone.SetJointLevel(m_target.transform, m_target.BaseSettings.ToBlittableJointMutable()); foreach (var p in m_target.Particles) { - m_vrm.Runtime.SpringBone.SetJointLevel(p.Transform, p.Settings); + m_vrm.Runtime.SpringBone.SetJointLevel(p.Transform, p.Settings.ToBlittableJointMutable()); } } } @@ -153,7 +152,7 @@ namespace UniVRM10.ClothWarp.Components p = m_target.GetParticleFromTransform(p.Transform); var t = p.Transform; Handles.color = Color.green; - Handles.SphereHandleCap(t.GetInstanceID(), t.position, t.rotation, p.Settings.radius * 2, EventType.Repaint); + Handles.SphereHandleCap(t.GetInstanceID(), t.position, t.rotation, p.Settings.Radius * 2, EventType.Repaint); } } } diff --git a/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Components/ClothWarpRoot.cs b/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Components/ClothWarpRoot.cs index 10f176109..003531651 100644 --- a/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Components/ClothWarpRoot.cs +++ b/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Components/ClothWarpRoot.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Linq; using UniGLTF; -using UniGLTF.SpringBoneJobs.Blittables; using UnityEngine; using UnityEngine.UIElements; @@ -17,18 +16,6 @@ namespace UniVRM10.ClothWarp.Components /// public class ClothWarpRoot : MonoBehaviour { - public static BlittableJointMutable DefaultSetting() - { - return new BlittableJointMutable - { - stiffnessForce = 1.0f, - gravityPower = 0, - gravityDir = new Vector3(0, -1.0f, 0), - dragForce = 0.4f, - radius = 0.02f, - }; - } - public enum ParticleMode { /// @@ -53,28 +40,28 @@ namespace UniVRM10.ClothWarp.Components { public Transform Transform; public ParticleMode Mode; - public BlittableJointMutable Settings; + public Jobs.ParticleSettings Settings; - public Particle(Transform t, ParticleMode mode, BlittableJointMutable settings) + public Particle(Transform t, ParticleMode mode, Jobs.ParticleSettings settings) { Transform = t; Mode = mode; Settings = settings; } - public Particle(Transform t, BlittableJointMutable settings) + public Particle(Transform t, Jobs.ParticleSettings settings) : this(t, ParticleMode.Custom, settings) { } public Particle(Transform t) - : this(t, ParticleMode.Base, DefaultSetting()) + : this(t, ParticleMode.Base, Jobs.ParticleSettings.Default) { } } [SerializeField] - public BlittableJointMutable BaseSettings = DefaultSetting(); + public Jobs.ParticleSettings BaseSettings = Jobs.ParticleSettings.Default; /// /// null のときは world root ではなく model root で処理 @@ -214,7 +201,7 @@ namespace UniVRM10.ClothWarp.Components } } - public void SetSettings(Transform t, BlittableJointMutable settings) + public void SetSettings(Transform t, Jobs.ParticleSettings settings) { if (t == null) return; for (int i = 0; i < m_particles.Count; ++i) @@ -232,7 +219,7 @@ namespace UniVRM10.ClothWarp.Components public void OnDrawGizmosSelected() { - Gizmos.DrawSphere(transform.position, BaseSettings.radius); + Gizmos.DrawSphere(transform.position, BaseSettings.Radius); foreach (var p in Particles) { @@ -240,7 +227,7 @@ namespace UniVRM10.ClothWarp.Components { continue; } - Gizmos.DrawWireSphere(p.Transform.position, p.Settings.radius); + Gizmos.DrawWireSphere(p.Transform.position, p.Settings.Radius); if (TryGetClosestParent(p.Transform, out var parent)) { diff --git a/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Components/ClothWarpRuntimeProvider.cs b/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Components/ClothWarpRuntimeProvider.cs index 31116a6a6..965722b55 100644 --- a/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Components/ClothWarpRuntimeProvider.cs +++ b/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Components/ClothWarpRuntimeProvider.cs @@ -70,24 +70,23 @@ namespace UniVRM10.ClothWarp.Components var joint = joints[i]; // mod ? - var stiffness = joint.m_stiffnessForce; + var stiffness = Mathf.Min(0.08f, joint.m_stiffnessForce * 0.1f); - var settings = new UniGLTF.SpringBoneJobs.Blittables.BlittableJointMutable + var settings = new Jobs.ParticleSettings { - dragForce = joint.m_dragForce, - gravityDir = joint.m_gravityDir, - gravityPower = joint.m_gravityPower, - stiffnessForce = stiffness, + Deceleration = joint.m_dragForce, + Gravity = joint.m_gravityDir * joint.m_gravityPower, + Stiffness = stiffness, }; if (i == 0) { - settings.radius = joints[0].m_jointRadius; + settings.Radius = joints[0].m_jointRadius; warp.BaseSettings = settings; } else { // breaking change from vrm-1.0 - settings.radius = joints[i - 1].m_jointRadius; + settings.Radius = joints[i - 1].m_jointRadius; var useInheritSettings = warp.BaseSettings.Equals(settings); if (useInheritSettings) { diff --git a/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Jobs/ClothWarpJobRuntime.cs b/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Jobs/ClothWarpJobRuntime.cs index 98d7a7708..af904f4ff 100644 --- a/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Jobs/ClothWarpJobRuntime.cs +++ b/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Jobs/ClothWarpJobRuntime.cs @@ -549,11 +549,11 @@ namespace UniVRM10.ClothWarp.Jobs break; case TransformType.ClothWarp: Gizmos.color = Color.white; - Gizmos.DrawSphere(v, info.Settings.radius); + Gizmos.DrawSphere(v, info.Settings.Radius); break; case TransformType.Particle: Gizmos.color = Color.cyan; - Gizmos.DrawWireSphere(v, info.Settings.radius); + Gizmos.DrawWireSphere(v, info.Settings.Radius); break; } } @@ -565,7 +565,7 @@ namespace UniVRM10.ClothWarp.Jobs if (i != -1) { var info = _info[i]; - info.Settings = jointSettings; + info.Settings.FromBlittableJointMutable(jointSettings); _info[i] = info; } } diff --git a/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Jobs/Particle.cs b/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Jobs/Particle.cs index c82e235c5..61625b3e3 100644 --- a/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Jobs/Particle.cs +++ b/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Jobs/Particle.cs @@ -1,3 +1,4 @@ +using System; using UniGLTF.SpringBoneJobs.Blittables; using Unity.Collections; using Unity.Jobs; @@ -7,13 +8,56 @@ using UnityEngine.Jobs; namespace UniVRM10.ClothWarp.Jobs { + /// + /// UniGLTF.SpringBoneJobs.Blittables.BlittableJointMutable と同じ。 + /// Range が違う。 + /// + [Serializable] + public struct ParticleSettings + { + [Range(0, 1)] + public float Stiffness; + [Range(0, 1)] + public float Deceleration; + public Vector3 Gravity; + public float Radius; + + public static readonly ParticleSettings Default = new ParticleSettings + { + Stiffness = 0.08f, + Gravity = new Vector3(0, -1.0f, 0), + Deceleration = 0.4f, + Radius = 0.02f, + }; + + public void FromBlittableJointMutable(BlittableJointMutable src) + { + Stiffness = src.stiffnessForce; + Gravity = src.gravityDir * src.gravityPower; + Deceleration = src.dragForce; + Radius = src.radius; + } + + public BlittableJointMutable ToBlittableJointMutable() + { + return new BlittableJointMutable + { + stiffnessForce = Stiffness, + gravityPower = 1.0f, + gravityDir = Gravity, + dragForce = Deceleration, + radius = Radius, + }; + } + } + public struct TransformInfo { public TransformType TransformType; public int ParentIndex; public Quaternion InitLocalRotation; public Vector3 InitLocalPosition; - public BlittableJointMutable Settings; + public ParticleSettings Settings; public int WarpIndex; } @@ -87,16 +131,13 @@ namespace UniVRM10.ClothWarp.Jobs var local_rest = parentParentRotation * parent.InitLocalRotation * particle.InitLocalPosition; var world_rest = CurrentPositions[particle.ParentIndex] + local_rest; var resilience_force = world_rest - CurrentPositions[particleIndex]; - var velocity = (CurrentPositions[particleIndex] - PrevPositions[particleIndex]) * (1.0f - particle.Settings.dragForce); - var resilience = parentParentRotation * parent.InitLocalRotation * particle.InitLocalPosition.normalized * - particle.Settings.stiffnessForce; - var external = particle.Settings.gravityDir * particle.Settings.gravityPower + Frame.Force; + var velocity = (CurrentPositions[particleIndex] - PrevPositions[particleIndex]) * (1.0f - particle.Settings.Deceleration); var newPosition = CurrentPositions[particleIndex] + velocity + ImpulsiveForces[particleIndex] - + resilience * Frame.DeltaTime - + external * Frame.DeltaTime + + resilience_force * particle.Settings.Stiffness + + (particle.Settings.Gravity + Frame.Force) * Frame.SqDeltaTime ; NextPositions[particleIndex] = newPosition; diff --git a/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Jobs/WarpCollisionJob.cs b/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Jobs/WarpCollisionJob.cs index e8905c32e..795a8b1f6 100644 --- a/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Jobs/WarpCollisionJob.cs +++ b/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/Jobs/WarpCollisionJob.cs @@ -46,7 +46,7 @@ namespace UniVRM10.ClothWarp.Jobs if (c.colliderType == BlittableColliderType.Capsule) { if (SphereSphereCollision.TryCollideCapsuleAndSphere(m.MultiplyPoint(c.offset), m.MultiplyPoint(c.tailOrNormal), c.radius, - pos, info.Settings.radius, out var l)) + pos, info.Settings.Radius, out var l)) { pos += l.GetDelta(c.radius); } @@ -54,7 +54,7 @@ namespace UniVRM10.ClothWarp.Jobs else { if (SphereSphereCollision.TryCollideSphereAndSphere(m.MultiplyPoint(c.offset), c.radius, - pos, info.Settings.radius, out var l)) + pos, info.Settings.Radius, out var l)) { pos += l.GetDelta(c.radius); } diff --git a/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/ParticleList.cs b/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/ParticleList.cs index bfafc842f..c2ea5a964 100644 --- a/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/ParticleList.cs +++ b/Assets/VRM10_Samples/ClothSample/ClothWarp/Runtime/ParticleList.cs @@ -23,7 +23,7 @@ namespace UniVRM10.ClothWarp foreach (var particle in warp.Particles) { var child_index = _MakeAParticle(joint, env, particle.Transform, - particle.Settings.radius, 1); + particle.Settings.Radius, 1); var child = _particles[child_index]; strand.Particles.Add(child); joint.Children.Add(child);