unity黑白滤镜_Unity图片处理类,包括压缩、截屏和滤镜
http://www.cnblogs.com/EndOfYear/p/4334952.html
先上代码:
using System.Threading;
using UnityEngine;
using System.IO;
using System.Collections;
public class TextureUtility
{
public class ThreadData
{
public int start;
public int end;
public ThreadData (int s, int e) {
start = s;
end = e;
}
}
private static Color[] texColors;
private static Color[] newColors;
private static int w;
private static float ratioX;
private static float ratioY;
private static int w2;
private static int finishCount;
private static Mutex mutex;
public static void ScalePoint (Texture2D tex, int newWidth, int newHeight)
{
ThreadedScale (tex, newWidth, newHeight, false);
}
public static void ScaleBilinear (Texture2D tex, int newWidth, int newHeight)
{
ThreadedScale (tex, newWidth, newHeight, true);
}
private static void ThreadedScale (Texture2D tex, int newWidth, int newHeight, bool useBilinear)
{
texColors = tex.GetPixels();
newColors = new Color[newWidth * newHeight];
if (useBilinear)
{
ratioX = 1.0f / ((float)newWidth / (tex.width-1));
ratioY = 1.0f / ((float)newHeight / (tex.height-1));
}
else {
ratioX = ((float)tex.width) / newWidth;
ratioY = ((float)tex.height) / newHeight;
}
w = tex.width;
w2 = newWidth;
var cores = Mathf.Min(SystemInfo.processorCount, newHeight);
var slice = newHeight/cores;
finishCount = 0;
if (mutex == null) {
mutex = new Mutex(false);
}
if (cores > 1)
{
int i = 0;
ThreadData threadData;
for (i = 0; i < cores-1; i++) {
threadData = new ThreadData(slice * i, slice * (i + 1));
ParameterizedThreadStart ts = useBilinear ? new ParameterizedThreadStart(BilinearScale) : new ParameterizedThreadStart(PointScale);
Thread thread = new Thread(ts);
thread.Start(threadData);
}
threadData = new ThreadData(slice*i, newHeight);
if (useBilinear)
{
BilinearScale(threadData);
}
else
{
PointScale(threadData);
}
while (finishCount < cores)
{
Thread.Sleep(1);
}
}
else
{
ThreadData threadData = new ThreadData(0, newHeight);
if (useBilinear)
{
BilinearScale(threadData);
}
else
{
PointScale(threadData);
}
}
tex.Resize(newWidth, newHeight);
tex.SetPixels(newColors);
tex.Apply();
}
public static void BilinearScale (System.Object obj)
{
ThreadData threadData = (ThreadData) obj;
for (var y = threadData.start; y < threadData.end; y++)
{
int yFloor = (int)Mathf.Floor(y * ratioY);
var y1 = yFloor * w;
var y2 = (yFloor+1) * w;
var yw = y * w2;
for (var x = 0; x < w2; x++) {
int xFloor = (int)Mathf.Floor(x * ratioX);
var xLerp = x * ratioX-xFloor;
newColors[yw + x] = ColorLerpUnclamped(ColorLerpUnclamped(texColors[y1 + xFloor], texColors[y1 + xFloor+1], xLerp),
ColorLerpUnclamped(texColors[y2 + xFloor], texColors[y2 + xFloor+1], xLerp),
y*ratioY-yFloor);
}
}
mutex.WaitOne();
finishCount++;
mutex.ReleaseMutex();
}
public static void PointScale (System.Object obj)
{
ThreadData threadData = (ThreadData) obj;
for (var y = threadData.start; y < threadData.end; y++)
{
var thisY = (int)(ratioY * y) * w;
var yw = y * w2;
for (var x = 0; x < w2; x++) {
newColors[yw + x] = texColors[(int)(thisY + ratioX*x)];
}
}
mutex.WaitOne();
finishCount++;
mutex.ReleaseMutex();
}
private static Color ColorLerpUnclamped (Color c1, Color c2, float value)
{
return new Color (c1.r + (c2.r - c1.r)*value,
c1.g + (c2.g - c1.g)*value,
c1.b + (c2.b - c1.b)*value,
c1.a + (c2.a - c1.a)*value);
}
public static Texture2D LoadTexture(string filePath) {
Texture2D tex = null;
byte[] fileData;
if (File.Exists(filePath)) {
fileData = File.ReadAllBytes(filePath);
tex = new Texture2D(2, 2,TextureFormat.ARGB32,false);
tex.LoadImage(fileData);
}
return tex;
}
// Save ScreenShot
public static void SaveScreenShotAsync(string path, Vector2 size)
{
SWMain.sharedSWMain.StartCoroutine(TextureUtility.SaveScreenShot(path, size));
}
public static void SaveScreenShotAsync(string path, Rect rect, Vector2 size)
{
SWMain.sharedSWMain.StartCoroutine(TextureUtility.SaveScreenShot(path, rect, size));
}
public static IEnumerator SaveScreenShot(string path, Vector2 size)
{
Rect rect = new Rect (0, 0, Screen.width, Screen.height);
yield return SWMain.sharedSWMain.StartCoroutine(TextureUtility.SaveScreenShot(path,rect, size));
}
public static IEnumerator SaveScreenShot(string path, Rect rect, Vector2 size = default(Vector2))
{
// We should only read the screen bufferafter rendering is complete
yield return new WaitForEndOfFrame();
// 要保存图片的大小
Texture2D tex = new Texture2D ((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
// 截取的区域
tex.ReadPixels(rect, 0, 0);
tex.Apply();
// 把图片数据转换为byte数组
if (size != default(Vector2))
{
ScaleBilinear(tex, (int)size.x, (int)size.y);
}
byte[] buffer = tex.EncodeToJPG (100);
Object.Destroy(tex);
// 然后保存为图片
File.WriteAllBytes(path, buffer);
}
public static Texture2D Copy(Texture2D tex)
{
return new Texture2D(tex.width, tex.height, tex.format, false);
}
///
/// Applies sepia effect to the texture.
///
/// Texture to process.
public static Texture2D SetSepia(Texture2D tex)
{
Texture2D t = Copy(tex);
Color[] colors = tex.GetPixels();
for (int i = 0; i < colors.Length; i++)
{
float alpha = colors[i].a;
float grayScale = ((colors[i].r * .299f) + (colors[i].g * .587f) + (colors[i].b * .114f));
Color c = new Color(grayScale, grayScale, grayScale);
colors[i] = new Color(c.r * 1, c.g * 0.95f, c.b * 0.82f, alpha);
}
t.SetPixels(colors);
t.Apply();
return t;
}
///
/// Applies grayscale effect to the texture and changes colors to grayscale.
///
/// Texture to process.
public static Texture2D SetGrayscale(Texture2D tex)
{
Texture2D t = Copy(tex);
Color[] colors = tex.GetPixels();
for (int i = 0; i < colors.Length; i++)
{
float val = (colors [i].r + colors [i].g + colors [i].b) / 3;
colors [i] = new Color(val, val, val);
}
t.SetPixels(colors);
t.Apply();
return t;
}
///
/// Pixelates the texture.
///
/// Texture to process.
/// Size of the pixel.
public static Texture2D SetPixelate(Texture2D tex, int size)
{
Texture2D t = Copy(tex);
Rect rectangle = new Rect(0, 0, tex.width, tex.height);
for (int xx = (int)rectangle.x; xx < rectangle.x + rectangle.width && xx < tex.width; xx += size)
{
for (int yy = (int)rectangle.y; yy < rectangle.y + rectangle.height && yy < tex.height; yy += size)
{
int offsetX = size / 2;
int offsetY = size / 2;
while (xx + offsetX >= tex.width) offsetX--;
while (yy + offsetY >= tex.height) offsetY--;
Color pixel = tex.GetPixel(xx + offsetX, yy + offsetY);
for (int x = xx; x < xx + size && x < tex.width; x++)
for (int y = yy; y < yy + size && y < tex.height; y++)
t.SetPixel(x, y, pixel);
}
}
t.Apply();
return t;
}
///
/// Inverts colors of the texture.
///
/// Texture to process.
public static Texture2D SetNegative(Texture2D tex)
{
Texture2D t = Copy(tex);
Color[] colors = tex.GetPixels();
Color pixel;
for (int i = 0; i < colors.Length; i++)
{
pixel = colors[i];
colors[i] = new Color(1 - pixel.r, 1 - pixel.g, 1 - pixel.b);
}
t.SetPixels(colors);
t.Apply();
return t;
}
///
/// Sets the foggy effect.雾化效果
///
/// texture processed.
/// texture to process.
public static Texture2D SetFoggy(Texture2D tex)
{
Texture2D t = Copy(tex);
Color pixel;
for (int x = 1; x < tex.width - 1; x++)
for (int y = 1; y < tex.height - 1; y++)
{
int k = UnityEngine.Random.Range(0, 123456);
//像素块大小
int dx = x + k % 19;
int dy = y + k % 19;
if (dx >= tex.width)
dx = tex.width - 1;
if (dy >= tex.height)
dy = tex.height - 1;
pixel = tex.GetPixel(dx, dy);
t.SetPixel(x, y, pixel);
}
t.Apply();
return t;
}
///
/// Sets the soft effect.柔化效果
///
/// texture processed.
/// texture to process.
public static Texture2D SetSoft(Texture2D tex)
{
Texture2D t = Copy(tex);
int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
for (int x = 1; x < tex.width - 1; x++)
for (int y = 1; y < tex.height - 1; y++)
{
float r = 0, g = 0, b = 0;
int Index = 0;
for (int col = -1; col <= 1; col++)
for (int row = -1; row <= 1; row++)
{
Color pixel = tex.GetPixel(x + row, y + col);
r += pixel.r * Gauss[Index];
g += pixel.g * Gauss[Index];
b += pixel.b * Gauss[Index];
Index++;
}
r /= 16;
g /= 16;
b /= 16;
//处理颜色值溢出
r = r > 1 ? 1 : r;
r = r < 0 ? 0 : r;
g = g > 1 ? 1 : g;
g = g < 0 ? 0 : g;
b = b > 1 ? 1 : b;
b = b < 0 ? 0 : b;
t.SetPixel(x - 1, y - 1, new Color(r, g, b));
}
t.Apply();
return t;
}
///
/// Sets the sharp.锐化效果
///
/// The sharp.
/// Tex.
public static Texture2D SetSharp(Texture2D tex)
{
Texture2D t = Copy(tex);
Color pixel;
//拉普拉斯模板
int[] Laplacian ={ -1, -1, -1, -1, 9, -1, -1, -1, -1 };
for (int x = 1; x < tex.width - 1; x++)
for (int y = 1; y < tex.height - 1; y++)
{
float r = 0, g = 0, b = 0;
int index = 0;
for (int col = -1; col <= 1; col++)
for (int row = -1; row <= 1; row++)
{
pixel = tex.GetPixel(x + row, y + col); r += pixel.r * Laplacian[index];
g += pixel.g * Laplacian[index];
b += pixel.b * Laplacian[index];
index++;
}
//处理颜色值溢出
r = r > 1 ? 1 : r;
r = r < 0 ? 0 : r;
g = g > 1 ? 1 : g;
g = g < 0 ? 0 : g;
b = b > 1 ? 1 : b;
b = b < 0 ? 0 : b;
t.SetPixel(x - 1, y - 1, new Color(r, g, b));
}
t.Apply();
return t;
}
///
/// Sets the relief.浮雕效果
///
/// The relief.
/// Tex.
public static Texture2D SetRelief(Texture2D tex)
{
Texture2D t = Copy(tex);
for (int x = 0; x < tex.width - 1; x++)
{
for (int y = 0; y < tex.height - 1; y++)
{
float r = 0, g = 0, b = 0;
Color pixel1 = tex.GetPixel(x, y);
Color pixel2 = tex.GetPixel(x + 1, y + 1);
r = Mathf.Abs(pixel1.r - pixel2.r + 0.5f);
g = Mathf.Abs(pixel1.g - pixel2.g + 0.5f);
b = Mathf.Abs(pixel1.b - pixel2.b + 0.5f);
if (r > 1)
r = 1;
if (r < 0)
r = 0;
if (g > 1)
g = 1;
if (g < 0)
g = 0;
if (b > 1)
b = 1;
if (b < 0)
b = 0;
t.SetPixel(x, y, new Color(r, g, b));
}
}
t.Apply();
return t;
}
}
如何调用:
1. 压缩图片:
TextureUtility.ScalePoint(Texture2D tex, int newWidth, int newHeight),第一个参数是要传入的Texture2D, 第二个参数是新的图片的宽度,第三个参数则是新的图片的高度。也可以调用TextureUtility.ScaleBilinear(Texture2D tex, int newWidth, int newHeight)。
2. 截屏:
yield return StartCoroutine(TextureUtility.SaveScreenShot(string path, Vector2 size, Vector2 size = default(Vector2)))
或者调用异步方法TextureUtility.SaveScreenShotAsync(string path, Rect rect, Vector2 size)
3. 图片滤镜:
如灰阶滤镜:TextureUtility.SetGrayscale(Texture2D tex);
底片滤镜:TextureUtility.SetNegative(Texture2D tex)等。
unity黑白滤镜_Unity图片处理类,包括压缩、截屏和滤镜相关推荐
- Tools_@截屏工具@OCR识别工具@图片文字翻译工具长截屏,普通截屏套件推荐(by QQ)@鼠标键盘动作录制
文章目录 Tools_@截屏工具@OCR识别工具@图片文字翻译工具长截屏,普通截屏套件推荐(QQ自带) 全局录屏/长截屏: OCR 优点 不足 quicker动作 鼠键录制工具
- java图片压缩工具类(指定压缩大小)
1:先导入依赖 <!--thumbnailator图片处理--><dependency><groupId>net.coobird</groupId>&l ...
- php图片地址怎么看,电脑截屏的图片在哪里找
很多人不知道自己的电脑截屏图片保存在哪里,其实非常简单的,你只需要手一动就可以找到图片了!下面,我们就一起来看一下你的截图到底去了哪儿? 电脑截屏保存在哪里: 在win10系统下,打开电脑,打开&qu ...
- android截屏功能实现方式汇总【包括后台截屏】
前言 对于android实现截屏功能,简单讲述一下可行的方法和之间的利弊 使用canvas View v = getWindow().getDecorView(); Bitmap bitmap = B ...
- Java图片压缩工具类(递归压缩到指定大小范围)
Java图片压缩工具 工具类使用场景 公司做人脸识别项目时候需要上传学生.家长.教师.访客的正面照图片,但是人脸识别机器有限制只接收200KB-1M的图片,所以必须做图片压缩到指定范围大少. APP上 ...
- node将图片转换成html文件,node+puppeteer将整个网页html转换为图片并保存【滚动截屏】...
Puppeteer 是 Chrome 开发团队在 2017 年发布的一个 Node.js 包,用来模拟 Chrome 浏览器的运行. demo只支持将简单不需要翻页,不需要登陆的页面转换为图片 需要n ...
- 使用CSS3滤镜让图片反转颜色
CSS提供的滤镜也是一大亮点,我一直痴迷其中,有些滤镜的效果很有用,可是有些的滤镜效果可能只是为了玩玩儿,CSS常见的滤镜有这些:grayscale, blur, sepia,所有常见的过滤器.但是如 ...
- swift 将图片保存到本地_Swift实现截屏并保存相册
func saveToLocal() { //截屏 let screenRect = UIScreen.mainScreen().bounds UIGraphicsBeginImageContext( ...
- Unity 入门笔记 - 05 - 动画事件类音效对话框
Unity 入门笔记 - 05 - 动画事件&类&音效&对话框 前言:无 目录 Unity 入门笔记 - 05 - 动画事件&类&音效&对话框 一.动画 ...
最新文章
- (转)iOS 各种控件默认高度(图示)
- 网盘php资料,怎么搜索百度网盘里的资料(php版)
- Java easycms 版本2.0发布
- PaperNotes(8)-Stein Variational Gradient Descent A General Purpose Bayesian Inference Algorithm
- Linux常用命令—权限管理命令—权限管理命令chmod
- 切换图片 ImageSwitcher
- 用c语言递归函数实现焚天塔的过程,梵天塔问题.PPT
- windows eclipse python环境搭建
- SQLServer 2008 已成功与服务器建立连接,但是在登录前的握手期间发生错误。 (provider: SSL Provider, error: 0 - 等待的操作过时。...
- 乔丹LeCun李开复隔空对话:我们对智能一无所知;AI研究的12大趋势
- c语言strTrimed函数用法介绍,linux type命令用法_转
- MySQL几种常见的数据类型
- 读书笔记 - 简约之美:软件设计之道
- Python绘制一箭穿双心 动画代码
- 测试计划一般包括哪些方面?
- Jenkins配置-腾讯企业微信邮箱
- XUPT 寒假算法集训第三周
- 华为云计算机访问手机软件,手机也能当电脑使用?华为黑科技:手机云电脑
- matlab 如何axis,在matlab中axis是什么意思,matlab中axis的用法
- 【CSS】绝对定位元素设置 水平 / 垂直 居中 ( 绝对定位元素居中设置 - 先偏移 50% 再回退子元素一半尺寸 | 绝对定位居中设置 )