29 : this(localSphere.
center, localSphere.
radius, withTransform) { }
32 : this(default(Vector3),
radius, transformSource) { }
35 : this(default(Vector3),
radius, null) { }
46 this.transform = (transformSource ==
null ? null : transformSource.transform);
70 return Matrix4x4.Translate(
center);
83 return this.
matrix.MultiplyPoint3x4(Vector3.zero);
92 var copy =
new Sphere(
this); copy.center =
center;
return copy;
100 return Collision.DoesOverlap(
this, box);
108 return Collision.DistanceBetween(
this, rect);
112 return Collision.DoesOverlap(
this, rect);
117 #region Debug Rendering
119 public void DrawLines(Action<Vector3, Vector3> lineDrawingFunc,
120 int latitudinalDivisions = 5,
121 int longitudinalDivisions = 5,
122 int numCircleSegments = 7,
123 Matrix4x4? matrixOverride =
null) {
124 Matrix4x4 m = Matrix4x4.identity;
128 if (matrixOverride.HasValue) {
129 m = matrixOverride.Value;
139 var x = Vector3.right;
143 int latDiv = latitudinalDivisions;
144 float latAngle = 180f / latDiv;
float accumLatAngle = 0f;
145 int lonDiv = longitudinalDivisions;
146 float lonAngle = 180f / lonDiv;
147 Quaternion lonRot = Quaternion.AngleAxis(lonAngle, y);
148 Vector3 lonNormal = x;
149 for (
int i = 0; i < latDiv; i++) {
150 accumLatAngle += latAngle;
154 radius: Mathf.Sin(accumLatAngle * Mathf.Deg2Rad) *
radius,
155 numCircleSegments: numCircleSegments,
156 lineDrawingFunc: lineDrawingFunc,
157 fractionOfCircleToDraw: 1.0f,
158 radialStartDirection: x,
162 for (
int i = 0; i < lonDiv; i++) {
167 numCircleSegments: numCircleSegments,
168 lineDrawingFunc: lineDrawingFunc,
169 fractionOfCircleToDraw: 1.0f,
170 radialStartDirection: y,
173 lonNormal = lonRot * lonNormal;
178 Matrix4x4 m = Matrix4x4.identity;
183 var origDrawerColor = drawer.
color;
185 Vector3
center = m.MultiplyPoint3x4(this.center);
186 float radius = m.MultiplyPoint3x4(Vector3.right).magnitude * this.
radius;
187 Vector3 x = m.MultiplyVector(Vector3.right);
188 Vector3 y = m.MultiplyVector(Vector3.up);
192 drawer.
color = drawer.
color.WithAlpha(origDrawerColor.a * 0.05f);
196 drawer.
color = drawer.
color.WithAlpha(origDrawerColor.a * 0.2f);
198 float latAngle = 180f / latDiv;
float accumLatAngle = 0f;
200 float lonAngle = 180f / lonDiv;
201 Quaternion lonRot = Quaternion.AngleAxis(lonAngle, y);
202 Vector3 lonNormal = x;
203 for (
int i = 0; i < latDiv; i++) {
204 accumLatAngle += latAngle;
207 radialStartDirection: x,
208 radius: Mathf.Sin(accumLatAngle * Mathf.Deg2Rad) *
radius,
209 fractionOfCircleToDraw: 1.0f,
210 numCircleSegments: 22);
212 for (
int i = 0; i < latDiv; i++) {
215 radialStartDirection: y,
217 fractionOfCircleToDraw: 1.0f,
218 numCircleSegments: 22);
219 lonNormal = lonRot * lonNormal;
222 drawer.
color = origDrawerColor;
229 public static class SphereExtensions {
234 public static Sphere ToSphere(
this Vector3 vec3,
float radius) {
235 return new Sphere(vec3, radius);
UnityEngine.Component Component
void DrawWireArc(Vector3 center, Vector3 normal, Vector3 radialStartDirection, float radius, float fractionOfCircleToDraw, int numCircleSegments=16)
Color color
Sets or gets the color for the gizmos that will be drawn next.
void DrawSphere(Vector3 center, float radius)
Draws a filled gizmo sphere at the given position with the given radius.
UnityEngine.Rect UnityRect
static void DrawWireArc(Vector3 center, Vector3 normal, float radius, int numCircleSegments, Action< Vector3, Vector3 > lineDrawingFunc, float fractionOfCircleToDraw=1.0f, Matrix4x4? matrix=null, Vector3? radialStartDirection=null)
void DrawRuntimeGizmos(RuntimeGizmoDrawer drawer)
Sphere(Vector3 center=default(Vector3), float radius=0.10f, Component transformSource=null, Matrix4x4? overrideMatrix=null)
Matrix4x4? overrideMatrix
Matrix4x4 matrix
Local-to-world matrix for this Sphere.
Sphere(float radius=0.10f, Component transformSource=null)
Sphere(Vector3 center=default(Vector3), float radius=0.10f, Transform transform=null)
Vector3 position
The world position of the center of this sphere (read only). This is dependent on the state of its Tr...
Sphere(float radius=0.10f, Matrix4x4? overrideMatrix=null)
Sphere WithCenter(Vector3 center)
Sphere(float radius=0.10f)
Sphere(LocalSphere localSphere, Transform withTransform)
void DrawLines(Action< Vector3, Vector3 > lineDrawingFunc, int latitudinalDivisions=5, int longitudinalDivisions=5, int numCircleSegments=7, Matrix4x4? matrixOverride=null)
Sphere(Vector3 center=default(Vector3), float radius=0.10f)
float DistanceTo(Rect rect)
Returns the distance between the closest points on the Rect and the Sphere, or 0 if the two overlap.