Material.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Urho.Resources;
  4. using Urho.Urho2D;
  5. namespace Urho
  6. {
  7. partial class Material
  8. {
  9. public static Material FromImage(string image, string normals)
  10. {
  11. if (string.IsNullOrEmpty(normals))
  12. return FromImage(image);
  13. var cache = Application.Current.ResourceCache;
  14. var diff = cache.GetTexture2D(image);
  15. if (diff == null)
  16. return null;
  17. var normal = cache.GetTexture2D(normals);
  18. if (normal == null)
  19. return FromImage(image);
  20. var material = new Material();
  21. material.SetTexture(TextureUnit.Diffuse, diff);
  22. material.SetTexture(TextureUnit.Normal, normal);
  23. material.SetTechnique(0, CoreAssets.Techniques.DiffNormal, 0, 0);
  24. return material;
  25. }
  26. public static Material FromImage(string image)
  27. {
  28. var cache = Application.Current.ResourceCache;
  29. var diff = cache.GetTexture2D(image);
  30. if (diff == null)
  31. return null;
  32. var material = new Material();
  33. material.SetTexture(TextureUnit.Diffuse, diff);
  34. material.SetTechnique(0, CoreAssets.Techniques.Diff, 0, 0);
  35. return material;
  36. }
  37. public static Material FromImage(Image image, bool useAlpha = false)
  38. {
  39. var texture = new Texture2D();
  40. texture.SetData(image, useAlpha);
  41. var material = new Material();
  42. material.SetTechnique(0, CoreAssets.Techniques.Diff, 0, 0);
  43. material.SetTexture(TextureUnit.Diffuse, texture);
  44. return material;
  45. }
  46. public static Material FromColor(Color color)
  47. {
  48. return FromColor(color, false);
  49. }
  50. public static Material FromColor(Color color, bool unlit)
  51. {
  52. var material = new Material();
  53. var cache = Application.Current.ResourceCache;
  54. float tolerance = 0.001f;
  55. if (unlit)
  56. material.SetTechnique(0, Math.Abs(color.A - 1) < tolerance ? CoreAssets.Techniques.NoTextureUnlit : CoreAssets.Techniques.NoTextureUnlitAlpha, 1, 1);
  57. else
  58. material.SetTechnique(0, Math.Abs(color.A - 1) < tolerance ? CoreAssets.Techniques.NoTexture : CoreAssets.Techniques.NoTextureAlpha, 1, 1);
  59. material.SetShaderParameter("MatDiffColor", color);
  60. return material;
  61. }
  62. public static Material SkyboxFromImages(
  63. string imagePositiveX,
  64. string imageNegativeX,
  65. string imagePositiveY,
  66. string imageNegativeY,
  67. string imagePositiveZ,
  68. string imageNegativeZ)
  69. {
  70. var cache = Application.Current.ResourceCache;
  71. var material = new Material();
  72. TextureCube cube = new TextureCube();
  73. cube.SetData(CubeMapFace.PositiveX, cache.GetFile(imagePositiveX, false));
  74. cube.SetData(CubeMapFace.NegativeX, cache.GetFile(imageNegativeX, false));
  75. cube.SetData(CubeMapFace.PositiveY, cache.GetFile(imagePositiveY, false));
  76. cube.SetData(CubeMapFace.NegativeY, cache.GetFile(imageNegativeY, false));
  77. cube.SetData(CubeMapFace.PositiveZ, cache.GetFile(imagePositiveZ, false));
  78. cube.SetData(CubeMapFace.NegativeZ, cache.GetFile(imageNegativeZ, false));
  79. material.SetTexture(TextureUnit.Diffuse, cube);
  80. material.SetTechnique(0, CoreAssets.Techniques.DiffSkybox, 0, 0);
  81. material.CullMode = CullMode.None;
  82. return material;
  83. }
  84. public static Material SkyboxFromImage(string image)
  85. {
  86. return SkyboxFromImages(image, image, image, image, image, image);
  87. }
  88. //TODO: Surface Variant& GetShaderParameter
  89. [DllImport(Consts.NativeImport, CallingConvention = CallingConvention.Cdecl)]
  90. static extern Color Material_GetShaderParameterColor(IntPtr target, string paramName);
  91. public Color GetShaderParameter(string parameterName)
  92. {
  93. return Material_GetShaderParameterColor(Handle, parameterName);
  94. }
  95. }
  96. }