private Vector3 GetSumOfForcesOnBone(SpringBone springBone) { var sumOfForces = gravity; var providerCount = forceProviders.Length; for (var providerIndex = 0; providerIndex < providerCount; ++providerIndex) { var forceProvider = forceProviders[providerIndex]; if (forceProvider != null && forceProvider.isActiveAndEnabled) { sumOfForces += forceProvider.GetForceOnBone(springBone); } } providerCount = privateForceProviders.Count; for (var providerIndex = 0; providerIndex < providerCount; ++providerIndex) { var forceProvider = privateForceProviders[providerIndex]; if (forceProvider != null && forceProvider.isActiveAndEnabled) { sumOfForces += forceProvider.GetForceOnBone(springBone); } } return(sumOfForces); }
private static SpringBoneBaseSerializer SpringBoneToBaseSerializer(SpringBone sourceBone) { return(new SpringBoneBaseSerializer { boneName = sourceBone.name, radius = sourceBone.radius, stiffness = sourceBone.stiffnessForce, drag = sourceBone.dragForce, springForce = sourceBone.springForce, windInfluence = sourceBone.windInfluence, pivotName = (sourceBone.pivotNode != null) ? sourceBone.pivotNode.name : "", yAngleLimits = AngleLimitsToSerializer(sourceBone.yAngleLimits), zAngleLimits = AngleLimitsToSerializer(sourceBone.zAngleLimits), angularStiffness = sourceBone.angularStiffness, lodLevel = sourceBone.lodLevel, lodMode = (int)sourceBone.lodMode, lengthLimitStiffness = sourceBone.lengthLimitStiffness, isLengthLimitPercentage = sourceBone.isLengthLimitPercentage, lengthLimitMode = (int)sourceBone.lengthLimitMode, lengthLimits = sourceBone.lengthLimitTargets .Where(item => item != null) .Select(item => new LengthLimitSerializer { objectName = item.name, ratio = 1.01f }) .ToArray(), }); }
private static void SelectSpringManager(SpringBone bone) { var manager = bone.gameObject.GetComponentInParent <SpringManager>(); if (manager != null) { Selection.objects = new Object[] { manager.gameObject }; } }
public void ShowEntryUI(Rect rect) { var halfWidth = 0.5f * rect.width; var sourceRect = new Rect(rect.x, rect.y, halfWidth, rect.height); sourceBone = (SpringBone)EditorGUI.ObjectField(sourceRect, sourceBone, typeof(SpringBone), true); var targetRect = new Rect(rect.x + halfWidth, rect.y, halfWidth, rect.height); targetBone = (SpringBone)EditorGUI.ObjectField(targetRect, targetBone, typeof(SpringBone), true); }
private static void SelectPivotNode(SpringBone bone) { var pivotObjects = new List <GameObject>(); foreach (var gameObject in Selection.gameObjects) { var springBone = gameObject.GetComponent <SpringBone>(); if (springBone != null && springBone.pivotNode != null) { pivotObjects.Add(springBone.pivotNode.gameObject); } } Selection.objects = pivotObjects.ToArray(); }
private void MirrorPivot(SpringBone sourceBone, SpringBone targetBone) { var sourcePivot = sourceBone.pivotNode; var targetPivot = targetBone.pivotNode; var targetPosition = MirrorPosition(sourcePivot.position, mirrorAxis); var pivotDirection = -sourcePivot.right; pivotDirection = MirrorPosition(pivotDirection, mirrorAxis); var upDirection = MirrorPosition(sourcePivot.forward, mirrorAxis); targetPivot.position = targetPosition; targetPivot.LookAt(targetPosition + pivotDirection, upDirection); targetPivot.Rotate(-90f, 90f, 0f, Space.Self); }
public override Vector3 GetForceOnBone(SpringBone springBone) { var fullWeight = weight * strength; if ((fullWeight <= 0.0001f) | (period <= 0.001f)) { return(Vector3.zero); } var localPosition = transform.InverseTransformPoint(springBone.transform.position); var positionalFactor = GetPositionalFactor(localPosition.x, localPosition.z); var offsetMultiplier = Mathf.Sin(timeFactor + positionalFactor); var forceAtPosition = fullWeight * (transform.forward + offsetMultiplier * offsetVector).normalized; return(springBone.windInfluence * forceAtPosition); }
private static bool TryToGetPivotSideDirection ( SpringBone springBone, Vector3 lookDirection, out Vector3 sideDirection ) { sideDirection = Vector3.up; var meshNormal = FindClosestMeshNormalToPoint(springBone.transform.root, springBone.transform.position); var upDirection = Vector3.Cross(meshNormal, lookDirection).normalized; var possibleSideDirection = Vector3.Cross(lookDirection, upDirection).normalized; var isSideDirectionValid = IsPivotSideDirectionValid(lookDirection, possibleSideDirection); if (isSideDirectionValid) { sideDirection = possibleSideDirection; } return(isSideDirectionValid); }
// 基点ノードを作成 public static GameObject CreateSpringPivotNode(SpringBone springBone) { var pivotObject = new GameObject(springBone.name + "_Pivot"); pivotObject.transform.parent = springBone.transform.parent; pivotObject.transform.rotation = GetPivotRotation(springBone); pivotObject.transform.position = springBone.transform.position; pivotObject.AddComponent <SpringBonePivot>(); var oldPivotNode = springBone.pivotNode; if (oldPivotNode != null) { var skinBones = GameObjectUtil.GetAllBones(springBone.transform.root.gameObject); if (IsPivotProbablySafeToDestroy(oldPivotNode, skinBones)) { DestroyUnityObject(oldPivotNode.gameObject); } } springBone.pivotNode = pivotObject.transform; springBone.yAngleLimits.active = true; if (springBone.yAngleLimits.min > -0.5f && springBone.yAngleLimits.max < 0.5f) { springBone.yAngleLimits.min = -20f; springBone.yAngleLimits.max = 20f; } springBone.zAngleLimits.active = true; if (springBone.zAngleLimits.min > -0.5f && springBone.zAngleLimits.max < 0.5f) { springBone.zAngleLimits.min = 0f; springBone.zAngleLimits.max = 20f; } return(pivotObject); }
private static Quaternion GetPivotRotation(SpringBone springBone) { var lookDirection = springBone.ComputeChildPosition() - springBone.transform.position; lookDirection.Normalize(); Vector3 sideDirection; if (!TryToGetPivotSideDirection(springBone, lookDirection, out sideDirection)) { sideDirection = springBone.transform.position; sideDirection.y = 0f; sideDirection.Normalize(); if (!IsPivotSideDirectionValid(lookDirection, sideDirection)) { sideDirection = Vector3.up; if (!IsPivotSideDirectionValid(lookDirection, sideDirection)) { sideDirection = Vector3.forward; } } } var flattenedPosition = springBone.transform.position; flattenedPosition.y = 0f; if (Vector3.Dot(sideDirection, flattenedPosition) < 0f) { sideDirection = -sideDirection; } var upDirection = Vector3.Cross(lookDirection, sideDirection).normalized; var lookRotation = Quaternion.LookRotation(lookDirection, upDirection); var axisShift = Quaternion.Euler(180f, 90f, 0f); return(lookRotation * axisShift); }
public virtual Vector3 GetForceOnBone(SpringBone springBone) { return(Vector3.zero); }
public override Vector3 GetForceOnBone(SpringBone springBone) { return(weight * strength * transform.forward); }
private static SpringBone FindMirroredSpringBone(SpringBone sourceBone, Axis mirrorAxis, float threshold = 0.001f) { var manager = sourceBone.GetComponentInParent <SpringManager>(); return(FindMirroredComponent(manager.gameObject, sourceBone, mirrorAxis, threshold)); }
public BoneEntry(SpringBone newSourceBone, SpringBone newTargetBone) { sourceBone = newSourceBone; targetBone = newTargetBone; }