Skip to content

Commit

Permalink
Extensions moved from MyPhysics to MyExtensions
Browse files Browse the repository at this point in the history
Bunch of extensions moved from MyPhysics to MyExtensions; "SetCameraDepthFrom" to "WorldPointOffsetByDepth" replacement suggestion
  • Loading branch information
Deadcows committed Aug 22, 2021
1 parent e53d9a1 commit ac3babe
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 51 deletions.
74 changes: 74 additions & 0 deletions Extensions/MyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,80 @@ public static bool IsWorldPointInViewport(this Camera camera, Vector3 point)
var position = camera.WorldToViewportPoint(point);
return position.x > 0 && position.y > 0;
}

/// <summary>
/// Gets a point with the same screen point as the source point,
/// but at the specified distance from camera.
/// </summary>
public static Vector3 WorldPointOffsetByDepth(this Camera camera,
Vector3 source,
float distanceFromCamera,
Camera.MonoOrStereoscopicEye eye = Camera.MonoOrStereoscopicEye.Mono)
{
var screenPoint = camera.WorldToScreenPoint(source, eye);
return camera.ScreenToWorldPoint(screenPoint.SetZ(distanceFromCamera),
eye);
}

/// <summary>
/// Gets a point with the same screen point on the specified Camera as the
/// source point, but at the specified distance from said Camera.
/// </summary>
public static Vector3 SetCameraDepthFrom(this Vector3 worldPos,
Camera projectingCamera,
float distance,
Camera.MonoOrStereoscopicEye eye = Camera.MonoOrStereoscopicEye.Mono)
{
var screenPoint = projectingCamera.WorldToScreenPoint(worldPos, eye);
return projectingCamera.ScreenToWorldPoint(screenPoint.SetZ(distance),
eye);
}


/// <summary>
/// Sets the lossy scale of the source Transform.
/// </summary>
public static Transform SetLossyScale(this Transform source,
Vector3 targetLossyScale)
{
source.localScale = source.lossyScale.Pow(-1).ScaleBy(targetLossyScale)
.ScaleBy(source.localScale);
return source;
}

/// <summary>
/// Sets a layer to the source's attached GameObject and all of its children
/// in the hierarchy.
/// </summary>
public static T SetLayerRecursively<T>(this T source, string layer)
where T : Component
{
source.gameObject.SetLayerRecursively(LayerMask.NameToLayer(layer));
return source;
}

/// <summary>
/// Sets a layer to the source's attached GameObject and all of its children
/// in the hierarchy.
/// </summary>
public static T SetLayerRecursively<T>(this T source, int layer)
where T : Component
{
source.gameObject.SetLayerRecursively(layer);
return source;
}

/// <summary>
/// Sets a layer to the source GameObject and all of its children in the
/// hierarchy.
/// </summary>
public static GameObject SetLayerRecursively(this GameObject source,
int layer)
{
var allTransforms = source.GetComponentsInChildren<Transform>(true);
foreach (var tf in allTransforms) tf.gameObject.layer = layer;
return source;
}


public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
Expand Down
48 changes: 0 additions & 48 deletions Extensions/MyPhysics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,5 @@ public static Rigidbody2D ToggleConstraints(this Rigidbody2D source,
source.constraints = source.constraints.BitwiseToggle(constraints, state);
return source;
}

/// <summary>
/// Gets a point with the same screen point on the specified Camera as the
/// source point, but at the specified distance from said Camera.
/// </summary>
public static Vector3 SetCameraDepthFrom(this Vector3 worldPos,
Camera projectingCamera,
float distance,
Camera.MonoOrStereoscopicEye eye = Camera.MonoOrStereoscopicEye.Mono)
{
var screenPoint = projectingCamera.WorldToScreenPoint(worldPos, eye);
return projectingCamera.ScreenToWorldPoint(screenPoint.SetZ(distance),
eye);
}

/// <summary>
/// Sets the lossy scale of the source Transform.
/// </summary>
public static Transform SetLossyScale(this Transform source,
Vector3 targetLossyScale)
{
source.localScale = source.lossyScale.Pow(-1).ScaleBy(targetLossyScale)
.ScaleBy(source.localScale);
return source;
}

/// <summary>
/// Sets a layer to the source's attached GameObject and all of its children
/// in the hierarchy.
/// </summary>
public static T SetLayerRecursively<T>(this T source, int layer)
where T : Component
{
source.gameObject.SetLayerRecursively(layer);
return source;
}

/// <summary>
/// Sets a layer to the source GameObject and all of its children in the
/// hierarchy.
/// </summary>
public static GameObject SetLayerRecursively(this GameObject source,
int layer)
{
var allTransforms = source.GetComponentsInChildren<Transform>(true);
foreach (var tf in allTransforms) tf.gameObject.layer = layer;
return source;
}
}
}
4 changes: 2 additions & 2 deletions Extensions/MyUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static void SetCanvasState(CanvasGroup canvas, bool setOn)
/// <summary>
/// Toggle CanvasGroup Alpha, Interactable and BlocksRaycasts settings
/// </summary>
public static void SetState(this CanvasGroup _canvas, bool isOn)
public static void SetState(this CanvasGroup canvas, bool isOn)
{
SetCanvasState(_canvas, isOn);
SetCanvasState(canvas, isOn);
}


Expand Down
12 changes: 11 additions & 1 deletion Extensions/MyVectors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public static bool Approximately(this Vector2 vector, Vector2 compared, float th
#endregion


#region Get Closest
#region Get Closest

/// <summary>
/// Finds the position closest to the given one.
Expand Down Expand Up @@ -515,6 +515,9 @@ public static Vector3 To(this Component source, Vector3 destination) =>

#endregion


#region Pow

/// <summary>
/// Raise each component of the source Vector2 to the specified power.
/// </summary>
Expand All @@ -539,6 +542,11 @@ public static Vector4 Pow(this Vector4 source, float exponent) =>
Mathf.Pow(source.z, exponent),
Mathf.Pow(source.w, exponent));

#endregion


#region ScaleBy

/// <summary>
/// Immutably returns the result of the source vector multiplied with
/// another vector component-wise.
Expand All @@ -559,5 +567,7 @@ public static Vector3 ScaleBy(this Vector3 source, Vector3 right) =>
/// </summary>
public static Vector4 ScaleBy(this Vector4 source, Vector4 right) =>
Vector4.Scale(source, right);

#endregion
}
}

0 comments on commit ac3babe

Please sign in to comment.