MaterialChecks.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 STRAIGHT_ALPHA_KEYWORD = "_STRAIGHT_ALPHA_INPUT";
  38. static readonly string[] FIXED_NORMALS_KEYWORDS = {
  39. "_FIXED_NORMALS_VIEWSPACE",
  40. "_FIXED_NORMALS_VIEWSPACE_BACKFACE",
  41. "_FIXED_NORMALS_MODELSPACE",
  42. "_FIXED_NORMALS_MODELSPACE_BACKFACE",
  43. "_FIXED_NORMALS_WORLDSPACE"
  44. };
  45. static readonly string NORMALMAP_KEYWORD = "_NORMALMAP";
  46. static readonly string CANVAS_GROUP_COMPATIBLE_KEYWORD = "_CANVAS_GROUP_COMPATIBLE";
  47. public static readonly string kPMANotSupportedLinearMessage =
  48. "\nWarning: Premultiply-alpha atlas textures not supported in Linear color space!\n\nPlease\n"
  49. + "a) re-export atlas as straight alpha texture with 'premultiply alpha' unchecked\n"
  50. + " (if you have already done this, please set the 'Straight Alpha Texture' Material parameter to 'true') or\n"
  51. + "b) switch to Gamma color space via\nProject Settings - Player - Other Settings - Color Space.\n";
  52. public static readonly string kZSpacingRequiredMessage =
  53. "\nWarning: Z Spacing required on selected shader! Otherwise you will receive incorrect results.\n\nPlease\n"
  54. + "1) make sure at least minimal 'Z Spacing' is set at the SkeletonRenderer/SkeletonAnimation component under 'Advanced' and\n"
  55. + "2) ensure that the skeleton has overlapping parts on different Z depth. You can adjust this in Spine via draw order.\n";
  56. public static readonly string kZSpacingRecommendedMessage =
  57. "\nWarning: Z Spacing recommended on selected shader configuration!\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 kAddNormalsMessage =
  61. "\nWarning: 'Add Normals' required when not using 'Fixed Normals'!\n\nPlease\n"
  62. + "a) enable 'Add Normals' at the SkeletonRenderer/SkeletonAnimation component under 'Advanced' or\n"
  63. + "b) enable 'Fixed Normals' at the Material.\n";
  64. public static readonly string kSolveTangentsMessage =
  65. "\nWarning: 'Solve Tangents' required when using a Normal Map!\n\nPlease\n"
  66. + "a) enable 'Solve Tangents' at the SkeletonRenderer/SkeletonAnimation component under 'Advanced' or\n"
  67. + "b) clear the 'Normal Map' parameter at the Material.\n";
  68. public static readonly string kNoSkeletonGraphicMaterialMessage =
  69. "\nWarning: Normal non-UI shaders other than 'Spine/SkeletonGraphic *' are not compatible with 'SkeletonGraphic' components! "
  70. + "This will lead to incorrect rendering on some devices.\n\n"
  71. + "Please change the assigned Material to e.g. 'SkeletonGraphicDefault' or change the used shader to one of the 'Spine/SkeletonGraphic *' shaders.\n\n"
  72. + "Note that 'Spine/SkeletonGraphic *' shall still be used when using URP.\n";
  73. public static readonly string kNoSkeletonGraphicTintBlackMaterialMessage =
  74. "\nWarning: Only enable 'Canvas Group Tint Black' when using a 'SkeletonGraphic Tint Black' shader!\n"
  75. + "This will lead to incorrect rendering.\n\nPlease\n"
  76. + "a) disable 'Canvas Group Tint Black' under 'Advanced' or\n"
  77. + "b) use a 'SkeletonGraphic Tint Black' Material if you need Tint Black on a CanvasGroup.\n";
  78. public static readonly string kTintBlackMessage =
  79. "\nWarning: 'Advanced - Tint Black' required when using any 'Tint Black' shader!\n\nPlease\n"
  80. + "a) enable 'Tint Black' at the SkeletonRenderer/SkeletonGraphic component under 'Advanced' or\n"
  81. + "b) use a different shader at the Material.\n";
  82. public static readonly string kCanvasTintBlackMessage =
  83. "\nWarning: Canvas 'Additional Shader Channels' 'uv1' and 'uv2' are required when 'Advanced - Tint Black' is enabled!\n\n"
  84. + "Please enable both 'uv1' and 'uv2' channels at the parent Canvas component parameter 'Additional Shader Channels'.\n";
  85. public static readonly string kCanvasGroupCompatibleMessage =
  86. "\nWarning: 'Canvas Group Tint Black' is enabled at SkeletonGraphic but not 'CanvasGroup Compatible' at the Material!\n\nPlease\n"
  87. + "a) enable 'CanvasGroup Compatible' at the Material or\n"
  88. + "b) disable 'Canvas Group Tint Black' at the SkeletonGraphic component under 'Advanced'.\n"
  89. + "You may want to duplicate the 'SkeletonGraphicDefault' material and change settings at the duplicate to not affect all instances.";
  90. public static bool IsMaterialSetupProblematic (SkeletonRenderer renderer, ref string errorMessage) {
  91. var materials = renderer.GetComponent<Renderer>().sharedMaterials;
  92. bool isProblematic = false;
  93. foreach (var material in materials) {
  94. if (material == null) continue;
  95. isProblematic |= IsMaterialSetupProblematic(material, ref errorMessage);
  96. if (renderer.zSpacing == 0) {
  97. isProblematic |= IsZSpacingRequired(material, ref errorMessage);
  98. }
  99. if (renderer.addNormals == false && RequiresMeshNormals(material)) {
  100. isProblematic = true;
  101. errorMessage += kAddNormalsMessage;
  102. }
  103. if (renderer.calculateTangents == false && RequiresTangents(material)) {
  104. isProblematic = true;
  105. errorMessage += kSolveTangentsMessage;
  106. }
  107. if (renderer.tintBlack == false && RequiresTintBlack(material)) {
  108. isProblematic = true;
  109. errorMessage += kTintBlackMessage;
  110. }
  111. }
  112. return isProblematic;
  113. }
  114. public static bool IsMaterialSetupProblematic(SkeletonGraphic skeletonGraphic, ref string errorMessage)
  115. {
  116. var material = skeletonGraphic.material;
  117. bool isProblematic = false;
  118. if (material) {
  119. isProblematic |= IsMaterialSetupProblematic(material, ref errorMessage);
  120. var settings = skeletonGraphic.MeshGenerator.settings;
  121. if (settings.zSpacing == 0) {
  122. isProblematic |= IsZSpacingRequired(material, ref errorMessage);
  123. }
  124. if (IsSpineNonSkeletonGraphicMaterial(material)) {
  125. isProblematic = true;
  126. errorMessage += kNoSkeletonGraphicMaterialMessage;
  127. }
  128. if (settings.tintBlack == false && RequiresTintBlack(material)) {
  129. isProblematic = true;
  130. errorMessage += kTintBlackMessage;
  131. }
  132. if (settings.tintBlack == true && CanvasNotSetupForTintBlack(skeletonGraphic)) {
  133. isProblematic = true;
  134. errorMessage += kCanvasTintBlackMessage;
  135. }
  136. if (settings.canvasGroupTintBlack == true && !IsSkeletonGraphicTintBlackMaterial(material)) {
  137. isProblematic = true;
  138. errorMessage += kNoSkeletonGraphicTintBlackMaterialMessage;
  139. }
  140. if (settings.canvasGroupTintBlack == true && !IsCanvasGroupCompatible(material)) {
  141. isProblematic = true;
  142. errorMessage += kCanvasGroupCompatibleMessage;
  143. }
  144. }
  145. return isProblematic;
  146. }
  147. public static bool IsMaterialSetupProblematic(Material material, ref string errorMessage) {
  148. return !IsColorSpaceSupported(material, ref errorMessage);
  149. }
  150. public static bool IsZSpacingRequired(Material material, ref string errorMessage) {
  151. bool hasForwardAddPass = material.FindPass("FORWARD_DELTA") >= 0;
  152. if (hasForwardAddPass) {
  153. errorMessage += kZSpacingRequiredMessage;
  154. return true;
  155. }
  156. bool zWrite = material.HasProperty("_ZWrite") && material.GetFloat("_ZWrite") > 0.0f;
  157. if (zWrite) {
  158. errorMessage += kZSpacingRecommendedMessage;
  159. return true;
  160. }
  161. return false;
  162. }
  163. public static bool IsColorSpaceSupported (Material material, ref string errorMessage) {
  164. if (QualitySettings.activeColorSpace == ColorSpace.Linear) {
  165. if (IsPMAMaterial(material)) {
  166. errorMessage += kPMANotSupportedLinearMessage;
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. public static bool UsesSpineShader (Material material) {
  173. return material.shader.name.Contains("Spine/");
  174. }
  175. public static bool IsTextureSetupProblematic (Material material, ColorSpace colorSpace,
  176. bool sRGBTexture, bool mipmapEnabled, bool alphaIsTransparency,
  177. string texturePath, string materialPath,
  178. ref string errorMessage) {
  179. if (material == null || !UsesSpineShader(material)) {
  180. return false;
  181. }
  182. bool isProblematic = false;
  183. if (IsPMAMaterial(material)) {
  184. // 'sRGBTexture = true' generates incorrectly weighted mipmaps at PMA textures,
  185. // causing white borders due to undesired custom weighting.
  186. if (sRGBTexture && mipmapEnabled && colorSpace == ColorSpace.Gamma) {
  187. errorMessage += string.Format("`{0}` : Problematic Texture Settings found: " +
  188. "When enabling `Generate Mip Maps` in Gamma color space, it is recommended " +
  189. "to disable `sRGB (Color Texture)` on `Premultiply alpha` textures. Otherwise " +
  190. "you will receive white border artifacts on an atlas exported with default " +
  191. "`Premultiply alpha` settings.\n" +
  192. "(You can disable this warning in `Edit - Preferences - Spine`)\n", texturePath);
  193. isProblematic = true;
  194. }
  195. if (alphaIsTransparency) {
  196. string materialName = System.IO.Path.GetFileName(materialPath);
  197. errorMessage += string.Format("`{0}` and material `{1}` : Problematic " +
  198. "Texture / Material Settings found: It is recommended to disable " +
  199. "`Alpha Is Transparency` on `Premultiply alpha` textures.\n" +
  200. "Assuming `Premultiply alpha` texture because `Straight Alpha Texture` " +
  201. "is disabled at material). " +
  202. "(You can disable this warning in `Edit - Preferences - Spine`)\n", texturePath, materialName);
  203. isProblematic = true;
  204. }
  205. }
  206. else { // straight alpha texture
  207. if (!alphaIsTransparency) {
  208. string materialName = System.IO.Path.GetFileName(materialPath);
  209. errorMessage += string.Format("`{0}` and material `{1}` : Incorrect" +
  210. "Texture / Material Settings found: It is strongly recommended " +
  211. "to enable `Alpha Is Transparency` on `Straight alpha` textures.\n" +
  212. "Assuming `Straight alpha` texture because `Straight Alpha Texture` " +
  213. "is enabled at material). " +
  214. "(You can disable this warning in `Edit - Preferences - Spine`)\n", texturePath, materialName);
  215. isProblematic = true;
  216. }
  217. }
  218. return isProblematic;
  219. }
  220. public static void EnablePMAAtMaterial (Material material, bool enablePMA) {
  221. if (material.HasProperty(STRAIGHT_ALPHA_PARAM_ID)) {
  222. material.SetInt(STRAIGHT_ALPHA_PARAM_ID, enablePMA ? 0 : 1);
  223. if (enablePMA)
  224. material.DisableKeyword(STRAIGHT_ALPHA_KEYWORD);
  225. else
  226. material.EnableKeyword(STRAIGHT_ALPHA_KEYWORD);
  227. }
  228. else {
  229. if (enablePMA)
  230. material.EnableKeyword(ALPHAPREMULTIPLY_ON_KEYWORD);
  231. else
  232. material.DisableKeyword(ALPHAPREMULTIPLY_ON_KEYWORD);
  233. }
  234. }
  235. static bool IsPMAMaterial (Material material) {
  236. bool usesAlphaPremultiplyKeyword = IsSpriteShader(material);
  237. if (usesAlphaPremultiplyKeyword)
  238. return material.IsKeywordEnabled(ALPHAPREMULTIPLY_ON_KEYWORD);
  239. else
  240. return material.HasProperty(STRAIGHT_ALPHA_PARAM_ID) && material.GetInt(STRAIGHT_ALPHA_PARAM_ID) == 0;
  241. }
  242. static bool IsURP3DMaterial (Material material) {
  243. return material.shader.name.Contains("Universal Render Pipeline/Spine");
  244. }
  245. static bool IsSpineNonSkeletonGraphicMaterial (Material material) {
  246. return material.shader.name.Contains("Spine") && !material.shader.name.Contains("SkeletonGraphic");
  247. }
  248. static bool IsSkeletonGraphicTintBlackMaterial (Material material) {
  249. return material.shader.name.Contains("Spine") && material.shader.name.Contains("SkeletonGraphic")
  250. && material.shader.name.Contains("Black");
  251. }
  252. static bool AreShadowsDisabled (Material material) {
  253. return material.IsKeywordEnabled("_RECEIVE_SHADOWS_OFF");
  254. }
  255. static bool RequiresMeshNormals (Material material) {
  256. bool anyFixedNormalSet = false;
  257. foreach (string fixedNormalKeyword in FIXED_NORMALS_KEYWORDS) {
  258. if (material.IsKeywordEnabled(fixedNormalKeyword)) {
  259. anyFixedNormalSet = true;
  260. break;
  261. }
  262. }
  263. bool isShaderWithMeshNormals = IsSpriteShader(material);
  264. return isShaderWithMeshNormals && !anyFixedNormalSet;
  265. }
  266. static bool IsSpriteShader (Material material) {
  267. string shaderName = material.shader.name;
  268. return shaderName.Contains("Spine/Sprite/Pixel Lit") ||
  269. shaderName.Contains("Spine/Sprite/Vertex Lit") ||
  270. shaderName.Contains("2D/Spine/Sprite") || // covers both URP and LWRP
  271. shaderName.Contains("Pipeline/Spine/Sprite"); // covers both URP and LWRP
  272. }
  273. static bool RequiresTintBlack (Material material) {
  274. bool isTintBlackShader =
  275. material.shader.name.Contains("Spine") &&
  276. material.shader.name.Contains("Tint Black");
  277. return isTintBlackShader;
  278. }
  279. static bool RequiresTangents (Material material) {
  280. return material.IsKeywordEnabled(NORMALMAP_KEYWORD);
  281. }
  282. static bool IsCanvasGroupCompatible (Material material) {
  283. return material.IsKeywordEnabled(CANVAS_GROUP_COMPATIBLE_KEYWORD);
  284. }
  285. static bool CanvasNotSetupForTintBlack (SkeletonGraphic skeletonGraphic) {
  286. Canvas canvas = skeletonGraphic.canvas;
  287. if (!canvas)
  288. return false;
  289. var requiredChannels =
  290. AdditionalCanvasShaderChannels.TexCoord1 |
  291. AdditionalCanvasShaderChannels.TexCoord2;
  292. return (canvas.additionalShaderChannels & requiredChannels) != requiredChannels;
  293. }
  294. }
  295. }
  296. #endif // UNITY_EDITOR