// returns a world rotation public static Quaternion GetDesiredRotationForObject(AttachObject obj, RotationBehavior behavior, ParamSelector rotation, ParamSelector lookAtPosition, ParamSelector up, ParamSelector forward, ParamSelector offset) { Quaternion result; switch (behavior) { case RotationBehavior.DoNothing: throw new System.InvalidOperationException("RotationBehavior is to do nothing"); case RotationBehavior.Snap: result = rotation.GetWorldRotation(obj.attachment, obj); break; case RotationBehavior.LookAt: result = GetLookAtRotation(obj.attachable, obj.attachment, lookAtPosition, up, obj); break; case RotationBehavior.LookAt2D: result = GetLookAt2DRotation(obj.attachable, obj.attachment, lookAtPosition, obj, obj.spriteRenderer ? obj.spriteRenderer.flipX : false); break; case RotationBehavior.LookDirection: result = GetLookDirectionRotation(obj.attachable, obj.attachment, forward, up, obj); break; default: throw new System.NotImplementedException("Rotation behavior not implemented."); } Quaternion off = offset.GetWorldRotation(obj.attachment, obj); return(result * off); }
// returns a world position public static Vector3 GetSnapPosition(GameObject gameObjectToSnap, Attachment attachment, ParamSelector anchor1, ParamSelector anchor2, AttachObject reference = null) { Vector3 worldAnchor1 = anchor1.GetWorldPosition(attachment, reference); Vector3 worldAnchor2 = anchor2.GetWorldPosition(attachment, reference); return(worldAnchor2 - (worldAnchor1 - gameObjectToSnap.transform.position)); }
// returns a world rotation public static Quaternion GetLookDirectionRotation(GameObject gameObjectToRotate, Attachment attachment, ParamSelector forward, ParamSelector up, AttachObject reference = null) { Vector3 worldForward = forward.GetWorldDirection(attachment, reference); Vector3 worldUp = up.GetWorldDirection(attachment, reference); return(Quaternion.LookRotation(worldForward, worldUp)); }
// returns a world rotation public static Quaternion GetLookAt2DRotation(GameObject gameObjectToRotate, Attachment attachment, ParamSelector lookAtPos, AttachObject reference = null, bool flipX = false) { Vector3 lookAtWorldPos = lookAtPos.GetWorldPosition(attachment, reference); if (gameObjectToRotate.transform.position == lookAtWorldPos) { return(gameObjectToRotate.transform.rotation); } return(ClingyUtils.LookAt2D(gameObjectToRotate.transform.position, lookAtWorldPos, flipX)); }
// returns a world rotation public static Quaternion GetLookAtRotation(GameObject gameObjectToRotate, Attachment attachment, ParamSelector lookAtPos, ParamSelector up, AttachObject reference = null) { Vector3 lookAtWorldPos = lookAtPos.GetWorldPosition(attachment, reference); Vector3 worldUp = up.GetWorldDirection(attachment, reference); if (gameObjectToRotate.transform.position == lookAtWorldPos) { return(gameObjectToRotate.transform.rotation); } return(Quaternion.LookRotation(lookAtWorldPos - gameObjectToRotate.transform.position, worldUp)); }
// returns a world position public static Vector3 GetDesiredPositionForObject(AttachObject obj, PositionBehavior behavior, ParamSelector anchor1, ParamSelector anchor2) { switch (behavior) { case PositionBehavior.DoNothing: throw new System.InvalidOperationException("PositionBehavior is to do nothing"); // return obj.gameObject.transform.position; case PositionBehavior.Snap: return(GetSnapPosition(obj.attachable, obj.attachment, anchor1, anchor2, obj)); } throw new System.NotImplementedException("Position behavior not implemented."); }
public static void RefreshLineRenderer(AttachStrategyLineRendererDescription lineRendererDescription, LineState state, AttachObject[] objects, AttachObject reference = null, bool hideLineRendererInInspector = true) { AttachStrategyLineRendererDescription lrd = lineRendererDescription; Attachment attachment = objects[0].attachment; if (state.points == null || state.points.Length != objects.Length) { state.points = new Vector3[objects.Length]; } state.widthCurve = new AnimationCurve(); state.colorGradient = new Gradient(); GradientColorKey[] colors = new GradientColorKey[objects.Length]; GradientAlphaKey[] alphas = new GradientAlphaKey[objects.Length]; int i = 0; foreach (AttachObject obj in objects) { ParamSelector selector = lrd.GetPositionParamSelector(obj); state.points[i] = selector.GetWorldPosition(attachment, reference ?? obj); if (lrd.perObjectWidth) { selector = lrd.GetWidthParamSelector(obj); state.widthCurve.AddKey(i / (objects.Length - 1), selector.GetParam(attachment, reference ?? obj).floatValue); } if (lrd.perObjectColor) { selector = lrd.GetColorParamSelector(obj); colors[i] = new GradientColorKey(selector.GetParam(attachment, reference ?? obj).colorValue, i / (objects.Length - 1)); alphas[i] = new GradientAlphaKey(selector.GetParam(attachment, reference ?? obj).colorValue.a, i / (objects.Length - 1)); } i++; } if (lrd.perObjectColor) { state.colorGradient.SetKeys(colors, alphas); } lrd.UpdateLineRenderer(state.lineRenderer, state.points, lrd.perObjectColor ? state.colorGradient : null, lrd.perObjectWidth ? state.widthCurve : null); state.lineRenderer.hideFlags = hideLineRendererInInspector ? HideFlags.HideInInspector : HideFlags.None; }