using o0.Geometry; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UnityEngine; using UnityEngine.UI; namespace o0.Project { public enum ColorChannel { Red, Green, Blue, } public static partial class Extension { static public Matrix Too0Mat(this WebCamTexture Tex, ColorChannel channel = ColorChannel.Red) { Matrix mat = new Matrix(new Geometry2D.Vector(Tex.width, Tex.height)); var length = Tex.width * Tex.height; UnityEngine.Color[] pixel = Tex.GetPixels(); switch (channel) { default: case ColorChannel.Red: Parallel.For(0, length, i => mat.Element[i] = pixel[i].r); break; case ColorChannel.Green: Parallel.For(0, length, i => mat.Element[i] = pixel[i].g); break; case ColorChannel.Blue: Parallel.For(0, length, i => mat.Element[i] = pixel[i].b); break; } return mat; } static public Matrix Too0Mat(this Texture2D Tex, ColorChannel channel = ColorChannel.Red) { Matrix mat = new Matrix(new Geometry2D.Vector(Tex.width, Tex.height)); var length = Tex.width * Tex.height; UnityEngine.Color[] pixel = Tex.GetPixels(); switch (channel) { default: case ColorChannel.Red: Parallel.For(0, length, i => mat.Element[i] = pixel[i].r); break; case ColorChannel.Green: Parallel.For(0, length, i => mat.Element[i] = pixel[i].g); break; case ColorChannel.Blue: Parallel.For(0, length, i => mat.Element[i] = pixel[i].b); break; } return mat; } static public Texture2D ToTex(this Matrix mat, ColorChannel channel = ColorChannel.Red) { var length = mat.Size.x * mat.Size.y; var ele = mat.Element; UnityEngine.Color[] pixel = new UnityEngine.Color[length]; switch (channel) { default: case ColorChannel.Red: Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(ele[i], 0, 0)); break; case ColorChannel.Green: Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(0, ele[i], 0)); break; case ColorChannel.Blue: Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(0, 0, ele[i])); break; } var tex = new Texture2D(mat.Size.x, mat.Size.y); tex.SetPixels(pixel); tex.Apply(); return tex; } static public Texture2D ToTexRGBA(this Matrix mat, Func ToColorFunc) { var length = mat.Size.x * mat.Size.y; var ele = mat.Element; UnityEngine.Color[] pixel = new UnityEngine.Color[length]; Parallel.For(0, length, i => pixel[i] = ToColorFunc(ele[i])); var tex = new Texture2D(mat.Size.x, mat.Size.y); tex.SetPixels(pixel); tex.Apply(); return tex; } static public Texture2D ToRGB(this Texture2D Tex, ColorChannel channel = ColorChannel.Red) { var length = Tex.width * Tex.height; UnityEngine.Color[] ele = Tex.GetPixels(); UnityEngine.Color[] pixel = new UnityEngine.Color[length]; switch (channel) { default: case ColorChannel.Red: Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(ele[i].r, 0, 0)); break; case ColorChannel.Green: Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(0, ele[i].g, 0)); break; case ColorChannel.Blue: Parallel.For(0, length, i => pixel[i] = new UnityEngine.Color(0, 0, ele[i].b)); break; } var newTex = new Texture2D(Tex.width, Tex.height); newTex.SetPixels(pixel); newTex.Apply(); return newTex; } } }