WheelGraphic.cs 5.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace DevilLink2.UI
  4. {
  5. // Equal clockwise sectors; angle zero and sector ordering match LuckyWheelScreen.SectorAngle.
  6. [RequireComponent(typeof(CanvasRenderer))]
  7. public sealed class WheelGraphic : MaskableGraphic
  8. {
  9. private Material aaMaterial;
  10. protected override void OnEnable()
  11. {
  12. aaMaterial=Resources.Load<Material>("DevilLink2WheelAA");
  13. if(aaMaterial && aaMaterial.shader && aaMaterial.shader.isSupported)material=aaMaterial;
  14. else {aaMaterial=null;material=null;Debug.LogWarning("Wheel AA material unavailable: using original mesh fallback.");}
  15. base.OnEnable();
  16. }
  17. protected override void OnPopulateMesh(VertexHelper mesh)
  18. {
  19. mesh.Clear();var rect=GetPixelAdjustedRect();float r=Mathf.Min(rect.width,rect.height)*.5f;
  20. if(r<=.0001f)return;
  21. if(aaMaterial){
  22. // Measure actual pixel density, including the existing PortraitFrame scale.
  23. var camera=canvas && canvas.renderMode!=RenderMode.ScreenSpaceOverlay?canvas.worldCamera:null;
  24. var origin=RectTransformUtility.WorldToScreenPoint(camera,transform.TransformPoint(Vector3.zero));
  25. float pixels=Mathf.Min(Vector2.Distance(origin,RectTransformUtility.WorldToScreenPoint(camera,transform.TransformPoint(Vector3.right))),Vector2.Distance(origin,RectTransformUtility.WorldToScreenPoint(camera,transform.TransformPoint(Vector3.up))));
  26. float pad=1.5f/Mathf.Max(.01f,pixels),extent=r+pad,uv=extent/r;
  27. var center=rect.center;
  28. mesh.AddVert(center+new Vector2(-extent,-extent),color,new Vector2(-uv,-uv));
  29. mesh.AddVert(center+new Vector2(-extent,extent),color,new Vector2(-uv,uv));
  30. mesh.AddVert(center+new Vector2(extent,extent),color,new Vector2(uv,uv));
  31. mesh.AddVert(center+new Vector2(extent,-extent),color,new Vector2(uv,-uv));
  32. mesh.AddTriangle(0,1,2);mesh.AddTriangle(0,2,3);return;
  33. }
  34. for(int sector=0;sector<8;sector++){
  35. var shade=UIBuilder.Hex(sector%2==0?"60305D":"38244F");
  36. // Continuous radial tones plus a soft directional light, never brown sector fills.
  37. var radii=new[]{0f,.28f,.48f,.68f,.82f,.925f};
  38. for(int band=0;band<radii.Length-1;band++){
  39. float inner=radii[band],outer=radii[band+1];
  40. Arc(mesh,rect.center,r*inner,r*outer,90-sector*45,45,24,SectorColor(shade,inner),SectorColor(shade,outer),true);
  41. }
  42. float a=(90-sector*45)*Mathf.Deg2Rad;var direction=new Vector2(Mathf.Cos(a),Mathf.Sin(a));
  43. var normal=new Vector2(-direction.y,direction.x)*.45f;int n=mesh.currentVertCount;
  44. mesh.AddVert(rect.center+direction*r*.28f-normal,UIBuilder.Hex("796071"),Vector2.zero);
  45. mesh.AddVert(rect.center+direction*r*.925f-normal,UIBuilder.Hex("D5BA82"),Vector2.zero);
  46. mesh.AddVert(rect.center+direction*r*.925f+normal,UIBuilder.Hex("D5BA82"),Vector2.zero);
  47. mesh.AddVert(rect.center+direction*r*.28f+normal,UIBuilder.Hex("796071"),Vector2.zero);
  48. mesh.AddTriangle(n,n+1,n+2);mesh.AddTriangle(n,n+2,n+3);
  49. }
  50. // Thin inner wire, recessed violet groove, bevel and narrow champagne highlight.
  51. Arc(mesh,rect.center,r*.925f,r*.932f,90,360,128,UIBuilder.Hex("E8D4AA"),UIBuilder.Hex("AC9061"),true);
  52. Arc(mesh,rect.center,r*.932f,r*.950f,90,360,128,UIBuilder.Hex("24152F"),UIBuilder.Hex("453047"));
  53. Arc(mesh,rect.center,r*.950f,r*.975f,90,360,128,UIBuilder.Hex("7D6A60"),UIBuilder.Hex("C4AB80"),true);
  54. Arc(mesh,rect.center,r*.975f,r*.982f,90,360,128,UIBuilder.Hex("F7E8C7"),UIBuilder.Hex("D8BD8D"),true);
  55. Arc(mesh,rect.center,r*.982f,r*.992f,90,360,128,UIBuilder.Hex("8D7967"),UIBuilder.Hex("3B283B"));
  56. Arc(mesh,rect.center,r*.992f,r,90,360,128,UIBuilder.Hex("271A32"),UIBuilder.Hex("130D1C"));
  57. }
  58. private static Color SectorColor(Color shade,float radius)
  59. {
  60. float light=.40f+.43f*Mathf.Sin(radius/.925f*Mathf.PI)+.08f*radius;
  61. return Color.Lerp(UIBuilder.Hex("140F23"),shade,light);
  62. }
  63. private static Color Lit(Color color,float angle,bool light)
  64. {
  65. if(!light)return color;
  66. float strength=.84f+.16f*(.5f+.5f*Mathf.Cos(angle-135*Mathf.Deg2Rad));
  67. return new Color(color.r*strength,color.g*strength,color.b*strength,color.a);
  68. }
  69. private static void Arc(VertexHelper mesh,Vector2 center,float inner,float outer,float start,float span,int steps,Color inside,Color outside,bool light=false)
  70. {
  71. for(int i=0;i<steps;i++){
  72. float a=(start-span*i/steps)*Mathf.Deg2Rad,b=(start-span*(i+1)/steps)*Mathf.Deg2Rad;
  73. var x=new Vector2(Mathf.Cos(a),Mathf.Sin(a));var y=new Vector2(Mathf.Cos(b),Mathf.Sin(b));int n=mesh.currentVertCount;
  74. mesh.AddVert(center+x*inner,Lit(inside,a,light),Vector2.zero);mesh.AddVert(center+x*outer,Lit(outside,a,light),Vector2.zero);
  75. mesh.AddVert(center+y*outer,Lit(outside,b,light),Vector2.zero);mesh.AddVert(center+y*inner,Lit(inside,b,light),Vector2.zero);
  76. mesh.AddTriangle(n,n+1,n+2);mesh.AddTriangle(n,n+2,n+3);
  77. }
  78. }
  79. }
  80. }