From 29474d17c705abba00603ae4e40da598d8f23fd3 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 15 Nov 2024 21:13:57 +0900 Subject: [PATCH] InitializeCloth --- .../Runtime/Components/RectCloth.cs | 58 ++++++++++++++++++- .../RotateParticle/Runtime/ParticleList.cs | 1 + .../RotateParticleSpringboneRuntime.cs | 11 +++- .../Runtime/RotateParticleSystem.cs | 41 ++++++++----- 4 files changed, 92 insertions(+), 19 deletions(-) diff --git a/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/Components/RectCloth.cs b/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/Components/RectCloth.cs index dda267afb..cf5208cba 100644 --- a/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/Components/RectCloth.cs +++ b/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/Components/RectCloth.cs @@ -1,4 +1,3 @@ -using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -14,5 +13,62 @@ namespace RotateParticle.Components [SerializeField] public List Warps = new(); + + void Reset() + { + for (int i = 0; i < transform.childCount; ++i) + { + var child = transform.GetChild(i); + if (child.TryGetComponent(out var warp)) + { + Warps.Add(warp); + } + } + } + + public void OnDrawGizmosSelected() + { + if (Warps.Count == 0) + { + return; + } + + // Gizmos.color = Color.red; + Gizmos.color = new Color(1, 0.5f, 0); + for (int i = 0; i < Warps.Count; ++i) + { + if (i + 1 == Warps.Count) + { + if (LoopIsClosed) + { + DrawWeft(Warps[i], Warps[0]); + } + } + else + { + DrawWeft(Warps[i], Warps[i + 1]); + } + } + } + + void DrawWeft(Warp w0, Warp w1) + { + if (w0 == null || w1 == null) + { + return; + } + + Gizmos.DrawLine(w0.transform.position, w1.transform.position); + + for (int i = 0; i < w0.Particles.Count && i < w1.Particles.Count; ++i) + { + var p0 = w0.Particles[i]; + var p1 = w1.Particles[i]; + if (p0 != null && p0.Transform != null && p1 != null && p1.Transform != null) + { + Gizmos.DrawLine(p0.Transform.position, p1.Transform.position); + } + } + } } } \ No newline at end of file diff --git a/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/ParticleList.cs b/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/ParticleList.cs index ca5af0133..dcf4f91d6 100644 --- a/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/ParticleList.cs +++ b/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/ParticleList.cs @@ -26,6 +26,7 @@ namespace RotateParticle var child_index = _MakeAParticle(joint, env, particle.Transform, particle.GetSettings(warp.BaseSettings).HitRadius, 1, strand.CollisionMask); var child = _particles[child_index]; + strand.Particles.Add(child); joint.Children.Add(child); joint = child; } diff --git a/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/RotateParticleSpringboneRuntime.cs b/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/RotateParticleSpringboneRuntime.cs index f8c04ca7b..5454c075f 100644 --- a/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/RotateParticleSpringboneRuntime.cs +++ b/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/RotateParticleSpringboneRuntime.cs @@ -39,15 +39,20 @@ namespace RotateParticle foreach (var warp in instance.GetComponentsInChildren()) { _system._warps.Add(warp); - - await awaitCaller.NextFrame(); } + foreach (var cloth in instance.GetComponentsInChildren()) + { + _system._cloths.Add(cloth); + } + + await awaitCaller.NextFrame(); _system.Initialize(); } public void Process() { + if (_system == null) return; _system.Process(Time.deltaTime); } @@ -70,6 +75,8 @@ namespace RotateParticle public void DrawGizmos() { + if (_system == null) return; + _system.DrawGizmos(); } } } \ No newline at end of file diff --git a/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/RotateParticleSystem.cs b/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/RotateParticleSystem.cs index 5a7ce3184..509f73c49 100644 --- a/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/RotateParticleSystem.cs +++ b/Assets/VRM10_Samples/ClothSample/RotateParticle/Runtime/RotateParticleSystem.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using RotateParticle.Components; using SphereTriangle; using UnityEngine; @@ -15,7 +16,10 @@ namespace RotateParticle public SimulationEnv Env = new(); [SerializeField] - public List _warps = new(); + public List _warps = new(); + + [SerializeField] + public List _cloths = new(); [SerializeField] public List _colliderGroups = new(); @@ -96,15 +100,15 @@ namespace RotateParticle HashSet _clothUsedParticles = new(); public void InitializeCloth( - Components.RectCloth g, + RectCloth cloth, ParticleList list, - List strands, - List<(SpringConstraint, ClothRectCollision)> clothRects) + List<(SpringConstraint, ClothRectCollision)> clothRects, + Dictionary strandMap) { - for (int i = 1; i < strands.Count; ++i) + for (int i = 1; i < cloth.Warps.Count; ++i) { - var s0 = strands[i - 1]; - var s1 = strands[i]; + var s0 = strandMap[cloth.Warps[i - 1]]; + var s1 = strandMap[cloth.Warps[i]]; for (int j = 1; j < s0.Particles.Count && j < s1.Particles.Count; ++j) { // d x x c @@ -134,13 +138,13 @@ namespace RotateParticle } } - if (strands.Count >= 3) + if (cloth.Warps.Count >= 3) { - if (g.LoopIsClosed) + if (cloth.LoopIsClosed) { - var i = strands.Count; - var s0 = strands.Last(); - var s1 = strands.First(); + var i = cloth.Warps.Count; + var s0 = strandMap[cloth.Warps.Last()]; + var s1 = strandMap[cloth.Warps.First()]; for (int j = 1; j < s0.Particles.Count && j < s1.Particles.Count; ++j) { var a = s0.Particles[j]; @@ -173,14 +177,19 @@ namespace RotateParticle public void Initialize() { - foreach (var g in _warps) + var strandMap = new Dictionary(); + foreach (var warp in _warps) { var strands = new List(); - var strand = _list.MakeParticleStrand(Env, g, default); + var strand = _list.MakeParticleStrand(Env, warp, default); strands.Add(strand); - - // InitializeCloth(g, _list, strands, _clothRects); _strands.AddRange(strands); + strandMap.Add(warp, strand); + } + + foreach (var cloth in _cloths) + { + InitializeCloth(cloth, _list, _clothRects, strandMap); } _newPos = new(_list._particles.Count);