o0Extension.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 Matrix Too0Mat(this WebCamTexture Tex, ColorChannel channel = ColorChannel.Red)
  20. {
  21. Matrix mat = new Matrix(new Geometry2D.Vector<int>(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 Matrix Too0Mat(this Texture2D Tex, ColorChannel channel = ColorChannel.Red)
  40. {
  41. Matrix mat = new Matrix(new Geometry2D.Vector<int>(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 Matrix mat, ColorChannel channel = ColorChannel.Red)
  60. {
  61. var length = mat.Size.x * mat.Size.y;
  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.Size.x, mat.Size.y);
  78. tex.SetPixels(pixel);
  79. tex.Apply();
  80. return tex;
  81. }
  82. static public Texture2D ToTexRGBA(this Matrix mat, Func<float, UnityEngine.Color> ToColorFunc)
  83. {
  84. var length = mat.Size.x * mat.Size.y;
  85. var ele = mat.Element;
  86. UnityEngine.Color[] pixel = new UnityEngine.Color[length];
  87. Parallel.For(0, length, i => pixel[i] = ToColorFunc(ele[i]));
  88. var tex = new Texture2D(mat.Size.x, mat.Size.y);
  89. tex.SetPixels(pixel);
  90. tex.Apply();
  91. return tex;
  92. }
  93. static public Texture2D ToRGB(this Texture2D Tex, ColorChannel channel = ColorChannel.Red)
  94. {
  95. var length = Tex.width * Tex.height;
  96. UnityEngine.Color[] ele = Tex.GetPixels();
  97. UnityEngine.Color[] pixel = new UnityEngine.Color[length];
  98. switch (channel)
  99. {
  100. default:
  101. case ColorChannel.Red:
  102. Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(ele[i].r, 0, 0));
  103. break;
  104. case ColorChannel.Green:
  105. Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(0, ele[i].g, 0));
  106. break;
  107. case ColorChannel.Blue:
  108. Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(0, 0, ele[i].b));
  109. break;
  110. }
  111. var newTex = new Texture2D(Tex.width, Tex.height);
  112. newTex.SetPixels(pixel);
  113. newTex.Apply();
  114. return newTex;
  115. }
  116. }
  117. }