NeonPanel.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace DiamondArrow
  4. {
  5. [RequireComponent(typeof(CanvasRenderer))]
  6. public sealed class NeonPanel : MaskableGraphic
  7. {
  8. public Color top=new Color(.18f,.025f,.29f), bottom=new Color(.045f,0,.085f), accent=Neon.Pink;
  9. public float radius=24; public bool frame=true, stripes;
  10. // 0 flat, 1 glass panel, 3 primary button, 4 secondary button, 5 inset board, 6 backdrop.
  11. public float surface=1;
  12. protected override void OnPopulateMesh(VertexHelper vh)
  13. {
  14. vh.Clear();Rect r=GetPixelAdjustedRect();
  15. if(material!=null&&material.shader.name=="DiamondArrow/Neon Surface")
  16. {
  17. float style=frame?surface:stripes?6:0;float pad=frame?4:0;float rad=frame?radius:Mathf.Min(radius,Mathf.Min(r.width,r.height)*.5f);
  18. Vector2[] points={new Vector2(r.xMin-pad,r.yMin-pad),new Vector2(r.xMin-pad,r.yMax+pad),new Vector2(r.xMax+pad,r.yMax+pad),new Vector2(r.xMax+pad,r.yMin-pad)};
  19. for(int i=0;i<4;i++){var v=UIVertex.simpleVert;v.position=points[i];v.color=top;v.uv0=new Vector2((points[i].x-r.xMin)/Mathf.Max(1,r.width),(points[i].y-r.yMin)/Mathf.Max(1,r.height));v.uv1=new Vector4(r.width,r.height,rad,style);v.uv2=bottom;v.uv3=accent;vh.AddVert(v);}vh.AddTriangle(0,1,2);vh.AddTriangle(0,2,3);return;
  20. }
  21. if(frame)
  22. {
  23. Fill(vh,r,radius,new Color(accent.r,accent.g,accent.b,.18f),new Color(accent.r,accent.g,accent.b,.18f));
  24. r=Inset(r,3);Fill(vh,r,radius,accent,accent*.65f);
  25. r=Inset(r,2);Fill(vh,r,radius-2,Color.white,new Color(.24f,.12f,.34f));
  26. r=Inset(r,3);Fill(vh,r,radius-4,new Color(.27f,.12f,.39f),new Color(.8f,.66f,.9f));
  27. r=Inset(r,2);Fill(vh,r,radius-6,accent,accent);
  28. r=Inset(r,2);
  29. }
  30. Fill(vh,r,Mathf.Max(0,radius-(frame?10:0)),top,bottom);
  31. if(stripes) for(float y=r.yMin+6;y<r.yMax-6;y+=7) Quad(vh,new Rect(r.xMin+8,y,r.width-16,1),new Color(1,.4f,.9f,.025f));
  32. }
  33. static Rect Inset(Rect r,float p)=>new Rect(r.x+p,r.y+p,r.width-2*p,r.height-2*p);
  34. static void Quad(VertexHelper vh,Rect r,Color c) { int i=vh.currentVertCount;vh.AddVert(new Vector3(r.xMin,r.yMin),c,Vector2.zero);vh.AddVert(new Vector3(r.xMin,r.yMax),c,Vector2.zero);vh.AddVert(new Vector3(r.xMax,r.yMax),c,Vector2.zero);vh.AddVert(new Vector3(r.xMax,r.yMin),c,Vector2.zero);vh.AddTriangle(i,i+1,i+2);vh.AddTriangle(i,i+2,i+3); }
  35. static void Fill(VertexHelper vh,Rect r,float radius,Color top,Color bottom)
  36. {
  37. radius=Mathf.Clamp(radius,0,Mathf.Min(r.width,r.height)/2);int center=vh.currentVertCount;
  38. vh.AddVert(r.center,Color.Lerp(bottom,top,.5f),Vector2.zero);
  39. var corners=new[]{new Vector2(r.xMax-radius,r.yMax-radius),new Vector2(r.xMin+radius,r.yMax-radius),new Vector2(r.xMin+radius,r.yMin+radius),new Vector2(r.xMax-radius,r.yMin+radius)};
  40. for(int c=0;c<4;c++)for(int s=0;s<=8;s++) { float a=(c*90+s*90f/8)*Mathf.Deg2Rad;Vector2 p=corners[c]+new Vector2(Mathf.Cos(a),Mathf.Sin(a))*radius;vh.AddVert(p,Color.Lerp(bottom,top,Mathf.InverseLerp(r.yMin,r.yMax,p.y)),Vector2.zero); }
  41. for(int i=0;i<36;i++)vh.AddTriangle(center,center+1+i,center+1+(i+1)%36);
  42. }
  43. }
  44. }