WheelSpinOrnament.cs 5.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace DevilLink2.UI
  5. {
  6. // Hollow decoration only. The single Spin RoundedPanel remains the sole solid button face.
  7. [RequireComponent(typeof(CanvasRenderer))]
  8. public sealed class WheelSpinOrnament : Image
  9. {
  10. protected override void OnEnable()
  11. {
  12. base.OnEnable();sprite=Resources.Load<Sprite>("WheelOrnaments/Spin_Ornament_Ornate") ?? Resources.Load<Sprite>("WheelOrnaments/Spin_Ornament");
  13. preserveAspect=true;raycastTarget=false;
  14. }
  15. protected override void OnPopulateMesh(VertexHelper mesh)
  16. {
  17. if(sprite){base.OnPopulateMesh(mesh);return;}
  18. mesh.Clear();var r=GetPixelAdjustedRect();float radius=Mathf.Min(r.width,r.height)*.5f;
  19. var gold=UIBuilder.Hex("B99252");var light=UIBuilder.Hex("DFBD7A");
  20. Ring(mesh,r.center,radius-1,radius-1.6f,new Color(gold.r,gold.g,gold.b,0),gold);
  21. Ring(mesh,r.center,radius-1.6f,radius-2.8f,gold,light);
  22. Ring(mesh,r.center,radius-2.8f,radius-3.4f,light,new Color(light.r,light.g,light.b,0));
  23. Ring(mesh,r.center,radius-5,radius-5.6f,new Color(gold.r,gold.g,gold.b,.5f),new Color(gold.r,gold.g,gold.b,0));
  24. Gem(mesh,r.center+Vector2.up*(radius-8),3,4);
  25. Gem(mesh,r.center-Vector2.up*(radius-8),3,4);
  26. }
  27. private static void Ring(VertexHelper mesh,Vector2 center,float outer,float inner,Color a,Color b)
  28. {
  29. const int steps=96;
  30. for(int i=0;i<steps;i++){
  31. float x=i*Mathf.PI*2/steps,y=(i+1)*Mathf.PI*2/steps;
  32. var u=new Vector2(Mathf.Cos(x),Mathf.Sin(x));var v=new Vector2(Mathf.Cos(y),Mathf.Sin(y));int n=mesh.currentVertCount;
  33. mesh.AddVert(center+u*outer,a,Vector2.zero);mesh.AddVert(center+v*outer,a,Vector2.zero);
  34. mesh.AddVert(center+v*inner,b,Vector2.zero);mesh.AddVert(center+u*inner,b,Vector2.zero);
  35. mesh.AddTriangle(n,n+1,n+2);mesh.AddTriangle(n,n+2,n+3);
  36. }
  37. }
  38. private static void Gem(VertexHelper mesh,Vector2 c,float w,float h)
  39. {
  40. int n=mesh.currentVertCount;
  41. mesh.AddVert(c,UIBuilder.Hex("C490D2"),Vector2.zero);
  42. mesh.AddVert(c+Vector2.up*h,UIBuilder.Hex("DFBD7A"),Vector2.zero);
  43. mesh.AddVert(c+Vector2.right*w,UIBuilder.Hex("795080"),Vector2.zero);
  44. mesh.AddVert(c-Vector2.up*h,UIBuilder.Hex("38203F"),Vector2.zero);
  45. mesh.AddVert(c-Vector2.right*w,UIBuilder.Hex("AF789F"),Vector2.zero);
  46. for(int i=0;i<4;i++)mesh.AddTriangle(n,n+1+i,n+1+(i+1)%4);
  47. }
  48. }
  49. public sealed class WheelSpinFaceLighting : BaseMeshEffect
  50. {
  51. public override void ModifyMesh(VertexHelper mesh)
  52. {
  53. if(!IsActive())return;
  54. var r=((RectTransform)transform).rect;float radius=Mathf.Min(r.width,r.height)*.5f;
  55. var vertex=new UIVertex();
  56. for(int i=0;i<mesh.currentVertCount;i++){
  57. mesh.PopulateUIVertex(ref vertex,i);
  58. float t=Mathf.Clamp01(Vector2.Distance(vertex.position,r.center)/radius);
  59. vertex.color=Color.Lerp(UIBuilder.Hex("633874"),UIBuilder.Hex("21102E"),t);
  60. mesh.SetUIVertex(vertex,i);
  61. }
  62. }
  63. }
  64. internal static class WheelDecorationMesh
  65. {
  66. // Symmetric quadratic contour. Rounded bottom is exactly at yMin and x=center.
  67. public static void Pointer(VertexHelper mesh,Rect r)
  68. {
  69. mesh.Clear();var points=new List<Vector2>();
  70. Curve(points,new Vector2(0,.96f),new Vector2(.47f,1),new Vector2(.47f,.76f));
  71. Curve(points,new Vector2(.47f,.76f),new Vector2(.47f,.44f),new Vector2(.14f,.08f));
  72. Curve(points,new Vector2(.14f,.08f),new Vector2(0,-.08f),new Vector2(-.14f,.08f));
  73. Curve(points,new Vector2(-.14f,.08f),new Vector2(-.47f,.44f),new Vector2(-.47f,.76f));
  74. Curve(points,new Vector2(-.47f,.76f),new Vector2(-.47f,1),new Vector2(0,.96f));
  75. var center=new Vector2(0,.55f);var gold=UIBuilder.Hex("B99252");var pale=UIBuilder.Hex("DEBC7A");
  76. for(int i=0;i<points.Count;i++){
  77. var a=points[i];var b=points[(i+1)%points.Count];
  78. Band(mesh,r,a,b,center,1,.96f,new Color(gold.r,gold.g,gold.b,0),pale);
  79. Band(mesh,r,a,b,center,.96f,.82f,pale,gold);
  80. var u=Vector2.Lerp(center,a,.82f);var v=Vector2.Lerp(center,b,.82f);int n=mesh.currentVertCount;
  81. mesh.AddVert(Point(r,center),UIBuilder.Hex("885091"),Vector2.zero);
  82. mesh.AddVert(Point(r,u),Facet(u),Vector2.zero);mesh.AddVert(Point(r,v),Facet(v),Vector2.zero);mesh.AddTriangle(n,n+1,n+2);
  83. }
  84. }
  85. private static Color Facet(Vector2 p)
  86. {return Color.Lerp(UIBuilder.Hex("281331"),p.x<0?UIBuilder.Hex("B37AAF"):UIBuilder.Hex("613466"),Mathf.Clamp01(p.y));}
  87. private static Vector2 Point(Rect r,Vector2 p) => new Vector2(r.center.x+p.x*r.width,r.yMin+p.y*r.height);
  88. private static void Curve(List<Vector2> points,Vector2 a,Vector2 control,Vector2 b)
  89. {for(int i=0;i<12;i++){float t=i/12f;points.Add((1-t)*(1-t)*a+2*(1-t)*t*control+t*t*b);}}
  90. private static void Band(VertexHelper mesh,Rect r,Vector2 a,Vector2 b,Vector2 center,float outer,float inner,Color x,Color y)
  91. {
  92. int n=mesh.currentVertCount;mesh.AddVert(Point(r,Vector2.Lerp(center,a,outer)),x,Vector2.zero);mesh.AddVert(Point(r,Vector2.Lerp(center,b,outer)),x,Vector2.zero);
  93. mesh.AddVert(Point(r,Vector2.Lerp(center,b,inner)),y,Vector2.zero);mesh.AddVert(Point(r,Vector2.Lerp(center,a,inner)),y,Vector2.zero);
  94. mesh.AddTriangle(n,n+1,n+2);mesh.AddTriangle(n,n+2,n+3);
  95. }
  96. }
  97. }