MaterialChecks.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System.Collections.Generic;
  30. using UnityEngine;
  31. #if UNITY_EDITOR
  32. namespace Spine.Unity {
  33. /// <summary>Utility class providing methods to check material settings for incorrect combinations.</summary>
  34. public class MaterialChecks {
  35. static readonly int STRAIGHT_ALPHA_PARAM_ID = Shader.PropertyToID("_StraightAlphaInput");
  36. static readonly string ALPHAPREMULTIPLY_ON_KEYWORD = "_ALPHAPREMULTIPLY_ON";
  37. static readonly string ALPHAPREMULTIPLY_VERTEX_ONLY_ON_KEYWORD = "_ALPHAPREMULTIPLY_VERTEX_ONLY";
  38. static readonly string ALPHABLEND_ON_KEYWORD = "_ALPHABLEND_ON";
  39. static readonly string STRAIGHT_ALPHA_KEYWORD = "_STRAIGHT_ALPHA_INPUT";
  40. static readonly string[] FIXED_NORMALS_KEYWORDS = {
  41. "_FIXED_NORMALS_VIEWSPACE",
  42. "_FIXED_NORMALS_VIEWSPACE_BACKFACE",
  43. "_FIXED_NORMALS_MODELSPACE",
  44. "_FIXED_NORMALS_MODELSPACE_BACKFACE",
  45. "_FIXED_NORMALS_WORLDSPACE"
  46. };
  47. static readonly string NORMALMAP_KEYWORD = "_NORMALMAP";
  48. static readonly string CANVAS_GROUP_COMPATIBLE_KEYWORD = "_CANVAS_GROUP_COMPATIBLE";
  49. public static readonly string kPMANotSupportedLinearMessage =
  50. "\nWarning: Premultiply-alpha atlas textures not supported in Linear color space!"
  51. + "You can use a straight alpha texture with 'PMA Vertex Color' by choosing blend mode 'PMA Vertex, Straight Texture'.\n\n"
  52. + "If you have a PMA Texture, please\n"
  53. + "a) re-export atlas as straight alpha texture with 'premultiply alpha' unchecked\n"
  54. + " (if you have already done this, please set the 'Straight Alpha Texture' Material parameter to 'true') or\n"
  55. + "b) switch to Gamma color space via\nProject Settings - Player - Other Settings - Color Space.\n";
  56. public static readonly string kZSpacingRequiredMessage =
  57. "\nWarning: Z Spacing required on selected shader! Otherwise you will receive incorrect results.\n\nPlease\n"
  58. + "1) make sure at least minimal 'Z Spacing' is set at the SkeletonRenderer/SkeletonAnimation component under 'Advanced' and\n"
  59. + "2) ensure that the skeleton has overlapping parts on different Z depth. You can adjust this in Spine via draw order.\n";
  60. public static readonly string kZSpacingRecommendedMessage =
  61. "\nWarning: Z Spacing recommended on selected shader configuration!\n\nPlease\n"
  62. + "1) make sure at least minimal 'Z Spacing' is set at the SkeletonRenderer/SkeletonAnimation component under 'Advanced' and\n"
  63. + "2) ensure that the skeleton has overlapping parts on different Z depth. You can adjust this in Spine via draw order.\n";
  64. public static readonly string kAddNormalsMessage =
  65. "\nWarning: 'Add Normals' required when not using 'Fixed Normals'!\n\nPlease\n"
  66. + "a) enable 'Add Normals' at the SkeletonRenderer/SkeletonAnimation component under 'Advanced' or\n"
  67. + "b) enable 'Fixed Normals' at the Material.\n";
  68. public static readonly string kSolveTangentsMessage =
  69. "\nWarning: 'Solve Tangents' required when using a Normal Map!\n\nPlease\n"
  70. + "a) enable 'Solve Tangents' at the SkeletonRenderer/SkeletonAnimation component under 'Advanced' or\n"
  71. + "b) clear the 'Normal Map' parameter at the Material.\n";
  72. public static readonly string kNoSkeletonGraphicMaterialMessage =
  73. "\nWarning: Normal non-UI shaders other than 'Spine/SkeletonGraphic *' are not compatible with 'SkeletonGraphic' components! "
  74. + "This will lead to incorrect rendering on some devices.\n\n"
  75. + "Please change the assigned Material to e.g. 'SkeletonGraphicDefault' or change the used shader to one of the 'Spine/SkeletonGraphic *' shaders.\n\n"
  76. + "Note that 'Spine/SkeletonGraphic *' shall still be used when using URP.\n";
  77. public static readonly string kNoSkeletonGraphicTintBlackMaterialMessage =
  78. "\nWarning: Only enable 'Canvas Group Tint Black' when using a 'SkeletonGraphic Tint Black' shader!\n"
  79. + "This will lead to incorrect rendering.\n\nPlease\n"
  80. + "a) disable 'Canvas Group Tint Black' under 'Advanced' or\n"
  81. + "b) use a 'SkeletonGraphic Tint Black' Material if you need Tint Black on a CanvasGroup.\n";
  82. public static readonly string kTintBlackMessage =
  83. "\nWarning: 'Advanced - Tint Black' required when using any 'Tint Black' shader!\n\nPlease\n"
  84. + "a) enable 'Tint Black' at the SkeletonRenderer/SkeletonGraphic component under 'Advanced' or\n"
  85. + "b) use a different shader at the Material.\n";
  86. public static readonly string kCanvasTintBlackMessage =
  87. "\nWarning: Canvas 'Additional Shader Channels' 'uv1' and 'uv2' are required when 'Advanced - Tint Black' is enabled!\n\n"
  88. + "Please enable both 'uv1' and 'uv2' channels at the parent Canvas component parameter 'Additional Shader Channels'.\n";
  89. public static readonly string kCanvasGroupCompatibleMessage =
  90. "\nWarning: 'Canvas Group Tint Black' is enabled at SkeletonGraphic but not 'CanvasGroup Compatible' at the Material!\n\nPlease\n"
  91. + "a) enable 'CanvasGroup Compatible' at the Material or\n"
  92. + "b) disable 'Canvas Group Tint Black' at the SkeletonGraphic component under 'Advanced'.\n"
  93. + "You may want to duplicate the 'SkeletonGraphicDefault' material and change settings at the duplicate to not affect all instances.";
  94. public static bool IsMaterialSetupProblematic (SkeletonRenderer renderer, ref string errorMessage) {
  95. var materials = renderer.GetComponent<Renderer>().sharedMaterials;
  96. bool isProblematic = false;
  97. foreach (var material in materials) {
  98. if (material == null) continue;
  99. isProblematic |= IsMaterialSetupProblematic(material, ref errorMessage);
  100. if (renderer.zSpacing == 0) {
  101. isProblematic |= IsZSpacingRequired(material, ref errorMessage);
  102. }
  103. if (renderer.addNormals == false && RequiresMeshNormals(material)) {
  104. isProblematic = true;
  105. errorMessage += kAddNormalsMessage;
  106. }
  107. if (renderer.calculateTangents == false && RequiresTangents(material)) {
  108. isProblematic = true;
  109. errorMessage += kSolveTangentsMessage;
  110. }
  111. if (renderer.tintBlack == false && RequiresTintBlack(material)) {
  112. isProblematic = true;
  113. errorMessage += kTintBlackMessage;
  114. }
  115. }
  116. return isProblematic;
  117. }
  118. public static bool IsMaterialSetupProblematic (SkeletonGraphic skeletonGraphic, ref string errorMessage) {
  119. var material = skeletonGraphic.material;
  120. bool isProblematic = false;
  121. if (material) {
  122. isProblematic |= IsMaterialSetupProblematic(material, ref errorMessage);
  123. var settings = skeletonGraphic.MeshGenerator.settings;
  124. if (settings.zSpacing == 0) {
  125. isProblematic |= IsZSpacingRequired(material, ref errorMessage);
  126. }
  127. if (IsSpineNonSkeletonGraphicMaterial(material)) {
  128. isProblematic = true;
  129. errorMessage += kNoSkeletonGraphicMaterialMessage;
  130. }
  131. if (settings.tintBlack == false && RequiresTintBlack(material)) {
  132. isProblematic = true;
  133. errorMessage += kTintBlackMessage;
  134. }
  135. if (settings.tintBlack == true && CanvasNotSetupForTintBlack(skeletonGraphic)) {
  136. isProblematic = true;
  137. errorMessage += kCanvasTintBlackMessage;
  138. }
  139. if (settings.canvasGroupTintBlack == true && !IsSkeletonGraphicTintBlackMaterial(material)) {
  140. isProblematic = true;
  141. errorMessage += kNoSkeletonGraphicTintBlackMaterialMessage;
  142. }
  143. if (settings.canvasGroupTintBlack == true && !IsCanvasGroupCompatible(material)) {
  144. isProblematic = true;
  145. errorMessage += kCanvasGroupCompatibleMessage;
  146. }
  147. }
  148. return isProblematic;
  149. }
  150. public static bool IsMaterialSetupProblematic (Material material, ref string errorMessage) {
  151. return !IsColorSpaceSupported(material, ref errorMessage);
  152. }
  153. public static bool IsZSpacingRequired (Material material, ref string errorMessage) {
  154. bool hasForwardAddPass = material.FindPass("FORWARD_DELTA") >= 0;
  155. if (hasForwardAddPass) {
  156. errorMessage += kZSpacingRequiredMessage;
  157. return true;
  158. }
  159. bool zWrite = material.HasProperty("_ZWrite") && material.GetFloat("_ZWrite") > 0.0f;
  160. if (zWrite) {
  161. errorMessage += kZSpacingRecommendedMessage;
  162. return true;
  163. }
  164. return false;
  165. }
  166. public static bool IsColorSpaceSupported (Material material, ref string errorMessage) {
  167. if (QualitySettings.activeColorSpace == ColorSpace.Linear) {
  168. if (IsPMATextureMaterial(material)) {
  169. errorMessage += kPMANotSupportedLinearMessage;
  170. return false;
  171. }
  172. }
  173. return true;
  174. }
  175. public static bool UsesSpineShader (Material material) {
  176. return material.shader.name.Contains("Spine/");
  177. }
  178. public static bool IsTextureSetupProblematic (Material material, ColorSpace colorSpace,
  179. bool sRGBTexture, bool mipmapEnabled, bool alphaIsTransparency,
  180. string texturePath, string materialPath,
  181. ref string errorMessage) {
  182. if (material == null || !UsesSpineShader(material)) {
  183. return false;
  184. }
  185. bool isProblematic = false;
  186. if (IsPMATextureMaterial(material)) {
  187. // 'sRGBTexture = true' generates incorrectly weighted mipmaps at PMA textures,
  188. // causing white borders due to undesired custom weighting.
  189. if (sRGBTexture && mipmapEnabled && colorSpace == ColorSpace.Gamma) {
  190. errorMessage += string.Format("`{0}` : Problematic Texture Settings found: " +
  191. "When enabling `Generate Mip Maps` in Gamma color space, it is recommended " +
  192. "to disable `sRGB (Color Texture)` on `Premultiply alpha` textures. Otherwise " +
  193. "you will receive white border artifacts on an atlas exported with default " +
  194. "`Premultiply alpha` settings.\n" +
  195. "(You can disable this warning in `Edit - Preferences - Spine`)\n", texturePath);
  196. isProblematic = true;
  197. }
  198. if (alphaIsTransparency) {
  199. string materialName = System.IO.Path.GetFileName(materialPath);
  200. errorMessage += string.Format("`{0}` and material `{1}` : Problematic " +
  201. "Texture / Material Settings found: It is recommended to disable " +
  202. "`Alpha Is Transparency` on `Premultiply alpha` textures.\n" +
  203. "Assuming `Premultiply alpha` texture because `Straight Alpha Texture` " +
  204. "is disabled at material). " +
  205. "(You can disable this warning in `Edit - Preferences - Spine`)\n", texturePath, materialName);
  206. isProblematic = true;
  207. }
  208. } else { // straight alpha texture
  209. if (!alphaIsTransparency) {
  210. string materialName = System.IO.Path.GetFileName(materialPath);
  211. errorMessage += string.Format("`{0}` and material `{1}` : Incorrect" +
  212. "Texture / Material Settings found: It is strongly recommended " +
  213. "to enable `Alpha Is Transparency` on `Straight alpha` textures.\n" +
  214. "Assuming `Straight alpha` texture because `Straight Alpha Texture` " +
  215. "is enabled at material). " +
  216. "(You can disable this warning in `Edit - Preferences - Spine`)\n", texturePath, materialName);
  217. isProblematic = true;
  218. }
  219. }
  220. return isProblematic;
  221. }
  222. public static void EnablePMATextureAtMaterial (Material material, bool enablePMATexture) {
  223. if (material.HasProperty(STRAIGHT_ALPHA_PARAM_ID)) {
  224. material.SetInt(STRAIGHT_ALPHA_PARAM_ID, enablePMATexture ? 0 : 1);
  225. if (enablePMATexture)
  226. material.DisableKeyword(STRAIGHT_ALPHA_KEYWORD);
  227. else
  228. material.EnableKeyword(STRAIGHT_ALPHA_KEYWORD);
  229. } else {
  230. if (enablePMATexture) {
  231. material.DisableKeyword(ALPHAPREMULTIPLY_ON_KEYWORD);
  232. material.DisableKeyword(ALPHABLEND_ON_KEYWORD);
  233. material.EnableKeyword(ALPHAPREMULTIPLY_VERTEX_ONLY_ON_KEYWORD);
  234. } else {
  235. material.DisableKeyword(ALPHAPREMULTIPLY_ON_KEYWORD);
  236. material.DisableKeyword(ALPHAPREMULTIPLY_VERTEX_ONLY_ON_KEYWORD);
  237. material.EnableKeyword(ALPHABLEND_ON_KEYWORD);
  238. }
  239. }
  240. }
  241. public static bool IsPMATextureMaterial (Material material) {
  242. bool usesAlphaPremultiplyKeyword = IsSpriteShader(material);
  243. if (usesAlphaPremultiplyKeyword)
  244. return material.IsKeywordEnabled(ALPHAPREMULTIPLY_ON_KEYWORD);
  245. else
  246. return material.HasProperty(STRAIGHT_ALPHA_PARAM_ID) && material.GetInt(STRAIGHT_ALPHA_PARAM_ID) == 0;
  247. }
  248. static bool IsURP3DMaterial (Material material) {
  249. return material.shader.name.Contains("Universal Render Pipeline/Spine");
  250. }
  251. static bool IsSpineNonSkeletonGraphicMaterial (Material material) {
  252. return material.shader.name.Contains("Spine") && !material.shader.name.Contains("SkeletonGraphic");
  253. }
  254. static bool IsSkeletonGraphicTintBlackMaterial (Material material) {
  255. return material.shader.name.Contains("Spine") && material.shader.name.Contains("SkeletonGraphic")
  256. && material.shader.name.Contains("Black");
  257. }
  258. static bool AreShadowsDisabled (Material material) {
  259. return material.IsKeywordEnabled("_RECEIVE_SHADOWS_OFF");
  260. }
  261. static bool RequiresMeshNormals (Material material) {
  262. bool anyFixedNormalSet = false;
  263. foreach (string fixedNormalKeyword in FIXED_NORMALS_KEYWORDS) {
  264. if (material.IsKeywordEnabled(fixedNormalKeyword)) {
  265. anyFixedNormalSet = true;
  266. break;
  267. }
  268. }
  269. bool isShaderWithMeshNormals = IsLitSpriteShader(material);
  270. return isShaderWithMeshNormals && !anyFixedNormalSet;
  271. }
  272. static bool IsLitSpriteShader (Material material) {
  273. string shaderName = material.shader.name;
  274. return shaderName.Contains("Spine/Sprite/Pixel Lit") ||
  275. shaderName.Contains("Spine/Sprite/Vertex Lit") ||
  276. shaderName.Contains("2D/Spine/Sprite") || // covers both URP and LWRP
  277. shaderName.Contains("Pipeline/Spine/Sprite"); // covers both URP and LWRP
  278. }
  279. static bool IsSpriteShader (Material material) {
  280. if (IsLitSpriteShader(material))
  281. return true;
  282. string shaderName = material.shader.name;
  283. return shaderName.Contains("Spine/Sprite/Unlit");
  284. }
  285. static bool RequiresTintBlack (Material material) {
  286. bool isTintBlackShader =
  287. material.shader.name.Contains("Spine") &&
  288. material.shader.name.Contains("Tint Black");
  289. return isTintBlackShader;
  290. }
  291. static bool RequiresTangents (Material material) {
  292. return material.IsKeywordEnabled(NORMALMAP_KEYWORD);
  293. }
  294. static bool IsCanvasGroupCompatible (Material material) {
  295. return material.IsKeywordEnabled(CANVAS_GROUP_COMPATIBLE_KEYWORD);
  296. }
  297. static bool CanvasNotSetupForTintBlack (SkeletonGraphic skeletonGraphic) {
  298. Canvas canvas = skeletonGraphic.canvas;
  299. if (!canvas)
  300. return false;
  301. var requiredChannels =
  302. AdditionalCanvasShaderChannels.TexCoord1 |
  303. AdditionalCanvasShaderChannels.TexCoord2;
  304. return (canvas.additionalShaderChannels & requiredChannels) != requiredChannels;
  305. }
  306. }
  307. }
  308. #endif // UNITY_EDITOR