Tanoda
LeapPanelOutlineGraphic.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System.Collections.Generic;
10using UnityEngine;
11using UnityEngine.Rendering;
12using Leap.Unity.Query;
14
16
21 [DisallowMultipleComponent]
23
24 #region Inspector
25
26 [Header("Outline Thickness")]
27
28 [Tooltip("Check this option to override the thickness of the sprite's borders with "
29 + "a custom thickness. This will stretch the sprite's borders to match the "
30 + "custom thickness.")]
31 [SerializeField, EditTimeOnly]
32 private bool _overrideSpriteBorders = false;
39 get {
40 return _overrideSpriteBorders;
41 }
42 }
43
44 [Tooltip("The local-space thickness of the panel edges that are constructed. If the"
45 + "source data for the panel is a Sprite, this value is overridden to reflect "
46 + "the sprite's nine-slicing.")]
47 [MinValue(0f)]
48 [SerializeField, EditTimeOnly]
49 private Vector2 _thickness = new Vector2(0.01F, 0.01F);
53 public Vector2 thickness {
54 get {
55 return _thickness;
56 }
57 }
58
59 #endregion
60
61 #region Unity Events
62
63 protected override void OnValidate() {
64 base.OnValidate();
65
67 }
68
69 #endregion
70
71 public override void RefreshSlicedMeshData(Vector2i resolution,
72 RectMargins meshMargins,
73 RectMargins uvMargins) {
74 resolution.x = Mathf.Max(resolution.x, 4);
75 resolution.y = Mathf.Max(resolution.y, 4);
76
78 // For this calculation, we ignore the provided meshMargins because the Outline
79 // graphic allows the user to override its default nine-slicing behaviour for
80 // borders.
81 meshMargins = new RectMargins(_thickness.x, _thickness.y, _thickness.x, _thickness.y);
82 }
83 if (!nineSliced) {
84 float xRatio = _thickness.x / rect.width;
85 float yRatio = _thickness.y / rect.height;
86 xRatio = Mathf.Clamp(xRatio, 0F, 0.5F);
87 yRatio = Mathf.Clamp(yRatio, 0F, 0.5F);
88 uvMargins = new RectMargins(left: xRatio, right: xRatio,
89 top: yRatio, bottom: yRatio);
90 }
91
92 List<Vector3> verts = new List<Vector3>();
93 List<Vector2> uvs = new List<Vector2>();
94 List<int> tris = new List<int>();
95
96 for (int vy = 0; vy < resolution.y; vy++) {
97 for (int vx = 0; vx < resolution.x; vx++) {
98 // Outline verts only.
99 if ((vy > 1 && vy < resolution.y - 2) && (vx > 1 && vx < resolution.x - 2)) continue;
100
101 Vector2 vert;
102 vert.x = calculateVertAxis(vx, resolution.x, rect.width, meshMargins.left, meshMargins.right, true);
103 vert.y = calculateVertAxis(vy, resolution.y, rect.height, meshMargins.top, meshMargins.bottom, true);
104 verts.Add(vert + new Vector2(rect.x, rect.y));
105
106 Vector2 uv;
107 uv.x = calculateVertAxis(vx, resolution.x, 1, uvMargins.left, uvMargins.right, true);
108 uv.y = calculateVertAxis(vy, resolution.y, 1, uvMargins.top, uvMargins.bottom, true);
109 uvs.Add(uv);
110 }
111 }
112
113 int indicesSkippedPerRow = resolution.x - 4;
114 int indicesSkipped = 0;
115 for (int vy = 0; vy < resolution.y; vy++) {
116 for (int vx = 0; vx < resolution.x; vx++) {
117
118 if (vx == resolution.x - 1 || vy == resolution.y - 1) {
119 continue;
120 }
121 if ((vx == 1 && (vy > 0 && vy < resolution.y - 2)) || (vy == 1 && (vx > 0 && vx < resolution.x - 2))) {
122 continue;
123 }
124 if ((vx > 1 && vx < resolution.x - 2) && (vy > 1 && vy < resolution.y - 2)) {
125 indicesSkipped += 1;
126 continue;
127 }
128
129 int vertIndex = vy * resolution.x + vx - indicesSkipped;
130
131 int right = 1;
132 int down;
133 if (vy == 0 || (vy == 1 && vx < 2) || (vy == resolution.y - 3 && vx > resolution.x - 3) || (vy == resolution.y - 2)) {
134 down = resolution.x;
135 }
136 else {
137 down = resolution.x - indicesSkippedPerRow;
138 }
139
140 // Add quad
141 tris.Add(vertIndex);
142 tris.Add(vertIndex + right + down);
143 tris.Add(vertIndex + right);
144
145 tris.Add(vertIndex);
146 tris.Add(vertIndex + down);
147 tris.Add(vertIndex + right + down);
148 }
149 }
150
151 if (mesh == null) {
152 mesh = new Mesh();
153 }
154
155 mesh.name = "Panel Outline Mesh";
156 mesh.hideFlags = HideFlags.HideAndDontSave;
157
158 mesh.Clear(keepVertexLayout: false);
159 mesh.SetVertices(verts);
160 mesh.SetTriangles(tris, 0);
161 mesh.SetUVs(uvChannel.Index(), uvs);
162 mesh.RecalculateBounds();
163
164 remappableChannels = UVChannelFlags.UV0;
165 }
166
167 }
168}
Mesh mesh
Returns the mesh that represents this graphic. It can have any topology, any number of uv channels,...
UVChannelFlags remappableChannels
Returns an enum mask that represents the union of all channels that are allowed to be remapped for th...
The Panel Outline Graphic acts just like a Panel Graphic, but only produces quads for the outermost e...
bool overrideSpriteBorders
Gets whether the Panel Outline Graphic is overriding the borders of its sprite data source....
override void RefreshSlicedMeshData(Vector2i resolution, RectMargins meshMargins, RectMargins uvMargins)
Set the mesh property equal to the correct mesh given the Sliced Graphic's current settings.
Vector2 thickness
Gets the thickness of the panel edges.
The base class for LeapPanelGraphic, LeapBoxGraphic, and similar generators.
float calculateVertAxis(int vertIdx, int vertCount, float size, float border0, float border1, bool alwaysRespectBorder=false)
Given a vertex index from an edge, the total vertCount and size along the current dimension,...
override void RefreshMeshData()
When this method is called, the mesh property and the remappableChannels property must be assigned to...
Rect rect
Returns the current local-space rect of this panel. If there is a RectTransform attached to this pane...
UVChannelFlags uvChannel
Returns which uv channel is being used for this panel. It will always match the uv channel being used...
bool nineSliced
Gets or sets whether or not this panel is currently using nine slicing.