MPMaterials.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using UnityEngine;
  2. namespace MPUIKIT {
  3. public static class MPMaterials {
  4. private const string MpBasicProceduralShaderName = "MPUI/Basic Procedural Image";
  5. private static string[] MpShapeKeywords = {"CIRCLE", "TRIANGLE", "RECTANGLE", "NSTAR_POLYGON"};
  6. private const string MpStrokeKeyword = "STROKE";
  7. private const string MpOutlineKeyword = "OUTLINED";
  8. private const string MpOutlinedStrokeKeyword = "OUTLINED_STROKE";
  9. private static Shader _proceduralShader;
  10. internal static Shader MPBasicProceduralShader
  11. {
  12. get {
  13. if (_proceduralShader == null)
  14. _proceduralShader = Shader.Find(MpBasicProceduralShaderName);
  15. return _proceduralShader;
  16. }
  17. }
  18. private static Material[] _materialDB = new Material[16];
  19. internal static ref Material GetMaterial(int shapeIndex, bool stroked, bool outlined) {
  20. int index = shapeIndex * 4;
  21. if (stroked && outlined) index += 3;
  22. else if (outlined) index += 2;
  23. else if (stroked) index += 1;
  24. ref Material mat = ref _materialDB[index];
  25. if (mat != null) return ref mat;
  26. mat = new Material(MPBasicProceduralShader);
  27. string shapeKeyword = MpShapeKeywords[shapeIndex];
  28. mat.name = $"Basic Procedural Sprite - {shapeKeyword} {(stroked?MpStrokeKeyword:string.Empty)} {(outlined?MpOutlineKeyword:string.Empty)}";
  29. mat.EnableKeyword(shapeKeyword);
  30. if(stroked && outlined) mat.EnableKeyword(MpOutlinedStrokeKeyword);
  31. else if(stroked) mat.EnableKeyword(MpStrokeKeyword);
  32. else if(outlined) mat.EnableKeyword(MpOutlineKeyword);
  33. return ref mat;
  34. }
  35. }
  36. }