o0Extension.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using o0.Geometry;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. namespace o0.Project
  10. {
  11. public enum ColorChannel
  12. {
  13. Red,
  14. Green,
  15. Blue,
  16. }
  17. public static partial class Extension
  18. {
  19. static public MatrixF2D Too0Mat(this WebCamTexture Tex, ColorChannel channel = ColorChannel.Red)
  20. {
  21. MatrixF2D mat = new MatrixF2D(Tex.width, Tex.height);
  22. var length = Tex.width * Tex.height;
  23. UnityEngine.Color[] pixel = Tex.GetPixels();
  24. switch (channel)
  25. {
  26. default:
  27. case ColorChannel.Red:
  28. Parallel.For(0, length, i => mat.Element[i] = pixel[i].r);
  29. break;
  30. case ColorChannel.Green:
  31. Parallel.For(0, length, i => mat.Element[i] = pixel[i].g);
  32. break;
  33. case ColorChannel.Blue:
  34. Parallel.For(0, length, i => mat.Element[i] = pixel[i].b);
  35. break;
  36. }
  37. return mat;
  38. }
  39. static public MatrixF2D Too0Mat(this Texture2D Tex, ColorChannel channel = ColorChannel.Red)
  40. {
  41. MatrixF2D mat = new MatrixF2D(Tex.width, Tex.height);
  42. var length = Tex.width * Tex.height;
  43. UnityEngine.Color[] pixel = Tex.GetPixels();
  44. switch (channel)
  45. {
  46. default:
  47. case ColorChannel.Red:
  48. Parallel.For(0, length, i => mat.Element[i] = pixel[i].r);
  49. break;
  50. case ColorChannel.Green:
  51. Parallel.For(0, length, i => mat.Element[i] = pixel[i].g);
  52. break;
  53. case ColorChannel.Blue:
  54. Parallel.For(0, length, i => mat.Element[i] = pixel[i].b);
  55. break;
  56. }
  57. return mat;
  58. }
  59. static public Texture2D ToTex(this MatrixF2D mat, ColorChannel channel = ColorChannel.Red)
  60. {
  61. var length = mat.Width * mat.Height;
  62. var ele = mat.Element;
  63. UnityEngine.Color[] pixel = new UnityEngine.Color[length];
  64. switch (channel)
  65. {
  66. default:
  67. case ColorChannel.Red:
  68. Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(ele[i], 0, 0));
  69. break;
  70. case ColorChannel.Green:
  71. Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(0, ele[i], 0));
  72. break;
  73. case ColorChannel.Blue:
  74. Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(0, 0, ele[i]));
  75. break;
  76. }
  77. var tex = new Texture2D(mat.Width, mat.Height);
  78. tex.SetPixels(pixel);
  79. tex.Apply();
  80. return tex;
  81. }
  82. static public Texture2D ToRGB(this Texture2D Tex, ColorChannel channel = ColorChannel.Red)
  83. {
  84. var length = Tex.width * Tex.height;
  85. UnityEngine.Color[] ele = Tex.GetPixels();
  86. UnityEngine.Color[] pixel = new UnityEngine.Color[length];
  87. switch (channel)
  88. {
  89. default:
  90. case ColorChannel.Red:
  91. Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(ele[i].r, 0, 0));
  92. break;
  93. case ColorChannel.Green:
  94. Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(0, ele[i].g, 0));
  95. break;
  96. case ColorChannel.Blue:
  97. Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(0, 0, ele[i].b));
  98. break;
  99. }
  100. var newTex = new Texture2D(Tex.width, Tex.height);
  101. newTex.SetPixels(pixel);
  102. newTex.Apply();
  103. return newTex;
  104. }
  105. }
  106. }