修改调整
This commit is contained in:
75
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/CompositeMesh.cs
Normal file
75
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/CompositeMesh.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FairyGUI
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class CompositeMesh : IMeshFactory, IHitTest
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public readonly List<IMeshFactory> elements;
|
||||
|
||||
/// <summary>
|
||||
/// If it is -1, means all elements are active, otherwise, only the specific element is active
|
||||
/// </summary>
|
||||
public int activeIndex;
|
||||
|
||||
public CompositeMesh()
|
||||
{
|
||||
elements = new List<IMeshFactory>();
|
||||
activeIndex = -1;
|
||||
}
|
||||
|
||||
public void OnPopulateMesh(VertexBuffer vb)
|
||||
{
|
||||
int cnt = elements.Count;
|
||||
if (cnt == 1)
|
||||
elements[0].OnPopulateMesh(vb);
|
||||
else
|
||||
{
|
||||
VertexBuffer vb2 = VertexBuffer.Begin(vb);
|
||||
|
||||
for (int i = 0; i < cnt; i++)
|
||||
{
|
||||
if (activeIndex == -1 || i == activeIndex)
|
||||
{
|
||||
vb2.Clear();
|
||||
elements[i].OnPopulateMesh(vb2);
|
||||
vb.Append(vb2);
|
||||
}
|
||||
}
|
||||
|
||||
vb2.End();
|
||||
}
|
||||
}
|
||||
|
||||
public bool HitTest(Rect contentRect, Vector2 point)
|
||||
{
|
||||
if (!contentRect.Contains(point))
|
||||
return false;
|
||||
|
||||
bool flag = false;
|
||||
int cnt = elements.Count;
|
||||
for (int i = 0; i < cnt; i++)
|
||||
{
|
||||
if (activeIndex == -1 || i == activeIndex)
|
||||
{
|
||||
IHitTest ht = elements[i] as IHitTest;
|
||||
if (ht != null)
|
||||
{
|
||||
if (ht.HitTest(contentRect, point))
|
||||
return true;
|
||||
}
|
||||
else
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba3d921c1f6e95f4e9105f45fd67bda5
|
||||
timeCreated: 1546159121
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
200
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/EllipseMesh.cs
Normal file
200
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/EllipseMesh.cs
Normal file
@@ -0,0 +1,200 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace FairyGUI
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class EllipseMesh : IMeshFactory, IHitTest
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Rect? drawRect;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float lineWidth;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32 lineColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32? centerColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32? fillColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float startDegree;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float endDegreee;
|
||||
|
||||
static int[] SECTOR_CENTER_TRIANGLES = new int[] {
|
||||
0, 4, 1,
|
||||
0, 3, 4,
|
||||
0, 2, 3,
|
||||
0, 8, 5,
|
||||
0, 7, 8,
|
||||
0, 6, 7,
|
||||
6, 5, 2,
|
||||
2, 1, 6
|
||||
};
|
||||
|
||||
public EllipseMesh()
|
||||
{
|
||||
lineColor = Color.black;
|
||||
startDegree = 0;
|
||||
endDegreee = 360;
|
||||
}
|
||||
|
||||
public void OnPopulateMesh(VertexBuffer vb)
|
||||
{
|
||||
Rect rect = drawRect != null ? (Rect)drawRect : vb.contentRect;
|
||||
Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;
|
||||
|
||||
float sectionStart = Mathf.Clamp(startDegree, 0, 360);
|
||||
float sectionEnd = Mathf.Clamp(endDegreee, 0, 360);
|
||||
bool clipped = sectionStart > 0 || sectionEnd < 360;
|
||||
sectionStart = sectionStart * Mathf.Deg2Rad;
|
||||
sectionEnd = sectionEnd * Mathf.Deg2Rad;
|
||||
Color32 centerColor2 = centerColor == null ? color : (Color32)centerColor;
|
||||
|
||||
float radiusX = rect.width / 2;
|
||||
float radiusY = rect.height / 2;
|
||||
int sides = Mathf.CeilToInt(Mathf.PI * (radiusX + radiusY) / 4);
|
||||
sides = Mathf.Clamp(sides, 40, 800);
|
||||
float angleDelta = 2 * Mathf.PI / sides;
|
||||
float angle = 0;
|
||||
float lineAngle = 0;
|
||||
|
||||
if (lineWidth > 0 && clipped)
|
||||
{
|
||||
lineAngle = lineWidth / Mathf.Max(radiusX, radiusY);
|
||||
sectionStart += lineAngle;
|
||||
sectionEnd -= lineAngle;
|
||||
}
|
||||
|
||||
int vpos = vb.currentVertCount;
|
||||
float centerX = rect.x + radiusX;
|
||||
float centerY = rect.y + radiusY;
|
||||
vb.AddVert(new Vector3(centerX, centerY, 0), centerColor2);
|
||||
for (int i = 0; i < sides; i++)
|
||||
{
|
||||
if (angle < sectionStart)
|
||||
angle = sectionStart;
|
||||
else if (angle > sectionEnd)
|
||||
angle = sectionEnd;
|
||||
Vector3 vec = new Vector3(Mathf.Cos(angle) * (radiusX - lineWidth) + centerX, Mathf.Sin(angle) * (radiusY - lineWidth) + centerY, 0);
|
||||
vb.AddVert(vec, color);
|
||||
if (lineWidth > 0)
|
||||
{
|
||||
vb.AddVert(vec, lineColor);
|
||||
vb.AddVert(new Vector3(Mathf.Cos(angle) * radiusX + centerX, Mathf.Sin(angle) * radiusY + centerY, 0), lineColor);
|
||||
}
|
||||
angle += angleDelta;
|
||||
}
|
||||
|
||||
if (lineWidth > 0)
|
||||
{
|
||||
int cnt = sides * 3;
|
||||
for (int i = 0; i < cnt; i += 3)
|
||||
{
|
||||
if (i != cnt - 3)
|
||||
{
|
||||
vb.AddTriangle(0, i + 1, i + 4);
|
||||
vb.AddTriangle(i + 5, i + 2, i + 3);
|
||||
vb.AddTriangle(i + 3, i + 6, i + 5);
|
||||
}
|
||||
else if (!clipped)
|
||||
{
|
||||
vb.AddTriangle(0, i + 1, 1);
|
||||
vb.AddTriangle(2, i + 2, i + 3);
|
||||
vb.AddTriangle(i + 3, 3, 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
vb.AddTriangle(0, i + 1, i + 1);
|
||||
vb.AddTriangle(i + 2, i + 2, i + 3);
|
||||
vb.AddTriangle(i + 3, i + 3, i + 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < sides; i++)
|
||||
{
|
||||
if (i != sides - 1)
|
||||
vb.AddTriangle(0, i + 1, i + 2);
|
||||
else if (!clipped)
|
||||
vb.AddTriangle(0, i + 1, 1);
|
||||
else
|
||||
vb.AddTriangle(0, i + 1, i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (lineWidth > 0 && clipped)
|
||||
{
|
||||
//扇形内边缘的线条
|
||||
|
||||
vb.AddVert(new Vector3(radiusX, radiusY, 0), lineColor);
|
||||
float centerRadius = lineWidth * 0.5f;
|
||||
|
||||
sectionStart -= lineAngle;
|
||||
angle = sectionStart + lineAngle * 0.5f + Mathf.PI * 0.5f;
|
||||
vb.AddVert(new Vector3(Mathf.Cos(angle) * centerRadius + radiusX, Mathf.Sin(angle) * centerRadius + radiusY, 0), lineColor);
|
||||
angle -= Mathf.PI;
|
||||
vb.AddVert(new Vector3(Mathf.Cos(angle) * centerRadius + radiusX, Mathf.Sin(angle) * centerRadius + radiusY, 0), lineColor);
|
||||
vb.AddVert(new Vector3(Mathf.Cos(sectionStart) * radiusX + radiusX, Mathf.Sin(sectionStart) * radiusY + radiusY, 0), lineColor);
|
||||
vb.AddVert(vb.GetPosition(vpos + 3), lineColor);
|
||||
|
||||
sectionEnd += lineAngle;
|
||||
angle = sectionEnd - lineAngle * 0.5f + Mathf.PI * 0.5f;
|
||||
vb.AddVert(new Vector3(Mathf.Cos(angle) * centerRadius + radiusX, Mathf.Sin(angle) * centerRadius + radiusY, 0), lineColor);
|
||||
angle -= Mathf.PI;
|
||||
vb.AddVert(new Vector3(Mathf.Cos(angle) * centerRadius + radiusX, Mathf.Sin(angle) * centerRadius + radiusY, 0), lineColor);
|
||||
vb.AddVert(vb.GetPosition(vpos + sides * 3), lineColor);
|
||||
vb.AddVert(new Vector3(Mathf.Cos(sectionEnd) * radiusX + radiusX, Mathf.Sin(sectionEnd) * radiusY + radiusY, 0), lineColor);
|
||||
|
||||
vb.AddTriangles(SECTOR_CENTER_TRIANGLES, sides * 3 + 1);
|
||||
}
|
||||
}
|
||||
|
||||
public bool HitTest(Rect contentRect, Vector2 point)
|
||||
{
|
||||
if (!contentRect.Contains(point))
|
||||
return false;
|
||||
|
||||
float radiusX = contentRect.width * 0.5f;
|
||||
float raduisY = contentRect.height * 0.5f;
|
||||
float xx = point.x - radiusX - contentRect.x;
|
||||
float yy = point.y - raduisY - contentRect.y;
|
||||
if (Mathf.Pow(xx / radiusX, 2) + Mathf.Pow(yy / raduisY, 2) < 1)
|
||||
{
|
||||
if (startDegree != 0 || endDegreee != 360)
|
||||
{
|
||||
float deg = Mathf.Atan2(yy, xx) * Mathf.Rad2Deg;
|
||||
if (deg < 0)
|
||||
deg += 360;
|
||||
return deg >= startDegree && deg <= endDegreee;
|
||||
}
|
||||
else
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7ea96854995120948847301fb48d1675
|
||||
timeCreated: 1545987173
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
400
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/FillMesh.cs
Normal file
400
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/FillMesh.cs
Normal file
@@ -0,0 +1,400 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace FairyGUI
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class FillMesh : IMeshFactory
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public FillMethod method;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public int origin;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float amount;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public bool clockwise;
|
||||
|
||||
public FillMesh()
|
||||
{
|
||||
clockwise = true;
|
||||
amount = 1;
|
||||
}
|
||||
|
||||
public void OnPopulateMesh(VertexBuffer vb)
|
||||
{
|
||||
float amount = Mathf.Clamp01(this.amount);
|
||||
switch (method)
|
||||
{
|
||||
case FillMethod.Horizontal:
|
||||
FillHorizontal(vb, vb.contentRect, origin, amount);
|
||||
break;
|
||||
|
||||
case FillMethod.Vertical:
|
||||
FillVertical(vb, vb.contentRect, origin, amount);
|
||||
break;
|
||||
|
||||
case FillMethod.Radial90:
|
||||
FillRadial90(vb, vb.contentRect, (Origin90)origin, amount, clockwise);
|
||||
break;
|
||||
|
||||
case FillMethod.Radial180:
|
||||
FillRadial180(vb, vb.contentRect, (Origin180)origin, amount, clockwise);
|
||||
break;
|
||||
|
||||
case FillMethod.Radial360:
|
||||
FillRadial360(vb, vb.contentRect, (Origin360)origin, amount, clockwise);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void FillHorizontal(VertexBuffer vb, Rect vertRect, int origin, float amount)
|
||||
{
|
||||
float a = vertRect.width * amount;
|
||||
if ((OriginHorizontal)origin == OriginHorizontal.Right || (OriginVertical)origin == OriginVertical.Bottom)
|
||||
vertRect.x += (vertRect.width - a);
|
||||
vertRect.width = a;
|
||||
|
||||
vb.AddQuad(vertRect);
|
||||
vb.AddTriangles();
|
||||
}
|
||||
|
||||
static void FillVertical(VertexBuffer vb, Rect vertRect, int origin, float amount)
|
||||
{
|
||||
float a = vertRect.height * amount;
|
||||
if ((OriginHorizontal)origin == OriginHorizontal.Right || (OriginVertical)origin == OriginVertical.Bottom)
|
||||
vertRect.y += (vertRect.height - a);
|
||||
vertRect.height = a;
|
||||
|
||||
vb.AddQuad(vertRect);
|
||||
vb.AddTriangles();
|
||||
}
|
||||
|
||||
//4 vertex
|
||||
static void FillRadial90(VertexBuffer vb, Rect vertRect, Origin90 origin, float amount, bool clockwise)
|
||||
{
|
||||
bool flipX = origin == Origin90.TopRight || origin == Origin90.BottomRight;
|
||||
bool flipY = origin == Origin90.BottomLeft || origin == Origin90.BottomRight;
|
||||
if (flipX != flipY)
|
||||
clockwise = !clockwise;
|
||||
|
||||
float ratio = clockwise ? amount : (1 - amount);
|
||||
float tan = Mathf.Tan(Mathf.PI * 0.5f * ratio);
|
||||
bool thresold = false;
|
||||
if (ratio != 1)
|
||||
thresold = (vertRect.height / vertRect.width - tan) > 0;
|
||||
if (!clockwise)
|
||||
thresold = !thresold;
|
||||
float x = vertRect.x + (ratio == 0 ? float.MaxValue : (vertRect.height / tan));
|
||||
float y = vertRect.y + (ratio == 1 ? float.MaxValue : (vertRect.width * tan));
|
||||
float x2 = x;
|
||||
float y2 = y;
|
||||
if (flipX)
|
||||
x2 = vertRect.width - x;
|
||||
if (flipY)
|
||||
y2 = vertRect.height - y;
|
||||
float xMin = flipX ? (vertRect.width - vertRect.x) : vertRect.xMin;
|
||||
float yMin = flipY ? (vertRect.height - vertRect.y) : vertRect.yMin;
|
||||
float xMax = flipX ? -vertRect.xMin : vertRect.xMax;
|
||||
float yMax = flipY ? -vertRect.yMin : vertRect.yMax;
|
||||
|
||||
vb.AddVert(new Vector3(xMin, yMin, 0));
|
||||
|
||||
if (clockwise)
|
||||
vb.AddVert(new Vector3(xMax, yMin, 0));
|
||||
|
||||
if (y > vertRect.yMax)
|
||||
{
|
||||
if (thresold)
|
||||
vb.AddVert(new Vector3(x2, yMax, 0));
|
||||
else
|
||||
vb.AddVert(new Vector3(xMax, yMax, 0));
|
||||
}
|
||||
else
|
||||
vb.AddVert(new Vector3(xMax, y2, 0));
|
||||
|
||||
if (x > vertRect.xMax)
|
||||
{
|
||||
if (thresold)
|
||||
vb.AddVert(new Vector3(xMax, y2, 0));
|
||||
else
|
||||
vb.AddVert(new Vector3(xMax, yMax, 0));
|
||||
}
|
||||
else
|
||||
vb.AddVert(new Vector3(x2, yMax, 0));
|
||||
|
||||
if (!clockwise)
|
||||
vb.AddVert(new Vector3(xMin, yMax, 0));
|
||||
|
||||
if (flipX == flipY)
|
||||
{
|
||||
vb.AddTriangle(0, 1, 2);
|
||||
vb.AddTriangle(0, 2, 3);
|
||||
}
|
||||
else
|
||||
{
|
||||
vb.AddTriangle(2, 1, 0);
|
||||
vb.AddTriangle(3, 2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
//8 vertex
|
||||
static void FillRadial180(VertexBuffer vb, Rect vertRect, Origin180 origin, float amount, bool clockwise)
|
||||
{
|
||||
switch (origin)
|
||||
{
|
||||
case Origin180.Top:
|
||||
if (amount <= 0.5f)
|
||||
{
|
||||
vertRect.width /= 2;
|
||||
if (clockwise)
|
||||
vertRect.x += vertRect.width;
|
||||
|
||||
FillRadial90(vb, vertRect, clockwise ? Origin90.TopLeft : Origin90.TopRight, amount / 0.5f, clockwise);
|
||||
Vector3 vec = vb.GetPosition(-4);
|
||||
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
else
|
||||
{
|
||||
vertRect.width /= 2;
|
||||
if (!clockwise)
|
||||
vertRect.x += vertRect.width;
|
||||
|
||||
FillRadial90(vb, vertRect, clockwise ? Origin90.TopRight : Origin90.TopLeft, (amount - 0.5f) / 0.5f, clockwise);
|
||||
|
||||
if (clockwise)
|
||||
vertRect.x += vertRect.width;
|
||||
else
|
||||
vertRect.x -= vertRect.width;
|
||||
vb.AddQuad(vertRect);
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
break;
|
||||
|
||||
case Origin180.Bottom:
|
||||
if (amount <= 0.5f)
|
||||
{
|
||||
vertRect.width /= 2;
|
||||
if (!clockwise)
|
||||
vertRect.x += vertRect.width;
|
||||
|
||||
FillRadial90(vb, vertRect, clockwise ? Origin90.BottomRight : Origin90.BottomLeft, amount / 0.5f, clockwise);
|
||||
Vector3 vec = vb.GetPosition(-4);
|
||||
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
else
|
||||
{
|
||||
vertRect.width /= 2;
|
||||
if (clockwise)
|
||||
vertRect.x += vertRect.width;
|
||||
|
||||
FillRadial90(vb, vertRect, clockwise ? Origin90.BottomLeft : Origin90.BottomRight, (amount - 0.5f) / 0.5f, clockwise);
|
||||
|
||||
if (clockwise)
|
||||
vertRect.x -= vertRect.width;
|
||||
else
|
||||
vertRect.x += vertRect.width;
|
||||
vb.AddQuad(vertRect);
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
break;
|
||||
|
||||
case Origin180.Left:
|
||||
if (amount <= 0.5f)
|
||||
{
|
||||
vertRect.height /= 2;
|
||||
if (!clockwise)
|
||||
vertRect.y += vertRect.height;
|
||||
|
||||
FillRadial90(vb, vertRect, clockwise ? Origin90.BottomLeft : Origin90.TopLeft, amount / 0.5f, clockwise);
|
||||
Vector3 vec = vb.GetPosition(-4);
|
||||
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
else
|
||||
{
|
||||
vertRect.height /= 2;
|
||||
if (clockwise)
|
||||
vertRect.y += vertRect.height;
|
||||
|
||||
FillRadial90(vb, vertRect, clockwise ? Origin90.TopLeft : Origin90.BottomLeft, (amount - 0.5f) / 0.5f, clockwise);
|
||||
|
||||
if (clockwise)
|
||||
vertRect.y -= vertRect.height;
|
||||
else
|
||||
vertRect.y += vertRect.height;
|
||||
vb.AddQuad(vertRect);
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
break;
|
||||
|
||||
case Origin180.Right:
|
||||
if (amount <= 0.5f)
|
||||
{
|
||||
vertRect.height /= 2;
|
||||
if (clockwise)
|
||||
vertRect.y += vertRect.height;
|
||||
|
||||
FillRadial90(vb, vertRect, clockwise ? Origin90.TopRight : Origin90.BottomRight, amount / 0.5f, clockwise);
|
||||
Vector3 vec = vb.GetPosition(-4);
|
||||
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
else
|
||||
{
|
||||
vertRect.height /= 2;
|
||||
if (!clockwise)
|
||||
vertRect.y += vertRect.height;
|
||||
|
||||
FillRadial90(vb, vertRect, clockwise ? Origin90.BottomRight : Origin90.TopRight, (amount - 0.5f) / 0.5f, clockwise);
|
||||
|
||||
if (clockwise)
|
||||
vertRect.y += vertRect.height;
|
||||
else
|
||||
vertRect.y -= vertRect.height;
|
||||
vb.AddQuad(vertRect);
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//12 vertex
|
||||
static void FillRadial360(VertexBuffer vb, Rect vertRect, Origin360 origin, float amount, bool clockwise)
|
||||
{
|
||||
switch (origin)
|
||||
{
|
||||
case Origin360.Top:
|
||||
if (amount < 0.5f)
|
||||
{
|
||||
vertRect.width /= 2;
|
||||
if (clockwise)
|
||||
vertRect.x += vertRect.width;
|
||||
|
||||
FillRadial180(vb, vertRect, clockwise ? Origin180.Left : Origin180.Right, amount / 0.5f, clockwise);
|
||||
Vector3 vec = vb.GetPosition(-8);
|
||||
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
else
|
||||
{
|
||||
vertRect.width /= 2;
|
||||
if (!clockwise)
|
||||
vertRect.x += vertRect.width;
|
||||
|
||||
FillRadial180(vb, vertRect, clockwise ? Origin180.Right : Origin180.Left, (amount - 0.5f) / 0.5f, clockwise);
|
||||
|
||||
if (clockwise)
|
||||
vertRect.x += vertRect.width;
|
||||
else
|
||||
vertRect.x -= vertRect.width;
|
||||
vb.AddQuad(vertRect);
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case Origin360.Bottom:
|
||||
if (amount < 0.5f)
|
||||
{
|
||||
vertRect.width /= 2;
|
||||
if (!clockwise)
|
||||
vertRect.x += vertRect.width;
|
||||
|
||||
FillRadial180(vb, vertRect, clockwise ? Origin180.Right : Origin180.Left, amount / 0.5f, clockwise);
|
||||
Vector3 vec = vb.GetPosition(-8);
|
||||
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
else
|
||||
{
|
||||
vertRect.width /= 2;
|
||||
if (clockwise)
|
||||
vertRect.x += vertRect.width;
|
||||
|
||||
FillRadial180(vb, vertRect, clockwise ? Origin180.Left : Origin180.Right, (amount - 0.5f) / 0.5f, clockwise);
|
||||
|
||||
if (clockwise)
|
||||
vertRect.x -= vertRect.width;
|
||||
else
|
||||
vertRect.x += vertRect.width;
|
||||
vb.AddQuad(vertRect);
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
break;
|
||||
|
||||
case Origin360.Left:
|
||||
if (amount < 0.5f)
|
||||
{
|
||||
vertRect.height /= 2;
|
||||
if (!clockwise)
|
||||
vertRect.y += vertRect.height;
|
||||
|
||||
FillRadial180(vb, vertRect, clockwise ? Origin180.Bottom : Origin180.Top, amount / 0.5f, clockwise);
|
||||
Vector3 vec = vb.GetPosition(-8);
|
||||
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
else
|
||||
{
|
||||
vertRect.height /= 2;
|
||||
if (clockwise)
|
||||
vertRect.y += vertRect.height;
|
||||
|
||||
FillRadial180(vb, vertRect, clockwise ? Origin180.Top : Origin180.Bottom, (amount - 0.5f) / 0.5f, clockwise);
|
||||
|
||||
if (clockwise)
|
||||
vertRect.y -= vertRect.height;
|
||||
else
|
||||
vertRect.y += vertRect.height;
|
||||
vb.AddQuad(vertRect);
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
break;
|
||||
|
||||
case Origin360.Right:
|
||||
if (amount < 0.5f)
|
||||
{
|
||||
vertRect.height /= 2;
|
||||
if (clockwise)
|
||||
vertRect.y += vertRect.height;
|
||||
|
||||
FillRadial180(vb, vertRect, clockwise ? Origin180.Top : Origin180.Bottom, amount / 0.5f, clockwise);
|
||||
Vector3 vec = vb.GetPosition(-8);
|
||||
vb.AddQuad(new Rect(vec.x, vec.y, 0, 0));
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
else
|
||||
{
|
||||
vertRect.height /= 2;
|
||||
if (!clockwise)
|
||||
vertRect.y += vertRect.height;
|
||||
|
||||
FillRadial180(vb, vertRect, clockwise ? Origin180.Bottom : Origin180.Top, (amount - 0.5f) / 0.5f, clockwise);
|
||||
|
||||
if (clockwise)
|
||||
vertRect.y += vertRect.height;
|
||||
else
|
||||
vertRect.y -= vertRect.height;
|
||||
vb.AddQuad(vertRect);
|
||||
vb.AddTriangles(-4);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/FillMesh.cs.meta
Normal file
13
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/FillMesh.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8b37d56dc1840734a8fdb4971f5bc733
|
||||
timeCreated: 1545987173
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
179
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/LineMesh.cs
Normal file
179
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/LineMesh.cs
Normal file
@@ -0,0 +1,179 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FairyGUI
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// Inspired by kim ki won (http://mypi.ruliweb.daum.net/mypi.htm?id=newtypekorea)
|
||||
/// </summary>
|
||||
public class LineMesh : IMeshFactory
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public GPath path;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float lineWidth;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public AnimationCurve lineWidthCurve;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Gradient gradient;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public bool roundEdge;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float fillStart;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float fillEnd;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float pointDensity;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public bool repeatFill;
|
||||
|
||||
static List<Vector3> points = new List<Vector3>();
|
||||
static List<float> ts = new List<float>();
|
||||
|
||||
public LineMesh()
|
||||
{
|
||||
path = new GPath();
|
||||
lineWidth = 2;
|
||||
fillStart = 0;
|
||||
fillEnd = 1;
|
||||
pointDensity = 0.1f;
|
||||
}
|
||||
|
||||
public void OnPopulateMesh(VertexBuffer vb)
|
||||
{
|
||||
Vector2 uvMin = vb.uvRect.position;
|
||||
Vector2 uvMax = new Vector2(vb.uvRect.xMax, vb.uvRect.yMax);
|
||||
float uvRatio = path.length / vb.textureSize.x;
|
||||
|
||||
int segCount = path.segmentCount;
|
||||
float t = 0;
|
||||
float lw = lineWidth;
|
||||
float u;
|
||||
for (int si = 0; si < segCount; si++)
|
||||
{
|
||||
float ratio = path.GetSegmentLength(si) / path.length;
|
||||
float t0 = Mathf.Clamp(fillStart - t, 0, ratio) / ratio;
|
||||
float t1 = Mathf.Clamp(fillEnd - t, 0, ratio) / ratio;
|
||||
if (t0 >= t1)
|
||||
{
|
||||
t += ratio;
|
||||
continue;
|
||||
}
|
||||
|
||||
points.Clear();
|
||||
ts.Clear();
|
||||
path.GetPointsInSegment(si, t0, t1, points, ts, pointDensity);
|
||||
int cnt = points.Count;
|
||||
|
||||
Color c0 = vb.vertexColor;
|
||||
Color c1 = vb.vertexColor;
|
||||
if (gradient != null)
|
||||
c0 = gradient.Evaluate(t);
|
||||
if (lineWidthCurve != null)
|
||||
lw = lineWidthCurve.Evaluate(t);
|
||||
|
||||
if (roundEdge && si == 0 && t0 == 0)
|
||||
DrawRoundEdge(vb, points[0], points[1], lw, c0, uvMin);
|
||||
|
||||
int vertCount = vb.currentVertCount;
|
||||
for (int i = 1; i < cnt; i++)
|
||||
{
|
||||
Vector3 p0 = points[i - 1];
|
||||
Vector3 p1 = points[i];
|
||||
int k = vertCount + (i - 1) * 2;
|
||||
float tc = t + ratio * ts[i];
|
||||
|
||||
Vector3 lineVector = p1 - p0;
|
||||
Vector3 widthVector = Vector3.Cross(lineVector, new Vector3(0, 0, 1));
|
||||
widthVector.Normalize();
|
||||
|
||||
if (i == 1)
|
||||
{
|
||||
if (repeatFill)
|
||||
u = tc * uvRatio * uvMax.x;
|
||||
else
|
||||
u = Mathf.Lerp(uvMin.x, uvMax.x, t + ratio * ts[i - 1]);
|
||||
vb.AddVert(p0 - widthVector * lw * 0.5f, c0, new Vector2(u, uvMax.y));
|
||||
vb.AddVert(p0 + widthVector * lw * 0.5f, c0, new Vector2(u, uvMin.y));
|
||||
|
||||
if (si != 0) //joint
|
||||
{
|
||||
vb.AddTriangle(k - 2, k - 1, k + 1);
|
||||
vb.AddTriangle(k - 2, k + 1, k);
|
||||
}
|
||||
}
|
||||
if (gradient != null)
|
||||
c1 = gradient.Evaluate(tc);
|
||||
|
||||
if (lineWidthCurve != null)
|
||||
lw = lineWidthCurve.Evaluate(tc);
|
||||
|
||||
if (repeatFill)
|
||||
u = tc * uvRatio * uvMax.x;
|
||||
else
|
||||
u = Mathf.Lerp(uvMin.x, uvMax.x, tc);
|
||||
vb.AddVert(p1 - widthVector * lw * 0.5f, c1, new Vector2(u, uvMax.y));
|
||||
vb.AddVert(p1 + widthVector * lw * 0.5f, c1, new Vector2(u, uvMin.y));
|
||||
|
||||
vb.AddTriangle(k, k + 1, k + 3);
|
||||
vb.AddTriangle(k, k + 3, k + 2);
|
||||
}
|
||||
|
||||
if (roundEdge && si == segCount - 1 && t1 == 1)
|
||||
DrawRoundEdge(vb, points[cnt - 1], points[cnt - 2], lw, c1, uvMax);
|
||||
|
||||
t += ratio;
|
||||
}
|
||||
}
|
||||
|
||||
void DrawRoundEdge(VertexBuffer vb, Vector2 p0, Vector2 p1, float lw, Color32 color, Vector2 uv)
|
||||
{
|
||||
Vector2 widthVector = Vector3.Cross(p0 - p1, new Vector3(0, 0, 1));
|
||||
widthVector.Normalize();
|
||||
widthVector = widthVector * lw / 2f;
|
||||
Vector2 lineVector = (p0 - p1).normalized * lw / 2f;
|
||||
|
||||
int sides = Mathf.CeilToInt(Mathf.PI * lw / 2);
|
||||
if (sides < 6)
|
||||
sides = 6;
|
||||
int current = vb.currentVertCount;
|
||||
float angleUnit = Mathf.PI / (sides - 1);
|
||||
|
||||
vb.AddVert(p0, color, uv);
|
||||
vb.AddVert(p0 + widthVector, color, uv);
|
||||
|
||||
for (int n = 0; n < sides; n++)
|
||||
{
|
||||
vb.AddVert(p0 + Mathf.Cos(angleUnit * n) * widthVector + Mathf.Sin(angleUnit * n) * lineVector, color, uv);
|
||||
vb.AddTriangle(current, current + 1 + n, current + 2 + n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/LineMesh.cs.meta
Normal file
13
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/LineMesh.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6d1935a924fabf74a92c72b22a042279
|
||||
timeCreated: 1546519483
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
15
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/MeshFactory.cs
Normal file
15
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/MeshFactory.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
namespace FairyGUI
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public interface IMeshFactory
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="vb"></param>
|
||||
void OnPopulateMesh(VertexBuffer vb);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 274275eaa3fbbc94697cbe90a0fb52f1
|
||||
timeCreated: 1545987172
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
51
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/PlaneMesh.cs
Normal file
51
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/PlaneMesh.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace FairyGUI
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class PlaneMesh : IMeshFactory
|
||||
{
|
||||
public int gridSize = 30;
|
||||
|
||||
public void OnPopulateMesh(VertexBuffer vb)
|
||||
{
|
||||
float w = vb.contentRect.width;
|
||||
float h = vb.contentRect.height;
|
||||
float xMax = vb.contentRect.xMax;
|
||||
float yMax = vb.contentRect.yMax;
|
||||
int hc = (int)Mathf.Min(Mathf.CeilToInt(w / gridSize), 9);
|
||||
int vc = (int)Mathf.Min(Mathf.CeilToInt(h / gridSize), 9);
|
||||
int eachPartX = Mathf.FloorToInt(w / hc);
|
||||
int eachPartY = Mathf.FloorToInt(h / vc);
|
||||
float x, y;
|
||||
for (int i = 0; i <= vc; i++)
|
||||
{
|
||||
if (i == vc)
|
||||
y = yMax;
|
||||
else
|
||||
y = vb.contentRect.y + i * eachPartY;
|
||||
for (int j = 0; j <= hc; j++)
|
||||
{
|
||||
if (j == hc)
|
||||
x = xMax;
|
||||
else
|
||||
x = vb.contentRect.x + j * eachPartX;
|
||||
vb.AddVert(new Vector3(x, y, 0));
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < vc; i++)
|
||||
{
|
||||
int k = i * (hc + 1);
|
||||
for (int j = 1; j <= hc; j++)
|
||||
{
|
||||
int m = k + j;
|
||||
vb.AddTriangle(m - 1, m, m + hc);
|
||||
vb.AddTriangle(m, m + hc + 1, m + hc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/PlaneMesh.cs.meta
Normal file
13
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/PlaneMesh.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 121b756a7a8240c49b63fa9de560691a
|
||||
timeCreated: 1547970144
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
291
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/PolygonMesh.cs
Normal file
291
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/PolygonMesh.cs
Normal file
@@ -0,0 +1,291 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FairyGUI
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class PolygonMesh : IMeshFactory, IHitTest
|
||||
{
|
||||
/// <summary>
|
||||
/// points must be in clockwise order, and must start from bottom-left if stretchUV is set.
|
||||
/// </summary>
|
||||
public readonly List<Vector2> points;
|
||||
|
||||
/// <summary>
|
||||
/// if you dont want to provide uv, leave it empty.
|
||||
/// </summary>
|
||||
public readonly List<Vector2> texcoords;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float lineWidth;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32 lineColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32? fillColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32[] colors;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public bool usePercentPositions;
|
||||
|
||||
static List<int> sRestIndices = new List<int>();
|
||||
|
||||
public PolygonMesh()
|
||||
{
|
||||
points = new List<Vector2>();
|
||||
texcoords = new List<Vector2>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="point"></param>
|
||||
public void Add(Vector2 point)
|
||||
{
|
||||
points.Add(point);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="point"></param>
|
||||
/// <param name="texcoord"></param>
|
||||
public void Add(Vector2 point, Vector2 texcoord)
|
||||
{
|
||||
points.Add(point);
|
||||
texcoords.Add(texcoord);
|
||||
}
|
||||
|
||||
public void OnPopulateMesh(VertexBuffer vb)
|
||||
{
|
||||
int numVertices = points.Count;
|
||||
if (numVertices < 3)
|
||||
return;
|
||||
|
||||
int restIndexPos, numRestIndices;
|
||||
Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;
|
||||
|
||||
float w = vb.contentRect.width;
|
||||
float h = vb.contentRect.height;
|
||||
bool useTexcoords = texcoords.Count >= numVertices;
|
||||
bool fullUV = true;
|
||||
for (int i = 0; i < numVertices; i++)
|
||||
{
|
||||
Vector3 vec = new Vector3(points[i].x, points[i].y, 0);
|
||||
if (usePercentPositions)
|
||||
{
|
||||
vec.x *= w;
|
||||
vec.y *= h;
|
||||
}
|
||||
if (useTexcoords)
|
||||
{
|
||||
Vector2 uv = texcoords[i];
|
||||
if (uv.x != 0 && uv.x != 1 || uv.y != 0 && uv.y != 1)
|
||||
fullUV = false;
|
||||
uv.x = Mathf.Lerp(vb.uvRect.x, vb.uvRect.xMax, uv.x);
|
||||
uv.y = Mathf.Lerp(vb.uvRect.y, vb.uvRect.yMax, uv.y);
|
||||
vb.AddVert(vec, color, uv);
|
||||
}
|
||||
else
|
||||
vb.AddVert(vec, color);
|
||||
}
|
||||
|
||||
if (useTexcoords && fullUV && numVertices == 4)
|
||||
vb._isArbitraryQuad = true;
|
||||
|
||||
// Algorithm "Ear clipping method" described here:
|
||||
// -> https://en.wikipedia.org/wiki/Polygon_triangulation
|
||||
//
|
||||
// Implementation inspired by:
|
||||
// -> http://polyk.ivank.net
|
||||
// -> Starling
|
||||
|
||||
sRestIndices.Clear();
|
||||
for (int i = 0; i < numVertices; ++i)
|
||||
sRestIndices.Add(i);
|
||||
|
||||
restIndexPos = 0;
|
||||
numRestIndices = numVertices;
|
||||
|
||||
Vector2 a, b, c, p;
|
||||
int otherIndex;
|
||||
bool earFound;
|
||||
int i0, i1, i2;
|
||||
|
||||
while (numRestIndices > 3)
|
||||
{
|
||||
earFound = false;
|
||||
i0 = sRestIndices[restIndexPos % numRestIndices];
|
||||
i1 = sRestIndices[(restIndexPos + 1) % numRestIndices];
|
||||
i2 = sRestIndices[(restIndexPos + 2) % numRestIndices];
|
||||
|
||||
a = points[i0];
|
||||
b = points[i1];
|
||||
c = points[i2];
|
||||
|
||||
if ((a.y - b.y) * (c.x - b.x) + (b.x - a.x) * (c.y - b.y) >= 0)
|
||||
{
|
||||
earFound = true;
|
||||
for (int i = 3; i < numRestIndices; ++i)
|
||||
{
|
||||
otherIndex = sRestIndices[(restIndexPos + i) % numRestIndices];
|
||||
p = points[otherIndex];
|
||||
|
||||
if (IsPointInTriangle(ref p, ref a, ref b, ref c))
|
||||
{
|
||||
earFound = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (earFound)
|
||||
{
|
||||
vb.AddTriangle(i0, i1, i2);
|
||||
sRestIndices.RemoveAt((restIndexPos + 1) % numRestIndices);
|
||||
|
||||
numRestIndices--;
|
||||
restIndexPos = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
restIndexPos++;
|
||||
if (restIndexPos == numRestIndices) break; // no more ears
|
||||
}
|
||||
}
|
||||
vb.AddTriangle(sRestIndices[0], sRestIndices[1], sRestIndices[2]);
|
||||
|
||||
if (colors != null)
|
||||
vb.RepeatColors(colors, 0, vb.currentVertCount);
|
||||
|
||||
if (lineWidth > 0)
|
||||
DrawOutline(vb);
|
||||
}
|
||||
|
||||
void DrawOutline(VertexBuffer vb)
|
||||
{
|
||||
int numVertices = points.Count;
|
||||
int start = vb.currentVertCount - numVertices;
|
||||
int k = vb.currentVertCount;
|
||||
for (int i = 0; i < numVertices; i++)
|
||||
{
|
||||
Vector3 p0 = vb.vertices[start + i];
|
||||
p0.y = -p0.y;
|
||||
Vector3 p1;
|
||||
if (i < numVertices - 1)
|
||||
p1 = vb.vertices[start + i + 1];
|
||||
else
|
||||
p1 = vb.vertices[start];
|
||||
p1.y = -p1.y;
|
||||
|
||||
Vector3 lineVector = p1 - p0;
|
||||
Vector3 widthVector = Vector3.Cross(lineVector, new Vector3(0, 0, 1));
|
||||
widthVector.Normalize();
|
||||
|
||||
vb.AddVert(p0 - widthVector * lineWidth * 0.5f, lineColor);
|
||||
vb.AddVert(p0 + widthVector * lineWidth * 0.5f, lineColor);
|
||||
vb.AddVert(p1 - widthVector * lineWidth * 0.5f, lineColor);
|
||||
vb.AddVert(p1 + widthVector * lineWidth * 0.5f, lineColor);
|
||||
|
||||
k += 4;
|
||||
vb.AddTriangle(k - 4, k - 3, k - 1);
|
||||
vb.AddTriangle(k - 4, k - 1, k - 2);
|
||||
|
||||
//joint
|
||||
if (i != 0)
|
||||
{
|
||||
vb.AddTriangle(k - 6, k - 5, k - 3);
|
||||
vb.AddTriangle(k - 6, k - 3, k - 4);
|
||||
}
|
||||
if (i == numVertices - 1)
|
||||
{
|
||||
start += numVertices;
|
||||
vb.AddTriangle(k - 2, k - 1, start + 1);
|
||||
vb.AddTriangle(k - 2, start + 1, start);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool IsPointInTriangle(ref Vector2 p, ref Vector2 a, ref Vector2 b, ref Vector2 c)
|
||||
{
|
||||
// From Starling
|
||||
// This algorithm is described well in this article:
|
||||
// http://www.blackpawn.com/texts/pointinpoly/default.html
|
||||
|
||||
float v0x = c.x - a.x;
|
||||
float v0y = c.y - a.y;
|
||||
float v1x = b.x - a.x;
|
||||
float v1y = b.y - a.y;
|
||||
float v2x = p.x - a.x;
|
||||
float v2y = p.y - a.y;
|
||||
|
||||
float dot00 = v0x * v0x + v0y * v0y;
|
||||
float dot01 = v0x * v1x + v0y * v1y;
|
||||
float dot02 = v0x * v2x + v0y * v2y;
|
||||
float dot11 = v1x * v1x + v1y * v1y;
|
||||
float dot12 = v1x * v2x + v1y * v2y;
|
||||
|
||||
float invDen = 1.0f / (dot00 * dot11 - dot01 * dot01);
|
||||
float u = (dot11 * dot02 - dot01 * dot12) * invDen;
|
||||
float v = (dot00 * dot12 - dot01 * dot02) * invDen;
|
||||
|
||||
return (u >= 0) && (v >= 0) && (u + v < 1);
|
||||
}
|
||||
|
||||
public bool HitTest(Rect contentRect, Vector2 point)
|
||||
{
|
||||
if (!contentRect.Contains(point))
|
||||
return false;
|
||||
|
||||
// Algorithm & implementation thankfully taken from:
|
||||
// -> http://alienryderflex.com/polygon/
|
||||
// inspired by Starling
|
||||
int len = points.Count;
|
||||
int i;
|
||||
int j = len - 1;
|
||||
bool oddNodes = false;
|
||||
float w = contentRect.width;
|
||||
float h = contentRect.height;
|
||||
|
||||
for (i = 0; i < len; ++i)
|
||||
{
|
||||
float ix = points[i].x;
|
||||
float iy = points[i].y;
|
||||
float jx = points[j].x;
|
||||
float jy = points[j].y;
|
||||
if (usePercentPositions)
|
||||
{
|
||||
ix *= w;
|
||||
iy *= h;
|
||||
ix *= w;
|
||||
iy *= h;
|
||||
}
|
||||
|
||||
if ((iy < point.y && jy >= point.y || jy < point.y && iy >= point.y) && (ix <= point.x || jx <= point.x))
|
||||
{
|
||||
if (ix + (point.y - iy) / (jy - iy) * (jx - ix) < point.x)
|
||||
oddNodes = !oddNodes;
|
||||
}
|
||||
|
||||
j = i;
|
||||
}
|
||||
|
||||
return oddNodes;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: db020628cd79e714fbafdbaaa2cc355f
|
||||
timeCreated: 1545987173
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
85
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/RectMesh.cs
Normal file
85
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/RectMesh.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace FairyGUI
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class RectMesh : IMeshFactory, IHitTest
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Rect? drawRect;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float lineWidth;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32 lineColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32? fillColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32[] colors;
|
||||
|
||||
public RectMesh()
|
||||
{
|
||||
lineColor = Color.black;
|
||||
}
|
||||
|
||||
public void OnPopulateMesh(VertexBuffer vb)
|
||||
{
|
||||
Rect rect = drawRect != null ? (Rect)drawRect : vb.contentRect;
|
||||
Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;
|
||||
if (lineWidth == 0)
|
||||
{
|
||||
if (color.a != 0)//optimized
|
||||
vb.AddQuad(rect, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
Rect part;
|
||||
|
||||
//left,right
|
||||
part = new Rect(rect.x, rect.y, lineWidth, rect.height);
|
||||
vb.AddQuad(part, lineColor);
|
||||
part = new Rect(rect.xMax - lineWidth, rect.y, lineWidth, rect.height);
|
||||
vb.AddQuad(part, lineColor);
|
||||
|
||||
//top, bottom
|
||||
part = new Rect(rect.x + lineWidth, rect.y, rect.width - lineWidth * 2, lineWidth);
|
||||
vb.AddQuad(part, lineColor);
|
||||
part = new Rect(rect.x + lineWidth, rect.yMax - lineWidth, rect.width - lineWidth * 2, lineWidth);
|
||||
vb.AddQuad(part, lineColor);
|
||||
|
||||
//middle
|
||||
if (color.a != 0)//optimized
|
||||
{
|
||||
part = Rect.MinMaxRect(rect.x + lineWidth, rect.y + lineWidth, rect.xMax - lineWidth, rect.yMax - lineWidth);
|
||||
if (part.width > 0 && part.height > 0)
|
||||
vb.AddQuad(part, color);
|
||||
}
|
||||
}
|
||||
|
||||
if (colors != null)
|
||||
vb.RepeatColors(colors, 0, vb.currentVertCount);
|
||||
|
||||
vb.AddTriangles();
|
||||
}
|
||||
|
||||
public bool HitTest(Rect contentRect, Vector2 point)
|
||||
{
|
||||
return contentRect.Contains(point);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/RectMesh.cs.meta
Normal file
13
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/RectMesh.cs.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 13a3a9d0d365d834696112114d7bf951
|
||||
timeCreated: 1545987172
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
128
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/RegularPolygonMesh.cs
Normal file
128
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/RegularPolygonMesh.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace FairyGUI
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class RegularPolygonMesh : IMeshFactory, IHitTest
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Rect? drawRect;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public int sides;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float lineWidth;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32 lineColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32? centerColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32? fillColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float[] distances;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float rotation;
|
||||
|
||||
public RegularPolygonMesh()
|
||||
{
|
||||
sides = 3;
|
||||
lineColor = Color.black;
|
||||
}
|
||||
|
||||
public void OnPopulateMesh(VertexBuffer vb)
|
||||
{
|
||||
if (distances != null && distances.Length < sides)
|
||||
{
|
||||
Debug.LogError("distances.Length<sides");
|
||||
return;
|
||||
}
|
||||
|
||||
Rect rect = drawRect != null ? (Rect)drawRect : vb.contentRect;
|
||||
Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;
|
||||
|
||||
float angleDelta = 2 * Mathf.PI / sides;
|
||||
float angle = rotation * Mathf.Deg2Rad;
|
||||
float radius = Mathf.Min(rect.width / 2, rect.height / 2);
|
||||
|
||||
float centerX = radius + rect.x;
|
||||
float centerY = radius + rect.y;
|
||||
vb.AddVert(new Vector3(centerX, centerY, 0), centerColor == null ? color : (Color32)centerColor);
|
||||
for (int i = 0; i < sides; i++)
|
||||
{
|
||||
float r = radius;
|
||||
if (distances != null)
|
||||
r *= distances[i];
|
||||
float xv = Mathf.Cos(angle) * (r - lineWidth);
|
||||
float yv = Mathf.Sin(angle) * (r - lineWidth);
|
||||
Vector3 vec = new Vector3(xv + centerX, yv + centerY, 0);
|
||||
vb.AddVert(vec, color);
|
||||
if (lineWidth > 0)
|
||||
{
|
||||
vb.AddVert(vec, lineColor);
|
||||
|
||||
xv = Mathf.Cos(angle) * r + centerX;
|
||||
yv = Mathf.Sin(angle) * r + centerY;
|
||||
vb.AddVert(new Vector3(xv, yv, 0), lineColor);
|
||||
}
|
||||
angle += angleDelta;
|
||||
}
|
||||
|
||||
if (lineWidth > 0)
|
||||
{
|
||||
int tmp = sides * 3;
|
||||
for (int i = 0; i < tmp; i += 3)
|
||||
{
|
||||
if (i != tmp - 3)
|
||||
{
|
||||
vb.AddTriangle(0, i + 1, i + 4);
|
||||
vb.AddTriangle(i + 5, i + 2, i + 3);
|
||||
vb.AddTriangle(i + 3, i + 6, i + 5);
|
||||
}
|
||||
else
|
||||
{
|
||||
vb.AddTriangle(0, i + 1, 1);
|
||||
vb.AddTriangle(2, i + 2, i + 3);
|
||||
vb.AddTriangle(i + 3, 3, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < sides; i++)
|
||||
vb.AddTriangle(0, i + 1, (i == sides - 1) ? 1 : i + 2);
|
||||
}
|
||||
}
|
||||
|
||||
public bool HitTest(Rect contentRect, Vector2 point)
|
||||
{
|
||||
if (drawRect != null)
|
||||
return ((Rect)drawRect).Contains(point);
|
||||
else
|
||||
return contentRect.Contains(point);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a4084bdb2f1738340a28ab5699977f3b
|
||||
timeCreated: 1545987173
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
177
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/RoundedRectMesh.cs
Normal file
177
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/RoundedRectMesh.cs
Normal file
@@ -0,0 +1,177 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace FairyGUI
|
||||
{
|
||||
public class RoundedRectMesh : IMeshFactory, IHitTest
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Rect? drawRect;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float lineWidth;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32 lineColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32? fillColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float topLeftRadius;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float topRightRadius;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float bottomLeftRadius;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float bottomRightRadius;
|
||||
|
||||
public RoundedRectMesh()
|
||||
{
|
||||
lineColor = Color.black;
|
||||
}
|
||||
|
||||
public void OnPopulateMesh(VertexBuffer vb)
|
||||
{
|
||||
Rect rect = drawRect != null ? (Rect)drawRect : vb.contentRect;
|
||||
Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;
|
||||
|
||||
float radiusX = rect.width / 2;
|
||||
float radiusY = rect.height / 2;
|
||||
float cornerMaxRadius = Mathf.Min(radiusX, radiusY);
|
||||
float centerX = radiusX + rect.x;
|
||||
float centerY = radiusY + rect.y;
|
||||
|
||||
vb.AddVert(new Vector3(centerX, centerY, 0), color);
|
||||
|
||||
int cnt = vb.currentVertCount;
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
float radius = 0;
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
radius = bottomRightRadius;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
radius = bottomLeftRadius;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
radius = topLeftRadius;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
radius = topRightRadius;
|
||||
break;
|
||||
}
|
||||
radius = Mathf.Min(cornerMaxRadius, radius);
|
||||
|
||||
float offsetX = rect.x;
|
||||
float offsetY = rect.y;
|
||||
|
||||
if (i == 0 || i == 3)
|
||||
offsetX = rect.xMax - radius * 2;
|
||||
if (i == 0 || i == 1)
|
||||
offsetY = rect.yMax - radius * 2;
|
||||
|
||||
if (radius != 0)
|
||||
{
|
||||
int partNumSides = Mathf.Max(1, Mathf.CeilToInt(Mathf.PI * radius / 8)) + 1;
|
||||
float angleDelta = Mathf.PI / 2 / partNumSides;
|
||||
float angle = Mathf.PI / 2 * i;
|
||||
float startAngle = angle;
|
||||
|
||||
for (int j = 1; j <= partNumSides; j++)
|
||||
{
|
||||
if (j == partNumSides) //消除精度误差带来的不对齐
|
||||
angle = startAngle + Mathf.PI / 2;
|
||||
Vector3 v1 = new Vector3(offsetX + Mathf.Cos(angle) * (radius - lineWidth) + radius,
|
||||
offsetY + Mathf.Sin(angle) * (radius - lineWidth) + radius, 0);
|
||||
vb.AddVert(v1, color);
|
||||
if (lineWidth != 0)
|
||||
{
|
||||
vb.AddVert(v1, lineColor);
|
||||
vb.AddVert(new Vector3(offsetX + Mathf.Cos(angle) * radius + radius,
|
||||
offsetY + Mathf.Sin(angle) * radius + radius, 0), lineColor);
|
||||
}
|
||||
angle += angleDelta;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Vector3 v1 = new Vector3(offsetX, offsetY, 0);
|
||||
if (lineWidth != 0)
|
||||
{
|
||||
if (i == 0 || i == 3)
|
||||
offsetX -= lineWidth;
|
||||
else
|
||||
offsetX += lineWidth;
|
||||
if (i == 0 || i == 1)
|
||||
offsetY -= lineWidth;
|
||||
else
|
||||
offsetY += lineWidth;
|
||||
Vector3 v2 = new Vector3(offsetX, offsetY, 0);
|
||||
vb.AddVert(v2, color);
|
||||
vb.AddVert(v2, lineColor);
|
||||
vb.AddVert(v1, lineColor);
|
||||
}
|
||||
else
|
||||
vb.AddVert(v1, color);
|
||||
}
|
||||
}
|
||||
cnt = vb.currentVertCount - cnt;
|
||||
|
||||
if (lineWidth > 0)
|
||||
{
|
||||
for (int i = 0; i < cnt; i += 3)
|
||||
{
|
||||
if (i != cnt - 3)
|
||||
{
|
||||
vb.AddTriangle(0, i + 1, i + 4);
|
||||
vb.AddTriangle(i + 5, i + 2, i + 3);
|
||||
vb.AddTriangle(i + 3, i + 6, i + 5);
|
||||
}
|
||||
else
|
||||
{
|
||||
vb.AddTriangle(0, i + 1, 1);
|
||||
vb.AddTriangle(2, i + 2, i + 3);
|
||||
vb.AddTriangle(i + 3, 3, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < cnt; i++)
|
||||
vb.AddTriangle(0, i + 1, (i == cnt - 1) ? 1 : i + 2);
|
||||
}
|
||||
}
|
||||
|
||||
public bool HitTest(Rect contentRect, Vector2 point)
|
||||
{
|
||||
if (drawRect != null)
|
||||
return ((Rect)drawRect).Contains(point);
|
||||
else
|
||||
return contentRect.Contains(point);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2f910c4392a714c41b421c296621562b
|
||||
timeCreated: 1545987172
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,90 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace FairyGUI
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class StraightLineMesh : IMeshFactory
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color color;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Vector3 origin;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Vector3 end;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public float lineWidth;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public bool repeatFill;
|
||||
|
||||
public StraightLineMesh()
|
||||
{
|
||||
color = Color.black;
|
||||
lineWidth = 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="lineWidth"></param>
|
||||
/// <param name="color"></param>
|
||||
/// <param name="repeatFill"></param>
|
||||
public StraightLineMesh(float lineWidth, Color color, bool repeatFill)
|
||||
{
|
||||
this.lineWidth = lineWidth;
|
||||
this.color = color;
|
||||
this.repeatFill = repeatFill;
|
||||
}
|
||||
|
||||
public void OnPopulateMesh(VertexBuffer vb)
|
||||
{
|
||||
if (origin == end)
|
||||
return;
|
||||
|
||||
float length = Vector2.Distance(origin, end);
|
||||
Vector3 lineVector = end - origin;
|
||||
Vector3 widthVector = Vector3.Cross(lineVector, new Vector3(0, 0, 1));
|
||||
widthVector.Normalize();
|
||||
|
||||
Vector3 v0, v1, v2, v3;
|
||||
|
||||
if (repeatFill)
|
||||
{
|
||||
float ratio = length / vb.textureSize.x;
|
||||
v0 = VertexBuffer.NormalizedUV[0];
|
||||
v1 = VertexBuffer.NormalizedUV[1];
|
||||
v2 = new Vector2(ratio, 1);
|
||||
v3 = new Vector2(ratio, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
v0 = new Vector2(vb.uvRect.xMin, vb.uvRect.yMin);
|
||||
v1 = new Vector2(vb.uvRect.xMin, vb.uvRect.yMax);
|
||||
v2 = new Vector2(vb.uvRect.xMax, vb.uvRect.yMax);
|
||||
v3 = new Vector2(vb.uvRect.xMax, vb.uvRect.yMin);
|
||||
}
|
||||
|
||||
vb.AddVert(origin - widthVector * lineWidth * 0.5f, color, v0);
|
||||
vb.AddVert(origin + widthVector * lineWidth * 0.5f, color, v1);
|
||||
vb.AddVert(end + widthVector * lineWidth * 0.5f, color, v2);
|
||||
vb.AddVert(end - widthVector * lineWidth * 0.5f, color, v3);
|
||||
|
||||
vb.AddTriangles();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 27b002bc89d804a7b8058b9d16d5c506
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
519
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/VertexBuffer.cs
Normal file
519
Assets/Plugins/FairyGUI/Scripts/Core/Mesh/VertexBuffer.cs
Normal file
@@ -0,0 +1,519 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace FairyGUI
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public sealed class VertexBuffer
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Rect contentRect;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Rect uvRect;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Color32 vertexColor;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public Vector2 textureSize;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public readonly List<Vector3> vertices;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public readonly List<Color32> colors;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public readonly List<Vector2> uvs;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public readonly List<Vector2> uvs2;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public readonly List<int> triangles;
|
||||
|
||||
static public Vector2[] NormalizedUV = new Vector2[] {
|
||||
new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) };
|
||||
|
||||
static public Vector2[] NormalizedPosition = new Vector2[] {
|
||||
new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1) };
|
||||
|
||||
internal bool _alphaInVertexColor;
|
||||
internal bool _isArbitraryQuad;
|
||||
|
||||
static Stack<VertexBuffer> _pool = new Stack<VertexBuffer>();
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static VertexBuffer Begin()
|
||||
{
|
||||
if (_pool.Count > 0)
|
||||
{
|
||||
VertexBuffer inst = _pool.Pop();
|
||||
inst.Clear();
|
||||
return inst;
|
||||
}
|
||||
else
|
||||
return new VertexBuffer();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
public static VertexBuffer Begin(VertexBuffer source)
|
||||
{
|
||||
VertexBuffer vb = Begin();
|
||||
vb.contentRect = source.contentRect;
|
||||
vb.uvRect = source.uvRect;
|
||||
vb.vertexColor = source.vertexColor;
|
||||
vb.textureSize = source.textureSize;
|
||||
|
||||
return vb;
|
||||
}
|
||||
|
||||
private VertexBuffer()
|
||||
{
|
||||
vertices = new List<Vector3>();
|
||||
colors = new List<Color32>();
|
||||
uvs = new List<Vector2>();
|
||||
uvs2 = new List<Vector2>();
|
||||
triangles = new List<int>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public void End()
|
||||
{
|
||||
_pool.Push(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
vertices.Clear();
|
||||
colors.Clear();
|
||||
uvs.Clear();
|
||||
uvs2.Clear();
|
||||
triangles.Clear();
|
||||
|
||||
_isArbitraryQuad = false;
|
||||
_alphaInVertexColor = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public int currentVertCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return vertices.Count;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
public void AddVert(Vector3 position)
|
||||
{
|
||||
position.y = -position.y;
|
||||
vertices.Add(position);
|
||||
colors.Add(vertexColor);
|
||||
if (vertexColor.a != 255)
|
||||
_alphaInVertexColor = true;
|
||||
uvs.Add(new Vector2(
|
||||
Mathf.Lerp(uvRect.xMin, uvRect.xMax, (position.x - contentRect.xMin) / contentRect.width),
|
||||
Mathf.Lerp(uvRect.yMax, uvRect.yMin, (-position.y - contentRect.yMin) / contentRect.height)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <param name="color"></param>
|
||||
public void AddVert(Vector3 position, Color32 color)
|
||||
{
|
||||
position.y = -position.y;
|
||||
vertices.Add(position);
|
||||
colors.Add(color);
|
||||
if (color.a != 255)
|
||||
_alphaInVertexColor = true;
|
||||
uvs.Add(new Vector2(
|
||||
Mathf.Lerp(uvRect.xMin, uvRect.xMax, (position.x - contentRect.xMin) / contentRect.width),
|
||||
Mathf.Lerp(uvRect.yMax, uvRect.yMin, (-position.y - contentRect.yMin) / contentRect.height)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <param name="color"></param>
|
||||
/// <param name="uv"></param>
|
||||
public void AddVert(Vector3 position, Color32 color, Vector2 uv)
|
||||
{
|
||||
position.y = -position.y;
|
||||
vertices.Add(position);
|
||||
uvs.Add(new Vector2(uv.x, uv.y));
|
||||
colors.Add(color);
|
||||
if (color.a != 255)
|
||||
_alphaInVertexColor = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// 1---2
|
||||
/// | / |
|
||||
/// 0---3
|
||||
/// </summary>
|
||||
/// <param name="vertRect"></param>
|
||||
public void AddQuad(Rect vertRect)
|
||||
{
|
||||
AddVert(new Vector3(vertRect.xMin, vertRect.yMax, 0f));
|
||||
AddVert(new Vector3(vertRect.xMin, vertRect.yMin, 0f));
|
||||
AddVert(new Vector3(vertRect.xMax, vertRect.yMin, 0f));
|
||||
AddVert(new Vector3(vertRect.xMax, vertRect.yMax, 0f));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="vertRect"></param>
|
||||
/// <param name="color"></param>
|
||||
public void AddQuad(Rect vertRect, Color32 color)
|
||||
{
|
||||
AddVert(new Vector3(vertRect.xMin, vertRect.yMax, 0f), color);
|
||||
AddVert(new Vector3(vertRect.xMin, vertRect.yMin, 0f), color);
|
||||
AddVert(new Vector3(vertRect.xMax, vertRect.yMin, 0f), color);
|
||||
AddVert(new Vector3(vertRect.xMax, vertRect.yMax, 0f), color);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="vertRect"></param>
|
||||
/// <param name="color"></param>
|
||||
/// <param name="uvRect"></param>
|
||||
public void AddQuad(Rect vertRect, Color32 color, Rect uvRect)
|
||||
{
|
||||
vertices.Add(new Vector3(vertRect.xMin, -vertRect.yMax, 0f));
|
||||
vertices.Add(new Vector3(vertRect.xMin, -vertRect.yMin, 0f));
|
||||
vertices.Add(new Vector3(vertRect.xMax, -vertRect.yMin, 0f));
|
||||
vertices.Add(new Vector3(vertRect.xMax, -vertRect.yMax, 0f));
|
||||
|
||||
uvs.Add(new Vector2(uvRect.xMin, uvRect.yMin));
|
||||
uvs.Add(new Vector2(uvRect.xMin, uvRect.yMax));
|
||||
uvs.Add(new Vector2(uvRect.xMax, uvRect.yMax));
|
||||
uvs.Add(new Vector2(uvRect.xMax, uvRect.yMin));
|
||||
|
||||
colors.Add(color);
|
||||
colors.Add(color);
|
||||
colors.Add(color);
|
||||
colors.Add(color);
|
||||
|
||||
if (color.a != 255)
|
||||
_alphaInVertexColor = true;
|
||||
}
|
||||
|
||||
static List<Vector4> helperV4List = new List<Vector4>(4) { Vector4.zero, Vector4.zero, Vector4.zero, Vector4.zero };
|
||||
internal List<Vector4> FixUVForArbitraryQuad()
|
||||
{
|
||||
//ref1 http://www.reedbeta.com/blog/quadrilateral-interpolation-part-1/
|
||||
//ref2 https://bitlush.com/blog/arbitrary-quadrilaterals-in-opengl-es-2-0
|
||||
|
||||
Vector4 qq = Vector4.one;
|
||||
Vector2 a = vertices[2] - vertices[0];
|
||||
Vector2 b = vertices[1] - vertices[3];
|
||||
Vector2 c = vertices[0] - vertices[3];
|
||||
|
||||
float cross = a.x * b.y - a.y * b.x;
|
||||
if (cross != 0)
|
||||
{
|
||||
float s = (a.x * c.y - a.y * c.x) / cross;
|
||||
if (s > 0 && s < 1)
|
||||
{
|
||||
float t = (b.x * c.y - b.y * c.x) / cross;
|
||||
if (t > 0 && t < 1)
|
||||
{
|
||||
qq.x = 1 / (1 - t);
|
||||
qq.y = 1 / s;
|
||||
qq.z = 1 / t;
|
||||
qq.w = 1 / (1 - s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
Vector4 v = uvs[i];
|
||||
float q = qq[i];
|
||||
v.x *= q;
|
||||
v.y *= q;
|
||||
v.w = q;
|
||||
helperV4List[i] = v;
|
||||
}
|
||||
|
||||
return helperV4List;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="startIndex"></param>
|
||||
/// <param name="count"></param>
|
||||
public void RepeatColors(Color32[] value, int startIndex, int count)
|
||||
{
|
||||
int len = Mathf.Min(startIndex + count, vertices.Count);
|
||||
int colorCount = value.Length;
|
||||
int k = 0;
|
||||
for (int i = startIndex; i < len; i++)
|
||||
{
|
||||
Color32 c = value[(k++) % colorCount];
|
||||
colors[i] = c;
|
||||
if (c.a != 255)
|
||||
_alphaInVertexColor = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="idx0"></param>
|
||||
/// <param name="idx1"></param>
|
||||
/// <param name="idx2"></param>
|
||||
public void AddTriangle(int idx0, int idx1, int idx2)
|
||||
{
|
||||
triangles.Add(idx0);
|
||||
triangles.Add(idx1);
|
||||
triangles.Add(idx2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="idxList"></param>
|
||||
/// <param name="startVertexIndex"></param>
|
||||
public void AddTriangles(int[] idxList, int startVertexIndex = 0)
|
||||
{
|
||||
if (startVertexIndex != 0)
|
||||
{
|
||||
if (startVertexIndex < 0)
|
||||
startVertexIndex = vertices.Count + startVertexIndex;
|
||||
|
||||
int cnt = idxList.Length;
|
||||
for (int i = 0; i < cnt; i++)
|
||||
triangles.Add(idxList[i] + startVertexIndex);
|
||||
}
|
||||
else
|
||||
triangles.AddRange(idxList);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="startVertexIndex"></param>
|
||||
public void AddTriangles(int startVertexIndex = 0)
|
||||
{
|
||||
int cnt = vertices.Count;
|
||||
if (startVertexIndex < 0)
|
||||
startVertexIndex = cnt + startVertexIndex;
|
||||
|
||||
for (int i = startVertexIndex; i < cnt; i += 4)
|
||||
{
|
||||
triangles.Add(i);
|
||||
triangles.Add(i + 1);
|
||||
triangles.Add(i + 2);
|
||||
|
||||
triangles.Add(i + 2);
|
||||
triangles.Add(i + 3);
|
||||
triangles.Add(i);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="index"></param>
|
||||
/// <returns></returns>
|
||||
public Vector3 GetPosition(int index)
|
||||
{
|
||||
if (index < 0)
|
||||
index = vertices.Count + index;
|
||||
|
||||
Vector3 vec = vertices[index];
|
||||
vec.y = -vec.y;
|
||||
return vec;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <param name="usePercent"></param>
|
||||
/// <returns></returns>
|
||||
public Vector2 GetUVAtPosition(Vector2 position, bool usePercent)
|
||||
{
|
||||
if (usePercent)
|
||||
{
|
||||
return new Vector2(Mathf.Lerp(uvRect.xMin, uvRect.xMax, position.x),
|
||||
Mathf.Lerp(uvRect.yMax, uvRect.yMin, position.y));
|
||||
}
|
||||
else
|
||||
return new Vector2(Mathf.Lerp(uvRect.xMin, uvRect.xMax, (position.x - contentRect.xMin) / contentRect.width),
|
||||
Mathf.Lerp(uvRect.yMax, uvRect.yMin, (position.y - contentRect.yMin) / contentRect.height));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="vb"></param>
|
||||
public void Append(VertexBuffer vb)
|
||||
{
|
||||
int len = vertices.Count;
|
||||
vertices.AddRange(vb.vertices);
|
||||
uvs.AddRange(vb.uvs);
|
||||
uvs2.AddRange(vb.uvs2);
|
||||
colors.AddRange(vb.colors);
|
||||
if (len != 0)
|
||||
{
|
||||
int len1 = vb.triangles.Count;
|
||||
for (int i = 0; i < len1; i++)
|
||||
triangles.Add(vb.triangles[i] + len);
|
||||
}
|
||||
else
|
||||
triangles.AddRange(vb.triangles);
|
||||
|
||||
if (vb._alphaInVertexColor)
|
||||
_alphaInVertexColor = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="vb"></param>
|
||||
public void Insert(VertexBuffer vb)
|
||||
{
|
||||
vertices.InsertRange(0, vb.vertices);
|
||||
uvs.InsertRange(0, vb.uvs);
|
||||
uvs2.InsertRange(0, vb.uvs2);
|
||||
colors.InsertRange(0, vb.colors);
|
||||
int len = triangles.Count;
|
||||
if (len != 0)
|
||||
{
|
||||
int len1 = vb.vertices.Count;
|
||||
for (int i = 0; i < len; i++)
|
||||
triangles[i] += len1;
|
||||
}
|
||||
triangles.InsertRange(0, vb.triangles);
|
||||
|
||||
if (vb._alphaInVertexColor)
|
||||
_alphaInVertexColor = true;
|
||||
}
|
||||
|
||||
static float[] STROKE_OFFSET = new float[]
|
||||
{
|
||||
-1, 0, 1, 0,
|
||||
0, -1, 0, 1,
|
||||
-1, -1, 1, -1,
|
||||
-1, 1, 1, 1
|
||||
};
|
||||
public void GenerateOutline(int dirs, float width, Color color)
|
||||
{
|
||||
int count = vertices.Count;
|
||||
if (count + dirs * count > VerticesLimit)
|
||||
{
|
||||
Debug.LogWarning("Outline effect cannot be completed as mesh is too large.");
|
||||
return;
|
||||
}
|
||||
|
||||
VertexBuffer vb2 = VertexBuffer.Begin();
|
||||
if (color.a != 255)
|
||||
vb2._alphaInVertexColor = true;
|
||||
for (int j = 0; j < dirs; j++)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Vector3 vert = vertices[i];
|
||||
vb2.vertices.Add(new Vector3(vert.x + STROKE_OFFSET[j * 2] * width, vert.y + STROKE_OFFSET[j * 2 + 1] * width, 0));
|
||||
vb2.colors.Add(color);
|
||||
}
|
||||
|
||||
vb2.uvs.AddRange(uvs);
|
||||
if (uvs2.Count > 0)
|
||||
vb2.uvs2.AddRange(uvs2);
|
||||
}
|
||||
Insert(vb2);
|
||||
vb2.End();
|
||||
}
|
||||
|
||||
public void GenerateShadow(Vector2 offset, Color color)
|
||||
{
|
||||
int count = vertices.Count;
|
||||
if (count + count > VerticesLimit)
|
||||
{
|
||||
Debug.LogWarning("Shadow effect cannot be completed as mesh is too large.");
|
||||
return;
|
||||
}
|
||||
|
||||
VertexBuffer vb2 = VertexBuffer.Begin();
|
||||
if (color.a != 255)
|
||||
vb2._alphaInVertexColor = true;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
Vector3 vert = vertices[i];
|
||||
vb2.vertices.Add(new Vector3(vert.x + offset.x, vert.y - offset.y, 0));
|
||||
vb2.colors.Add(color);
|
||||
}
|
||||
|
||||
vb2.uvs.AddRange(uvs);
|
||||
if (uvs2.Count > 0)
|
||||
vb2.uvs2.AddRange(uvs2);
|
||||
Insert(vb2);
|
||||
vb2.End();
|
||||
}
|
||||
|
||||
const int VerticesLimit = 65000;
|
||||
public void CheckMeshLimit()
|
||||
{
|
||||
int count = vertices.Count;
|
||||
if (count > VerticesLimit)
|
||||
{
|
||||
Debug.LogWarning("A mesh may not have more than " + VerticesLimit + " vertices.");
|
||||
vertices.RemoveRange(VerticesLimit, count - VerticesLimit);
|
||||
colors.RemoveRange(VerticesLimit, count - VerticesLimit);
|
||||
uvs.RemoveRange(VerticesLimit, count - VerticesLimit);
|
||||
if (uvs2.Count > 0)
|
||||
uvs2.RemoveRange(VerticesLimit, count - VerticesLimit);
|
||||
count = VerticesLimit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8d5501e8f84e40e47835611ab3896029
|
||||
timeCreated: 1545987173
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user