// Token: 0x06000456 RID: 1110 RVA: 0x00023015 File Offset: 0x00021215 private static IEnumerable <Vector3> NewCatmullRom <T>(IList nodes, Interpolate.ToVector3 <T> toVector3, int slices, bool loop) { if (nodes.Count >= 2) { yield return(toVector3((T)((object)nodes[0]))); int last = nodes.Count - 1; int current = 0; while (loop || current < last) { if (loop && current > last) { current = 0; } int previous = (current == 0) ? (loop ? last : current) : (current - 1); int start = current; int end = (current == last) ? (loop ? 0 : current) : (current + 1); int next = (end == last) ? (loop ? 0 : end) : (end + 1); int stepCount = slices + 1; int num; for (int step = 1; step <= stepCount; step = num + 1) { yield return(Interpolate.CatmullRom(toVector3((T)((object)nodes[previous])), toVector3((T)((object)nodes[start])), toVector3((T)((object)nodes[end])), toVector3((T)((object)nodes[next])), (float)step, (float)stepCount)); num = step; } num = current; current = num + 1; } } yield break; }
public PrettyPolyPoint[] GetCatmullRom() { int len = points.Length; if (len <= 2) { return(points); } List <PrettyPolyPoint> newPoints = new List <PrettyPolyPoint>(); for (int i = 0; i < len; i++) { PrettyPolyPoint prev = points[(i - 1 + len) % len]; PrettyPolyPoint start = points[i]; PrettyPolyPoint end = points[(i + 1) % len]; PrettyPolyPoint next = points[(i + 2) % len]; if (!closed) { if (i == 0) { prev = start; } else if (i == len - 2) { next = end; } else if (i == len - 1) { return(newPoints.ToArray()); } } for (float j = 0; j < subdivisions; j++) { float t = j / (float)subdivisions; Vector3 pos = Interpolate.CatmullRom(prev.position, start.position, end.position, next.position, t); PrettyPolyPoint prettyPolyPoint = new PrettyPolyPoint(pos); prettyPolyPoint.color = Color.Lerp(start.color, end.color, t); prettyPolyPoint.size = Mathf.Lerp(start.size, end.size, t); newPoints.Add(prettyPolyPoint); } } return(newPoints.ToArray()); }
private void SmoothDraw() { int width = m_Points.GetLength(0); int height = m_Points.GetLength(1); for (int y = 1; y < height; y++) { for (int x = 1; x < width; x++) { Vector2 left = new Vector2(), up = new Vector2(); Vector2 p = ToVec2(m_Points[x, y].Position); if (x > 1) { left = ToVec2(m_Points[x - 1, y].Position); bool thickLine = y % 3 == 1; Color colour = outsideColour; float thickness = minLineWidth; if (thickLine) { colour = insideColour; thickness = maxLineWidth; } // use Catmull-Rom interpolation to help smooth bends in the grid int clampedX = Mathf.Min(x + 1, width - 1); Vector2 mid = Interpolate.CatmullRom(ToVec2(m_Points[x - 2, y].Position), left, p, ToVec2(m_Points[clampedX, y].Position), .5f, .5f); // If the grid is very straight here, draw a single straight line. Otherwise, draw lines to our // new interpolated midpoint var dist = Vector2.Distance(mid, (left + p) / 2); if (dist * dist > 1) { DrawLine(left, mid, colour, thickness, m_Renderers[m_LineIndex]); m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count; DrawLine(mid, p, colour, thickness, m_Renderers[m_LineIndex]); m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count; } else { DrawLine(left, p, colour, thickness, m_Renderers[m_LineIndex]); m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count; } } if (y > 1) { up = ToVec2(m_Points[x, y - 1].Position); bool thickLine = x % 3 == 1; Color colour = outsideColour; float thickness = minLineWidth; if (thickLine) { colour = insideColour; thickness = maxLineWidth; } int clampedY = Mathf.Min(y + 1, height - 1); Vector2 mid = Interpolate.CatmullRom(ToVec2(m_Points[x, y - 2].Position), up, p, ToVec2(m_Points[x, clampedY].Position), 0.5f, .5f); var dist = Vector2.Distance(mid, (up + p) / 2); if (dist * dist > 1) { DrawLine(up, mid, colour, thickness, m_Renderers[m_LineIndex]); m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count; DrawLine(mid, p, colour, thickness, m_Renderers[m_LineIndex]); m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count; } else { DrawLine(up, p, colour, thickness, m_Renderers[m_LineIndex]); m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count; } } // Add interpolated lines halfway between our point masses. This makes the grid look // denser without the cost of simulating more springs and point masses. if (x > 1 && y > 1) { Vector2 upLeft = ToVec2(m_Points[x - 1, y - 1].Position); DrawLine(0.5f * (upLeft + up), 0.5f * (left + p), outsideColour, minLineWidth, m_Renderers[m_LineIndex]); // vertical line m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count; DrawLine(0.5f * (upLeft + left), 0.5f * (up + p), outsideColour, minLineWidth, m_Renderers[m_LineIndex]); // horizontal line m_LineIndex = (m_LineIndex + 1) % m_Renderers.Count; } } } }
// invocation using float values and specifying an alpha value public static void JointLimit(ref float xMin, ref float xMax, ref float yMax, ref float zMax, Vector3 origin, Quaternion orientation, Vector3 axis, Vector3 secondaryAxis, float scale, float alpha) { // ConfigurableJoint defaults to Vector3.right if axis is Vector3.zero - contrary to documentation axis = (axis.sqrMagnitude > 0f)?axis:Vector3.right; // if secondaryAxis is Vector3.zero, then it defaults to Vector.up secondaryAxis = (secondaryAxis.sqrMagnitude > 0f)?secondaryAxis:Vector3.up; // if both secondaryAxis and axis are the same secondaryAxis = (Mathf.Abs(Vector3.Dot(axis, secondaryAxis)) == 1f)?Vector3.right:secondaryAxis; // normalize axes axis.Normalize(); secondaryAxis.Normalize(); // on a ConfigurableJoint, secondary axis is used for nothing if it the same as primary axis bool isSecondaryAxisValid = !(Mathf.Abs(Vector3.Dot(axis, secondaryAxis)) == 1f); // on a ConfigurableJoint, secondary axis is re-orthogonalized from Vector3.up or Vector3.forward (if axis is Vector3.up) if (!isSecondaryAxisValid) { secondaryAxis = (Mathf.Abs(Vector3.Dot(axis, Vector3.up)) == 1f)?Vector3.forward:Vector3.up; } // compute the third axis Vector3 tertiaryAxis = Vector3.Cross(axis, secondaryAxis); // orthogonalize secondary axis secondaryAxis = Vector3.Cross(tertiaryAxis, axis); // colors for each handle Color xLimitColor = Color.red; xLimitColor.a = alpha; Color yLimitColor = Color.green; yLimitColor.a = alpha; Color zLimitColor = Color.blue; zLimitColor.a = alpha; CustomHandleUtilities.SetHandleColor(xLimitColor); Handles.ArrowCap(0, origin, Quaternion.LookRotation(orientation * axis), scale * 0.2f); CustomHandleUtilities.SetHandleColor(yLimitColor); Handles.ArrowCap(0, origin, Quaternion.LookRotation(orientation * secondaryAxis), scale * 0.2f); CustomHandleUtilities.SetHandleColor(zLimitColor); Handles.ArrowCap(0, origin, Quaternion.LookRotation(orientation * tertiaryAxis), scale * 0.2f); // xMin/xMax Handles Quaternion handleOffset = Quaternion.LookRotation(tertiaryAxis, axis); // offset from orientation into handle's plane Quaternion handleOrientation = orientation * handleOffset; // composite orientation of the handle float val = -xMin; DiscHandles.Arc(ref val, origin, scale, handleOrientation, "", xLimitColor, false, false); xMin = Mathf.Min(-val, xMax); val = -xMax; DiscHandles.Arc(ref val, origin, scale, handleOrientation, "", xLimitColor, false, false); xMax = Mathf.Max(-val, xMin); CustomHandleUtilities.SetHandleColor(xLimitColor, xLimitColor.a * 0.1f); Vector3 xHandle1 = orientation * Quaternion.AngleAxis(-xMin, axis) * handleOffset * Vector3.forward; Vector3 xHandle2 = orientation * Quaternion.AngleAxis(-xMax, axis) * handleOffset * Vector3.forward; // yMax Handles handleOffset = Quaternion.LookRotation(tertiaryAxis, secondaryAxis); handleOrientation = orientation * handleOffset; val = yMax; DiscHandles.Arc(ref val, origin, scale, handleOrientation, "", yLimitColor, false, false); yMax = Mathf.Max(val, 0f); val *= -1f; DiscHandles.Arc(ref val, origin, scale, handleOrientation, "", yLimitColor, false, false); yMax = Mathf.Max(-val, 0f); Vector3 yHandle1 = orientation * Quaternion.AngleAxis(-yMax, secondaryAxis) * handleOffset * Vector3.forward; Vector3 yHandle2 = orientation * Quaternion.AngleAxis(yMax, secondaryAxis) * handleOffset * Vector3.forward; // a quaternion to describe the orientation of each handle Quaternion qX1 = Quaternion.LookRotation(xHandle1, tertiaryAxis); Quaternion qX2 = Quaternion.LookRotation(xHandle2, tertiaryAxis); Quaternion qY1 = Quaternion.LookRotation(yHandle1, tertiaryAxis); Quaternion qY2 = Quaternion.LookRotation(yHandle2, tertiaryAxis); // draw lines to shade the cone Vector3[] pts = new Vector3[5]; pts[0] = qX1 * Vector3.forward * scale; pts[1] = qY1 * Vector3.forward * scale; pts[2] = qX2 * Vector3.forward * scale; pts[3] = qY2 * Vector3.forward * scale; pts[4] = qX1 * Vector3.forward * scale; // use a catmull-rom spline to define the cone int last = pts.Length - 1; for (int current = 0; current < last; current++) { int previous = (current == 0)?last:current - 1; int start = current; int end = (current == last)?0:current + 1; int next = (end == last)?0:end + 1; // determine slice count based on arc length between points int slices = (int)(CustomHandleUtilities.GetIntegratorStep(origin, scale) * 50f * Vector3.Angle(pts[start], pts[end])); // adding one guarantees yielding at least the end point int stepCount = slices + 1; float oneOverStepCount = 1f / stepCount; Vector3 currentPt = pts[current]; Vector3 previousPt = currentPt; for (int step = 1; step <= stepCount; step++) { // compute current color Color col = Color.Lerp(xLimitColor, yLimitColor, (current == 1 || current == 3)?1f - step * oneOverStepCount:step * oneOverStepCount); // lines to fill cone CustomHandleUtilities.SetHandleColor(col, col.a * 0.25f); currentPt = Interpolate.CatmullRom(pts[previous], pts[start], pts[end], pts[next], step, stepCount).normalized *scale; Handles.DrawLine(origin, origin + Interpolate.CatmullRom(pts[previous], pts[start], pts[end], pts[next], step, stepCount).normalized *scale); // lines to draw outer arc CustomHandleUtilities.SetHandleColor(col); Handles.DrawLine(origin + previousPt, origin + currentPt); // increment previousPt = currentPt; } } // zMax Handles handleOrientation = orientation * Quaternion.LookRotation(axis, tertiaryAxis); Quaternion oppositeHandleOrientation = orientation * Quaternion.AngleAxis(180f, tertiaryAxis) * Quaternion.LookRotation(axis, tertiaryAxis); val = zMax; DiscHandles.Arc(ref val, origin, scale * 0.5f, handleOrientation, "", zLimitColor, true, false); zMax = Mathf.Max(val, 0f); val *= -1f; DiscHandles.Arc(ref val, origin, scale * 0.5f, handleOrientation, "", zLimitColor, true, false); zMax = Mathf.Max(-val, 0f); val = zMax; DiscHandles.Arc(ref val, origin, scale * 0.5f, oppositeHandleOrientation, "", zLimitColor, true, false); zMax = Mathf.Max(val, 0f); val *= -1f; DiscHandles.Arc(ref val, origin, scale * 0.5f, oppositeHandleOrientation, "", zLimitColor, true, false); zMax = Mathf.Max(-val, 0f); }