Esempio n. 1
0
        public SerializableDynamicBoneCollider ConvertCollider(SerializableSpringBoneColliderGroup springBoneColliderGroup)
        {
            var dynamicBoneCollider = new SerializableDynamicBoneCollider();

            dynamicBoneCollider.AttachedTo = springBoneColliderGroup.AttachedTo;

            return(dynamicBoneCollider);
        }
Esempio n. 2
0
        public List <SerializableSpringBoneColliderGroup> ConvertColliders(List <SerializableDynamicBoneCollider> dynamicBoneColliders, bool fillCollidersGap)
        {
            var springBoneColliderGroups = new Dictionary <string, SerializableSpringBoneColliderGroup>();

            foreach (var dyCollider in dynamicBoneColliders)
            {
                Debug.Log($"Object to attach: {dyCollider.AttachedTo}");

                var spCollider = new SerializableSpringBoneCollider(dyCollider.m_Center, dyCollider.m_Radius * dyCollider.TransformScale.x);
                Debug.Log($"New collider: R={spCollider.Radius} / (X,Y,Z)=({spCollider.Offset.x},{spCollider.Offset.y},{spCollider.Offset.z})");

                if (springBoneColliderGroups.ContainsKey(dyCollider.AttachedTo))
                {
                    // Just add new collider to existing SpringBoneColliderGroup
                    springBoneColliderGroups[dyCollider.AttachedTo].Colliders.Add(spCollider);
                }
                else
                {
                    // Create new SpringBoneColliderGroup with new collider
                    var springBoneColliderGroup = new SerializableSpringBoneColliderGroup();
                    springBoneColliderGroup.AttachedTo = dyCollider.AttachedTo;
                    springBoneColliderGroup.Colliders.Add(spCollider);
                    springBoneColliderGroups.Add(dyCollider.AttachedTo, springBoneColliderGroup);
                }

                if (fillCollidersGap && dyCollider.m_Height > 0)
                {
                    // Culculate height
                    var spHeight = dyCollider.m_Height * dyCollider.TransformScale[(int)dyCollider.m_Direction];

                    // fillCount should be positive when spHeight / 2 > spCollider.Radius. The gap exists when the height > r * 2.
                    var distanceToEdge = spHeight / 2 - spCollider.Radius;
                    var fillCount      = (int)Math.Ceiling(distanceToEdge / spCollider.Radius);

                    for (var i = 0; i < fillCount; i++)
                    {
                        var distance       = distanceToEdge / fillCount * (i + 1);
                        var distanceVector = new Vector3(0.0f, 0.0f, 0.0f);
                        distanceVector[(int)dyCollider.m_Direction] += distance;

                        var gapCollider = JsonConvert.DeserializeObject <SerializableSpringBoneCollider>(JsonConvert.SerializeObject(spCollider));
                        gapCollider.Offset += distanceVector;
                        springBoneColliderGroups[dyCollider.AttachedTo].Colliders.Add(gapCollider);

                        var gapCollider2 = JsonConvert.DeserializeObject <SerializableSpringBoneCollider>(JsonConvert.SerializeObject(spCollider));
                        gapCollider2.Offset -= distanceVector;
                        springBoneColliderGroups[dyCollider.AttachedTo].Colliders.Add(gapCollider2);
                    }
                }
            }

            return(springBoneColliderGroups.Values.ToList());
        }
        public SpringBoneConfiguration(GameObject gameObject)
        {
            foreach (var bone in gameObject.GetComponentsInChildren <VRMSpringBone>())
            {
                SpringBones.Add(SerializableSpringBone.FromSpringBone(bone));
            }

            foreach (var colliderGroup in gameObject.GetComponentsInChildren <VRMSpringBoneColliderGroup>())
            {
                SpringBoneColliderGroups.Add(SerializableSpringBoneColliderGroup.FromSpringBoneColliderGroup(colliderGroup));
            }
        }
        // Convert from VRMSpringBoneCollider
        public static SerializableSpringBoneColliderGroup FromSpringBoneColliderGroup(VRMSpringBoneColliderGroup colliderGroup)
        {
            Debug.Log($"Convert SpringBoneColliderGroup to serializable: Root={colliderGroup.transform.name}");

            var serializableCollider = new SerializableSpringBoneColliderGroup();

            serializableCollider.AttachedTo = HierarchyPath.FromTransform(colliderGroup.transform);

            // Copy values
            if (colliderGroup.Colliders != null)
            {
                foreach (var collider in colliderGroup.Colliders)
                {
                    serializableCollider.Colliders.Add(new SerializableSpringBoneCollider(collider.Offset, collider.Radius));
                }
            }

            return(serializableCollider);
        }