Tanoda
LeapSlicedGraphic.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
10using Leap.Unity.Query;
11using UnityEngine;
12using UnityEngine.Rendering;
13
15
25 [DisallowMultipleComponent]
26 public abstract class LeapSlicedGraphic : LeapMeshGraphicBase {
27
28 #region Constants
29
30 public const int MAX_RESOLUTION = 128;
31
32 #endregion
33
34 #region Inspector
35
37 [SerializeField]
38 protected int _sourceDataIndex = -1;
39
40 //************//
41 // Resolution //
42
43 [Tooltip("Specifies whether or not this panel has a specific resolution, or whether this " +
44 "panel automatically changes its resolution based on its size")]
46 [SerializeField]
47 protected ResolutionType _resolutionType = ResolutionType.VerticesPerRectilinearMeter;
48
49 [HideInInspector]
50 [SerializeField]
51 protected int _resolution_vert_x = 4, _resolution_vert_y = 4;
52
54 [SerializeField]
55 protected Vector2 _resolution_verts_per_meter = new Vector2(20, 20);
56
57 [MinValue(0)]
59 [SerializeField]
60 protected Vector2 _size = new Vector2(0.1f, 0.1f);
61
62 //**************//
63 // Nine Slicing //
64
65 [Tooltip("Uses sprite data to generate a nine sliced panel.")]
67 [SerializeField]
68 protected bool _nineSliced = false;
69
70 #endregion
71
72 #region Properties
73
78 get {
79 return _resolutionType;
80 }
81 }
82
88 public Rect rect {
89 get {
90 RectTransform rectTransform = GetComponent<RectTransform>();
91 if (rectTransform != null) {
92 _size = rectTransform.rect.size;
93 return rectTransform.rect;
94 }
95 else {
96 return new Rect(-_size / 2, _size);
97 }
98 }
99 }
100
104 public bool nineSliced {
105 get {
106 return _nineSliced && canNineSlice;
107 }
108 set {
109 _nineSliced = value;
111 }
112 }
113
117 public bool canNineSlice {
118 get {
119 var spriteData = sourceData as LeapSpriteData;
120 return spriteData != null && spriteData.sprite != null;
121 }
122 }
123
128 public UVChannelFlags uvChannel {
129 get {
130 if (sourceData == null) {
131 return UVChannelFlags.UV0;
132 }
133
134 var feature = sourceData.feature;
135 if (feature is LeapTextureFeature) {
136 return (feature as LeapTextureFeature).channel;
137 }
138 else if (feature is LeapSpriteFeature) {
139 return (feature as LeapSpriteFeature).channel;
140 }
141 else {
142 return UVChannelFlags.UV0;
143 }
144 }
145 }
146
147 #endregion
148
149 #region Unity Events
150
151 protected override void Reset() {
152 base.Reset();
153
156 }
157
158 protected override void OnValidate() {
159 base.OnValidate();
160
161 if (sourceData == null) {
163 }
164
167 _resolution_verts_per_meter = Vector2.Max(_resolution_verts_per_meter, Vector2.zero);
168
169 if (_resolutionType == ResolutionType.Vertices) {
172 }
173 else {
174 _resolution_vert_x = Mathf.RoundToInt(_resolution_verts_per_meter.x * rect.width);
175 _resolution_vert_y = Mathf.RoundToInt(_resolution_verts_per_meter.y * rect.height);
176 }
177
179 }
180
181 #endregion
182
183 #region Leap Graphic
184
189 get {
190 if (_sourceDataIndex == -1) {
192 }
193 if (_sourceDataIndex < 0 || _sourceDataIndex >= featureData.Count) {
194 return null;
195 }
197 }
198#if UNITY_EDITOR
199 set {
202 }
203#endif
204 }
205
211 public static bool IsValidDataSource(LeapFeatureData dataSource) {
212 return dataSource is LeapTextureData ||
213 dataSource is LeapSpriteData;
214 }
215
216 protected void assignDefaultSourceValue() {
218 }
219
220 protected void setSourceFeatureDirty() {
221 if (sourceData != null) {
223 }
224 }
225
226 #endregion
227
228 #region Leap Mesh Graphic
229
230 public override void RefreshMeshData() {
231 if (sourceData == null) {
233 }
234
235 Vector4 borderSize = Vector4.zero;
236 Vector4 borderUvs = Vector4.zero;
237
238 Rect rect;
239 RectTransform rectTransform = GetComponent<RectTransform>();
240 if (rectTransform != null) {
241 rect = rectTransform.rect;
242 _size = rect.size;
243 }
244 else {
245 rect = new Rect(-_size / 2, _size);
246 }
247
249 var spriteData = sourceData as LeapSpriteData;
250 if (spriteData.sprite == null) {
251 mesh = null;
253 return;
254 }
255
256 var sprite = spriteData.sprite;
257
258 Vector4 border = sprite.border;
259 borderSize = border / sprite.pixelsPerUnit;
260
261 borderUvs = border;
262 borderUvs.x /= sprite.textureRect.width;
263 borderUvs.z /= sprite.textureRect.width;
264 borderUvs.y /= sprite.textureRect.height;
265 borderUvs.w /= sprite.textureRect.height;
266 }
267
268 int vertsX, vertsY;
269 if (_resolutionType == ResolutionType.Vertices) {
270 vertsX = Mathf.RoundToInt(_resolution_vert_x);
271 vertsY = Mathf.RoundToInt(_resolution_vert_y);
272 }
273 else {
274 vertsX = Mathf.RoundToInt(rect.width * _resolution_verts_per_meter.x);
275 vertsY = Mathf.RoundToInt(rect.height * _resolution_verts_per_meter.y);
276 }
277
278 vertsX += _nineSliced ? 4 : 2;
279 vertsY += _nineSliced ? 4 : 2;
280
281 vertsX = Mathf.Min(vertsX, MAX_RESOLUTION);
282 vertsY = Mathf.Min(vertsY, MAX_RESOLUTION);
283
284 RefreshSlicedMeshData(new Vector2i() { x = vertsX, y = vertsY },
285 borderSize,
286 borderUvs);
287 }
288
289 #endregion
290
299 public abstract void RefreshSlicedMeshData(Vector2i resolution,
300 RectMargins meshMargins,
301 RectMargins uvMargins);
302
303 #region Mesh Data Support
304
328 protected float calculateVertAxis(int vertIdx, int vertCount, float size, float border0, float border1, bool alwaysRespectBorder = false) {
329 if (_nineSliced || alwaysRespectBorder) {
330 if (vertIdx == 0) {
331 return 0;
332 }
333 else if (vertIdx == (vertCount - 1)) {
334 return size;
335 }
336 else if (vertIdx == 1) {
337 return border0;
338 }
339 else if (vertIdx == (vertCount - 2)) {
340 return size - border1;
341 }
342 else {
343 return ((vertIdx - 1.0f) / (vertCount - 3.0f)) * (size - border0 - border1) + border0;
344 }
345 }
346 else {
347 return (vertIdx / (vertCount - 1.0f)) * size;
348 }
349 }
350
351 #endregion
352
353 #region Supporting Types
354
355 public enum ResolutionType {
356 Vertices,
357 VerticesPerRectilinearMeter
358 }
359
360 public struct Vector2i {
361 public int x;
362 public int y;
363 }
364
365 public struct RectMargins {
367 public float left;
368
370 public float top;
371
373 public float right;
374
376 public float bottom;
377
378 public RectMargins(float left, float top, float right, float bottom) {
379 this.left = left; this.top = top; this.right = right; this.bottom = bottom;
380 }
381
382 public static implicit operator Vector4(RectMargins m) {
383 return new Vector4(m.left, m.top, m.right, m.bottom);
384 }
385
386 public static implicit operator RectMargins(Vector4 v) {
387 return new RectMargins(v.x, v.y, v.z, v.w);
388 }
389 }
390
391 #endregion
392
393 }
394
395}
IList< LeapFeatureData > featureData
Returns a list of feature data attached to this graphic. If this graphic is attached to a group,...
Definition: LeapGraphic.cs:131
This class is a base class for all graphics that can be represented by a mesh object.
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 base class for LeapPanelGraphic, LeapBoxGraphic, and similar generators.
bool canNineSlice
Returns whether or not the current source supports nine slicing.
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...
LeapFeatureData sourceData
Returns the current feature data object being used as source.
abstract void RefreshSlicedMeshData(Vector2i resolution, RectMargins meshMargins, RectMargins uvMargins)
Set the mesh property equal to the correct mesh given the Sliced Graphic's current settings.
Rect rect
Returns the current local-space rect of this panel. If there is a RectTransform attached to this pane...
static bool IsValidDataSource(LeapFeatureData dataSource)
Returns whether or not a feature data object is a valid object that can be used to drive texture data...
UVChannelFlags uvChannel
Returns which uv channel is being used for this panel. It will always match the uv channel being used...
ResolutionType resolutionType
Returns the current resolution type being used for this panel.
bool nineSliced
Gets or sets whether or not this panel is currently using nine slicing.
int IndexOf(BaseType item)
RectMargins(float left, float top, float right, float bottom)