Gear Schematic HUD
Created in-flight equipment HUD & schematic display
This commit is contained in:
parent
6db7c65961
commit
1e273d691e
253 changed files with 19369 additions and 6296 deletions
|
|
@ -0,0 +1,9 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3b6b8774345aa954fb85ec83184b9095
|
||||
folderAsset: yes
|
||||
timeCreated: 1445521682
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,463 @@
|
|||
/// Credit jack.sydorenko, firagon
|
||||
/// Sourced from - http://forum.unity3d.com/threads/new-ui-and-line-drawing.253772/
|
||||
/// Updated/Refactored from - http://forum.unity3d.com/threads/new-ui-and-line-drawing.253772/#post-2528050
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
[AddComponentMenu("UI/Extensions/Primitives/UILineRenderer")]
|
||||
[RequireComponent(typeof(RectTransform))]
|
||||
public class UILineRenderer : UIPrimitiveBase
|
||||
{
|
||||
private enum SegmentType
|
||||
{
|
||||
Start,
|
||||
Middle,
|
||||
End,
|
||||
Full,
|
||||
}
|
||||
|
||||
public enum JoinType
|
||||
{
|
||||
Bevel,
|
||||
Miter
|
||||
}
|
||||
|
||||
public enum BezierType
|
||||
{
|
||||
None,
|
||||
Quick,
|
||||
Basic,
|
||||
Improved,
|
||||
Catenary,
|
||||
}
|
||||
|
||||
private const float MIN_MITER_JOIN = 15 * Mathf.Deg2Rad;
|
||||
|
||||
// A bevel 'nice' join displaces the vertices of the line segment instead of simply rendering a
|
||||
// quad to connect the endpoints. This improves the look of textured and transparent lines, since
|
||||
// there is no overlapping.
|
||||
private const float MIN_BEVEL_NICE_JOIN = 30 * Mathf.Deg2Rad;
|
||||
|
||||
private static Vector2 UV_TOP_LEFT, UV_BOTTOM_LEFT, UV_TOP_CENTER_LEFT, UV_TOP_CENTER_RIGHT, UV_BOTTOM_CENTER_LEFT, UV_BOTTOM_CENTER_RIGHT, UV_TOP_RIGHT, UV_BOTTOM_RIGHT;
|
||||
private static Vector2[] startUvs, middleUvs, endUvs, fullUvs;
|
||||
|
||||
[SerializeField, Tooltip("Points to draw lines between\n Can be improved using the Resolution Option")]
|
||||
internal Vector2[] m_points;
|
||||
[SerializeField, Tooltip("Segments to be drawn\n This is a list of arrays of points")]
|
||||
internal List<Vector2[]> m_segments;
|
||||
|
||||
[SerializeField, Tooltip("Thickness of the line")]
|
||||
internal float lineThickness = 2;
|
||||
[SerializeField, Tooltip("Use the relative bounds of the Rect Transform (0,0 -> 0,1) or screen space coordinates")]
|
||||
internal bool relativeSize;
|
||||
[SerializeField, Tooltip("Do the points identify a single line or split pairs of lines")]
|
||||
internal bool lineList;
|
||||
[SerializeField, Tooltip("Add end caps to each line\nMultiple caps when used with Line List")]
|
||||
internal bool lineCaps;
|
||||
[SerializeField, Tooltip("Resolution of the Bezier curve, different to line Resolution")]
|
||||
internal int bezierSegmentsPerCurve = 10;
|
||||
|
||||
public float LineThickness
|
||||
{
|
||||
get { return lineThickness; }
|
||||
set { lineThickness = value; SetAllDirty(); }
|
||||
}
|
||||
|
||||
public bool RelativeSize
|
||||
{
|
||||
get { return relativeSize; }
|
||||
set { relativeSize = value; SetAllDirty(); }
|
||||
}
|
||||
|
||||
public bool LineList
|
||||
{
|
||||
get { return lineList; }
|
||||
set { lineList = value; SetAllDirty(); }
|
||||
}
|
||||
|
||||
public bool LineCaps
|
||||
{
|
||||
get { return lineCaps; }
|
||||
set { lineCaps = value; SetAllDirty(); }
|
||||
}
|
||||
|
||||
[Tooltip("The type of Join used between lines, Square/Mitre or Curved/Bevel")]
|
||||
public JoinType LineJoins = JoinType.Bevel;
|
||||
|
||||
[Tooltip("Bezier method to apply to line, see docs for options\nCan't be used in conjunction with Resolution as Bezier already changes the resolution")]
|
||||
public BezierType BezierMode = BezierType.None;
|
||||
|
||||
public int BezierSegmentsPerCurve
|
||||
{
|
||||
get { return bezierSegmentsPerCurve; }
|
||||
set { bezierSegmentsPerCurve = value; }
|
||||
}
|
||||
|
||||
[HideInInspector]
|
||||
public bool drivenExternally = false;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Points to be drawn in the line.
|
||||
/// </summary>
|
||||
public Vector2[] Points
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_points;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (m_points == value)
|
||||
return;
|
||||
m_points = value;
|
||||
SetAllDirty();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// List of Segments to be drawn.
|
||||
/// </summary>
|
||||
public List<Vector2[]> Segments
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_segments;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
m_segments = value;
|
||||
SetAllDirty();
|
||||
}
|
||||
}
|
||||
|
||||
private void PopulateMesh(VertexHelper vh, Vector2[] pointsToDraw)
|
||||
{
|
||||
//If Bezier is desired, pick the implementation
|
||||
if (BezierMode != BezierType.None && BezierMode != BezierType.Catenary && pointsToDraw.Length > 3) {
|
||||
BezierPath bezierPath = new BezierPath ();
|
||||
|
||||
bezierPath.SetControlPoints (pointsToDraw);
|
||||
bezierPath.SegmentsPerCurve = bezierSegmentsPerCurve;
|
||||
List<Vector2> drawingPoints;
|
||||
switch (BezierMode) {
|
||||
case BezierType.Basic:
|
||||
drawingPoints = bezierPath.GetDrawingPoints0 ();
|
||||
break;
|
||||
case BezierType.Improved:
|
||||
drawingPoints = bezierPath.GetDrawingPoints1 ();
|
||||
break;
|
||||
default:
|
||||
drawingPoints = bezierPath.GetDrawingPoints2 ();
|
||||
break;
|
||||
}
|
||||
|
||||
pointsToDraw = drawingPoints.ToArray ();
|
||||
}
|
||||
if (BezierMode == BezierType.Catenary && pointsToDraw.Length == 2) {
|
||||
CableCurve cable = new CableCurve (pointsToDraw);
|
||||
cable.slack = Resolution;
|
||||
cable.steps = BezierSegmentsPerCurve;
|
||||
pointsToDraw = cable.Points ();
|
||||
}
|
||||
|
||||
if (ImproveResolution != ResolutionMode.None) {
|
||||
pointsToDraw = IncreaseResolution (pointsToDraw);
|
||||
}
|
||||
|
||||
// scale based on the size of the rect or use absolute, this is switchable
|
||||
var sizeX = !relativeSize ? 1 : rectTransform.rect.width;
|
||||
var sizeY = !relativeSize ? 1 : rectTransform.rect.height;
|
||||
var offsetX = -rectTransform.pivot.x * sizeX;
|
||||
var offsetY = -rectTransform.pivot.y * sizeY;
|
||||
|
||||
// Generate the quads that make up the wide line
|
||||
var segments = new List<UIVertex[]> ();
|
||||
if (lineList) {
|
||||
for (var i = 1; i < pointsToDraw.Length; i += 2) {
|
||||
var start = pointsToDraw [i - 1];
|
||||
var end = pointsToDraw [i];
|
||||
start = new Vector2 (start.x * sizeX + offsetX, start.y * sizeY + offsetY);
|
||||
end = new Vector2 (end.x * sizeX + offsetX, end.y * sizeY + offsetY);
|
||||
|
||||
if (lineCaps) {
|
||||
segments.Add (CreateLineCap (start, end, SegmentType.Start));
|
||||
}
|
||||
|
||||
segments.Add(CreateLineSegment(start, end, SegmentType.Middle, segments.Count > 1 ? segments[segments.Count - 2] : null));
|
||||
|
||||
if (lineCaps) {
|
||||
segments.Add (CreateLineCap (start, end, SegmentType.End));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (var i = 1; i < pointsToDraw.Length; i++) {
|
||||
var start = pointsToDraw [i - 1];
|
||||
var end = pointsToDraw [i];
|
||||
start = new Vector2 (start.x * sizeX + offsetX, start.y * sizeY + offsetY);
|
||||
end = new Vector2 (end.x * sizeX + offsetX, end.y * sizeY + offsetY);
|
||||
|
||||
if (lineCaps && i == 1) {
|
||||
segments.Add (CreateLineCap (start, end, SegmentType.Start));
|
||||
}
|
||||
|
||||
segments.Add (CreateLineSegment (start, end, SegmentType.Middle));
|
||||
|
||||
if (lineCaps && i == pointsToDraw.Length - 1) {
|
||||
segments.Add (CreateLineCap (start, end, SegmentType.End));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add the line segments to the vertex helper, creating any joins as needed
|
||||
for (var i = 0; i < segments.Count; i++) {
|
||||
if (!lineList && i < segments.Count - 1) {
|
||||
var vec1 = segments [i] [1].position - segments [i] [2].position;
|
||||
var vec2 = segments [i + 1] [2].position - segments [i + 1] [1].position;
|
||||
var angle = Vector2.Angle (vec1, vec2) * Mathf.Deg2Rad;
|
||||
|
||||
// Positive sign means the line is turning in a 'clockwise' direction
|
||||
var sign = Mathf.Sign (Vector3.Cross (vec1.normalized, vec2.normalized).z);
|
||||
|
||||
// Calculate the miter point
|
||||
var miterDistance = lineThickness / (2 * Mathf.Tan (angle / 2));
|
||||
var miterPointA = segments [i] [2].position - vec1.normalized * miterDistance * sign;
|
||||
var miterPointB = segments [i] [3].position + vec1.normalized * miterDistance * sign;
|
||||
|
||||
var joinType = LineJoins;
|
||||
if (joinType == JoinType.Miter) {
|
||||
// Make sure we can make a miter join without too many artifacts.
|
||||
if (miterDistance < vec1.magnitude / 2 && miterDistance < vec2.magnitude / 2 && angle > MIN_MITER_JOIN) {
|
||||
segments [i] [2].position = miterPointA;
|
||||
segments [i] [3].position = miterPointB;
|
||||
segments [i + 1] [0].position = miterPointB;
|
||||
segments [i + 1] [1].position = miterPointA;
|
||||
} else {
|
||||
joinType = JoinType.Bevel;
|
||||
}
|
||||
}
|
||||
|
||||
if (joinType == JoinType.Bevel) {
|
||||
if (miterDistance < vec1.magnitude / 2 && miterDistance < vec2.magnitude / 2 && angle > MIN_BEVEL_NICE_JOIN) {
|
||||
if (sign < 0) {
|
||||
segments [i] [2].position = miterPointA;
|
||||
segments [i + 1] [1].position = miterPointA;
|
||||
} else {
|
||||
segments [i] [3].position = miterPointB;
|
||||
segments [i + 1] [0].position = miterPointB;
|
||||
}
|
||||
}
|
||||
|
||||
var join = new UIVertex[] { segments [i] [2], segments [i] [3], segments [i + 1] [0], segments [i + 1] [1] };
|
||||
vh.AddUIVertexQuad (join);
|
||||
}
|
||||
}
|
||||
|
||||
vh.AddUIVertexQuad (segments [i]);
|
||||
}
|
||||
if (vh.currentVertCount > 64000) {
|
||||
Debug.LogError ("Max Verticies size is 64000, current mesh verticies count is [" + vh.currentVertCount + "] - Cannot Draw");
|
||||
vh.Clear ();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override void OnPopulateMesh(VertexHelper vh)
|
||||
{
|
||||
if (m_points != null && m_points.Length > 0) {
|
||||
GeneratedUVs ();
|
||||
vh.Clear ();
|
||||
|
||||
PopulateMesh (vh, m_points);
|
||||
|
||||
}
|
||||
else if (m_segments != null && m_segments.Count > 0) {
|
||||
GeneratedUVs ();
|
||||
vh.Clear ();
|
||||
|
||||
for (int s = 0; s < m_segments.Count; s++) {
|
||||
Vector2[] pointsToDraw = m_segments [s];
|
||||
PopulateMesh (vh, pointsToDraw);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private UIVertex[] CreateLineCap(Vector2 start, Vector2 end, SegmentType type)
|
||||
{
|
||||
if (type == SegmentType.Start)
|
||||
{
|
||||
var capStart = start - ((end - start).normalized * lineThickness / 2);
|
||||
return CreateLineSegment(capStart, start, SegmentType.Start);
|
||||
}
|
||||
else if (type == SegmentType.End)
|
||||
{
|
||||
var capEnd = end + ((end - start).normalized * lineThickness / 2);
|
||||
return CreateLineSegment(end, capEnd, SegmentType.End);
|
||||
}
|
||||
|
||||
Debug.LogError("Bad SegmentType passed in to CreateLineCap. Must be SegmentType.Start or SegmentType.End");
|
||||
return null;
|
||||
}
|
||||
|
||||
private UIVertex[] CreateLineSegment(Vector2 start, Vector2 end, SegmentType type, UIVertex[] previousVert = null)
|
||||
{
|
||||
Vector2 offset = new Vector2((start.y - end.y), end.x - start.x).normalized * lineThickness / 2;
|
||||
|
||||
Vector2 v1 = Vector2.zero;
|
||||
Vector2 v2 = Vector2.zero;
|
||||
if (previousVert != null) {
|
||||
v1 = new Vector2(previousVert[3].position.x, previousVert[3].position.y);
|
||||
v2 = new Vector2(previousVert[2].position.x, previousVert[2].position.y);
|
||||
} else {
|
||||
v1 = start - offset;
|
||||
v2 = start + offset;
|
||||
}
|
||||
|
||||
var v3 = end + offset;
|
||||
var v4 = end - offset;
|
||||
//Return the VDO with the correct uvs
|
||||
switch (type)
|
||||
{
|
||||
case SegmentType.Start:
|
||||
return SetVbo(new[] { v1, v2, v3, v4 }, startUvs);
|
||||
case SegmentType.End:
|
||||
return SetVbo(new[] { v1, v2, v3, v4 }, endUvs);
|
||||
case SegmentType.Full:
|
||||
return SetVbo(new[] { v1, v2, v3, v4 }, fullUvs);
|
||||
default:
|
||||
return SetVbo(new[] { v1, v2, v3, v4 }, middleUvs);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void GeneratedUVs()
|
||||
{
|
||||
if (activeSprite != null)
|
||||
{
|
||||
var outer = Sprites.DataUtility.GetOuterUV(activeSprite);
|
||||
var inner = Sprites.DataUtility.GetInnerUV(activeSprite);
|
||||
UV_TOP_LEFT = new Vector2(outer.x, outer.y);
|
||||
UV_BOTTOM_LEFT = new Vector2(outer.x, outer.w);
|
||||
UV_TOP_CENTER_LEFT = new Vector2(inner.x, inner.y);
|
||||
UV_TOP_CENTER_RIGHT = new Vector2(inner.z, inner.y);
|
||||
UV_BOTTOM_CENTER_LEFT = new Vector2(inner.x, inner.w);
|
||||
UV_BOTTOM_CENTER_RIGHT = new Vector2(inner.z, inner.w);
|
||||
UV_TOP_RIGHT = new Vector2(outer.z, outer.y);
|
||||
UV_BOTTOM_RIGHT = new Vector2(outer.z, outer.w);
|
||||
}
|
||||
else
|
||||
{
|
||||
UV_TOP_LEFT = Vector2.zero;
|
||||
UV_BOTTOM_LEFT = new Vector2(0, 1);
|
||||
UV_TOP_CENTER_LEFT = new Vector2(0.5f, 0);
|
||||
UV_TOP_CENTER_RIGHT = new Vector2(0.5f, 0);
|
||||
UV_BOTTOM_CENTER_LEFT = new Vector2(0.5f, 1);
|
||||
UV_BOTTOM_CENTER_RIGHT = new Vector2(0.5f, 1);
|
||||
UV_TOP_RIGHT = new Vector2(1, 0);
|
||||
UV_BOTTOM_RIGHT = Vector2.one;
|
||||
}
|
||||
|
||||
|
||||
startUvs = new[] { UV_TOP_LEFT, UV_BOTTOM_LEFT, UV_BOTTOM_CENTER_LEFT, UV_TOP_CENTER_LEFT };
|
||||
middleUvs = new[] { UV_TOP_CENTER_LEFT, UV_BOTTOM_CENTER_LEFT, UV_BOTTOM_CENTER_RIGHT, UV_TOP_CENTER_RIGHT };
|
||||
endUvs = new[] { UV_TOP_CENTER_RIGHT, UV_BOTTOM_CENTER_RIGHT, UV_BOTTOM_RIGHT, UV_TOP_RIGHT };
|
||||
fullUvs = new[] { UV_TOP_LEFT, UV_BOTTOM_LEFT, UV_BOTTOM_RIGHT, UV_TOP_RIGHT };
|
||||
}
|
||||
|
||||
protected override void ResolutionToNativeSize(float distance)
|
||||
{
|
||||
if (UseNativeSize)
|
||||
{
|
||||
m_Resolution = distance / (activeSprite.rect.width / pixelsPerUnit);
|
||||
lineThickness = activeSprite.rect.height / pixelsPerUnit;
|
||||
}
|
||||
}
|
||||
|
||||
private int GetSegmentPointCount()
|
||||
{
|
||||
if (Segments?.Count > 0)
|
||||
{
|
||||
int pointCount = 0;
|
||||
foreach (var segment in Segments)
|
||||
{
|
||||
pointCount += segment.Length;
|
||||
}
|
||||
return pointCount;
|
||||
}
|
||||
return Points.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the Vector2 position of a line index
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Positive numbers should be used to specify Index and Segment
|
||||
/// </remarks>
|
||||
/// <param name="index">Required Index of the point, starting from point 1</param>
|
||||
/// <param name="segmentIndex">(optional) Required Segment the point is held in, Starting from Segment 1</param>
|
||||
/// <returns>Vector2 position of the point within UI Space</returns>
|
||||
public Vector2 GetPosition(int index, int segmentIndex = 0)
|
||||
{
|
||||
if (segmentIndex > 0)
|
||||
{
|
||||
return Segments[segmentIndex - 1][index - 1];
|
||||
}
|
||||
else if (Segments.Count > 0)
|
||||
{
|
||||
var segmentIndexCount = 0;
|
||||
var indexCount = index;
|
||||
foreach (var segment in Segments)
|
||||
{
|
||||
if (indexCount - segment.Length > 0)
|
||||
{
|
||||
indexCount -= segment.Length;
|
||||
segmentIndexCount += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Segments[segmentIndexCount][indexCount - 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
return Points[index - 1];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the Vector2 position of a line within a specific segment
|
||||
/// </summary>
|
||||
/// <param name="index">Required Index of the point, starting from point 1</param>
|
||||
/// <param name="segmentIndex"> Required Segment the point is held in, Starting from Segment 1</param>
|
||||
/// <returns>Vector2 position of the point within UI Space</returns>
|
||||
public Vector2 GetPositionBySegment(int index, int segment)
|
||||
{
|
||||
return Segments[segment][index - 1];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the closest point between two given Vector2s from a given Vector2 point
|
||||
/// </summary>
|
||||
/// <param name="p1">Starting position</param>
|
||||
/// <param name="p2">End position</param>
|
||||
/// <param name="p3">Desired / Selected point</param>
|
||||
/// <returns>Closest Vector2 position of the target within UI Space</returns>
|
||||
public Vector2 GetClosestPoint(Vector2 p1, Vector2 p2, Vector2 p3)
|
||||
{
|
||||
Vector2 from_p1_to_p3 = p3 - p1;
|
||||
Vector2 from_p1_to_p2 = p2 - p1;
|
||||
float dot = Vector2.Dot(from_p1_to_p3, from_p1_to_p2.normalized);
|
||||
dot /= from_p1_to_p2.magnitude;
|
||||
float t = Mathf.Clamp01(dot);
|
||||
return p1 + from_p1_to_p2 * t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5b33b2e663e78774c9f0c9af55018725
|
||||
timeCreated: 1440845580
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,394 @@
|
|||
/// Credit jack.sydorenko, firagon
|
||||
/// Sourced from - http://forum.unity3d.com/threads/new-ui-and-line-drawing.253772/
|
||||
/// Updated/Refactored from - http://forum.unity3d.com/threads/new-ui-and-line-drawing.253772/#post-2528050
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
[AddComponentMenu("UI/Extensions/Primitives/UILineRendererList")]
|
||||
[RequireComponent(typeof(RectTransform))]
|
||||
public class UILineRendererList : UIPrimitiveBase
|
||||
{
|
||||
private enum SegmentType
|
||||
{
|
||||
Start,
|
||||
Middle,
|
||||
End,
|
||||
Full,
|
||||
}
|
||||
|
||||
public enum JoinType
|
||||
{
|
||||
Bevel,
|
||||
Miter
|
||||
}
|
||||
|
||||
public enum BezierType
|
||||
{
|
||||
None,
|
||||
Quick,
|
||||
Basic,
|
||||
Improved,
|
||||
Catenary,
|
||||
}
|
||||
|
||||
private const float MIN_MITER_JOIN = 15 * Mathf.Deg2Rad;
|
||||
|
||||
// A bevel 'nice' join displaces the vertices of the line segment instead of simply rendering a
|
||||
// quad to connect the endpoints. This improves the look of textured and transparent lines, since
|
||||
// there is no overlapping.
|
||||
private const float MIN_BEVEL_NICE_JOIN = 30 * Mathf.Deg2Rad;
|
||||
|
||||
private static Vector2 UV_TOP_LEFT, UV_BOTTOM_LEFT, UV_TOP_CENTER_LEFT, UV_TOP_CENTER_RIGHT, UV_BOTTOM_CENTER_LEFT, UV_BOTTOM_CENTER_RIGHT, UV_TOP_RIGHT, UV_BOTTOM_RIGHT;
|
||||
private static Vector2[] startUvs, middleUvs, endUvs, fullUvs;
|
||||
|
||||
[SerializeField, Tooltip("Points to draw lines between\n Can be improved using the Resolution Option")]
|
||||
internal List<Vector2> m_points;
|
||||
// [SerializeField, Tooltip("Segments to be drawn\n This is a list of arrays of points")]
|
||||
//internal List<Vector2[]> m_segments;
|
||||
|
||||
[SerializeField, Tooltip("Thickness of the line")]
|
||||
internal float lineThickness = 2;
|
||||
[SerializeField, Tooltip("Use the relative bounds of the Rect Transform (0,0 -> 0,1) or screen space coordinates")]
|
||||
internal bool relativeSize;
|
||||
[SerializeField, Tooltip("Do the points identify a single line or split pairs of lines")]
|
||||
internal bool lineList;
|
||||
[SerializeField, Tooltip("Add end caps to each line\nMultiple caps when used with Line List")]
|
||||
internal bool lineCaps;
|
||||
[SerializeField, Tooltip("Resolution of the Bezier curve, different to line Resolution")]
|
||||
internal int bezierSegmentsPerCurve = 10;
|
||||
|
||||
public float LineThickness
|
||||
{
|
||||
get { return lineThickness; }
|
||||
set { lineThickness = value; SetAllDirty(); }
|
||||
}
|
||||
|
||||
public bool RelativeSize
|
||||
{
|
||||
get { return relativeSize; }
|
||||
set { relativeSize = value; SetAllDirty(); }
|
||||
}
|
||||
|
||||
public bool LineList
|
||||
{
|
||||
get { return lineList; }
|
||||
set { lineList = value; SetAllDirty(); }
|
||||
}
|
||||
|
||||
public bool LineCaps
|
||||
{
|
||||
get { return lineCaps; }
|
||||
set { lineCaps = value; SetAllDirty(); }
|
||||
}
|
||||
|
||||
[Tooltip("The type of Join used between lines, Square/Mitre or Curved/Bevel")]
|
||||
public JoinType LineJoins = JoinType.Bevel;
|
||||
|
||||
[Tooltip("Bezier method to apply to line, see docs for options\nCan't be used in conjunction with Resolution as Bezier already changes the resolution")]
|
||||
public BezierType BezierMode = BezierType.None;
|
||||
|
||||
public int BezierSegmentsPerCurve
|
||||
{
|
||||
get { return bezierSegmentsPerCurve; }
|
||||
set { bezierSegmentsPerCurve = value; }
|
||||
}
|
||||
|
||||
[HideInInspector]
|
||||
public bool drivenExternally = false;
|
||||
|
||||
/// <summary>
|
||||
/// Points to be drawn in the line.
|
||||
/// </summary>
|
||||
/// <remarks>Don't add points to the list directly, use the add / remove functions</remarks>
|
||||
public List<Vector2> Points
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_points;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (m_points == value)
|
||||
return;
|
||||
m_points = value;
|
||||
SetAllDirty();
|
||||
}
|
||||
}
|
||||
|
||||
// /// <summary>
|
||||
// /// List of Segments to be drawn.
|
||||
// /// </summary>
|
||||
// public List<Vector2[]> Segments
|
||||
//{
|
||||
// get
|
||||
// {
|
||||
// return m_segments;
|
||||
// }
|
||||
|
||||
// set
|
||||
// {
|
||||
// m_segments = value;
|
||||
// SetAllDirty();
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
public void AddPoint(Vector2 pointToAdd)
|
||||
{
|
||||
m_points.Add(pointToAdd);
|
||||
SetAllDirty();
|
||||
}
|
||||
|
||||
public void RemovePoint(Vector2 pointToRemove)
|
||||
{
|
||||
m_points.Remove(pointToRemove);
|
||||
SetAllDirty();
|
||||
}
|
||||
|
||||
public void ClearPoints()
|
||||
{
|
||||
m_points.Clear();
|
||||
SetAllDirty();
|
||||
}
|
||||
|
||||
private void PopulateMesh(VertexHelper vh, List<Vector2> pointsToDraw)
|
||||
{
|
||||
//If Bezier is desired, pick the implementation
|
||||
if (BezierMode != BezierType.None && BezierMode != BezierType.Catenary && pointsToDraw.Count > 3) {
|
||||
BezierPath bezierPath = new BezierPath ();
|
||||
|
||||
bezierPath.SetControlPoints (pointsToDraw);
|
||||
bezierPath.SegmentsPerCurve = bezierSegmentsPerCurve;
|
||||
List<Vector2> drawingPoints;
|
||||
switch (BezierMode) {
|
||||
case BezierType.Basic:
|
||||
drawingPoints = bezierPath.GetDrawingPoints0 ();
|
||||
break;
|
||||
case BezierType.Improved:
|
||||
drawingPoints = bezierPath.GetDrawingPoints1 ();
|
||||
break;
|
||||
default:
|
||||
drawingPoints = bezierPath.GetDrawingPoints2 ();
|
||||
break;
|
||||
}
|
||||
|
||||
pointsToDraw = drawingPoints;
|
||||
}
|
||||
if (BezierMode == BezierType.Catenary && pointsToDraw.Count == 2) {
|
||||
CableCurve cable = new CableCurve (pointsToDraw);
|
||||
cable.slack = Resolution;
|
||||
cable.steps = BezierSegmentsPerCurve;
|
||||
pointsToDraw.Clear();
|
||||
pointsToDraw.AddRange(cable.Points());
|
||||
}
|
||||
|
||||
if (ImproveResolution != ResolutionMode.None) {
|
||||
pointsToDraw = IncreaseResolution (pointsToDraw);
|
||||
}
|
||||
|
||||
// scale based on the size of the rect or use absolute, this is switchable
|
||||
var sizeX = !relativeSize ? 1 : rectTransform.rect.width;
|
||||
var sizeY = !relativeSize ? 1 : rectTransform.rect.height;
|
||||
var offsetX = -rectTransform.pivot.x * sizeX;
|
||||
var offsetY = -rectTransform.pivot.y * sizeY;
|
||||
|
||||
// Generate the quads that make up the wide line
|
||||
var segments = new List<UIVertex[]> ();
|
||||
if (lineList) {
|
||||
for (var i = 1; i < pointsToDraw.Count; i += 2) {
|
||||
var start = pointsToDraw [i - 1];
|
||||
var end = pointsToDraw [i];
|
||||
start = new Vector2 (start.x * sizeX + offsetX, start.y * sizeY + offsetY);
|
||||
end = new Vector2 (end.x * sizeX + offsetX, end.y * sizeY + offsetY);
|
||||
|
||||
if (lineCaps) {
|
||||
segments.Add (CreateLineCap (start, end, SegmentType.Start));
|
||||
}
|
||||
|
||||
//segments.Add(CreateLineSegment(start, end, SegmentType.Full));
|
||||
segments.Add (CreateLineSegment (start, end, SegmentType.Middle));
|
||||
|
||||
if (lineCaps) {
|
||||
segments.Add (CreateLineCap (start, end, SegmentType.End));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (var i = 1; i < pointsToDraw.Count; i++) {
|
||||
var start = pointsToDraw [i - 1];
|
||||
var end = pointsToDraw [i];
|
||||
start = new Vector2 (start.x * sizeX + offsetX, start.y * sizeY + offsetY);
|
||||
end = new Vector2 (end.x * sizeX + offsetX, end.y * sizeY + offsetY);
|
||||
|
||||
if (lineCaps && i == 1) {
|
||||
segments.Add (CreateLineCap (start, end, SegmentType.Start));
|
||||
}
|
||||
|
||||
segments.Add (CreateLineSegment (start, end, SegmentType.Middle));
|
||||
//segments.Add(CreateLineSegment(start, end, SegmentType.Full));
|
||||
|
||||
if (lineCaps && i == pointsToDraw.Count - 1) {
|
||||
segments.Add (CreateLineCap (start, end, SegmentType.End));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add the line segments to the vertex helper, creating any joins as needed
|
||||
for (var i = 0; i < segments.Count; i++) {
|
||||
if (!lineList && i < segments.Count - 1) {
|
||||
var vec1 = segments [i] [1].position - segments [i] [2].position;
|
||||
var vec2 = segments [i + 1] [2].position - segments [i + 1] [1].position;
|
||||
var angle = Vector2.Angle (vec1, vec2) * Mathf.Deg2Rad;
|
||||
|
||||
// Positive sign means the line is turning in a 'clockwise' direction
|
||||
var sign = Mathf.Sign (Vector3.Cross (vec1.normalized, vec2.normalized).z);
|
||||
|
||||
// Calculate the miter point
|
||||
var miterDistance = lineThickness / (2 * Mathf.Tan (angle / 2));
|
||||
var miterPointA = segments [i] [2].position - vec1.normalized * miterDistance * sign;
|
||||
var miterPointB = segments [i] [3].position + vec1.normalized * miterDistance * sign;
|
||||
|
||||
var joinType = LineJoins;
|
||||
if (joinType == JoinType.Miter) {
|
||||
// Make sure we can make a miter join without too many artifacts.
|
||||
if (miterDistance < vec1.magnitude / 2 && miterDistance < vec2.magnitude / 2 && angle > MIN_MITER_JOIN) {
|
||||
segments [i] [2].position = miterPointA;
|
||||
segments [i] [3].position = miterPointB;
|
||||
segments [i + 1] [0].position = miterPointB;
|
||||
segments [i + 1] [1].position = miterPointA;
|
||||
} else {
|
||||
joinType = JoinType.Bevel;
|
||||
}
|
||||
}
|
||||
|
||||
if (joinType == JoinType.Bevel) {
|
||||
if (miterDistance < vec1.magnitude / 2 && miterDistance < vec2.magnitude / 2 && angle > MIN_BEVEL_NICE_JOIN) {
|
||||
if (sign < 0) {
|
||||
segments [i] [2].position = miterPointA;
|
||||
segments [i + 1] [1].position = miterPointA;
|
||||
} else {
|
||||
segments [i] [3].position = miterPointB;
|
||||
segments [i + 1] [0].position = miterPointB;
|
||||
}
|
||||
}
|
||||
|
||||
var join = new UIVertex[] { segments [i] [2], segments [i] [3], segments [i + 1] [0], segments [i + 1] [1] };
|
||||
vh.AddUIVertexQuad (join);
|
||||
}
|
||||
}
|
||||
|
||||
vh.AddUIVertexQuad (segments [i]);
|
||||
}
|
||||
if (vh.currentVertCount > 64000) {
|
||||
Debug.LogError ("Max Verticies size is 64000, current mesh verticies count is [" + vh.currentVertCount + "] - Cannot Draw");
|
||||
vh.Clear ();
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override void OnPopulateMesh(VertexHelper vh)
|
||||
{
|
||||
if (m_points != null && m_points.Count > 0) {
|
||||
GeneratedUVs ();
|
||||
vh.Clear ();
|
||||
|
||||
PopulateMesh (vh, m_points);
|
||||
|
||||
}
|
||||
//else if (m_segments != null && m_segments.Count > 0) {
|
||||
// GeneratedUVs ();
|
||||
// vh.Clear ();
|
||||
|
||||
// for (int s = 0; s < m_segments.Count; s++) {
|
||||
// Vector2[] pointsToDraw = m_segments [s];
|
||||
// PopulateMesh (vh, pointsToDraw);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
private UIVertex[] CreateLineCap(Vector2 start, Vector2 end, SegmentType type)
|
||||
{
|
||||
if (type == SegmentType.Start)
|
||||
{
|
||||
var capStart = start - ((end - start).normalized * lineThickness / 2);
|
||||
return CreateLineSegment(capStart, start, SegmentType.Start);
|
||||
}
|
||||
else if (type == SegmentType.End)
|
||||
{
|
||||
var capEnd = end + ((end - start).normalized * lineThickness / 2);
|
||||
return CreateLineSegment(end, capEnd, SegmentType.End);
|
||||
}
|
||||
|
||||
Debug.LogError("Bad SegmentType passed in to CreateLineCap. Must be SegmentType.Start or SegmentType.End");
|
||||
return null;
|
||||
}
|
||||
|
||||
private UIVertex[] CreateLineSegment(Vector2 start, Vector2 end, SegmentType type)
|
||||
{
|
||||
Vector2 offset = new Vector2((start.y - end.y), end.x - start.x).normalized * lineThickness / 2;
|
||||
|
||||
var v1 = start - offset;
|
||||
var v2 = start + offset;
|
||||
var v3 = end + offset;
|
||||
var v4 = end - offset;
|
||||
//Return the VDO with the correct uvs
|
||||
switch (type)
|
||||
{
|
||||
case SegmentType.Start:
|
||||
return SetVbo(new[] { v1, v2, v3, v4 }, startUvs);
|
||||
case SegmentType.End:
|
||||
return SetVbo(new[] { v1, v2, v3, v4 }, endUvs);
|
||||
case SegmentType.Full:
|
||||
return SetVbo(new[] { v1, v2, v3, v4 }, fullUvs);
|
||||
default:
|
||||
return SetVbo(new[] { v1, v2, v3, v4 }, middleUvs);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void GeneratedUVs()
|
||||
{
|
||||
if (activeSprite != null)
|
||||
{
|
||||
var outer = Sprites.DataUtility.GetOuterUV(activeSprite);
|
||||
var inner = Sprites.DataUtility.GetInnerUV(activeSprite);
|
||||
UV_TOP_LEFT = new Vector2(outer.x, outer.y);
|
||||
UV_BOTTOM_LEFT = new Vector2(outer.x, outer.w);
|
||||
UV_TOP_CENTER_LEFT = new Vector2(inner.x, inner.y);
|
||||
UV_TOP_CENTER_RIGHT = new Vector2(inner.z, inner.y);
|
||||
UV_BOTTOM_CENTER_LEFT = new Vector2(inner.x, inner.w);
|
||||
UV_BOTTOM_CENTER_RIGHT = new Vector2(inner.z, inner.w);
|
||||
UV_TOP_RIGHT = new Vector2(outer.z, outer.y);
|
||||
UV_BOTTOM_RIGHT = new Vector2(outer.z, outer.w);
|
||||
}
|
||||
else
|
||||
{
|
||||
UV_TOP_LEFT = Vector2.zero;
|
||||
UV_BOTTOM_LEFT = new Vector2(0, 1);
|
||||
UV_TOP_CENTER_LEFT = new Vector2(0.5f, 0);
|
||||
UV_TOP_CENTER_RIGHT = new Vector2(0.5f, 0);
|
||||
UV_BOTTOM_CENTER_LEFT = new Vector2(0.5f, 1);
|
||||
UV_BOTTOM_CENTER_RIGHT = new Vector2(0.5f, 1);
|
||||
UV_TOP_RIGHT = new Vector2(1, 0);
|
||||
UV_BOTTOM_RIGHT = Vector2.one;
|
||||
}
|
||||
|
||||
|
||||
startUvs = new[] { UV_TOP_LEFT, UV_BOTTOM_LEFT, UV_BOTTOM_CENTER_LEFT, UV_TOP_CENTER_LEFT };
|
||||
middleUvs = new[] { UV_TOP_CENTER_LEFT, UV_BOTTOM_CENTER_LEFT, UV_BOTTOM_CENTER_RIGHT, UV_TOP_CENTER_RIGHT };
|
||||
endUvs = new[] { UV_TOP_CENTER_RIGHT, UV_BOTTOM_CENTER_RIGHT, UV_BOTTOM_RIGHT, UV_TOP_RIGHT };
|
||||
fullUvs = new[] { UV_TOP_LEFT, UV_BOTTOM_LEFT, UV_BOTTOM_RIGHT, UV_TOP_RIGHT };
|
||||
}
|
||||
|
||||
protected override void ResolutionToNativeSize(float distance)
|
||||
{
|
||||
if (UseNativeSize)
|
||||
{
|
||||
m_Resolution = distance / (activeSprite.rect.width / pixelsPerUnit);
|
||||
lineThickness = activeSprite.rect.height / pixelsPerUnit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 34861a5dcec3dbf438f8288be97dd133
|
||||
timeCreated: 1440845580
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,159 @@
|
|||
/// Credit jonbro5556
|
||||
/// Based on original LineRender script by jack.sydorenko
|
||||
/// Sourced from - http://forum.unity3d.com/threads/new-ui-and-line-drawing.253772/
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
[AddComponentMenu("UI/Extensions/Primitives/UILineTextureRenderer")]
|
||||
public class UILineTextureRenderer : UIPrimitiveBase
|
||||
{
|
||||
[SerializeField]
|
||||
Rect m_UVRect = new Rect(0f, 0f, 1f, 1f);
|
||||
[SerializeField]
|
||||
private Vector2[] m_points;
|
||||
|
||||
public float LineThickness = 2;
|
||||
public bool UseMargins;
|
||||
public Vector2 Margin;
|
||||
public bool relativeSize;
|
||||
|
||||
/// <summary>
|
||||
/// UV rectangle used by the texture.
|
||||
/// </summary>
|
||||
public Rect uvRect
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_UVRect;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (m_UVRect == value)
|
||||
return;
|
||||
m_UVRect = value;
|
||||
SetVerticesDirty();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Points to be drawn in the line.
|
||||
/// </summary>
|
||||
public Vector2[] Points
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_points;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (m_points == value)
|
||||
return;
|
||||
m_points = value;
|
||||
SetAllDirty();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnPopulateMesh(VertexHelper vh)
|
||||
{
|
||||
// requires sets of quads
|
||||
if (m_points == null || m_points.Length < 2)
|
||||
m_points = new[] { new Vector2(0, 0), new Vector2(1, 1) };
|
||||
var capSize = 24;
|
||||
var sizeX = rectTransform.rect.width;
|
||||
var sizeY = rectTransform.rect.height;
|
||||
var offsetX = -rectTransform.pivot.x * rectTransform.rect.width;
|
||||
var offsetY = -rectTransform.pivot.y * rectTransform.rect.height;
|
||||
|
||||
// don't want to scale based on the size of the rect, so this is switchable now
|
||||
if (!relativeSize)
|
||||
{
|
||||
sizeX = 1;
|
||||
sizeY = 1;
|
||||
}
|
||||
// build a new set of m_points taking into account the cap sizes.
|
||||
// would be cool to support corners too, but that might be a bit tough :)
|
||||
var pointList = new List<Vector2>();
|
||||
pointList.Add(m_points[0]);
|
||||
var capPoint = m_points[0] + (m_points[1] - m_points[0]).normalized * capSize;
|
||||
pointList.Add(capPoint);
|
||||
|
||||
// should bail before the last point to add another cap point
|
||||
for (int i = 1; i < m_points.Length - 1; i++)
|
||||
{
|
||||
pointList.Add(m_points[i]);
|
||||
}
|
||||
capPoint = m_points[m_points.Length - 1] - (m_points[m_points.Length - 1] - m_points[m_points.Length - 2]).normalized * capSize;
|
||||
pointList.Add(capPoint);
|
||||
pointList.Add(m_points[m_points.Length - 1]);
|
||||
|
||||
var Tempm_points = pointList.ToArray();
|
||||
if (UseMargins)
|
||||
{
|
||||
sizeX -= Margin.x;
|
||||
sizeY -= Margin.y;
|
||||
offsetX += Margin.x / 2f;
|
||||
offsetY += Margin.y / 2f;
|
||||
}
|
||||
|
||||
vh.Clear();
|
||||
|
||||
Vector2 prevV1 = Vector2.zero;
|
||||
Vector2 prevV2 = Vector2.zero;
|
||||
|
||||
for (int i = 1; i < Tempm_points.Length; i++)
|
||||
{
|
||||
var prev = Tempm_points[i - 1];
|
||||
var cur = Tempm_points[i];
|
||||
prev = new Vector2(prev.x * sizeX + offsetX, prev.y * sizeY + offsetY);
|
||||
cur = new Vector2(cur.x * sizeX + offsetX, cur.y * sizeY + offsetY);
|
||||
|
||||
float angle = Mathf.Atan2(cur.y - prev.y, cur.x - prev.x) * 180f / Mathf.PI;
|
||||
|
||||
var v1 = prev + new Vector2(0, -LineThickness / 2);
|
||||
var v2 = prev + new Vector2(0, +LineThickness / 2);
|
||||
var v3 = cur + new Vector2(0, +LineThickness / 2);
|
||||
var v4 = cur + new Vector2(0, -LineThickness / 2);
|
||||
|
||||
v1 = RotatePointAroundPivot(v1, prev, new Vector3(0, 0, angle));
|
||||
v2 = RotatePointAroundPivot(v2, prev, new Vector3(0, 0, angle));
|
||||
v3 = RotatePointAroundPivot(v3, cur, new Vector3(0, 0, angle));
|
||||
v4 = RotatePointAroundPivot(v4, cur, new Vector3(0, 0, angle));
|
||||
|
||||
Vector2 uvTopLeft = Vector2.zero;
|
||||
Vector2 uvBottomLeft = new Vector2(0, 1);
|
||||
|
||||
Vector2 uvTopCenter = new Vector2(0.5f, 0);
|
||||
Vector2 uvBottomCenter = new Vector2(0.5f, 1);
|
||||
|
||||
Vector2 uvTopRight = new Vector2(1, 0);
|
||||
Vector2 uvBottomRight = new Vector2(1, 1);
|
||||
|
||||
Vector2[] uvs = new[] { uvTopCenter, uvBottomCenter, uvBottomCenter, uvTopCenter };
|
||||
|
||||
if (i > 1)
|
||||
vh.AddUIVertexQuad(SetVbo(new[] { prevV1, prevV2, v1, v2 }, uvs));
|
||||
|
||||
if (i == 1)
|
||||
uvs = new[] { uvTopLeft, uvBottomLeft, uvBottomCenter, uvTopCenter };
|
||||
else if (i == Tempm_points.Length - 1)
|
||||
uvs = new[] { uvTopCenter, uvBottomCenter, uvBottomRight, uvTopRight };
|
||||
|
||||
vh.AddUIVertexQuad(SetVbo(new[] { v1, v2, v3, v4 }, uvs));
|
||||
|
||||
|
||||
prevV1 = v3;
|
||||
prevV2 = v4;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles)
|
||||
{
|
||||
Vector3 dir = point - pivot; // get point direction relative to pivot
|
||||
dir = Quaternion.Euler(angles) * dir; // rotate it
|
||||
point = dir + pivot; // calculate rotated point
|
||||
return point; // return it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b801876ddb189f94f905cd1646564fbf
|
||||
timeCreated: 1440845581
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,362 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
public enum ResolutionMode
|
||||
{
|
||||
None,
|
||||
PerSegment,
|
||||
PerLine
|
||||
}
|
||||
|
||||
[RequireComponent(typeof(CanvasRenderer))]
|
||||
public class UIPrimitiveBase : MaskableGraphic, ILayoutElement, ICanvasRaycastFilter
|
||||
{
|
||||
static protected Material s_ETC1DefaultUI = null;
|
||||
List<Vector2> outputList = new List<Vector2>();
|
||||
|
||||
[SerializeField] private Sprite m_Sprite;
|
||||
public Sprite sprite { get { return m_Sprite; } set { if (SetPropertyUtility.SetClass(ref m_Sprite, value)) GeneratedUVs(); SetAllDirty(); } }
|
||||
|
||||
[NonSerialized]
|
||||
private Sprite m_OverrideSprite;
|
||||
public Sprite overrideSprite { get { return activeSprite; } set { if (SetPropertyUtility.SetClass(ref m_OverrideSprite, value)) GeneratedUVs(); SetAllDirty(); } }
|
||||
|
||||
protected Sprite activeSprite { get { return m_OverrideSprite != null ? m_OverrideSprite : sprite; } }
|
||||
|
||||
// Not serialized until we support read-enabled sprites better.
|
||||
internal float m_EventAlphaThreshold = 1;
|
||||
public float eventAlphaThreshold { get { return m_EventAlphaThreshold; } set { m_EventAlphaThreshold = value; } }
|
||||
|
||||
[SerializeField]
|
||||
private ResolutionMode m_improveResolution;
|
||||
public ResolutionMode ImproveResolution { get { return m_improveResolution; } set { m_improveResolution = value; SetAllDirty(); } }
|
||||
|
||||
[SerializeField]
|
||||
protected float m_Resolution;
|
||||
public float Resolution { get { return m_Resolution; } set { m_Resolution = value; SetAllDirty(); } }
|
||||
|
||||
[SerializeField]
|
||||
private bool m_useNativeSize;
|
||||
public bool UseNativeSize { get { return m_useNativeSize; } set { m_useNativeSize = value; SetAllDirty(); } }
|
||||
|
||||
protected UIPrimitiveBase()
|
||||
{
|
||||
useLegacyMeshGeneration = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Default material used to draw everything if no explicit material was specified.
|
||||
/// </summary>
|
||||
|
||||
static public Material defaultETC1GraphicMaterial
|
||||
{
|
||||
get
|
||||
{
|
||||
if (s_ETC1DefaultUI == null)
|
||||
s_ETC1DefaultUI = Canvas.GetETC1SupportedCanvasMaterial();
|
||||
return s_ETC1DefaultUI;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Image's texture comes from the UnityEngine.Image.
|
||||
/// </summary>
|
||||
public override Texture mainTexture
|
||||
{
|
||||
get
|
||||
{
|
||||
if (activeSprite == null)
|
||||
{
|
||||
if (material != null && material.mainTexture != null)
|
||||
{
|
||||
return material.mainTexture;
|
||||
}
|
||||
return s_WhiteTexture;
|
||||
}
|
||||
|
||||
return activeSprite.texture;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether the Image has a border to work with.
|
||||
/// </summary>
|
||||
|
||||
public bool hasBorder
|
||||
{
|
||||
get
|
||||
{
|
||||
if (activeSprite != null)
|
||||
{
|
||||
Vector4 v = activeSprite.border;
|
||||
return v.sqrMagnitude > 0f;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public float pixelsPerUnit
|
||||
{
|
||||
get
|
||||
{
|
||||
float spritePixelsPerUnit = 100;
|
||||
if (activeSprite)
|
||||
spritePixelsPerUnit = activeSprite.pixelsPerUnit;
|
||||
|
||||
float referencePixelsPerUnit = 100;
|
||||
if (canvas)
|
||||
referencePixelsPerUnit = canvas.referencePixelsPerUnit;
|
||||
|
||||
return spritePixelsPerUnit / referencePixelsPerUnit;
|
||||
}
|
||||
}
|
||||
|
||||
public override Material material
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_Material != null)
|
||||
return m_Material;
|
||||
|
||||
if (activeSprite && activeSprite.associatedAlphaSplitTexture != null)
|
||||
return defaultETC1GraphicMaterial;
|
||||
|
||||
return defaultMaterial;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
base.material = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected UIVertex[] SetVbo(Vector2[] vertices, Vector2[] uvs)
|
||||
{
|
||||
UIVertex[] vbo = new UIVertex[4];
|
||||
for (int i = 0; i < vertices.Length; i++)
|
||||
{
|
||||
var vert = UIVertex.simpleVert;
|
||||
vert.color = color;
|
||||
vert.position = vertices[i];
|
||||
vert.uv0 = uvs[i];
|
||||
vbo[i] = vert;
|
||||
}
|
||||
return vbo;
|
||||
}
|
||||
|
||||
protected Vector2[] IncreaseResolution(Vector2[] input)
|
||||
{
|
||||
return IncreaseResolution(new List<Vector2>(input)).ToArray();
|
||||
}
|
||||
|
||||
protected List<Vector2> IncreaseResolution(List<Vector2> input)
|
||||
{
|
||||
outputList.Clear();
|
||||
|
||||
switch (ImproveResolution)
|
||||
{
|
||||
case ResolutionMode.PerLine:
|
||||
float totalDistance = 0, increments = 0;
|
||||
for (int i = 0; i < input.Count - 1; i++)
|
||||
{
|
||||
totalDistance += Vector2.Distance(input[i], input[i + 1]);
|
||||
}
|
||||
ResolutionToNativeSize(totalDistance);
|
||||
increments = totalDistance / m_Resolution;
|
||||
var incrementCount = 0;
|
||||
for (int i = 0; i < input.Count - 1; i++)
|
||||
{
|
||||
var p1 = input[i];
|
||||
outputList.Add(p1);
|
||||
var p2 = input[i + 1];
|
||||
var segmentDistance = Vector2.Distance(p1, p2) / increments;
|
||||
var incrementTime = 1f / segmentDistance;
|
||||
for (int j = 0; j < segmentDistance; j++)
|
||||
{
|
||||
outputList.Add(Vector2.Lerp(p1, (Vector2)p2, j * incrementTime));
|
||||
incrementCount++;
|
||||
}
|
||||
outputList.Add(p2);
|
||||
}
|
||||
break;
|
||||
case ResolutionMode.PerSegment:
|
||||
for (int i = 0; i < input.Count - 1; i++)
|
||||
{
|
||||
var p1 = input[i];
|
||||
outputList.Add(p1);
|
||||
var p2 = input[i + 1];
|
||||
ResolutionToNativeSize(Vector2.Distance(p1, p2));
|
||||
increments = 1f / m_Resolution;
|
||||
for (Single j = 1; j < m_Resolution; j++)
|
||||
{
|
||||
outputList.Add(Vector2.Lerp(p1, (Vector2)p2, increments * j));
|
||||
}
|
||||
outputList.Add(p2);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return outputList;
|
||||
}
|
||||
|
||||
protected virtual void GeneratedUVs() { }
|
||||
|
||||
protected virtual void ResolutionToNativeSize(float distance) { }
|
||||
|
||||
|
||||
#region ILayoutElement Interface
|
||||
|
||||
public virtual void CalculateLayoutInputHorizontal() { }
|
||||
public virtual void CalculateLayoutInputVertical() { }
|
||||
|
||||
public virtual float minWidth { get { return 0; } }
|
||||
|
||||
public virtual float preferredWidth
|
||||
{
|
||||
get
|
||||
{
|
||||
if (overrideSprite == null)
|
||||
return 0;
|
||||
return overrideSprite.rect.size.x / pixelsPerUnit;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual float flexibleWidth { get { return -1; } }
|
||||
|
||||
public virtual float minHeight { get { return 0; } }
|
||||
|
||||
public virtual float preferredHeight
|
||||
{
|
||||
get
|
||||
{
|
||||
if (overrideSprite == null)
|
||||
return 0;
|
||||
return overrideSprite.rect.size.y / pixelsPerUnit;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual float flexibleHeight { get { return -1; } }
|
||||
|
||||
public virtual int layoutPriority { get { return 0; } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region ICanvasRaycastFilter Interface
|
||||
public virtual bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
|
||||
{
|
||||
// add test for line check
|
||||
if (m_EventAlphaThreshold >= 1)
|
||||
return true;
|
||||
|
||||
Sprite sprite = overrideSprite;
|
||||
if (sprite == null)
|
||||
return true;
|
||||
|
||||
Vector2 local;
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPoint, eventCamera, out local);
|
||||
|
||||
Rect rect = GetPixelAdjustedRect();
|
||||
|
||||
// Convert to have lower left corner as reference point.
|
||||
local.x += rectTransform.pivot.x * rect.width;
|
||||
local.y += rectTransform.pivot.y * rect.height;
|
||||
|
||||
local = MapCoordinate(local, rect);
|
||||
|
||||
//test local coord with Mesh
|
||||
|
||||
// Normalize local coordinates.
|
||||
Rect spriteRect = sprite.textureRect;
|
||||
Vector2 normalized = new Vector2(local.x / spriteRect.width, local.y / spriteRect.height);
|
||||
|
||||
// Convert to texture space.
|
||||
float x = Mathf.Lerp(spriteRect.x, spriteRect.xMax, normalized.x) / sprite.texture.width;
|
||||
float y = Mathf.Lerp(spriteRect.y, spriteRect.yMax, normalized.y) / sprite.texture.height;
|
||||
|
||||
try
|
||||
{
|
||||
return sprite.texture.GetPixelBilinear(x, y).a >= m_EventAlphaThreshold;
|
||||
}
|
||||
catch (UnityException e)
|
||||
{
|
||||
Debug.LogError("Using clickAlphaThreshold lower than 1 on Image whose sprite texture cannot be read. " + e.Message + " Also make sure to disable sprite packing for this sprite.", this);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return image adjusted position
|
||||
/// **Copied from Unity's Image component for now and simplified for UI Extensions primitives
|
||||
/// </summary>
|
||||
/// <param name="local"></param>
|
||||
/// <param name="rect"></param>
|
||||
/// <returns></returns>
|
||||
private Vector2 MapCoordinate(Vector2 local, Rect rect)
|
||||
{
|
||||
Rect spriteRect = sprite.rect;
|
||||
//if (type == Type.Simple || type == Type.Filled)
|
||||
return new Vector2(local.x * rect.width, local.y * rect.height);
|
||||
|
||||
//Vector4 border = sprite.border;
|
||||
//Vector4 adjustedBorder = GetAdjustedBorders(border / pixelsPerUnit, rect);
|
||||
|
||||
//for (int i = 0; i < 2; i++)
|
||||
//{
|
||||
// if (local[i] <= adjustedBorder[i])
|
||||
// continue;
|
||||
|
||||
// if (rect.size[i] - local[i] <= adjustedBorder[i + 2])
|
||||
// {
|
||||
// local[i] -= (rect.size[i] - spriteRect.size[i]);
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// if (type == Type.Sliced)
|
||||
// {
|
||||
// float lerp = Mathf.InverseLerp(adjustedBorder[i], rect.size[i] - adjustedBorder[i + 2], local[i]);
|
||||
// local[i] = Mathf.Lerp(border[i], spriteRect.size[i] - border[i + 2], lerp);
|
||||
// continue;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// local[i] -= adjustedBorder[i];
|
||||
// local[i] = Mathf.Repeat(local[i], spriteRect.size[i] - border[i] - border[i + 2]);
|
||||
// local[i] += border[i];
|
||||
// continue;
|
||||
// }
|
||||
//}
|
||||
|
||||
//return local;
|
||||
}
|
||||
|
||||
Vector4 GetAdjustedBorders(Vector4 border, Rect rect)
|
||||
{
|
||||
for (int axis = 0; axis <= 1; axis++)
|
||||
{
|
||||
// If the rect is smaller than the combined borders, then there's not room for the borders at their normal size.
|
||||
// In order to avoid artefact's with overlapping borders, we scale the borders down to fit.
|
||||
float combinedBorders = border[axis] + border[axis + 2];
|
||||
if (rect.size[axis] < combinedBorders && combinedBorders != 0)
|
||||
{
|
||||
float borderScaleRatio = rect.size[axis] / combinedBorders;
|
||||
border[axis] *= borderScaleRatio;
|
||||
border[axis + 2] *= borderScaleRatio;
|
||||
}
|
||||
}
|
||||
return border;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region onEnable
|
||||
protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
SetAllDirty();
|
||||
}
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 65af11d29c6cb8c45b3d78d97d98440d
|
||||
timeCreated: 1464131942
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8ed31929d883df147b53ffeddcb4f25c
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
userData:
|
||||
|
|
@ -0,0 +1,350 @@
|
|||
/**
|
||||
|
||||
This class demonstrates the code discussed in these two articles:
|
||||
|
||||
http://devmag.org.za/2011/04/05/bzier-curves-a-tutorial/
|
||||
http://devmag.org.za/2011/06/23/bzier-path-algorithms/
|
||||
|
||||
Use this code as you wish, at your own risk. If it blows up
|
||||
your computer, makes a plane crash, or otherwise cause damage,
|
||||
injury, or death, it is not my fault.
|
||||
|
||||
@author Herman Tulleken, dev.mag.org.za
|
||||
|
||||
*/
|
||||
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
/**
|
||||
Class for representing a Bezier path, and methods for getting suitable points to
|
||||
draw the curve with line segments.
|
||||
*/
|
||||
public class BezierPath
|
||||
{
|
||||
public int SegmentsPerCurve = 10;
|
||||
public float MINIMUM_SQR_DISTANCE = 0.01f;
|
||||
|
||||
// This corresponds to about 172 degrees, 8 degrees from a straight line
|
||||
public float DIVISION_THRESHOLD = -0.99f;
|
||||
|
||||
private List<Vector2> controlPoints;
|
||||
|
||||
private int curveCount; //how many bezier curves in this path?
|
||||
|
||||
/**
|
||||
Constructs a new empty Bezier curve. Use one of these methods
|
||||
to add points: SetControlPoints, Interpolate, SamplePoints.
|
||||
*/
|
||||
public BezierPath()
|
||||
{
|
||||
controlPoints = new List<Vector2>();
|
||||
}
|
||||
|
||||
/**
|
||||
Sets the control points of this Bezier path.
|
||||
Points 0-3 forms the first Bezier curve, points
|
||||
3-6 forms the second curve, etc.
|
||||
*/
|
||||
public void SetControlPoints(List<Vector2> newControlPoints)
|
||||
{
|
||||
controlPoints.Clear();
|
||||
controlPoints.AddRange(newControlPoints);
|
||||
curveCount = (controlPoints.Count - 1) / 3;
|
||||
}
|
||||
|
||||
public void SetControlPoints(Vector2[] newControlPoints)
|
||||
{
|
||||
controlPoints.Clear();
|
||||
controlPoints.AddRange(newControlPoints);
|
||||
curveCount = (controlPoints.Count - 1) / 3;
|
||||
}
|
||||
|
||||
/**
|
||||
Returns the control points for this Bezier curve.
|
||||
*/
|
||||
public List<Vector2> GetControlPoints()
|
||||
{
|
||||
return controlPoints;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Calculates a Bezier interpolated path for the given points.
|
||||
*/
|
||||
public void Interpolate(List<Vector2> segmentPoints, float scale)
|
||||
{
|
||||
controlPoints.Clear();
|
||||
|
||||
if (segmentPoints.Count < 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < segmentPoints.Count; i++)
|
||||
{
|
||||
if (i == 0) // is first
|
||||
{
|
||||
Vector2 p1 = segmentPoints[i];
|
||||
Vector2 p2 = segmentPoints[i + 1];
|
||||
|
||||
Vector2 tangent = (p2 - p1);
|
||||
Vector2 q1 = p1 + scale * tangent;
|
||||
|
||||
controlPoints.Add(p1);
|
||||
controlPoints.Add(q1);
|
||||
}
|
||||
else if (i == segmentPoints.Count - 1) //last
|
||||
{
|
||||
Vector2 p0 = segmentPoints[i - 1];
|
||||
Vector2 p1 = segmentPoints[i];
|
||||
Vector2 tangent = (p1 - p0);
|
||||
Vector2 q0 = p1 - scale * tangent;
|
||||
|
||||
controlPoints.Add(q0);
|
||||
controlPoints.Add(p1);
|
||||
}
|
||||
else
|
||||
{
|
||||
Vector2 p0 = segmentPoints[i - 1];
|
||||
Vector2 p1 = segmentPoints[i];
|
||||
Vector2 p2 = segmentPoints[i + 1];
|
||||
Vector2 tangent = (p2 - p0).normalized;
|
||||
Vector2 q0 = p1 - scale * tangent * (p1 - p0).magnitude;
|
||||
Vector2 q1 = p1 + scale * tangent * (p2 - p1).magnitude;
|
||||
|
||||
controlPoints.Add(q0);
|
||||
controlPoints.Add(p1);
|
||||
controlPoints.Add(q1);
|
||||
}
|
||||
}
|
||||
|
||||
curveCount = (controlPoints.Count - 1) / 3;
|
||||
}
|
||||
|
||||
/**
|
||||
Sample the given points as a Bezier path.
|
||||
*/
|
||||
public void SamplePoints(List<Vector2> sourcePoints, float minSqrDistance, float maxSqrDistance, float scale)
|
||||
{
|
||||
if (sourcePoints.Count < 2)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Stack<Vector2> samplePoints = new Stack<Vector2>();
|
||||
|
||||
samplePoints.Push(sourcePoints[0]);
|
||||
|
||||
Vector2 potentialSamplePoint = sourcePoints[1];
|
||||
|
||||
int i = 2;
|
||||
|
||||
for (i = 2; i < sourcePoints.Count; i++)
|
||||
{
|
||||
if (
|
||||
((potentialSamplePoint - sourcePoints[i]).sqrMagnitude > minSqrDistance) &&
|
||||
((samplePoints.Peek() - sourcePoints[i]).sqrMagnitude > maxSqrDistance))
|
||||
{
|
||||
samplePoints.Push(potentialSamplePoint);
|
||||
}
|
||||
|
||||
potentialSamplePoint = sourcePoints[i];
|
||||
}
|
||||
|
||||
//now handle last bit of curve
|
||||
Vector2 p1 = samplePoints.Pop(); //last sample point
|
||||
Vector2 p0 = samplePoints.Peek(); //second last sample point
|
||||
Vector2 tangent = (p0 - potentialSamplePoint).normalized;
|
||||
float d2 = (potentialSamplePoint - p1).magnitude;
|
||||
float d1 = (p1 - p0).magnitude;
|
||||
p1 = p1 + tangent * ((d1 - d2) / 2);
|
||||
|
||||
samplePoints.Push(p1);
|
||||
samplePoints.Push(potentialSamplePoint);
|
||||
|
||||
|
||||
Interpolate(new List<Vector2>(samplePoints), scale);
|
||||
}
|
||||
|
||||
/**
|
||||
Calculates a point on the path.
|
||||
|
||||
@param curveIndex The index of the curve that the point is on. For example,
|
||||
the second curve (index 1) is the curve with control points 3, 4, 5, and 6.
|
||||
|
||||
@param t The parameter indicating where on the curve the point is. 0 corresponds
|
||||
to the "left" point, 1 corresponds to the "right" end point.
|
||||
*/
|
||||
public Vector2 CalculateBezierPoint(int curveIndex, float t)
|
||||
{
|
||||
int nodeIndex = curveIndex * 3;
|
||||
|
||||
Vector2 p0 = controlPoints[nodeIndex];
|
||||
Vector2 p1 = controlPoints[nodeIndex + 1];
|
||||
Vector2 p2 = controlPoints[nodeIndex + 2];
|
||||
Vector2 p3 = controlPoints[nodeIndex + 3];
|
||||
|
||||
return CalculateBezierPoint(t, p0, p1, p2, p3);
|
||||
}
|
||||
|
||||
/**
|
||||
Gets the drawing points. This implementation simply calculates a certain number
|
||||
of points per curve.
|
||||
*/
|
||||
public List<Vector2> GetDrawingPoints0()
|
||||
{
|
||||
List<Vector2> drawingPoints = new List<Vector2>();
|
||||
|
||||
for (int curveIndex = 0; curveIndex < curveCount; curveIndex++)
|
||||
{
|
||||
if (curveIndex == 0) //Only do this for the first end point.
|
||||
//When i != 0, this coincides with the
|
||||
//end point of the previous segment,
|
||||
{
|
||||
drawingPoints.Add(CalculateBezierPoint(curveIndex, 0));
|
||||
}
|
||||
|
||||
for (int j = 1; j <= SegmentsPerCurve; j++)
|
||||
{
|
||||
float t = j / (float)SegmentsPerCurve;
|
||||
drawingPoints.Add(CalculateBezierPoint(curveIndex, t));
|
||||
}
|
||||
}
|
||||
|
||||
return drawingPoints;
|
||||
}
|
||||
|
||||
/**
|
||||
Gets the drawing points. This implementation simply calculates a certain number
|
||||
of points per curve.
|
||||
|
||||
This is a slightly different implementation from the one above.
|
||||
*/
|
||||
public List<Vector2> GetDrawingPoints1()
|
||||
{
|
||||
List<Vector2> drawingPoints = new List<Vector2>();
|
||||
|
||||
for (int i = 0; i < controlPoints.Count - 3; i += 3)
|
||||
{
|
||||
Vector2 p0 = controlPoints[i];
|
||||
Vector2 p1 = controlPoints[i + 1];
|
||||
Vector2 p2 = controlPoints[i + 2];
|
||||
Vector2 p3 = controlPoints[i + 3];
|
||||
|
||||
if (i == 0) //only do this for the first end point. When i != 0, this coincides with the end point of the previous segment,
|
||||
{
|
||||
drawingPoints.Add(CalculateBezierPoint(0, p0, p1, p2, p3));
|
||||
}
|
||||
|
||||
for (int j = 1; j <= SegmentsPerCurve; j++)
|
||||
{
|
||||
float t = j / (float)SegmentsPerCurve;
|
||||
drawingPoints.Add(CalculateBezierPoint(t, p0, p1, p2, p3));
|
||||
}
|
||||
}
|
||||
|
||||
return drawingPoints;
|
||||
}
|
||||
|
||||
/**
|
||||
This gets the drawing points of a bezier curve, using recursive division,
|
||||
which results in less points for the same accuracy as the above implementation.
|
||||
*/
|
||||
public List<Vector2> GetDrawingPoints2()
|
||||
{
|
||||
List<Vector2> drawingPoints = new List<Vector2>();
|
||||
|
||||
for (int curveIndex = 0; curveIndex < curveCount; curveIndex++)
|
||||
{
|
||||
List<Vector2> bezierCurveDrawingPoints = FindDrawingPoints(curveIndex);
|
||||
|
||||
if (curveIndex != 0)
|
||||
{
|
||||
//remove the fist point, as it coincides with the last point of the previous Bezier curve.
|
||||
bezierCurveDrawingPoints.RemoveAt(0);
|
||||
}
|
||||
|
||||
drawingPoints.AddRange(bezierCurveDrawingPoints);
|
||||
}
|
||||
|
||||
return drawingPoints;
|
||||
}
|
||||
|
||||
List<Vector2> FindDrawingPoints(int curveIndex)
|
||||
{
|
||||
List<Vector2> pointList = new List<Vector2>();
|
||||
|
||||
Vector2 left = CalculateBezierPoint(curveIndex, 0);
|
||||
Vector2 right = CalculateBezierPoint(curveIndex, 1);
|
||||
|
||||
pointList.Add(left);
|
||||
pointList.Add(right);
|
||||
|
||||
FindDrawingPoints(curveIndex, 0, 1, pointList, 1);
|
||||
|
||||
return pointList;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@returns the number of points added.
|
||||
*/
|
||||
int FindDrawingPoints(int curveIndex, float t0, float t1,
|
||||
List<Vector2> pointList, int insertionIndex)
|
||||
{
|
||||
Vector2 left = CalculateBezierPoint(curveIndex, t0);
|
||||
Vector2 right = CalculateBezierPoint(curveIndex, t1);
|
||||
|
||||
if ((left - right).sqrMagnitude < MINIMUM_SQR_DISTANCE)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
float tMid = (t0 + t1) / 2;
|
||||
Vector2 mid = CalculateBezierPoint(curveIndex, tMid);
|
||||
|
||||
Vector2 leftDirection = (left - mid).normalized;
|
||||
Vector2 rightDirection = (right - mid).normalized;
|
||||
|
||||
if (Vector2.Dot(leftDirection, rightDirection) > DIVISION_THRESHOLD || Mathf.Abs(tMid - 0.5f) < 0.0001f)
|
||||
{
|
||||
int pointsAddedCount = 0;
|
||||
|
||||
pointsAddedCount += FindDrawingPoints(curveIndex, t0, tMid, pointList, insertionIndex);
|
||||
pointList.Insert(insertionIndex + pointsAddedCount, mid);
|
||||
pointsAddedCount++;
|
||||
pointsAddedCount += FindDrawingPoints(curveIndex, tMid, t1, pointList, insertionIndex + pointsAddedCount);
|
||||
|
||||
return pointsAddedCount;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
Calculates a point on the Bezier curve represented with the four control points given.
|
||||
*/
|
||||
private Vector2 CalculateBezierPoint(float t, Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3)
|
||||
{
|
||||
float u = 1 - t;
|
||||
float tt = t * t;
|
||||
float uu = u * u;
|
||||
float uuu = uu * u;
|
||||
float ttt = tt * t;
|
||||
|
||||
Vector2 p = uuu * p0; //first term
|
||||
|
||||
p += 3 * uu * t * p1; //second term
|
||||
p += 3 * u * tt * p2; //third term
|
||||
p += ttt * p3; //fourth term
|
||||
|
||||
return p;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 89b61e0416e1256469165fca78d5ed21
|
||||
timeCreated: 1463598733
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,233 @@
|
|||
/// Credit Farfarer
|
||||
/// Sourced from - https://gist.github.com/Farfarer/a765cd07920d48a8713a0c1924db6d70
|
||||
/// Updated for UI / 2D - SimonDarksideJ
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
[System.Serializable]
|
||||
public class CableCurve
|
||||
{
|
||||
[SerializeField]
|
||||
Vector2 m_start;
|
||||
[SerializeField]
|
||||
Vector2 m_end;
|
||||
[SerializeField]
|
||||
float m_slack;
|
||||
[SerializeField]
|
||||
int m_steps;
|
||||
[SerializeField]
|
||||
bool m_regen;
|
||||
|
||||
static Vector2[] emptyCurve = new Vector2[] { new Vector2(0.0f, 0.0f), new Vector2(0.0f, 0.0f) };
|
||||
[SerializeField]
|
||||
Vector2[] points;
|
||||
|
||||
public bool regenPoints
|
||||
{
|
||||
get { return m_regen; }
|
||||
set
|
||||
{
|
||||
m_regen = value;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 start
|
||||
{
|
||||
get { return m_start; }
|
||||
set
|
||||
{
|
||||
if (value != m_start)
|
||||
m_regen = true;
|
||||
m_start = value;
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 end
|
||||
{
|
||||
get { return m_end; }
|
||||
set
|
||||
{
|
||||
if (value != m_end)
|
||||
m_regen = true;
|
||||
m_end = value;
|
||||
}
|
||||
}
|
||||
public float slack
|
||||
{
|
||||
get { return m_slack; }
|
||||
set
|
||||
{
|
||||
if (value != m_slack)
|
||||
m_regen = true;
|
||||
m_slack = Mathf.Max(0.0f, value);
|
||||
}
|
||||
}
|
||||
public int steps
|
||||
{
|
||||
get { return m_steps; }
|
||||
set
|
||||
{
|
||||
if (value != m_steps)
|
||||
m_regen = true;
|
||||
m_steps = Mathf.Max(2, value);
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 midPoint
|
||||
{
|
||||
get
|
||||
{
|
||||
Vector2 mid = Vector2.zero;
|
||||
if (m_steps == 2)
|
||||
{
|
||||
return (points[0] + points[1]) * 0.5f;
|
||||
}
|
||||
else if (m_steps > 2)
|
||||
{
|
||||
int m = m_steps / 2;
|
||||
if ((m_steps % 2) == 0)
|
||||
{
|
||||
mid = (points[m] + points[m + 1]) * 0.5f;
|
||||
}
|
||||
else
|
||||
{
|
||||
mid = points[m];
|
||||
}
|
||||
}
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
|
||||
public CableCurve()
|
||||
{
|
||||
points = emptyCurve;
|
||||
m_start = Vector2.up;
|
||||
m_end = Vector2.up + Vector2.right;
|
||||
m_slack = 0.5f;
|
||||
m_steps = 20;
|
||||
m_regen = true;
|
||||
}
|
||||
|
||||
public CableCurve(Vector2[] inputPoints)
|
||||
{
|
||||
points = inputPoints;
|
||||
m_start = inputPoints[0];
|
||||
m_end = inputPoints[1];
|
||||
m_slack = 0.5f;
|
||||
m_steps = 20;
|
||||
m_regen = true;
|
||||
}
|
||||
|
||||
public CableCurve(List<Vector2> inputPoints)
|
||||
{
|
||||
points = inputPoints.ToArray();
|
||||
m_start = inputPoints[0];
|
||||
m_end = inputPoints[1];
|
||||
m_slack = 0.5f;
|
||||
m_steps = 20;
|
||||
m_regen = true;
|
||||
}
|
||||
|
||||
public CableCurve(CableCurve v)
|
||||
{
|
||||
points = v.Points();
|
||||
m_start = v.start;
|
||||
m_end = v.end;
|
||||
m_slack = v.slack;
|
||||
m_steps = v.steps;
|
||||
m_regen = v.regenPoints;
|
||||
}
|
||||
|
||||
public Vector2[] Points()
|
||||
{
|
||||
if (!m_regen)
|
||||
return points;
|
||||
|
||||
if (m_steps < 2)
|
||||
return emptyCurve;
|
||||
|
||||
float lineDist = Vector2.Distance(m_end, m_start);
|
||||
float lineDistH = Vector2.Distance(new Vector2(m_end.x, m_start.y), m_start);
|
||||
float l = lineDist + Mathf.Max(0.0001f, m_slack);
|
||||
float r = 0.0f;
|
||||
float s = m_start.y;
|
||||
float u = lineDistH;
|
||||
float v = end.y;
|
||||
|
||||
if ((u - r) == 0.0f)
|
||||
return emptyCurve;
|
||||
|
||||
float ztarget = Mathf.Sqrt(Mathf.Pow(l, 2.0f) - Mathf.Pow(v - s, 2.0f)) / (u - r);
|
||||
|
||||
int loops = 30;
|
||||
int iterationCount = 0;
|
||||
int maxIterations = loops * 10; // For safety.
|
||||
bool found = false;
|
||||
|
||||
float z = 0.0f;
|
||||
float ztest = 0.0f;
|
||||
float zstep = 100.0f;
|
||||
float ztesttarget = 0.0f;
|
||||
for (int i = 0; i < loops; i++)
|
||||
{
|
||||
for (int j = 0; j < 10; j++)
|
||||
{
|
||||
iterationCount++;
|
||||
ztest = z + zstep;
|
||||
ztesttarget = (float)Math.Sinh(ztest) / ztest;
|
||||
|
||||
if (float.IsInfinity(ztesttarget))
|
||||
continue;
|
||||
|
||||
if (ztesttarget == ztarget)
|
||||
{
|
||||
found = true;
|
||||
z = ztest;
|
||||
break;
|
||||
}
|
||||
else if (ztesttarget > ztarget)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
z = ztest;
|
||||
}
|
||||
|
||||
if (iterationCount > maxIterations)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found)
|
||||
break;
|
||||
|
||||
zstep *= 0.1f;
|
||||
}
|
||||
|
||||
float a = (u - r) / 2.0f / z;
|
||||
float p = (r + u - a * Mathf.Log((l + v - s) / (l - v + s))) / 2.0f;
|
||||
float q = (v + s - l * (float)Math.Cosh(z) / (float)Math.Sinh(z)) / 2.0f;
|
||||
|
||||
points = new Vector2[m_steps];
|
||||
float stepsf = m_steps - 1;
|
||||
float stepf;
|
||||
for (int i = 0; i < m_steps; i++)
|
||||
{
|
||||
stepf = i / stepsf;
|
||||
Vector2 pos = Vector2.zero;
|
||||
pos.x = Mathf.Lerp(start.x, end.x, stepf);
|
||||
pos.y = a * (float)Math.Cosh(((stepf * lineDistH) - p) / a) + q;
|
||||
points[i] = pos;
|
||||
}
|
||||
|
||||
m_regen = false;
|
||||
return points;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2444f085ab49ff24b888b175f46b55c7
|
||||
timeCreated: 1501871616
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
/// <summary>
|
||||
/// Tool script taken from the UI source as it's set to Internal for some reason. So to use in the extensions project it is needed here also.
|
||||
/// </summary>
|
||||
///
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
internal static class SetPropertyUtility
|
||||
{
|
||||
public static bool SetColor(ref Color currentValue, Color newValue)
|
||||
{
|
||||
if (currentValue.r == newValue.r && currentValue.g == newValue.g && currentValue.b == newValue.b && currentValue.a == newValue.a)
|
||||
return false;
|
||||
|
||||
currentValue = newValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool SetStruct<T>(ref T currentValue, T newValue) where T: struct
|
||||
{
|
||||
if (currentValue.Equals(newValue))
|
||||
return false;
|
||||
|
||||
currentValue = newValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool SetClass<T>(ref T currentValue, T newValue) where T: class
|
||||
{
|
||||
if ((currentValue == null && newValue == null) || (currentValue != null && currentValue.Equals(newValue)))
|
||||
return false;
|
||||
|
||||
currentValue = newValue;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d373cce4b1f361a40a126bd74d26ebc9
|
||||
timeCreated: 1440851864
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Reference in a new issue